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 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 d2cdc636 2020-03-18 stsp #include <util.h>
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.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 820059fa 2020-09-25 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 6879ba42 2020-10-01 naddy __dead static void usage(int, 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 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
106 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
107 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
108 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
109 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
110 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
111 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
112 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
113 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
114 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
116 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
117 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
118 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
119 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
121 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
124 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
125 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
126 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
127 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
128 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
129 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
130 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
131 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
132 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
133 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
134 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
135 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
136 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
137 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
138 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
139 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
140 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
141 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
142 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
143 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
145 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
146 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
147 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
148 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
149 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
150 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
151 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
152 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
153 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
154 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
155 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
156 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
157 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
158 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
159 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
160 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
161 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
162 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
163 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
164 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
165 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
166 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
167 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
168 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
169 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
170 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
171 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
172 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
173 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
176 ce5b7c56 2019-07-09 stsp static void
177 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
181 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
182 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
183 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
184 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
186 6879ba42 2020-10-01 naddy fputc('\n', fp);
189 ff69268e 2020-12-13 stsp __dead static void
190 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
192 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
196 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
198 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
201 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
202 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
203 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
204 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
207 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
209 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
210 5c860e29 2018-03-12 stsp switch (ch) {
218 6879ba42 2020-10-01 naddy usage(hflag, 1);
219 5c860e29 2018-03-12 stsp /* NOTREACHED */
223 5c860e29 2018-03-12 stsp argc -= optind;
224 5c860e29 2018-03-12 stsp argv += optind;
225 9814e6a3 2020-09-27 naddy optind = 1;
226 9814e6a3 2020-09-27 naddy optreset = 1;
228 53ccebc2 2019-07-30 stsp if (Vflag) {
229 53ccebc2 2019-07-30 stsp got_version_print_str();
233 5c860e29 2018-03-12 stsp if (argc <= 0)
234 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
236 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
237 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
239 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
240 d7d4f210 2018-03-12 stsp const struct got_error *error;
242 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
244 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
245 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
249 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
251 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
252 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
253 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
254 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
255 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
256 70015d7a 2019-11-08 stsp !(sigint_received &&
257 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
258 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
265 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
266 6879ba42 2020-10-01 naddy list_commands(stderr);
270 4ed7e80c 2018-05-20 stsp __dead static void
271 6879ba42 2020-10-01 naddy usage(int hflag, int status)
273 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
275 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
276 53ccebc2 2019-07-30 stsp getprogname());
278 6879ba42 2020-10-01 naddy list_commands(fp);
279 6879ba42 2020-10-01 naddy exit(status);
282 0266afb7 2019-01-04 stsp static const struct got_error *
283 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
285 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
286 e2ba3d07 2019-05-13 stsp const char *editor;
288 8920fa04 2019-08-18 stsp *abspath = NULL;
290 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
291 e2ba3d07 2019-05-13 stsp if (editor == NULL)
292 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
294 0ee7065d 2019-05-13 stsp if (editor) {
295 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
297 0ee7065d 2019-05-13 stsp return err;
300 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
301 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
302 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
303 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
306 e2ba3d07 2019-05-13 stsp return NULL;
309 e2ba3d07 2019-05-13 stsp static const struct got_error *
310 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
311 c530dc23 2019-07-23 stsp const char *worktree_path)
313 163ce85a 2019-05-13 stsp const struct got_error *err;
315 37c06ea4 2019-07-15 stsp #ifdef PROFILE
316 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
317 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
319 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
320 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
322 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
323 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
325 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
326 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
328 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
329 163ce85a 2019-05-13 stsp if (err != NULL)
330 163ce85a 2019-05-13 stsp return err;
332 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
333 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
335 0266afb7 2019-01-04 stsp return NULL;
338 2c7829a4 2019-06-17 stsp __dead static void
339 2c7829a4 2019-06-17 stsp usage_init(void)
341 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
345 2c7829a4 2019-06-17 stsp static const struct got_error *
346 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
348 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
349 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
352 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
353 2c7829a4 2019-06-17 stsp switch (ch) {
355 2c7829a4 2019-06-17 stsp usage_init();
356 2c7829a4 2019-06-17 stsp /* NOTREACHED */
360 2c7829a4 2019-06-17 stsp argc -= optind;
361 2c7829a4 2019-06-17 stsp argv += optind;
363 2c7829a4 2019-06-17 stsp #ifndef PROFILE
364 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
365 2c7829a4 2019-06-17 stsp err(1, "pledge");
367 2c7829a4 2019-06-17 stsp if (argc != 1)
368 bc20e173 2019-06-17 stsp usage_init();
370 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
371 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
372 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
374 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
376 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
377 2c7829a4 2019-06-17 stsp if (error &&
378 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
381 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
385 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
387 3ce1b845 2019-07-15 stsp free(repo_path);
388 3ce1b845 2019-07-15 stsp return error;
391 3ce1b845 2019-07-15 stsp __dead static void
392 3ce1b845 2019-07-15 stsp usage_import(void)
394 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
395 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
400 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
403 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
404 3ce1b845 2019-07-15 stsp int st = -1;
406 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
407 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
408 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
410 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
412 3ce1b845 2019-07-15 stsp goto doneediting;
414 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
415 3ce1b845 2019-07-15 stsp _exit(127);
418 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
419 3ce1b845 2019-07-15 stsp if (errno != EINTR)
422 3ce1b845 2019-07-15 stsp doneediting:
423 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
424 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
425 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
427 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
428 3ce1b845 2019-07-15 stsp errno = EINTR;
432 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
435 3ce1b845 2019-07-15 stsp static const struct got_error *
436 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
437 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
438 28cf319f 2021-01-28 stsp int require_modification)
440 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
441 bfa12d5e 2020-09-26 stsp char *line = NULL;
442 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
443 bfa12d5e 2020-09-26 stsp ssize_t linelen;
444 3ce1b845 2019-07-15 stsp struct stat st, st2;
445 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
446 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
447 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
449 3ce1b845 2019-07-15 stsp *logmsg = NULL;
451 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
452 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
454 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
455 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
457 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
458 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
460 28cf319f 2021-01-28 stsp if (require_modification &&
461 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
462 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
466 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
467 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
468 bfa12d5e 2020-09-26 stsp * has in fact been edited.
470 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
471 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
472 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
473 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
475 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
476 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
477 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
482 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
483 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
484 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
485 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
486 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
487 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
488 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
492 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
493 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
497 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
498 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
499 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
500 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
501 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
503 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
504 3ce1b845 2019-07-15 stsp if (fp == NULL) {
505 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
510 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
511 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
512 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
513 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
514 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
515 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
519 bfa12d5e 2020-09-26 stsp free(line);
520 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
521 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
524 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
525 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
529 bfa12d5e 2020-09-26 stsp if (len == 0) {
530 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
531 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
534 28cf319f 2021-01-28 stsp if (require_modification &&
535 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
536 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
537 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
539 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
541 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
542 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
544 3ce1b845 2019-07-15 stsp free(*logmsg);
545 3ce1b845 2019-07-15 stsp *logmsg = NULL;
547 3ce1b845 2019-07-15 stsp return err;
550 3ce1b845 2019-07-15 stsp static const struct got_error *
551 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
552 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
554 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
555 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
556 1601cb9f 2020-09-11 naddy int initial_content_len;
557 97972933 2020-09-11 stsp int fd = -1;
559 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
560 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
561 1601cb9f 2020-09-11 naddy branch_name);
562 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
563 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
565 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
566 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
570 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
571 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
575 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
576 0d5bb276 2020-12-15 stsp initial_content_len, 1);
578 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
579 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
580 3ce1b845 2019-07-15 stsp free(initial_content);
582 59f86c76 2020-09-11 stsp free(*logmsg_path);
583 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
585 3ce1b845 2019-07-15 stsp return err;
588 3ce1b845 2019-07-15 stsp static const struct got_error *
589 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
591 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
592 3ce1b845 2019-07-15 stsp return NULL;
595 3ce1b845 2019-07-15 stsp static const struct got_error *
596 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
597 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
599 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
600 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
601 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
603 84792843 2019-08-09 stsp *author = NULL;
605 50b0790e 2020-09-11 stsp if (worktree)
606 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
607 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
610 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
611 50b0790e 2020-09-11 stsp * significant to least significant:
612 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
613 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
614 50b0790e 2020-09-11 stsp * 3) repository's git config file
615 50b0790e 2020-09-11 stsp * 4) environment variables
616 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
619 50b0790e 2020-09-11 stsp if (worktree_conf)
620 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
621 50b0790e 2020-09-11 stsp if (got_author == NULL)
622 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
623 84792843 2019-08-09 stsp if (got_author == NULL) {
624 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
625 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
626 c9956ddf 2019-09-08 stsp if (name && email) {
627 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
628 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
629 c9956ddf 2019-09-08 stsp return NULL;
632 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
633 257add31 2020-09-09 stsp if (got_author == NULL) {
634 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
635 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
637 257add31 2020-09-09 stsp if (name && email) {
638 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
640 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
641 257add31 2020-09-09 stsp return NULL;
643 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
644 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
648 aba9c984 2019-09-08 stsp *author = strdup(got_author);
649 aba9c984 2019-09-08 stsp if (*author == NULL)
650 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
653 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
654 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
656 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
657 84792843 2019-08-09 stsp got_author++;
658 aba9c984 2019-09-08 stsp if (*got_author != '<') {
659 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
662 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
663 84792843 2019-08-09 stsp got_author++;
664 aba9c984 2019-09-08 stsp if (*got_author != '@') {
665 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
668 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
669 84792843 2019-08-09 stsp got_author++;
670 84792843 2019-08-09 stsp if (*got_author != '>')
671 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
674 aba9c984 2019-09-08 stsp free(*author);
675 aba9c984 2019-09-08 stsp *author = NULL;
677 aba9c984 2019-09-08 stsp return err;
680 c9956ddf 2019-09-08 stsp static const struct got_error *
681 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
683 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
685 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
686 c9956ddf 2019-09-08 stsp if (homedir) {
687 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
688 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
691 c9956ddf 2019-09-08 stsp return NULL;
694 84792843 2019-08-09 stsp static const struct got_error *
695 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
697 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
698 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
699 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
700 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
701 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
702 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
703 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
704 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
706 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
707 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
708 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
710 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
712 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
713 3ce1b845 2019-07-15 stsp switch (ch) {
715 3ce1b845 2019-07-15 stsp branch_name = optarg;
718 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
719 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
720 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
725 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
726 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
727 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
733 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
735 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
741 b2b65d18 2019-08-22 stsp usage_import();
742 3ce1b845 2019-07-15 stsp /* NOTREACHED */
746 3ce1b845 2019-07-15 stsp argc -= optind;
747 3ce1b845 2019-07-15 stsp argv += optind;
749 3ce1b845 2019-07-15 stsp #ifndef PROFILE
750 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
752 3ce1b845 2019-07-15 stsp NULL) == -1)
753 3ce1b845 2019-07-15 stsp err(1, "pledge");
755 3ce1b845 2019-07-15 stsp if (argc != 1)
756 3ce1b845 2019-07-15 stsp usage_import();
758 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
759 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
760 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
761 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
763 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
764 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
767 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
771 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
773 aba9c984 2019-09-08 stsp return error;
776 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
777 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
778 e560b7e0 2019-11-28 stsp * an unintended typo.
780 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
781 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
783 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
784 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
788 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
789 3ce1b845 2019-07-15 stsp if (error) {
790 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
793 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
794 3ce1b845 2019-07-15 stsp "import target branch already exists");
798 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
799 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
800 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
803 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
806 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
807 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
809 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
810 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
813 8e158b01 2019-09-22 stsp free(logmsg);
814 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
815 ef293bdd 2019-10-21 stsp path_dir, refname);
816 ef293bdd 2019-10-21 stsp if (error) {
817 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
818 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
819 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
824 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
825 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
826 ef293bdd 2019-10-21 stsp if (logmsg_path)
827 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
831 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
832 ef293bdd 2019-10-21 stsp if (error) {
833 ef293bdd 2019-10-21 stsp if (logmsg_path)
834 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
838 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
839 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
840 ef293bdd 2019-10-21 stsp if (error) {
841 ef293bdd 2019-10-21 stsp if (logmsg_path)
842 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
846 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
847 ef293bdd 2019-10-21 stsp if (error) {
848 ef293bdd 2019-10-21 stsp if (logmsg_path)
849 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
853 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
854 ef293bdd 2019-10-21 stsp if (error) {
855 ef293bdd 2019-10-21 stsp if (logmsg_path)
856 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
860 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
861 ef293bdd 2019-10-21 stsp if (error) {
862 ef293bdd 2019-10-21 stsp if (logmsg_path)
863 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
867 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
868 3ce1b845 2019-07-15 stsp if (error) {
869 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
870 ef293bdd 2019-10-21 stsp if (logmsg_path)
871 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
875 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
876 3ce1b845 2019-07-15 stsp branch_ref);
877 ef293bdd 2019-10-21 stsp if (error) {
878 ef293bdd 2019-10-21 stsp if (logmsg_path)
879 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
883 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
884 ef293bdd 2019-10-21 stsp if (error) {
885 ef293bdd 2019-10-21 stsp if (logmsg_path)
886 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
891 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
892 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
894 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
895 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
896 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
897 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
898 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
899 8e158b01 2019-09-22 stsp free(logmsg);
900 ef293bdd 2019-10-21 stsp free(logmsg_path);
901 2c7829a4 2019-06-17 stsp free(repo_path);
902 3ce1b845 2019-07-15 stsp free(editor);
903 3ce1b845 2019-07-15 stsp free(refname);
904 3ce1b845 2019-07-15 stsp free(new_commit_id);
905 3ce1b845 2019-07-15 stsp free(id_str);
906 aba9c984 2019-09-08 stsp free(author);
907 c9956ddf 2019-09-08 stsp free(gitconfig_path);
908 3ce1b845 2019-07-15 stsp if (branch_ref)
909 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
910 3ce1b845 2019-07-15 stsp if (head_ref)
911 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
912 2c7829a4 2019-06-17 stsp return error;
915 93658fb9 2020-03-18 stsp __dead static void
916 93658fb9 2020-03-18 stsp usage_clone(void)
918 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
919 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
923 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
924 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
925 892ac3b6 2020-03-18 stsp int last_p_indexed;
926 892ac3b6 2020-03-18 stsp int last_p_resolved;
927 68999b92 2020-03-18 stsp int verbosity;
929 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
931 04d9a9ec 2020-09-24 stsp int create_configs;
932 04d9a9ec 2020-09-24 stsp int configs_created;
934 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
935 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
936 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
937 04d9a9ec 2020-09-24 stsp const char *proto;
938 04d9a9ec 2020-09-24 stsp const char *host;
939 04d9a9ec 2020-09-24 stsp const char *port;
940 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
941 04d9a9ec 2020-09-24 stsp const char *git_url;
942 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
943 04d9a9ec 2020-09-24 stsp int mirror_references;
944 04d9a9ec 2020-09-24 stsp } config_info;
947 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
948 93658fb9 2020-03-18 stsp static const struct got_error *
949 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
950 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
951 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
952 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
953 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
955 04d9a9ec 2020-09-24 stsp static const struct got_error *
956 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
957 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
959 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
960 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
961 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
962 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
963 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
966 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
967 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
968 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
969 04d9a9ec 2020-09-24 stsp * we have all required information.
971 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
972 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
973 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
974 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
975 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
976 62d463ca 2020-10-20 naddy a->config_info.git_url,
977 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
978 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
979 62d463ca 2020-10-20 naddy a->config_info.symrefs,
980 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
981 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
983 04d9a9ec 2020-09-24 stsp return err;
984 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
987 68999b92 2020-03-18 stsp if (a->verbosity < 0)
988 68999b92 2020-03-18 stsp return NULL;
990 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
991 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
992 892ac3b6 2020-03-18 stsp fflush(stdout);
993 12d1281e 2020-03-19 stsp return NULL;
996 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
997 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
998 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
999 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1000 892ac3b6 2020-03-18 stsp print_size = 1;
1001 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1002 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1003 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1005 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1006 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1007 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1008 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1009 892ac3b6 2020-03-18 stsp print_indexed = 1;
1010 892ac3b6 2020-03-18 stsp print_size = 1;
1013 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1014 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1015 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1016 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1017 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1018 892ac3b6 2020-03-18 stsp print_resolved = 1;
1019 892ac3b6 2020-03-18 stsp print_indexed = 1;
1020 892ac3b6 2020-03-18 stsp print_size = 1;
1025 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1026 892ac3b6 2020-03-18 stsp printf("\r");
1027 892ac3b6 2020-03-18 stsp if (print_size)
1028 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1029 d715f13e 2020-03-19 stsp if (print_indexed)
1030 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1031 d715f13e 2020-03-19 stsp if (print_resolved)
1032 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1033 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1034 892ac3b6 2020-03-18 stsp fflush(stdout);
1036 531c3985 2020-03-18 stsp return NULL;
1039 531c3985 2020-03-18 stsp static const struct got_error *
1040 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1041 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1043 4ba14133 2020-03-20 stsp const struct got_error *err;
1044 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1046 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1048 4ba14133 2020-03-20 stsp return err;
1050 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1051 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1052 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1053 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1055 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1056 4ba14133 2020-03-20 stsp return err;
1059 4ba14133 2020-03-20 stsp static const struct got_error *
1060 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1061 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1063 41b0de12 2020-03-21 stsp const struct got_error *err;
1064 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1066 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1067 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1068 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1070 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1073 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1074 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1075 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1076 41b0de12 2020-03-21 stsp char *id_str;
1078 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1080 41b0de12 2020-03-21 stsp return err;
1081 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1082 41b0de12 2020-03-21 stsp free(id_str);
1085 41b0de12 2020-03-21 stsp return NULL;
1088 6338a6a1 2020-03-21 stsp static const struct got_error *
1089 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1090 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1092 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1093 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1094 6338a6a1 2020-03-21 stsp char *id_str;
1096 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1098 6338a6a1 2020-03-21 stsp return err;
1100 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1102 6338a6a1 2020-03-21 stsp goto done;
1104 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1105 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1107 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1108 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1110 6338a6a1 2020-03-21 stsp free(id_str);
1111 6338a6a1 2020-03-21 stsp return err;
1114 0e4002ca 2020-03-21 stsp static int
1115 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1117 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1119 0e4002ca 2020-03-21 stsp refname += 5;
1122 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1123 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1125 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1127 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1130 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1131 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1133 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1134 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1137 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1138 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1141 0e4002ca 2020-03-21 stsp static int
1142 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1144 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1146 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1147 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1154 41b0de12 2020-03-21 stsp static const struct got_error *
1155 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1156 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1158 0e4002ca 2020-03-21 stsp const struct got_error *err;
1159 0e4002ca 2020-03-21 stsp char *remote_refname;
1161 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1162 0e4002ca 2020-03-21 stsp refname += 5;
1164 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1165 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1166 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1168 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1169 0e4002ca 2020-03-21 stsp free(remote_refname);
1170 7c0b7f42 2020-09-24 stsp return err;
1173 7c0b7f42 2020-09-24 stsp static const struct got_error *
1174 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1175 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1176 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1177 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1178 99495ddb 2021-01-10 stsp struct got_repository *repo)
1180 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1181 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1182 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1183 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1184 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1185 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1186 7c0b7f42 2020-09-24 stsp ssize_t n;
1188 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1189 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1190 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1192 132af4a5 2021-01-05 stsp branchname = pe->path;
1193 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1194 132af4a5 2021-01-05 stsp branchname += 11;
1195 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1196 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1197 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1198 132af4a5 2021-01-05 stsp goto done;
1200 132af4a5 2021-01-05 stsp free(branches);
1201 132af4a5 2021-01-05 stsp branches = s;
1203 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1204 15d3c221 2021-01-05 stsp branchname = default_branch;
1205 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1206 15d3c221 2021-01-05 stsp branchname += 11;
1207 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1208 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1209 132af4a5 2021-01-05 stsp goto done;
1212 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1213 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1214 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1216 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1217 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1218 99495ddb 2021-01-10 stsp branchname += 5;
1219 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1220 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1221 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1222 99495ddb 2021-01-10 stsp goto done;
1224 99495ddb 2021-01-10 stsp free(refs);
1229 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1230 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1231 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1232 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1233 7c0b7f42 2020-09-24 stsp goto done;
1235 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1236 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1237 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1238 7c0b7f42 2020-09-24 stsp goto done;
1240 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1241 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1242 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1243 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1245 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1251 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1252 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1253 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1254 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1255 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1256 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1257 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1258 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1259 7c0b7f42 2020-09-24 stsp goto done;
1261 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1262 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1263 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1264 7c0b7f42 2020-09-24 stsp goto done;
1268 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1269 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1270 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1271 132af4a5 2021-01-05 stsp free(branches);
1272 7c0b7f42 2020-09-24 stsp return err;
1275 7c0b7f42 2020-09-24 stsp static const struct got_error *
1276 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1277 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1278 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1279 99495ddb 2021-01-10 stsp struct got_repository *repo)
1281 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1282 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1283 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1284 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1285 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1286 56d0a753 2021-01-20 stsp const char *branchname;
1287 7c0b7f42 2020-09-24 stsp ssize_t n;
1289 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1290 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1291 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1292 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1293 7c0b7f42 2020-09-24 stsp goto done;
1295 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1296 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1297 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1298 7c0b7f42 2020-09-24 stsp goto done;
1300 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1301 56d0a753 2021-01-20 stsp if (mirror_references) {
1302 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1303 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1304 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1305 56d0a753 2021-01-20 stsp goto done;
1307 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1308 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1309 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1310 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1311 7c0b7f42 2020-09-24 stsp goto done;
1313 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1314 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1315 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1317 132af4a5 2021-01-05 stsp branchname = pe->path;
1318 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1319 132af4a5 2021-01-05 stsp branchname += 11;
1320 56d0a753 2021-01-20 stsp if (mirror_references) {
1321 56d0a753 2021-01-20 stsp if (asprintf(&s,
1322 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1323 56d0a753 2021-01-20 stsp branches ? branches : "",
1324 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1325 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1326 56d0a753 2021-01-20 stsp goto done;
1328 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1329 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1330 132af4a5 2021-01-05 stsp branches ? branches : "",
1331 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1332 132af4a5 2021-01-05 stsp branchname) == -1) {
1333 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1334 132af4a5 2021-01-05 stsp goto done;
1336 132af4a5 2021-01-05 stsp free(branches);
1337 132af4a5 2021-01-05 stsp branches = s;
1341 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1342 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1344 04d9a9ec 2020-09-24 stsp if (default_branch) {
1345 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1346 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1347 7c0b7f42 2020-09-24 stsp branchname += 11;
1349 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1350 56d0a753 2021-01-20 stsp if (mirror_references) {
1351 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1352 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1353 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1354 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1355 56d0a753 2021-01-20 stsp goto done;
1357 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1358 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1359 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1360 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1361 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1362 7c0b7f42 2020-09-24 stsp goto done;
1365 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1366 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1367 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1369 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1370 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1371 99495ddb 2021-01-10 stsp refname += 5;
1372 56d0a753 2021-01-20 stsp if (mirror_references) {
1373 56d0a753 2021-01-20 stsp if (asprintf(&s,
1374 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1375 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1376 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1377 56d0a753 2021-01-20 stsp goto done;
1379 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1380 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1381 99495ddb 2021-01-10 stsp refs ? refs : "",
1382 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1383 99495ddb 2021-01-10 stsp refname) == -1) {
1384 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1385 99495ddb 2021-01-10 stsp goto done;
1387 99495ddb 2021-01-10 stsp free(refs);
1392 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1393 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1394 132af4a5 2021-01-05 stsp "\turl = %s\n"
1397 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1398 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1399 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1400 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1401 132af4a5 2021-01-05 stsp goto done;
1403 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1404 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1405 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1406 7c0b7f42 2020-09-24 stsp goto done;
1409 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1410 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1411 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1412 132af4a5 2021-01-05 stsp free(branches);
1413 0e4002ca 2020-03-21 stsp return err;
1416 0e4002ca 2020-03-21 stsp static const struct got_error *
1417 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1418 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1419 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1420 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1421 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1423 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1424 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1425 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1428 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1429 04d9a9ec 2020-09-24 stsp * one of those.
1431 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1432 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1433 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1435 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1436 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1437 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1438 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1440 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1443 04d9a9ec 2020-09-24 stsp default_branch = target;
1448 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1449 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1450 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1451 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1453 04d9a9ec 2020-09-24 stsp return err;
1455 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1456 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1457 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1460 04d9a9ec 2020-09-24 stsp static const struct got_error *
1461 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1463 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1464 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1465 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1466 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1467 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1468 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1469 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1470 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1471 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1472 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1473 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1474 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1475 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1476 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1477 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1479 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1480 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1481 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1482 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1484 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1485 93658fb9 2020-03-18 stsp switch (ch) {
1487 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1490 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1491 4ba14133 2020-03-20 stsp optarg, NULL);
1492 4ba14133 2020-03-20 stsp if (error)
1493 4ba14133 2020-03-20 stsp return error;
1496 41b0de12 2020-03-21 stsp list_refs_only = 1;
1499 469dd726 2020-03-20 stsp mirror_references = 1;
1502 68999b92 2020-03-18 stsp if (verbosity < 0)
1503 68999b92 2020-03-18 stsp verbosity = 0;
1504 68999b92 2020-03-18 stsp else if (verbosity < 3)
1505 68999b92 2020-03-18 stsp verbosity++;
1508 68999b92 2020-03-18 stsp verbosity = -1;
1511 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1512 0e4002ca 2020-03-21 stsp optarg, NULL);
1513 0e4002ca 2020-03-21 stsp if (error)
1514 0e4002ca 2020-03-21 stsp return error;
1517 93658fb9 2020-03-18 stsp usage_clone();
1521 93658fb9 2020-03-18 stsp argc -= optind;
1522 93658fb9 2020-03-18 stsp argv += optind;
1524 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1525 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1526 41b0de12 2020-03-21 stsp if (list_refs_only) {
1527 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1528 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1529 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1530 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1531 41b0de12 2020-03-21 stsp if (mirror_references)
1532 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1533 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1534 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1537 93658fb9 2020-03-18 stsp uri = argv[0];
1539 9df6f38b 2020-03-18 stsp if (argc == 1)
1540 93658fb9 2020-03-18 stsp dirname = NULL;
1541 9df6f38b 2020-03-18 stsp else if (argc == 2)
1542 93658fb9 2020-03-18 stsp dirname = argv[1];
1544 93658fb9 2020-03-18 stsp usage_clone();
1546 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1547 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1548 39c64a6a 2020-03-18 stsp if (error)
1549 09f63084 2020-03-20 stsp goto done;
1551 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1552 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1553 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1554 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1555 09838ffc 2020-03-18 stsp goto done;
1558 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1559 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1560 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1561 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1562 39c64a6a 2020-03-18 stsp err(1, "pledge");
1564 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1565 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1566 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1567 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1568 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1569 39c64a6a 2020-03-18 stsp err(1, "pledge");
1571 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1572 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1573 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1574 39c64a6a 2020-03-18 stsp goto done;
1576 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1577 39c64a6a 2020-03-18 stsp goto done;
1579 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1580 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1581 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1582 bb64b798 2020-03-18 stsp goto done;
1584 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1586 bb64b798 2020-03-18 stsp repo_path = dirname;
1588 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1589 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1590 2751fe64 2020-09-24 stsp if (error &&
1591 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1592 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1593 41b0de12 2020-03-21 stsp goto done;
1594 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1595 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1596 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1597 41b0de12 2020-03-21 stsp goto done;
1601 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1602 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1603 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1604 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1605 ee448f5f 2020-03-18 stsp goto done;
1608 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1609 ee448f5f 2020-03-18 stsp if (error)
1610 ee448f5f 2020-03-18 stsp goto done;
1612 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1613 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1614 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1616 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1617 9c52365f 2020-03-21 stsp server_path, verbosity);
1618 39c64a6a 2020-03-18 stsp if (error)
1619 bb64b798 2020-03-18 stsp goto done;
1621 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1622 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1623 2751fe64 2020-09-24 stsp if (error)
1624 2751fe64 2020-09-24 stsp goto done;
1625 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1626 2751fe64 2020-09-24 stsp if (error)
1627 2751fe64 2020-09-24 stsp goto done;
1630 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1631 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1632 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1633 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1634 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1635 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1636 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1637 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1639 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1644 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1645 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1646 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1647 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1648 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1649 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1650 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1651 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1652 39c64a6a 2020-03-18 stsp if (error)
1653 d9b4d0c0 2020-03-18 stsp goto done;
1655 41b0de12 2020-03-21 stsp if (list_refs_only) {
1656 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1657 41b0de12 2020-03-21 stsp goto done;
1660 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1661 39c64a6a 2020-03-18 stsp if (error)
1662 d9b4d0c0 2020-03-18 stsp goto done;
1663 68999b92 2020-03-18 stsp if (verbosity >= 0)
1664 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1665 d9b4d0c0 2020-03-18 stsp free(id_str);
1667 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1668 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1669 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1670 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1671 7ebc0570 2020-03-18 stsp char *remote_refname;
1673 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1674 0e4002ca 2020-03-21 stsp !mirror_references) {
1675 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1676 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1677 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1678 0e4002ca 2020-03-21 stsp if (error)
1679 0e4002ca 2020-03-21 stsp goto done;
1683 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1684 39c64a6a 2020-03-18 stsp if (error)
1685 d9b4d0c0 2020-03-18 stsp goto done;
1687 469dd726 2020-03-20 stsp if (mirror_references)
1690 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1693 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1694 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1695 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1696 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1697 7ebc0570 2020-03-18 stsp goto done;
1699 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1700 f298ae0f 2020-03-25 stsp free(remote_refname);
1701 39c64a6a 2020-03-18 stsp if (error)
1702 d9b4d0c0 2020-03-18 stsp goto done;
1705 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1706 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1707 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1708 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1709 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1710 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1712 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1715 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1716 39c64a6a 2020-03-18 stsp if (error) {
1717 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1718 55330abe 2020-03-20 stsp error = NULL;
1721 d9b4d0c0 2020-03-18 stsp goto done;
1724 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1725 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1726 f298ae0f 2020-03-25 stsp if (error)
1727 f298ae0f 2020-03-25 stsp goto done;
1729 f298ae0f 2020-03-25 stsp if (mirror_references)
1732 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1735 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1736 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1737 f298ae0f 2020-03-25 stsp refname) == -1) {
1738 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1739 f298ae0f 2020-03-25 stsp goto done;
1741 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1742 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1743 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1744 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1745 f298ae0f 2020-03-25 stsp free(remote_refname);
1746 f298ae0f 2020-03-25 stsp goto done;
1748 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1749 f298ae0f 2020-03-25 stsp if (error) {
1750 f298ae0f 2020-03-25 stsp free(remote_refname);
1751 f298ae0f 2020-03-25 stsp free(remote_target);
1752 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1753 f298ae0f 2020-03-25 stsp error = NULL;
1756 f298ae0f 2020-03-25 stsp goto done;
1758 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1759 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1760 f298ae0f 2020-03-25 stsp free(remote_refname);
1761 f298ae0f 2020-03-25 stsp free(remote_target);
1762 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1763 39c64a6a 2020-03-18 stsp if (error)
1764 d9b4d0c0 2020-03-18 stsp goto done;
1766 4ba14133 2020-03-20 stsp if (pe == NULL) {
1768 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1769 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1770 4ba14133 2020-03-20 stsp * which could be fetched instead.
1772 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1773 4ba14133 2020-03-20 stsp const char *target = pe->path;
1774 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1776 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1777 4ba14133 2020-03-20 stsp if (error) {
1778 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1779 4ba14133 2020-03-20 stsp error = NULL;
1782 4ba14133 2020-03-20 stsp goto done;
1785 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1786 04d9a9ec 2020-09-24 stsp verbosity, repo);
1787 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1788 4ba14133 2020-03-20 stsp if (error)
1789 4ba14133 2020-03-20 stsp goto done;
1794 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1795 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1796 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1798 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1799 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1800 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1801 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1802 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1804 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1805 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1806 1d0f4054 2021-06-17 stsp if (repo) {
1807 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1808 1d0f4054 2021-06-17 stsp if (error == NULL)
1809 1d0f4054 2021-06-17 stsp error = close_err;
1811 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1812 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1813 d9b4d0c0 2020-03-18 stsp free(pe->data);
1815 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1816 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1817 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1818 d9b4d0c0 2020-03-18 stsp free(pe->data);
1820 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1821 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1822 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1823 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1824 09838ffc 2020-03-18 stsp free(proto);
1825 09838ffc 2020-03-18 stsp free(host);
1826 09838ffc 2020-03-18 stsp free(port);
1827 09838ffc 2020-03-18 stsp free(server_path);
1828 09838ffc 2020-03-18 stsp free(repo_name);
1829 bb64b798 2020-03-18 stsp free(default_destdir);
1830 b46f3e71 2020-03-18 stsp free(git_url);
1831 39c64a6a 2020-03-18 stsp return error;
1834 7848a0e1 2020-03-19 stsp static const struct got_error *
1835 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1836 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1838 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1839 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1840 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1842 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1844 7848a0e1 2020-03-19 stsp goto done;
1846 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1847 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1848 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1850 88609724 2020-03-21 stsp goto done;
1851 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1852 88609724 2020-03-21 stsp goto done;
1853 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1854 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1855 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1857 db6d8ad8 2020-03-21 stsp goto done;
1860 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1861 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1862 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1863 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1864 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1866 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1868 7848a0e1 2020-03-19 stsp goto done;
1869 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1871 e8a967e0 2020-03-21 stsp goto done;
1873 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1875 7848a0e1 2020-03-19 stsp goto done;
1876 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1877 6338a6a1 2020-03-21 stsp goto done;
1879 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1881 6338a6a1 2020-03-21 stsp goto done;
1882 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1884 6338a6a1 2020-03-21 stsp goto done;
1887 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1888 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1889 6338a6a1 2020-03-21 stsp new_id_str);
1891 7848a0e1 2020-03-19 stsp free(old_id);
1892 7848a0e1 2020-03-19 stsp free(new_id_str);
1893 7848a0e1 2020-03-19 stsp return err;
1896 f1bcca34 2020-03-25 stsp static const struct got_error *
1897 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1898 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1900 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1901 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1902 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1904 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1905 bcf34b0e 2020-03-26 stsp if (err) {
1906 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1907 bcf34b0e 2020-03-26 stsp return err;
1908 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1910 bcf34b0e 2020-03-26 stsp goto done;
1912 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1914 bcf34b0e 2020-03-26 stsp goto done;
1916 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1917 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1918 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1919 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1921 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1923 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1924 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1925 bcf34b0e 2020-03-26 stsp goto done;
1927 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1928 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1930 bcf34b0e 2020-03-26 stsp goto done;
1932 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1934 bcf34b0e 2020-03-26 stsp goto done;
1936 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1937 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1938 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1942 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1943 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1944 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1945 bcf34b0e 2020-03-26 stsp err = unlock_err;
1947 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1948 f1bcca34 2020-03-25 stsp return err;
1951 2ab43947 2020-03-18 stsp __dead static void
1952 7848a0e1 2020-03-19 stsp usage_fetch(void)
1954 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1955 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1956 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1957 13f12b09 2020-03-21 stsp getprogname());
1961 7848a0e1 2020-03-19 stsp static const struct got_error *
1962 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1963 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1965 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1966 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1967 3789fd73 2020-03-26 stsp char *id_str = NULL;
1969 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1970 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1972 3789fd73 2020-03-26 stsp return err;
1973 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1974 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1975 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1976 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1979 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1981 3789fd73 2020-03-26 stsp return err;
1982 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1984 3789fd73 2020-03-26 stsp goto done;
1986 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1988 3789fd73 2020-03-26 stsp goto done;
1989 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1990 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1991 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1996 3789fd73 2020-03-26 stsp free(id_str);
1997 3789fd73 2020-03-26 stsp return NULL;
2000 3789fd73 2020-03-26 stsp static const struct got_error *
2001 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2002 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2003 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2004 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2006 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2007 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2008 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2009 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2010 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2011 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2013 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2015 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2017 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2019 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2021 3789fd73 2020-03-26 stsp goto done;
2023 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2024 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2026 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2027 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2028 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2029 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2030 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2032 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2033 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2034 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2035 3789fd73 2020-03-26 stsp goto done;
2037 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2041 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2042 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2045 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2048 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2049 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2052 3789fd73 2020-03-26 stsp if (pe != NULL)
2055 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2059 3789fd73 2020-03-26 stsp if (local_refname) {
2060 3789fd73 2020-03-26 stsp struct got_reference *ref;
2061 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2062 3789fd73 2020-03-26 stsp if (err) {
2063 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2065 3789fd73 2020-03-26 stsp free(local_refname);
2066 3789fd73 2020-03-26 stsp local_refname = NULL;
2069 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2072 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2073 3789fd73 2020-03-26 stsp got_ref_close(ref);
2074 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2075 3789fd73 2020-03-26 stsp err = unlock_err;
2079 3789fd73 2020-03-26 stsp free(local_refname);
2080 3789fd73 2020-03-26 stsp local_refname = NULL;
2084 3789fd73 2020-03-26 stsp free(remote_namespace);
2085 3789fd73 2020-03-26 stsp free(local_refname);
2086 0e4002ca 2020-03-21 stsp return err;
2089 0e4002ca 2020-03-21 stsp static const struct got_error *
2090 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2091 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2093 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2094 0e4002ca 2020-03-21 stsp char *remote_refname;
2095 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2097 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2098 0e4002ca 2020-03-21 stsp refname += 5;
2100 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2101 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2102 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2104 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2105 0e4002ca 2020-03-21 stsp if (err) {
2106 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2107 0e4002ca 2020-03-21 stsp goto done;
2108 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2110 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2111 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2112 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2113 9f142382 2020-03-21 stsp err = unlock_err;
2114 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2117 0e4002ca 2020-03-21 stsp free(remote_refname);
2118 161728eb 2021-07-24 stsp return err;
2121 161728eb 2021-07-24 stsp static const struct got_error *
2122 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2124 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2125 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2126 161728eb 2021-07-24 stsp char *id_str = NULL;
2127 161728eb 2021-07-24 stsp const char *target;
2129 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2130 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2132 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2134 161728eb 2021-07-24 stsp goto done;
2135 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2137 161728eb 2021-07-24 stsp goto done;
2138 161728eb 2021-07-24 stsp target = id_str;
2141 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2143 161728eb 2021-07-24 stsp goto done;
2145 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2148 161728eb 2021-07-24 stsp free(id_str);
2149 f21ec2f0 2020-03-21 stsp return err;
2152 f21ec2f0 2020-03-21 stsp static const struct got_error *
2153 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2155 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2156 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2157 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2158 161728eb 2021-07-24 stsp char *prefix;
2160 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2162 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2163 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2164 161728eb 2021-07-24 stsp goto done;
2166 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2168 161728eb 2021-07-24 stsp goto done;
2170 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2171 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2173 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2174 161728eb 2021-07-24 stsp return err;
2177 161728eb 2021-07-24 stsp static const struct got_error *
2178 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2180 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2181 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2182 7848a0e1 2020-03-19 stsp const char *remote_name;
2183 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2184 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2185 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2186 7848a0e1 2020-03-19 stsp int nremotes;
2187 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2188 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2189 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2190 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2191 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2192 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2193 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2194 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2195 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2196 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2197 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2198 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2200 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2201 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2202 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2203 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2205 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2206 7848a0e1 2020-03-19 stsp switch (ch) {
2208 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2211 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2212 4ba14133 2020-03-20 stsp optarg, NULL);
2213 4ba14133 2020-03-20 stsp if (error)
2214 4ba14133 2020-03-20 stsp return error;
2217 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2220 41b0de12 2020-03-21 stsp list_refs_only = 1;
2223 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2224 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2225 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2227 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2230 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2233 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2234 7848a0e1 2020-03-19 stsp verbosity = 0;
2235 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2236 7848a0e1 2020-03-19 stsp verbosity++;
2239 7848a0e1 2020-03-19 stsp verbosity = -1;
2242 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2243 0e4002ca 2020-03-21 stsp optarg, NULL);
2244 0e4002ca 2020-03-21 stsp if (error)
2245 0e4002ca 2020-03-21 stsp return error;
2248 161728eb 2021-07-24 stsp delete_remote = 1;
2251 7848a0e1 2020-03-19 stsp usage_fetch();
2255 7848a0e1 2020-03-19 stsp argc -= optind;
2256 7848a0e1 2020-03-19 stsp argv += optind;
2258 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2259 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2260 41b0de12 2020-03-21 stsp if (list_refs_only) {
2261 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2262 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2263 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2264 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2265 f21ec2f0 2020-03-21 stsp if (delete_refs)
2266 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2267 161728eb 2021-07-24 stsp if (delete_remote)
2268 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2270 161728eb 2021-07-24 stsp if (delete_remote) {
2271 161728eb 2021-07-24 stsp if (fetch_all_branches)
2272 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2273 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2274 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2275 161728eb 2021-07-24 stsp if (delete_refs)
2276 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2277 161728eb 2021-07-24 stsp if (replace_tags)
2278 161728eb 2021-07-24 stsp option_conflict('X', 't');
2279 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2280 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2283 161728eb 2021-07-24 stsp if (argc == 0) {
2284 161728eb 2021-07-24 stsp if (delete_remote)
2285 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2286 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2287 161728eb 2021-07-24 stsp } else if (argc == 1)
2288 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2290 7848a0e1 2020-03-19 stsp usage_fetch();
2292 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2293 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2294 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2295 7848a0e1 2020-03-19 stsp goto done;
2298 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2299 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2300 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2301 7848a0e1 2020-03-19 stsp goto done;
2303 7848a0e1 2020-03-19 stsp error = NULL;
2304 7848a0e1 2020-03-19 stsp if (worktree) {
2305 7848a0e1 2020-03-19 stsp repo_path =
2306 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2307 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2308 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2309 7848a0e1 2020-03-19 stsp if (error)
2310 7848a0e1 2020-03-19 stsp goto done;
2312 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2313 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2314 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2315 7848a0e1 2020-03-19 stsp goto done;
2320 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2321 7848a0e1 2020-03-19 stsp if (error)
2322 7848a0e1 2020-03-19 stsp goto done;
2324 161728eb 2021-07-24 stsp if (delete_remote) {
2325 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2326 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2329 50b0790e 2020-09-11 stsp if (worktree) {
2330 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2331 50b0790e 2020-09-11 stsp if (worktree_conf) {
2332 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2333 50b0790e 2020-09-11 stsp worktree_conf);
2334 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2335 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2336 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2342 50b0790e 2020-09-11 stsp if (remote == NULL) {
2343 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2344 50b0790e 2020-09-11 stsp if (repo_conf) {
2345 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2346 50b0790e 2020-09-11 stsp repo_conf);
2347 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2348 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2349 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2355 50b0790e 2020-09-11 stsp if (remote == NULL) {
2356 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2357 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2358 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2359 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2364 50b0790e 2020-09-11 stsp if (remote == NULL) {
2365 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2366 50b0790e 2020-09-11 stsp goto done;
2369 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2370 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2371 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2372 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2373 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2374 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2377 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2378 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2379 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2380 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2384 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2385 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2386 7848a0e1 2020-03-19 stsp if (error)
2387 7848a0e1 2020-03-19 stsp goto done;
2389 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2390 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2391 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2392 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2393 7848a0e1 2020-03-19 stsp err(1, "pledge");
2395 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2396 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2397 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2398 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2399 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2400 7848a0e1 2020-03-19 stsp err(1, "pledge");
2402 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2403 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2404 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2405 7848a0e1 2020-03-19 stsp goto done;
2407 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2408 7848a0e1 2020-03-19 stsp goto done;
2411 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2412 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2413 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2414 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2415 7848a0e1 2020-03-19 stsp goto done;
2418 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2419 7848a0e1 2020-03-19 stsp if (error)
2420 7848a0e1 2020-03-19 stsp goto done;
2422 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2423 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2424 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2426 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2427 9c52365f 2020-03-21 stsp server_path, verbosity);
2428 7848a0e1 2020-03-19 stsp if (error)
2429 7848a0e1 2020-03-19 stsp goto done;
2431 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2432 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2433 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2434 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2435 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2436 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2437 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2438 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2439 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2440 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2441 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2442 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2443 7848a0e1 2020-03-19 stsp if (error)
2444 7848a0e1 2020-03-19 stsp goto done;
2446 41b0de12 2020-03-21 stsp if (list_refs_only) {
2447 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2448 41b0de12 2020-03-21 stsp goto done;
2451 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2452 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2453 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2454 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2455 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2456 984065c8 2020-03-19 stsp if (error)
2457 984065c8 2020-03-19 stsp goto done;
2458 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2459 984065c8 2020-03-19 stsp free(id_str);
2460 984065c8 2020-03-19 stsp id_str = NULL;
2463 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2464 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2465 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2466 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2467 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2468 7848a0e1 2020-03-19 stsp char *remote_refname;
2470 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2471 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2472 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2473 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2474 0e4002ca 2020-03-21 stsp if (error)
2475 0e4002ca 2020-03-21 stsp goto done;
2479 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2480 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2481 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2482 7848a0e1 2020-03-19 stsp if (error) {
2483 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2484 7848a0e1 2020-03-19 stsp goto done;
2485 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2487 7848a0e1 2020-03-19 stsp if (error)
2488 7848a0e1 2020-03-19 stsp goto done;
2490 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2491 db6d8ad8 2020-03-21 stsp verbosity, repo);
2492 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2493 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2494 9f142382 2020-03-21 stsp error = unlock_err;
2495 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2496 7848a0e1 2020-03-19 stsp if (error)
2497 7848a0e1 2020-03-19 stsp goto done;
2499 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2500 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2501 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2502 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2503 7848a0e1 2020-03-19 stsp goto done;
2506 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2507 7848a0e1 2020-03-19 stsp if (error) {
2508 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2509 7848a0e1 2020-03-19 stsp goto done;
2510 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2511 688f11b3 2020-03-21 stsp verbosity, repo);
2512 7848a0e1 2020-03-19 stsp if (error)
2513 7848a0e1 2020-03-19 stsp goto done;
2515 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2516 db6d8ad8 2020-03-21 stsp verbosity, repo);
2517 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2518 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2519 9f142382 2020-03-21 stsp error = unlock_err;
2520 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2521 7848a0e1 2020-03-19 stsp if (error)
2522 7848a0e1 2020-03-19 stsp goto done;
2525 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2526 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2527 2ec30c80 2020-03-20 stsp if (error) {
2528 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2529 2ec30c80 2020-03-20 stsp goto done;
2530 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2532 2ec30c80 2020-03-20 stsp if (error)
2533 2ec30c80 2020-03-20 stsp goto done;
2535 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2536 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2537 9f142382 2020-03-21 stsp error = unlock_err;
2538 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2542 f1bcca34 2020-03-25 stsp if (delete_refs) {
2543 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2544 3789fd73 2020-03-26 stsp verbosity, repo);
2545 f1bcca34 2020-03-25 stsp if (error)
2546 f1bcca34 2020-03-25 stsp goto done;
2549 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2550 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2551 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2552 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2553 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2554 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2555 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2557 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2560 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2563 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2564 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2565 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2566 f1bcca34 2020-03-25 stsp goto done;
2568 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2569 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2570 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2571 f1bcca34 2020-03-25 stsp free(remote_refname);
2572 f1bcca34 2020-03-25 stsp goto done;
2575 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2577 f1bcca34 2020-03-25 stsp if (error) {
2578 f1bcca34 2020-03-25 stsp free(remote_refname);
2579 f1bcca34 2020-03-25 stsp free(remote_target);
2580 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2581 f1bcca34 2020-03-25 stsp error = NULL;
2584 f1bcca34 2020-03-25 stsp goto done;
2586 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2587 f1bcca34 2020-03-25 stsp verbosity, repo);
2588 f1bcca34 2020-03-25 stsp free(remote_refname);
2589 f1bcca34 2020-03-25 stsp free(remote_target);
2590 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2591 f1bcca34 2020-03-25 stsp if (error)
2592 f1bcca34 2020-03-25 stsp goto done;
2596 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2597 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2598 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2599 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2600 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2602 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2603 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2604 1d0f4054 2021-06-17 stsp if (repo) {
2605 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2606 1d0f4054 2021-06-17 stsp if (error == NULL)
2607 1d0f4054 2021-06-17 stsp error = close_err;
2609 7848a0e1 2020-03-19 stsp if (worktree)
2610 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2611 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2612 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2613 7848a0e1 2020-03-19 stsp free(pe->data);
2615 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2616 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2617 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2618 7848a0e1 2020-03-19 stsp free(pe->data);
2620 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2621 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2622 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2623 7848a0e1 2020-03-19 stsp free(id_str);
2624 7848a0e1 2020-03-19 stsp free(cwd);
2625 7848a0e1 2020-03-19 stsp free(repo_path);
2626 7848a0e1 2020-03-19 stsp free(pack_hash);
2627 7848a0e1 2020-03-19 stsp free(proto);
2628 7848a0e1 2020-03-19 stsp free(host);
2629 7848a0e1 2020-03-19 stsp free(port);
2630 7848a0e1 2020-03-19 stsp free(server_path);
2631 7848a0e1 2020-03-19 stsp free(repo_name);
2632 7848a0e1 2020-03-19 stsp return error;
2636 7848a0e1 2020-03-19 stsp __dead static void
2637 2ab43947 2020-03-18 stsp usage_checkout(void)
2639 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2640 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2644 2ab43947 2020-03-18 stsp static void
2645 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2647 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2648 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2649 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2650 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2651 2ab43947 2020-03-18 stsp getprogname());
2654 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2655 2ab43947 2020-03-18 stsp const char *worktree_path;
2656 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2659 2ab43947 2020-03-18 stsp static const struct got_error *
2660 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2662 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2664 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2665 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2666 2ab43947 2020-03-18 stsp return NULL;
2668 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2669 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2670 2ab43947 2020-03-18 stsp return NULL;
2673 2ab43947 2020-03-18 stsp while (path[0] == '/')
2676 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2677 2ab43947 2020-03-18 stsp return NULL;
2680 2ab43947 2020-03-18 stsp static const struct got_error *
2681 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2683 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2684 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2685 2ab43947 2020-03-18 stsp return NULL;
2688 2ab43947 2020-03-18 stsp static const struct got_error *
2689 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2690 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2691 2ab43947 2020-03-18 stsp struct got_repository *repo)
2693 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2694 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2696 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2697 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2699 2ab43947 2020-03-18 stsp return err;
2701 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2702 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2705 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2706 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2708 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2710 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2718 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2719 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2720 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2722 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2723 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2724 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2725 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2726 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2727 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2729 2ab43947 2020-03-18 stsp free(yca_id);
2730 2ab43947 2020-03-18 stsp return NULL;
2733 2ab43947 2020-03-18 stsp static const struct got_error *
2734 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2735 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2736 2ab43947 2020-03-18 stsp struct got_repository *repo)
2738 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2739 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2740 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2741 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2743 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2745 2ab43947 2020-03-18 stsp goto done;
2747 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2748 2ab43947 2020-03-18 stsp is_same_branch = 1;
2749 2ab43947 2020-03-18 stsp goto done;
2751 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2752 2ab43947 2020-03-18 stsp is_same_branch = 1;
2753 2ab43947 2020-03-18 stsp goto done;
2756 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2758 2ab43947 2020-03-18 stsp goto done;
2760 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2761 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2763 2ab43947 2020-03-18 stsp goto done;
2765 2ab43947 2020-03-18 stsp for (;;) {
2766 2ab43947 2020-03-18 stsp struct got_object_id *id;
2767 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2768 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2769 2ab43947 2020-03-18 stsp if (err) {
2770 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2771 2ab43947 2020-03-18 stsp err = NULL;
2776 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2778 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2779 2ab43947 2020-03-18 stsp is_same_branch = 1;
2785 2ab43947 2020-03-18 stsp if (graph)
2786 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2787 2ab43947 2020-03-18 stsp free(head_commit_id);
2788 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2789 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2790 2ab43947 2020-03-18 stsp return err;
2793 4ed7e80c 2018-05-20 stsp static const struct got_error *
2794 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2796 4b6c9460 2020-03-05 stsp static char msg[512];
2797 4b6c9460 2020-03-05 stsp const char *branch_name;
2799 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2800 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2802 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2804 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2805 4b6c9460 2020-03-05 stsp branch_name += 11;
2807 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2808 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2809 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2810 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2811 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2812 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2814 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2817 4b6c9460 2020-03-05 stsp static const struct got_error *
2818 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2820 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2821 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2822 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2823 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2824 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2825 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2826 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2827 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2828 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2829 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2830 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2831 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2832 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2834 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2836 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2837 0bb8a95e 2018-03-12 stsp switch (ch) {
2839 08573d5b 2019-05-14 stsp branch_name = optarg;
2842 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2843 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2844 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2847 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2850 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2853 2deda0b9 2019-03-07 stsp usage_checkout();
2854 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2858 0bb8a95e 2018-03-12 stsp argc -= optind;
2859 0bb8a95e 2018-03-12 stsp argv += optind;
2861 6715a751 2018-03-16 stsp #ifndef PROFILE
2862 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2863 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2864 c09a553d 2018-03-12 stsp err(1, "pledge");
2866 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2867 c47340da 2020-09-20 stsp char *base, *dotgit;
2868 f0207f6a 2020-10-19 stsp const char *path;
2869 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2870 76089277 2018-04-01 stsp if (repo_path == NULL)
2871 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2872 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2873 76089277 2018-04-01 stsp if (cwd == NULL) {
2874 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2875 76089277 2018-04-01 stsp goto done;
2877 c47340da 2020-09-20 stsp if (path_prefix[0])
2878 f0207f6a 2020-10-19 stsp path = path_prefix;
2880 f0207f6a 2020-10-19 stsp path = repo_path;
2881 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2882 f0207f6a 2020-10-19 stsp if (error)
2883 c47340da 2020-09-20 stsp goto done;
2884 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2885 c09a553d 2018-03-12 stsp if (dotgit)
2886 c09a553d 2018-03-12 stsp *dotgit = '\0';
2887 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2888 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2889 f0207f6a 2020-10-19 stsp free(base);
2890 76089277 2018-04-01 stsp goto done;
2892 f0207f6a 2020-10-19 stsp free(base);
2893 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2894 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2895 76089277 2018-04-01 stsp if (repo_path == NULL) {
2896 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2897 76089277 2018-04-01 stsp goto done;
2899 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2900 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2901 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2902 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2904 b4b3a7dd 2019-07-22 stsp goto done;
2906 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2907 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2908 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2909 b4b3a7dd 2019-07-22 stsp goto done;
2913 c09a553d 2018-03-12 stsp usage_checkout();
2915 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2916 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2918 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2919 c09a553d 2018-03-12 stsp if (error != NULL)
2920 c02c541e 2019-03-29 stsp goto done;
2922 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2923 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2924 c530dc23 2019-07-23 stsp if (error) {
2925 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2926 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2927 c530dc23 2019-07-23 stsp goto done;
2928 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2929 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2930 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2931 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2932 c530dc23 2019-07-23 stsp goto done;
2936 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2937 c02c541e 2019-03-29 stsp if (error)
2938 c09a553d 2018-03-12 stsp goto done;
2940 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2941 c09a553d 2018-03-12 stsp if (error != NULL)
2942 c09a553d 2018-03-12 stsp goto done;
2944 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2945 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2946 c09a553d 2018-03-12 stsp goto done;
2948 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2949 c09a553d 2018-03-12 stsp if (error != NULL)
2950 c09a553d 2018-03-12 stsp goto done;
2952 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2953 e5dc7198 2018-12-29 stsp path_prefix);
2954 e5dc7198 2018-12-29 stsp if (error != NULL)
2955 e5dc7198 2018-12-29 stsp goto done;
2956 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2957 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2958 49520a32 2018-12-29 stsp goto done;
2961 8069f636 2019-01-12 stsp if (commit_id_str) {
2962 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2963 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2964 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2965 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2967 84de9106 2020-12-26 stsp if (error)
2968 84de9106 2020-12-26 stsp goto done;
2969 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2970 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2971 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2972 30837e32 2019-07-25 stsp if (error)
2973 8069f636 2019-01-12 stsp goto done;
2974 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2975 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2976 8069f636 2019-01-12 stsp if (error != NULL) {
2977 8069f636 2019-01-12 stsp free(commit_id);
2978 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2979 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2980 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2982 8069f636 2019-01-12 stsp goto done;
2984 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2985 4b6c9460 2020-03-05 stsp if (error) {
2986 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2987 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2988 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2990 45d344f6 2019-05-14 stsp goto done;
2992 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2993 8069f636 2019-01-12 stsp commit_id);
2994 8069f636 2019-01-12 stsp free(commit_id);
2995 8069f636 2019-01-12 stsp if (error)
2996 8069f636 2019-01-12 stsp goto done;
2999 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3000 f2ea84fa 2019-07-27 stsp if (error)
3001 f2ea84fa 2019-07-27 stsp goto done;
3002 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3003 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3004 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3005 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3006 c09a553d 2018-03-12 stsp if (error != NULL)
3007 c09a553d 2018-03-12 stsp goto done;
3009 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3010 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3011 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3013 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3014 8069f636 2019-01-12 stsp free(commit_id_str);
3015 76089277 2018-04-01 stsp free(repo_path);
3016 507dc3bb 2018-12-29 stsp free(worktree_path);
3017 c47340da 2020-09-20 stsp free(cwd);
3018 507dc3bb 2018-12-29 stsp return error;
3021 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3022 9627c110 2020-04-18 stsp int did_something;
3023 9627c110 2020-04-18 stsp int conflicts;
3024 9627c110 2020-04-18 stsp int obstructed;
3025 9627c110 2020-04-18 stsp int not_updated;
3029 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3031 9627c110 2020-04-18 stsp if (!upa->did_something)
3034 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3035 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3036 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3037 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3038 9627c110 2020-04-18 stsp upa->obstructed);
3039 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3040 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3041 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3044 507dc3bb 2018-12-29 stsp __dead static void
3045 507dc3bb 2018-12-29 stsp usage_update(void)
3047 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
3048 507dc3bb 2018-12-29 stsp getprogname());
3052 1ee397ad 2019-07-12 stsp static const struct got_error *
3053 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3055 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3057 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3058 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3059 1ee397ad 2019-07-12 stsp return NULL;
3061 9627c110 2020-04-18 stsp upa->did_something = 1;
3063 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3064 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3065 1ee397ad 2019-07-12 stsp return NULL;
3067 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3068 9627c110 2020-04-18 stsp upa->conflicts++;
3069 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3070 9627c110 2020-04-18 stsp upa->obstructed++;
3071 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3072 9627c110 2020-04-18 stsp upa->not_updated++;
3074 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3076 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
3077 1ee397ad 2019-07-12 stsp return NULL;
3080 be7061eb 2018-12-30 stsp static const struct got_error *
3081 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3082 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3083 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3085 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3086 a1fb16d8 2019-05-24 stsp char *base_id_str;
3087 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3089 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3090 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3091 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3092 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3093 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3094 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3095 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3098 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3099 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3100 a1fb16d8 2019-05-24 stsp if (err) {
3101 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3102 a1fb16d8 2019-05-24 stsp return err;
3103 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3105 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3106 a1fb16d8 2019-05-24 stsp return NULL;
3108 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3109 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3110 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3112 a1fb16d8 2019-05-24 stsp return err;
3113 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3114 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3115 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3116 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3117 0ebf8283 2019-07-24 stsp return NULL;
3120 0ebf8283 2019-07-24 stsp static const struct got_error *
3121 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3123 0ebf8283 2019-07-24 stsp const struct got_error *err;
3124 0ebf8283 2019-07-24 stsp int in_progress;
3126 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3128 0ebf8283 2019-07-24 stsp return err;
3129 0ebf8283 2019-07-24 stsp if (in_progress)
3130 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3132 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3134 0ebf8283 2019-07-24 stsp return err;
3135 0ebf8283 2019-07-24 stsp if (in_progress)
3136 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3138 a1fb16d8 2019-05-24 stsp return NULL;
3141 a5edda0a 2019-07-27 stsp static const struct got_error *
3142 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3143 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3145 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3146 a5edda0a 2019-07-27 stsp char *path;
3149 a5edda0a 2019-07-27 stsp if (argc == 0) {
3150 a5edda0a 2019-07-27 stsp path = strdup("");
3151 a5edda0a 2019-07-27 stsp if (path == NULL)
3152 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3153 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3156 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3157 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3160 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3161 a5edda0a 2019-07-27 stsp if (err) {
3162 a5edda0a 2019-07-27 stsp free(path);
3167 a5edda0a 2019-07-27 stsp return err;
3170 a1fb16d8 2019-05-24 stsp static const struct got_error *
3171 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3172 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3174 fa51e947 2020-03-27 stsp const struct got_error *err;
3175 fa51e947 2020-03-27 stsp struct got_repository *repo;
3176 fa51e947 2020-03-27 stsp static char msg[512];
3178 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3180 fa51e947 2020-03-27 stsp return orig_err;
3182 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3183 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3184 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3185 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3186 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3187 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3188 fa51e947 2020-03-27 stsp got_repo_close(repo);
3189 fa51e947 2020-03-27 stsp return err;
3192 fa51e947 2020-03-27 stsp static const struct got_error *
3193 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3195 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3196 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3197 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3198 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3199 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3200 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3201 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3202 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3203 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3204 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3206 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3208 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3210 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3211 507dc3bb 2018-12-29 stsp switch (ch) {
3213 024e9686 2019-05-14 stsp branch_name = optarg;
3216 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3217 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3218 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3221 2deda0b9 2019-03-07 stsp usage_update();
3222 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3226 507dc3bb 2018-12-29 stsp argc -= optind;
3227 507dc3bb 2018-12-29 stsp argv += optind;
3229 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3230 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3231 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3232 507dc3bb 2018-12-29 stsp err(1, "pledge");
3234 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3235 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3236 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3237 c4cdcb68 2019-04-03 stsp goto done;
3239 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3240 fa51e947 2020-03-27 stsp if (error) {
3241 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3242 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3243 fa51e947 2020-03-27 stsp worktree_path);
3244 7d5807f4 2019-07-11 stsp goto done;
3247 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3248 f2ea84fa 2019-07-27 stsp if (error)
3249 f2ea84fa 2019-07-27 stsp goto done;
3251 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3253 507dc3bb 2018-12-29 stsp if (error != NULL)
3254 507dc3bb 2018-12-29 stsp goto done;
3256 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3257 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3258 087fb88c 2019-08-04 stsp if (error)
3259 087fb88c 2019-08-04 stsp goto done;
3261 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3262 0266afb7 2019-01-04 stsp if (error)
3263 0266afb7 2019-01-04 stsp goto done;
3265 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3266 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3267 024e9686 2019-05-14 stsp if (error != NULL)
3268 024e9686 2019-05-14 stsp goto done;
3269 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3270 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3271 9c4b8182 2019-01-02 stsp if (error != NULL)
3272 9c4b8182 2019-01-02 stsp goto done;
3273 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3274 507dc3bb 2018-12-29 stsp if (error != NULL)
3275 507dc3bb 2018-12-29 stsp goto done;
3277 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3278 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3279 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3281 84de9106 2020-12-26 stsp if (error)
3282 84de9106 2020-12-26 stsp goto done;
3283 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3284 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3285 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3286 04f57cb3 2019-07-25 stsp free(commit_id_str);
3287 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3288 30837e32 2019-07-25 stsp if (error)
3289 a297e751 2019-07-11 stsp goto done;
3290 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3291 a297e751 2019-07-11 stsp if (error)
3292 507dc3bb 2018-12-29 stsp goto done;
3295 a1fb16d8 2019-05-24 stsp if (branch_name) {
3296 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3297 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3298 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3300 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3301 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3302 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3303 024e9686 2019-05-14 stsp goto done;
3305 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3306 024e9686 2019-05-14 stsp if (error)
3307 024e9686 2019-05-14 stsp goto done;
3308 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3310 a367ff0f 2019-05-14 stsp free(head_commit_id);
3311 024e9686 2019-05-14 stsp if (error != NULL)
3312 024e9686 2019-05-14 stsp goto done;
3313 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3314 a367ff0f 2019-05-14 stsp if (error)
3315 a367ff0f 2019-05-14 stsp goto done;
3316 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3317 024e9686 2019-05-14 stsp if (error)
3318 024e9686 2019-05-14 stsp goto done;
3320 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3321 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3322 a367ff0f 2019-05-14 stsp if (error != NULL) {
3323 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3324 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3325 024e9686 2019-05-14 stsp goto done;
3327 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3328 a367ff0f 2019-05-14 stsp if (error)
3329 a367ff0f 2019-05-14 stsp goto done;
3332 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3333 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3334 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3335 507dc3bb 2018-12-29 stsp commit_id);
3336 507dc3bb 2018-12-29 stsp if (error)
3337 507dc3bb 2018-12-29 stsp goto done;
3340 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3341 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3342 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3343 507dc3bb 2018-12-29 stsp if (error != NULL)
3344 507dc3bb 2018-12-29 stsp goto done;
3346 9627c110 2020-04-18 stsp if (upa.did_something)
3347 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3349 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3350 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3352 c09a553d 2018-03-12 stsp free(worktree_path);
3353 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3354 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3355 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3356 507dc3bb 2018-12-29 stsp free(commit_id);
3357 9c4b8182 2019-01-02 stsp free(commit_id_str);
3358 c09a553d 2018-03-12 stsp return error;
3361 f42b1b34 2018-03-12 stsp static const struct got_error *
3362 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3363 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3364 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3366 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3367 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3369 44392932 2019-08-25 stsp if (blob_id1) {
3370 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3372 44392932 2019-08-25 stsp goto done;
3375 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3377 44392932 2019-08-25 stsp goto done;
3379 44392932 2019-08-25 stsp while (path[0] == '/')
3381 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3382 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3384 44392932 2019-08-25 stsp if (blob1)
3385 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3386 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3387 44392932 2019-08-25 stsp return err;
3390 44392932 2019-08-25 stsp static const struct got_error *
3391 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3392 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3393 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3395 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3396 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3397 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3399 44392932 2019-08-25 stsp if (tree_id1) {
3400 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3402 44392932 2019-08-25 stsp goto done;
3405 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3407 0f2b3dca 2018-12-22 stsp goto done;
3409 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3410 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3411 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3412 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3413 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3414 fe621944 2020-11-10 stsp arg.nlines = 0;
3415 44392932 2019-08-25 stsp while (path[0] == '/')
3417 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3418 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3420 79109fed 2018-03-27 stsp if (tree1)
3421 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3422 366e0a5f 2019-10-10 stsp if (tree2)
3423 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3424 44392932 2019-08-25 stsp return err;
3427 44392932 2019-08-25 stsp static const struct got_error *
3428 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3429 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3431 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3432 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3433 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3434 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3436 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3437 0208f208 2020-05-05 stsp if (qid != NULL) {
3438 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3439 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3442 0208f208 2020-05-05 stsp return err;
3444 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3445 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3446 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3447 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3448 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3450 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3454 0208f208 2020-05-05 stsp if (tree_id1) {
3455 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3457 0208f208 2020-05-05 stsp goto done;
3460 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3461 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3463 0208f208 2020-05-05 stsp goto done;
3465 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3466 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3468 0208f208 2020-05-05 stsp if (tree1)
3469 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3470 0208f208 2020-05-05 stsp if (tree2)
3471 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3472 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3473 0208f208 2020-05-05 stsp return err;
3476 0208f208 2020-05-05 stsp static const struct got_error *
3477 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3478 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3480 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3481 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3482 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3483 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3484 44392932 2019-08-25 stsp struct got_object_qid *qid;
3486 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3487 44392932 2019-08-25 stsp if (qid != NULL) {
3488 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3491 44392932 2019-08-25 stsp return err;
3494 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3495 44392932 2019-08-25 stsp int obj_type;
3496 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3498 44392932 2019-08-25 stsp goto done;
3499 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3500 44392932 2019-08-25 stsp if (err) {
3501 44392932 2019-08-25 stsp free(obj_id2);
3502 44392932 2019-08-25 stsp goto done;
3504 44392932 2019-08-25 stsp if (pcommit) {
3505 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3506 44392932 2019-08-25 stsp qid->id, path);
3507 44392932 2019-08-25 stsp if (err) {
3508 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3509 2e8c69d1 2020-05-04 stsp free(obj_id2);
3510 2e8c69d1 2020-05-04 stsp goto done;
3513 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3514 2e8c69d1 2020-05-04 stsp if (err) {
3515 2e8c69d1 2020-05-04 stsp free(obj_id2);
3516 2e8c69d1 2020-05-04 stsp goto done;
3520 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3521 44392932 2019-08-25 stsp if (err) {
3522 44392932 2019-08-25 stsp free(obj_id2);
3523 44392932 2019-08-25 stsp goto done;
3525 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3526 44392932 2019-08-25 stsp switch (obj_type) {
3527 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3528 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3529 64453f7e 2020-11-21 stsp 0, 0, repo);
3531 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3532 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3533 64453f7e 2020-11-21 stsp 0, 0, repo);
3536 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3539 44392932 2019-08-25 stsp free(obj_id1);
3540 44392932 2019-08-25 stsp free(obj_id2);
3542 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3543 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3545 44392932 2019-08-25 stsp goto done;
3546 579bd556 2020-10-24 stsp if (pcommit) {
3547 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3548 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3550 579bd556 2020-10-24 stsp goto done;
3552 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3554 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3558 0f2b3dca 2018-12-22 stsp free(id_str1);
3559 0f2b3dca 2018-12-22 stsp free(id_str2);
3560 44392932 2019-08-25 stsp if (pcommit)
3561 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3562 79109fed 2018-03-27 stsp return err;
3565 4bb494d5 2018-06-16 stsp static char *
3566 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3568 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3569 09867e48 2019-08-13 stsp char *p, *s;
3571 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3572 09867e48 2019-08-13 stsp if (tm == NULL)
3573 09867e48 2019-08-13 stsp return NULL;
3574 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3575 09867e48 2019-08-13 stsp if (s == NULL)
3576 09867e48 2019-08-13 stsp return NULL;
3577 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3579 6c281f94 2018-06-11 stsp *p = '\0';
3583 6841bf13 2019-11-29 kn static const struct got_error *
3584 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3585 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3587 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3588 6841bf13 2019-11-29 kn regmatch_t regmatch;
3589 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3591 6841bf13 2019-11-29 kn *have_match = 0;
3593 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3597 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3601 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3602 6841bf13 2019-11-29 kn *have_match = 1;
3604 6841bf13 2019-11-29 kn free(id_str);
3605 6841bf13 2019-11-29 kn free(logmsg);
3609 0208f208 2020-05-05 stsp static void
3610 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3611 0208f208 2020-05-05 stsp regex_t *regex)
3613 0208f208 2020-05-05 stsp regmatch_t regmatch;
3614 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3616 0208f208 2020-05-05 stsp *have_match = 0;
3618 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3619 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3620 0208f208 2020-05-05 stsp *have_match = 1;
3626 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3628 888b7d99 2020-12-26 stsp static const struct got_error*
3629 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3630 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3632 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3633 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3635 888b7d99 2020-12-26 stsp const char *name;
3637 888b7d99 2020-12-26 stsp *refs_str = NULL;
3639 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3640 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3641 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3644 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3645 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3647 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3648 199a4027 2019-02-02 stsp name += 5;
3649 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3651 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3652 e34f9ed6 2019-02-02 stsp name += 6;
3653 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3654 141c2bff 2019-02-04 stsp name += 8;
3655 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3656 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3659 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3662 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3663 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3664 5d844a1e 2019-08-13 stsp if (err) {
3665 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3666 48cae60d 2020-09-22 stsp free(ref_id);
3669 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3670 5d844a1e 2019-08-13 stsp err = NULL;
3671 5d844a1e 2019-08-13 stsp tag = NULL;
3674 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3675 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3676 48cae60d 2020-09-22 stsp free(ref_id);
3678 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3679 a436ad14 2019-08-13 stsp if (cmp != 0)
3681 888b7d99 2020-12-26 stsp s = *refs_str;
3682 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3683 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3686 888b7d99 2020-12-26 stsp *refs_str = NULL;
3692 888b7d99 2020-12-26 stsp return err;
3695 888b7d99 2020-12-26 stsp static const struct got_error *
3696 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3697 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3698 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3699 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3700 e600f124 2021-03-21 stsp const char *custom_refs_str)
3702 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3703 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3704 888b7d99 2020-12-26 stsp char datebuf[26];
3705 888b7d99 2020-12-26 stsp time_t committer_time;
3706 888b7d99 2020-12-26 stsp const char *author, *committer;
3707 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3709 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3711 8bf5b3c9 2018-03-17 stsp return err;
3713 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3714 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3715 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3716 e600f124 2021-03-21 stsp if (refs) {
3717 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3719 e600f124 2021-03-21 stsp goto done;
3723 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3724 e600f124 2021-03-21 stsp if (custom_refs_str)
3725 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3727 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3728 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3729 832c249c 2018-06-10 stsp free(id_str);
3730 d3d493d7 2019-02-21 stsp id_str = NULL;
3731 d3d493d7 2019-02-21 stsp free(refs_str);
3732 d3d493d7 2019-02-21 stsp refs_str = NULL;
3733 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3734 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3735 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3736 09867e48 2019-08-13 stsp if (datestr)
3737 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3738 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3739 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3740 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3741 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3742 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3743 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3744 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3745 3fe1abad 2018-06-10 stsp int n = 1;
3746 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3747 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3748 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3750 888b7d99 2020-12-26 stsp goto done;
3751 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3752 a0603db2 2018-06-10 stsp free(id_str);
3753 888b7d99 2020-12-26 stsp id_str = NULL;
3757 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3759 888b7d99 2020-12-26 stsp goto done;
3761 621015ac 2018-07-23 stsp logmsg = logmsg0;
3763 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3765 832c249c 2018-06-10 stsp printf(" %s\n", line);
3766 832c249c 2018-06-10 stsp } while (line);
3767 621015ac 2018-07-23 stsp free(logmsg0);
3769 0208f208 2020-05-05 stsp if (changed_paths) {
3770 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3771 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3772 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3773 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3775 0208f208 2020-05-05 stsp printf("\n");
3777 971751ac 2018-03-27 stsp if (show_patch) {
3778 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3779 971751ac 2018-03-27 stsp if (err == 0)
3780 971751ac 2018-03-27 stsp printf("\n");
3783 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3784 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3786 888b7d99 2020-12-26 stsp free(id_str);
3787 888b7d99 2020-12-26 stsp free(refs_str);
3788 07862c20 2018-09-15 stsp return err;
3791 f42b1b34 2018-03-12 stsp static const struct got_error *
3792 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3793 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3794 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3795 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3796 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3798 f42b1b34 2018-03-12 stsp const struct got_error *err;
3799 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3800 6841bf13 2019-11-29 kn regex_t regex;
3801 6841bf13 2019-11-29 kn int have_match;
3802 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3803 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3804 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3805 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3806 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3808 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3809 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3811 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3812 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3813 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3815 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3817 f42b1b34 2018-03-12 stsp return err;
3818 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3819 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3821 fcc85cad 2018-07-22 stsp goto done;
3823 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3825 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3828 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3829 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3830 372ccdbb 2018-06-10 stsp if (err) {
3831 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3832 9ba79e04 2018-06-11 stsp err = NULL;
3835 b43fbaa0 2018-06-11 stsp if (id == NULL)
3838 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3842 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3843 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3848 6841bf13 2019-11-29 kn if (search_pattern) {
3849 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3851 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3854 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3855 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3856 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3857 6841bf13 2019-11-29 kn if (have_match == 0) {
3858 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3859 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3860 0208f208 2020-05-05 stsp free((char *)pe->path);
3861 0208f208 2020-05-05 stsp free(pe->data);
3863 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3868 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3869 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3872 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3873 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3875 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3876 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3877 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3878 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3882 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3883 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3886 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3887 0208f208 2020-05-05 stsp free((char *)pe->path);
3888 0208f208 2020-05-05 stsp free(pe->data);
3890 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3892 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3893 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3894 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3897 502b9684 2020-07-31 stsp if (show_changed_paths) {
3898 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3899 502b9684 2020-07-31 stsp commit, repo);
3903 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3904 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3905 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3906 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3909 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3910 502b9684 2020-07-31 stsp free((char *)pe->path);
3911 502b9684 2020-07-31 stsp free(pe->data);
3913 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3917 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
3918 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
3919 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3920 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3922 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3923 0208f208 2020-05-05 stsp free((char *)pe->path);
3924 0208f208 2020-05-05 stsp free(pe->data);
3926 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3927 6841bf13 2019-11-29 kn if (search_pattern)
3928 6841bf13 2019-11-29 kn regfree(®ex);
3929 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3930 f42b1b34 2018-03-12 stsp return err;
3933 4ed7e80c 2018-05-20 stsp __dead static void
3934 6f3d1eb0 2018-03-12 stsp usage_log(void)
3936 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3937 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3938 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3942 b1ebc001 2019-08-13 stsp static int
3943 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3945 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3946 b1ebc001 2019-08-13 stsp long long n;
3947 b1ebc001 2019-08-13 stsp const char *errstr;
3949 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3950 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3952 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3953 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3958 4ed7e80c 2018-05-20 stsp static const struct got_error *
3959 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3961 f42b1b34 2018-03-12 stsp const struct got_error *error;
3962 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3963 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3964 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3965 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3966 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3967 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3968 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3969 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3970 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3971 64a96a6d 2018-04-01 stsp const char *errstr;
3972 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3973 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3975 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3977 6715a751 2018-03-16 stsp #ifndef PROFILE
3978 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3981 f42b1b34 2018-03-12 stsp err(1, "pledge");
3984 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3986 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3987 79109fed 2018-03-27 stsp switch (ch) {
3989 79109fed 2018-03-27 stsp show_patch = 1;
3992 0208f208 2020-05-05 stsp show_changed_paths = 1;
3995 d142fc45 2018-04-01 stsp start_commit = optarg;
3998 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4000 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4001 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4004 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4005 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4006 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4009 48c8c60d 2020-01-27 stsp log_branches = 1;
4012 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4013 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4014 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4016 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4019 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4022 6841bf13 2019-11-29 kn search_pattern = optarg;
4025 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4028 2deda0b9 2019-03-07 stsp usage_log();
4029 79109fed 2018-03-27 stsp /* NOTREACHED */
4033 79109fed 2018-03-27 stsp argc -= optind;
4034 79109fed 2018-03-27 stsp argv += optind;
4036 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4037 dc1edbfa 2019-11-29 kn diff_context = 3;
4038 dc1edbfa 2019-11-29 kn else if (!show_patch)
4039 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4041 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4042 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4043 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4044 04ca23f4 2018-07-16 stsp goto done;
4047 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4048 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4049 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4050 50f2fada 2020-04-24 stsp goto done;
4051 50f2fada 2020-04-24 stsp error = NULL;
4054 603cdeb0 2020-10-22 stsp if (argc == 1) {
4055 e7301579 2019-03-18 stsp if (worktree) {
4056 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4058 e7301579 2019-03-18 stsp if (error)
4059 e7301579 2019-03-18 stsp goto done;
4061 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4062 e7301579 2019-03-18 stsp if (path == NULL) {
4063 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4064 e7301579 2019-03-18 stsp goto done;
4067 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4068 cbd1af7a 2019-03-18 stsp usage_log();
4070 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4071 5486daa2 2019-05-11 stsp repo_path = worktree ?
4072 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4074 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4075 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4076 cffc0aa4 2019-02-05 stsp goto done;
4079 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4080 d7d4f210 2018-03-12 stsp if (error != NULL)
4081 04ca23f4 2018-07-16 stsp goto done;
4083 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4084 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4085 c02c541e 2019-03-29 stsp if (error)
4086 c02c541e 2019-03-29 stsp goto done;
4088 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4089 84de9106 2020-12-26 stsp if (error)
4090 84de9106 2020-12-26 stsp goto done;
4092 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4093 888b7d99 2020-12-26 stsp if (error)
4094 888b7d99 2020-12-26 stsp goto done;
4096 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4097 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4098 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4099 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4100 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4101 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4102 3235492e 2018-04-01 stsp if (error != NULL)
4103 d1fe46f9 2020-04-18 stsp goto done;
4104 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4105 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4106 3235492e 2018-04-01 stsp if (error != NULL)
4107 d1fe46f9 2020-04-18 stsp goto done;
4108 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4109 d1fe46f9 2020-04-18 stsp start_id);
4110 d1fe46f9 2020-04-18 stsp if (error != NULL)
4111 d1fe46f9 2020-04-18 stsp goto done;
4112 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4114 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4115 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4116 d1fe46f9 2020-04-18 stsp if (error != NULL)
4117 d1fe46f9 2020-04-18 stsp goto done;
4119 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4120 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4121 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4122 d1fe46f9 2020-04-18 stsp if (error != NULL)
4123 d1fe46f9 2020-04-18 stsp goto done;
4126 deeabeae 2019-08-27 stsp if (worktree) {
4128 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4129 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4130 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4132 603cdeb0 2020-10-22 stsp if (path) {
4133 603cdeb0 2020-10-22 stsp const char *prefix;
4134 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4135 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4136 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4137 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4138 603cdeb0 2020-10-22 stsp goto done;
4142 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4143 8fa913ec 2020-11-14 stsp path ? path : "");
4144 04ca23f4 2018-07-16 stsp if (error != NULL)
4145 04ca23f4 2018-07-16 stsp goto done;
4146 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4147 04ca23f4 2018-07-16 stsp free(path);
4148 04ca23f4 2018-07-16 stsp path = in_repo_path;
4151 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4152 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4153 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4155 04ca23f4 2018-07-16 stsp free(path);
4156 04ca23f4 2018-07-16 stsp free(repo_path);
4157 04ca23f4 2018-07-16 stsp free(cwd);
4158 cffc0aa4 2019-02-05 stsp if (worktree)
4159 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4160 ad242220 2018-09-08 stsp if (repo) {
4161 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4162 ad242220 2018-09-08 stsp if (error == NULL)
4163 1d0f4054 2021-06-17 stsp error = close_err;
4165 888b7d99 2020-12-26 stsp if (refs_idmap)
4166 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4167 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4168 8bf5b3c9 2018-03-17 stsp return error;
4171 4ed7e80c 2018-05-20 stsp __dead static void
4172 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4174 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4175 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4179 b72f483a 2019-02-05 stsp struct print_diff_arg {
4180 b72f483a 2019-02-05 stsp struct got_repository *repo;
4181 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4182 b72f483a 2019-02-05 stsp int diff_context;
4183 3fc0c068 2019-02-10 stsp const char *id_str;
4184 3fc0c068 2019-02-10 stsp int header_shown;
4185 98eaaa12 2019-08-03 stsp int diff_staged;
4186 63035f9f 2019-10-06 stsp int ignore_whitespace;
4187 64453f7e 2020-11-21 stsp int force_text_diff;
4191 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4192 39449a05 2020-07-23 stsp * it as content to the diff engine.
4194 39449a05 2020-07-23 stsp static const struct got_error *
4195 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4196 39449a05 2020-07-23 stsp const char *abspath)
4198 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4199 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4200 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4204 39449a05 2020-07-23 stsp if (dirfd != -1) {
4205 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4206 39449a05 2020-07-23 stsp if (target_len == -1)
4207 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4209 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4210 39449a05 2020-07-23 stsp if (target_len == -1)
4211 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4214 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4215 39449a05 2020-07-23 stsp if (*fd == -1)
4216 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4218 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4219 39449a05 2020-07-23 stsp if (outlen == -1) {
4220 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4221 39449a05 2020-07-23 stsp goto done;
4224 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4225 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4226 39449a05 2020-07-23 stsp goto done;
4229 39449a05 2020-07-23 stsp if (err) {
4230 39449a05 2020-07-23 stsp close(*fd);
4233 39449a05 2020-07-23 stsp return err;
4236 4ed7e80c 2018-05-20 stsp static const struct got_error *
4237 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4238 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4239 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4240 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4242 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4243 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4244 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4245 12463d8b 2019-12-13 stsp int fd = -1;
4246 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4247 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4248 b72f483a 2019-02-05 stsp struct stat sb;
4250 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4251 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4252 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4253 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4254 98eaaa12 2019-08-03 stsp return NULL;
4256 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4257 98eaaa12 2019-08-03 stsp return NULL;
4258 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4259 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4260 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4261 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4262 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4263 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4264 98eaaa12 2019-08-03 stsp return NULL;
4267 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4268 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4269 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4270 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4271 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4274 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4275 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4276 98eaaa12 2019-08-03 stsp switch (staged_status) {
4277 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4278 98eaaa12 2019-08-03 stsp label1 = path;
4279 98eaaa12 2019-08-03 stsp label2 = path;
4281 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4282 98eaaa12 2019-08-03 stsp label2 = path;
4284 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4285 98eaaa12 2019-08-03 stsp label1 = path;
4288 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4290 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4291 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4292 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4295 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4296 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4297 4ce46740 2019-08-08 stsp char *id_str;
4298 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4301 4ce46740 2019-08-08 stsp goto done;
4302 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4304 4ce46740 2019-08-08 stsp goto done;
4305 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4306 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4307 4ce46740 2019-08-08 stsp free(id_str);
4308 4ce46740 2019-08-08 stsp goto done;
4310 4ce46740 2019-08-08 stsp free(id_str);
4311 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4312 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4314 4ce46740 2019-08-08 stsp goto done;
4317 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4318 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4319 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4320 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4321 2ec1f75b 2019-03-26 stsp goto done;
4324 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4325 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4326 12463d8b 2019-12-13 stsp if (fd == -1) {
4327 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4328 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4330 39449a05 2020-07-23 stsp goto done;
4332 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4333 39449a05 2020-07-23 stsp de_name, abspath);
4335 39449a05 2020-07-23 stsp goto done;
4338 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4339 12463d8b 2019-12-13 stsp if (fd == -1) {
4340 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4341 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4343 39449a05 2020-07-23 stsp goto done;
4345 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4346 39449a05 2020-07-23 stsp de_name, abspath);
4348 39449a05 2020-07-23 stsp goto done;
4351 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4352 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4353 2ec1f75b 2019-03-26 stsp goto done;
4355 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4356 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4357 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4358 2ec1f75b 2019-03-26 stsp goto done;
4362 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4364 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4365 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4367 b72f483a 2019-02-05 stsp if (blob1)
4368 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4369 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4370 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4371 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4372 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4373 b72f483a 2019-02-05 stsp free(abspath);
4374 927df6b7 2019-02-10 stsp return err;
4377 927df6b7 2019-02-10 stsp static const struct got_error *
4378 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4380 b00d56cd 2018-04-01 stsp const struct got_error *error;
4381 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4382 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4383 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4384 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4385 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4386 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4387 15a94983 2018-12-23 stsp int type1, type2;
4388 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4389 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4390 c0cc5c62 2018-10-18 stsp const char *errstr;
4391 927df6b7 2019-02-10 stsp char *path = NULL;
4392 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4394 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4396 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4397 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4398 25eccc22 2019-01-04 stsp NULL) == -1)
4399 b00d56cd 2018-04-01 stsp err(1, "pledge");
4402 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4403 b00d56cd 2018-04-01 stsp switch (ch) {
4405 64453f7e 2020-11-21 stsp force_text_diff = 1;
4408 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4410 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4411 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4414 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4415 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4416 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4418 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4421 98eaaa12 2019-08-03 stsp diff_staged = 1;
4424 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4427 2deda0b9 2019-03-07 stsp usage_diff();
4428 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4432 b00d56cd 2018-04-01 stsp argc -= optind;
4433 b00d56cd 2018-04-01 stsp argv += optind;
4435 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4436 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4437 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4438 b72f483a 2019-02-05 stsp goto done;
4440 927df6b7 2019-02-10 stsp if (argc <= 1) {
4441 b72f483a 2019-02-05 stsp if (repo_path)
4443 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4444 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4445 fa51e947 2020-03-27 stsp if (error) {
4446 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4447 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4449 1f03b8da 2020-03-20 stsp goto done;
4451 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4452 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4453 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4454 927df6b7 2019-02-10 stsp goto done;
4456 927df6b7 2019-02-10 stsp if (argc == 1) {
4457 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4459 927df6b7 2019-02-10 stsp if (error)
4460 927df6b7 2019-02-10 stsp goto done;
4462 927df6b7 2019-02-10 stsp path = strdup("");
4463 927df6b7 2019-02-10 stsp if (path == NULL) {
4464 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4465 927df6b7 2019-02-10 stsp goto done;
4468 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4469 98eaaa12 2019-08-03 stsp if (diff_staged)
4470 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4471 98eaaa12 2019-08-03 stsp "objects in repository");
4472 15a94983 2018-12-23 stsp id_str1 = argv[0];
4473 15a94983 2018-12-23 stsp id_str2 = argv[1];
4474 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4475 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4476 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4477 30db809c 2019-06-05 stsp goto done;
4478 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4479 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4480 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4481 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4486 b00d56cd 2018-04-01 stsp usage_diff();
4488 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4489 76089277 2018-04-01 stsp free(repo_path);
4490 b00d56cd 2018-04-01 stsp if (error != NULL)
4491 b00d56cd 2018-04-01 stsp goto done;
4493 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4494 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4495 c02c541e 2019-03-29 stsp if (error)
4496 c02c541e 2019-03-29 stsp goto done;
4498 30db809c 2019-06-05 stsp if (argc <= 1) {
4499 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4500 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4501 b72f483a 2019-02-05 stsp char *id_str;
4503 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4505 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4506 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4507 b72f483a 2019-02-05 stsp if (error)
4508 b72f483a 2019-02-05 stsp goto done;
4509 b72f483a 2019-02-05 stsp arg.repo = repo;
4510 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4511 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4512 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4513 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4514 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4515 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4516 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4518 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4519 72ea6654 2019-07-27 stsp if (error)
4520 72ea6654 2019-07-27 stsp goto done;
4522 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4523 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4524 b72f483a 2019-02-05 stsp free(id_str);
4525 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4526 b72f483a 2019-02-05 stsp goto done;
4529 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4530 84de9106 2020-12-26 stsp if (error)
4531 84de9106 2020-12-26 stsp return error;
4533 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4534 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4535 0adb7196 2019-08-11 stsp if (error)
4536 0adb7196 2019-08-11 stsp goto done;
4538 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4539 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4540 0adb7196 2019-08-11 stsp if (error)
4541 0adb7196 2019-08-11 stsp goto done;
4543 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4544 15a94983 2018-12-23 stsp if (error)
4545 15a94983 2018-12-23 stsp goto done;
4547 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4548 15a94983 2018-12-23 stsp if (error)
4549 15a94983 2018-12-23 stsp goto done;
4551 15a94983 2018-12-23 stsp if (type1 != type2) {
4552 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4553 b00d56cd 2018-04-01 stsp goto done;
4556 15a94983 2018-12-23 stsp switch (type1) {
4557 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4558 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4559 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4560 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4562 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4563 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4564 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4565 64453f7e 2020-11-21 stsp repo, stdout);
4567 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4568 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4569 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4570 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4574 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4577 5e70831e 2019-05-28 stsp free(label1);
4578 5e70831e 2019-05-28 stsp free(label2);
4579 15a94983 2018-12-23 stsp free(id1);
4580 15a94983 2018-12-23 stsp free(id2);
4581 927df6b7 2019-02-10 stsp free(path);
4582 b72f483a 2019-02-05 stsp if (worktree)
4583 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4584 ad242220 2018-09-08 stsp if (repo) {
4585 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4586 ad242220 2018-09-08 stsp if (error == NULL)
4587 1d0f4054 2021-06-17 stsp error = close_err;
4589 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4590 b00d56cd 2018-04-01 stsp return error;
4593 404c43c4 2018-06-21 stsp __dead static void
4594 404c43c4 2018-06-21 stsp usage_blame(void)
4596 9270e621 2019-02-05 stsp fprintf(stderr,
4597 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4598 404c43c4 2018-06-21 stsp getprogname());
4602 28315671 2019-08-14 stsp struct blame_line {
4603 28315671 2019-08-14 stsp int annotated;
4604 28315671 2019-08-14 stsp char *id_str;
4605 82f6abb8 2019-08-14 stsp char *committer;
4606 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4609 28315671 2019-08-14 stsp struct blame_cb_args {
4610 28315671 2019-08-14 stsp struct blame_line *lines;
4611 28315671 2019-08-14 stsp int nlines;
4612 7ef28ff8 2019-08-14 stsp int nlines_prec;
4613 28315671 2019-08-14 stsp int lineno_cur;
4614 28315671 2019-08-14 stsp off_t *line_offsets;
4616 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4619 404c43c4 2018-06-21 stsp static const struct got_error *
4620 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4622 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4623 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4624 28315671 2019-08-14 stsp struct blame_line *bline;
4625 82f6abb8 2019-08-14 stsp char *line = NULL;
4626 28315671 2019-08-14 stsp size_t linesize = 0;
4627 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4628 28315671 2019-08-14 stsp off_t offset;
4629 bcb49d15 2019-08-14 stsp struct tm tm;
4630 bcb49d15 2019-08-14 stsp time_t committer_time;
4632 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4633 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4634 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4636 28315671 2019-08-14 stsp if (sigint_received)
4637 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4639 2839f8b9 2019-08-18 stsp if (lineno == -1)
4640 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4642 28315671 2019-08-14 stsp /* Annotate this line. */
4643 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4644 28315671 2019-08-14 stsp if (bline->annotated)
4645 28315671 2019-08-14 stsp return NULL;
4646 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4648 28315671 2019-08-14 stsp return err;
4650 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4652 82f6abb8 2019-08-14 stsp goto done;
4654 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4655 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4656 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4657 bcb49d15 2019-08-14 stsp goto done;
4660 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4661 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4662 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4663 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4664 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4665 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4666 82f6abb8 2019-08-14 stsp goto done;
4668 28315671 2019-08-14 stsp bline->annotated = 1;
4670 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4671 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4672 28315671 2019-08-14 stsp if (!bline->annotated)
4673 82f6abb8 2019-08-14 stsp goto done;
4675 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4676 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4677 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4678 82f6abb8 2019-08-14 stsp goto done;
4681 28315671 2019-08-14 stsp while (bline->annotated) {
4682 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4683 82f6abb8 2019-08-14 stsp size_t len;
4685 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4686 28315671 2019-08-14 stsp if (ferror(a->f))
4687 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4691 82f6abb8 2019-08-14 stsp committer = bline->committer;
4692 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4693 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4694 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4695 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4697 82f6abb8 2019-08-14 stsp *at = '\0';
4698 82f6abb8 2019-08-14 stsp len = strlen(committer);
4699 82f6abb8 2019-08-14 stsp if (len >= 9)
4700 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4702 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4704 28315671 2019-08-14 stsp *nl = '\0';
4705 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4706 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4708 28315671 2019-08-14 stsp a->lineno_cur++;
4709 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4712 82f6abb8 2019-08-14 stsp if (commit)
4713 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4714 28315671 2019-08-14 stsp free(line);
4715 28315671 2019-08-14 stsp return err;
4718 28315671 2019-08-14 stsp static const struct got_error *
4719 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4721 404c43c4 2018-06-21 stsp const struct got_error *error;
4722 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4723 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4724 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4725 0587e10c 2020-07-23 stsp char *link_target = NULL;
4726 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4727 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4728 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4729 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4730 28315671 2019-08-14 stsp struct blame_cb_args bca;
4731 28315671 2019-08-14 stsp int ch, obj_type, i;
4732 be659d10 2020-11-18 stsp off_t filesize;
4734 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4736 404c43c4 2018-06-21 stsp #ifndef PROFILE
4737 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4738 36e2fb66 2019-01-04 stsp NULL) == -1)
4739 404c43c4 2018-06-21 stsp err(1, "pledge");
4742 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4743 404c43c4 2018-06-21 stsp switch (ch) {
4745 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4748 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4749 66bea077 2018-08-02 stsp if (repo_path == NULL)
4750 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4752 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4755 2deda0b9 2019-03-07 stsp usage_blame();
4756 404c43c4 2018-06-21 stsp /* NOTREACHED */
4760 404c43c4 2018-06-21 stsp argc -= optind;
4761 404c43c4 2018-06-21 stsp argv += optind;
4763 a39318fd 2018-08-02 stsp if (argc == 1)
4764 404c43c4 2018-06-21 stsp path = argv[0];
4766 404c43c4 2018-06-21 stsp usage_blame();
4768 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4769 66bea077 2018-08-02 stsp if (cwd == NULL) {
4770 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4771 66bea077 2018-08-02 stsp goto done;
4773 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4774 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4775 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4776 66bea077 2018-08-02 stsp goto done;
4778 0c06baac 2019-02-05 stsp error = NULL;
4779 0c06baac 2019-02-05 stsp if (worktree) {
4780 0c06baac 2019-02-05 stsp repo_path =
4781 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4782 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4783 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4784 1f03b8da 2020-03-20 stsp if (error)
4785 1f03b8da 2020-03-20 stsp goto done;
4788 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4789 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4790 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4791 0c06baac 2019-02-05 stsp goto done;
4796 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4797 c02c541e 2019-03-29 stsp if (error != NULL)
4798 404c43c4 2018-06-21 stsp goto done;
4800 0c06baac 2019-02-05 stsp if (worktree) {
4801 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4804 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4805 01740607 2020-11-04 stsp if (error)
4806 01740607 2020-11-04 stsp goto done;
4807 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4808 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4809 01740607 2020-11-04 stsp p) == -1) {
4810 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4812 0c06baac 2019-02-05 stsp goto done;
4815 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4817 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4818 01740607 2020-11-04 stsp if (error)
4819 01740607 2020-11-04 stsp goto done;
4820 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4822 0c06baac 2019-02-05 stsp if (error)
4823 66bea077 2018-08-02 stsp goto done;
4825 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4826 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4827 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4828 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4829 404c43c4 2018-06-21 stsp if (error != NULL)
4830 66bea077 2018-08-02 stsp goto done;
4831 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4832 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4833 404c43c4 2018-06-21 stsp if (error != NULL)
4834 66bea077 2018-08-02 stsp goto done;
4836 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4837 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4838 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4840 84de9106 2020-12-26 stsp if (error)
4841 84de9106 2020-12-26 stsp goto done;
4842 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4843 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4844 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4845 30837e32 2019-07-25 stsp if (error)
4846 66bea077 2018-08-02 stsp goto done;
4849 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4850 0587e10c 2020-07-23 stsp commit_id, repo);
4851 0587e10c 2020-07-23 stsp if (error)
4852 0587e10c 2020-07-23 stsp goto done;
4854 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4855 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4856 28315671 2019-08-14 stsp if (error)
4857 28315671 2019-08-14 stsp goto done;
4859 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4860 28315671 2019-08-14 stsp if (error)
4861 28315671 2019-08-14 stsp goto done;
4863 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4864 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4865 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4866 28315671 2019-08-14 stsp goto done;
4869 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4870 28315671 2019-08-14 stsp if (error)
4871 28315671 2019-08-14 stsp goto done;
4872 28315671 2019-08-14 stsp bca.f = got_opentemp();
4873 28315671 2019-08-14 stsp if (bca.f == NULL) {
4874 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4875 28315671 2019-08-14 stsp goto done;
4877 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4878 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4879 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4880 28315671 2019-08-14 stsp goto done;
4882 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4883 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4884 b02560ec 2019-08-19 stsp bca.nlines--;
4886 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4887 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4888 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4889 28315671 2019-08-14 stsp goto done;
4891 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4892 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4893 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4894 7ef28ff8 2019-08-14 stsp while (i > 0) {
4896 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4898 82f6abb8 2019-08-14 stsp bca.repo = repo;
4900 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4901 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4903 66bea077 2018-08-02 stsp free(in_repo_path);
4904 0587e10c 2020-07-23 stsp free(link_target);
4905 66bea077 2018-08-02 stsp free(repo_path);
4906 66bea077 2018-08-02 stsp free(cwd);
4907 404c43c4 2018-06-21 stsp free(commit_id);
4908 28315671 2019-08-14 stsp free(obj_id);
4910 28315671 2019-08-14 stsp got_object_blob_close(blob);
4911 0c06baac 2019-02-05 stsp if (worktree)
4912 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4913 ad242220 2018-09-08 stsp if (repo) {
4914 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4915 ad242220 2018-09-08 stsp if (error == NULL)
4916 1d0f4054 2021-06-17 stsp error = close_err;
4918 3affba96 2019-09-22 stsp if (bca.lines) {
4919 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4920 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4921 3affba96 2019-09-22 stsp free(bline->id_str);
4922 3affba96 2019-09-22 stsp free(bline->committer);
4924 3affba96 2019-09-22 stsp free(bca.lines);
4926 28315671 2019-08-14 stsp free(bca.line_offsets);
4927 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4928 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4929 404c43c4 2018-06-21 stsp return error;
4932 5de5890b 2018-10-18 stsp __dead static void
4933 5de5890b 2018-10-18 stsp usage_tree(void)
4935 c1669e2e 2019-01-09 stsp fprintf(stderr,
4936 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4937 5de5890b 2018-10-18 stsp getprogname());
4941 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4942 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4943 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4945 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4946 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4947 848d6979 2019-08-12 stsp const char *modestr = "";
4948 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4949 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4951 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4952 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4955 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4956 63c5ca5d 2019-08-24 stsp modestr = "$";
4957 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4960 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4962 0d6c6ee3 2020-05-20 stsp return err;
4963 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4964 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4965 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4968 848d6979 2019-08-12 stsp modestr = "@";
4970 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4971 848d6979 2019-08-12 stsp modestr = "/";
4972 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4973 848d6979 2019-08-12 stsp modestr = "*";
4975 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4976 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4977 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4979 0d6c6ee3 2020-05-20 stsp free(link_target);
4980 0d6c6ee3 2020-05-20 stsp return NULL;
4983 5de5890b 2018-10-18 stsp static const struct got_error *
4984 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4985 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4986 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4988 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4989 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4990 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4991 56e0773d 2019-11-28 stsp int nentries, i;
4993 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4995 5de5890b 2018-10-18 stsp goto done;
4997 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4999 5de5890b 2018-10-18 stsp goto done;
5000 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5001 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5002 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5003 5de5890b 2018-10-18 stsp char *id = NULL;
5005 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5008 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5009 5de5890b 2018-10-18 stsp if (show_ids) {
5010 5de5890b 2018-10-18 stsp char *id_str;
5011 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5012 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5014 5de5890b 2018-10-18 stsp goto done;
5015 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5016 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5017 5de5890b 2018-10-18 stsp free(id_str);
5018 5de5890b 2018-10-18 stsp goto done;
5020 5de5890b 2018-10-18 stsp free(id_str);
5022 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5025 0d6c6ee3 2020-05-20 stsp goto done;
5027 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5028 c1669e2e 2019-01-09 stsp char *child_path;
5029 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5030 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5031 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5032 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5033 c1669e2e 2019-01-09 stsp goto done;
5035 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5036 c1669e2e 2019-01-09 stsp root_path, repo);
5037 c1669e2e 2019-01-09 stsp free(child_path);
5039 c1669e2e 2019-01-09 stsp goto done;
5044 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5045 5de5890b 2018-10-18 stsp free(tree_id);
5046 5de5890b 2018-10-18 stsp return err;
5049 5de5890b 2018-10-18 stsp static const struct got_error *
5050 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5052 5de5890b 2018-10-18 stsp const struct got_error *error;
5053 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5054 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5055 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5056 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5057 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5058 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5059 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5062 5de5890b 2018-10-18 stsp #ifndef PROFILE
5063 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5064 0f8d269b 2019-01-04 stsp NULL) == -1)
5065 5de5890b 2018-10-18 stsp err(1, "pledge");
5068 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5069 5de5890b 2018-10-18 stsp switch (ch) {
5071 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5074 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5075 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5076 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5078 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5081 5de5890b 2018-10-18 stsp show_ids = 1;
5084 c1669e2e 2019-01-09 stsp recurse = 1;
5087 2deda0b9 2019-03-07 stsp usage_tree();
5088 5de5890b 2018-10-18 stsp /* NOTREACHED */
5092 5de5890b 2018-10-18 stsp argc -= optind;
5093 5de5890b 2018-10-18 stsp argv += optind;
5095 5de5890b 2018-10-18 stsp if (argc == 1)
5096 5de5890b 2018-10-18 stsp path = argv[0];
5097 5de5890b 2018-10-18 stsp else if (argc > 1)
5098 5de5890b 2018-10-18 stsp usage_tree();
5100 7a2c19d6 2019-02-05 stsp path = NULL;
5102 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5103 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5104 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5105 9bf7a39b 2019-02-05 stsp goto done;
5107 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5108 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5109 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5110 7a2c19d6 2019-02-05 stsp goto done;
5112 7a2c19d6 2019-02-05 stsp error = NULL;
5113 7a2c19d6 2019-02-05 stsp if (worktree) {
5114 7a2c19d6 2019-02-05 stsp repo_path =
5115 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5116 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5117 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5118 7a2c19d6 2019-02-05 stsp if (error)
5119 7a2c19d6 2019-02-05 stsp goto done;
5121 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5122 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5123 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5124 7a2c19d6 2019-02-05 stsp goto done;
5129 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5130 5de5890b 2018-10-18 stsp if (error != NULL)
5131 c02c541e 2019-03-29 stsp goto done;
5133 4fedbf4c 2020-11-07 stsp if (worktree) {
5134 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5137 4fedbf4c 2020-11-07 stsp if (path == NULL)
5138 4fedbf4c 2020-11-07 stsp path = "";
5139 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5140 4fedbf4c 2020-11-07 stsp if (error)
5141 4fedbf4c 2020-11-07 stsp goto done;
5142 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5143 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5144 4fedbf4c 2020-11-07 stsp p) == -1) {
5145 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5147 4fedbf4c 2020-11-07 stsp goto done;
5150 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5151 4fedbf4c 2020-11-07 stsp if (error)
5152 4fedbf4c 2020-11-07 stsp goto done;
5154 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5155 4fedbf4c 2020-11-07 stsp if (error)
5156 4fedbf4c 2020-11-07 stsp goto done;
5157 4fedbf4c 2020-11-07 stsp if (path == NULL)
5158 9bf7a39b 2019-02-05 stsp path = "/";
5159 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5160 9bf7a39b 2019-02-05 stsp if (error != NULL)
5161 9bf7a39b 2019-02-05 stsp goto done;
5164 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5165 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5166 4e0a20a4 2020-03-23 tracey if (worktree)
5167 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5169 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5170 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5171 5de5890b 2018-10-18 stsp if (error != NULL)
5172 5de5890b 2018-10-18 stsp goto done;
5173 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5174 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5175 5de5890b 2018-10-18 stsp if (error != NULL)
5176 5de5890b 2018-10-18 stsp goto done;
5178 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5179 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5180 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5182 84de9106 2020-12-26 stsp if (error)
5183 84de9106 2020-12-26 stsp goto done;
5184 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5185 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5186 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5187 30837e32 2019-07-25 stsp if (error)
5188 5de5890b 2018-10-18 stsp goto done;
5191 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5192 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5194 5de5890b 2018-10-18 stsp free(in_repo_path);
5195 5de5890b 2018-10-18 stsp free(repo_path);
5196 5de5890b 2018-10-18 stsp free(cwd);
5197 5de5890b 2018-10-18 stsp free(commit_id);
5198 7a2c19d6 2019-02-05 stsp if (worktree)
5199 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5200 5de5890b 2018-10-18 stsp if (repo) {
5201 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5202 5de5890b 2018-10-18 stsp if (error == NULL)
5203 1d0f4054 2021-06-17 stsp error = close_err;
5205 5de5890b 2018-10-18 stsp return error;
5208 6bad629b 2019-02-04 stsp __dead static void
5209 6bad629b 2019-02-04 stsp usage_status(void)
5211 f6343036 2021-06-22 stsp fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5212 081470ac 2020-08-13 stsp getprogname());
5216 b72f483a 2019-02-05 stsp static const struct got_error *
5217 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5218 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5219 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5220 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5222 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5223 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5224 081470ac 2020-08-13 stsp if (arg) {
5225 081470ac 2020-08-13 stsp char *status_codes = arg;
5226 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5228 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5229 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5230 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5233 081470ac 2020-08-13 stsp if (i == ncodes)
5234 081470ac 2020-08-13 stsp return NULL;
5236 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5237 b72f483a 2019-02-05 stsp return NULL;
5240 6bad629b 2019-02-04 stsp static const struct got_error *
5241 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5243 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5244 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5245 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5246 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5247 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5248 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5249 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5251 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5253 f6343036 2021-06-22 stsp while ((ch = getopt(argc, argv, "Is:")) != -1) {
5254 6bad629b 2019-02-04 stsp switch (ch) {
5256 f6343036 2021-06-22 stsp no_ignores = 1;
5259 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5260 081470ac 2020-08-13 stsp switch (optarg[i]) {
5261 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5262 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5263 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5264 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5265 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5266 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5267 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5268 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5269 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5272 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5273 081470ac 2020-08-13 stsp optarg[i]);
5276 081470ac 2020-08-13 stsp status_codes = optarg;
5279 2deda0b9 2019-03-07 stsp usage_status();
5280 6bad629b 2019-02-04 stsp /* NOTREACHED */
5284 6bad629b 2019-02-04 stsp argc -= optind;
5285 6bad629b 2019-02-04 stsp argv += optind;
5287 6bad629b 2019-02-04 stsp #ifndef PROFILE
5288 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5289 6bad629b 2019-02-04 stsp NULL) == -1)
5290 6bad629b 2019-02-04 stsp err(1, "pledge");
5292 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5293 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5294 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5295 927df6b7 2019-02-10 stsp goto done;
5298 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5299 fa51e947 2020-03-27 stsp if (error) {
5300 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5301 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5302 a5edda0a 2019-07-27 stsp goto done;
5305 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5307 6bad629b 2019-02-04 stsp if (error != NULL)
5308 6bad629b 2019-02-04 stsp goto done;
5310 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5311 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5312 087fb88c 2019-08-04 stsp if (error)
5313 087fb88c 2019-08-04 stsp goto done;
5315 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5316 6bad629b 2019-02-04 stsp if (error)
5317 6bad629b 2019-02-04 stsp goto done;
5319 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5320 f6343036 2021-06-22 stsp print_status, status_codes, check_cancelled, NULL);
5322 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5323 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5324 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5325 927df6b7 2019-02-10 stsp free(cwd);
5326 d0eebce4 2019-03-11 stsp return error;
5329 d0eebce4 2019-03-11 stsp __dead static void
5330 d0eebce4 2019-03-11 stsp usage_ref(void)
5332 d0eebce4 2019-03-11 stsp fprintf(stderr,
5333 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5334 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5335 d0eebce4 2019-03-11 stsp getprogname());
5339 d0eebce4 2019-03-11 stsp static const struct got_error *
5340 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5342 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5343 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5344 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5346 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5347 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5349 d0eebce4 2019-03-11 stsp return err;
5351 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5352 d0eebce4 2019-03-11 stsp char *refstr;
5353 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5354 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5355 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5356 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5357 d0eebce4 2019-03-11 stsp free(refstr);
5360 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5361 d0eebce4 2019-03-11 stsp return NULL;
5364 d0eebce4 2019-03-11 stsp static const struct got_error *
5365 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5367 161728eb 2021-07-24 stsp const struct got_error *err;
5368 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5370 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5372 d0eebce4 2019-03-11 stsp return err;
5374 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5375 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5376 d0eebce4 2019-03-11 stsp return err;
5379 d0eebce4 2019-03-11 stsp static const struct got_error *
5380 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5382 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5383 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5384 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5387 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5388 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5389 d1644381 2019-07-14 stsp * an unintended typo.
5391 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5392 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5394 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5396 d83d9d5c 2019-05-13 stsp if (err) {
5397 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5399 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5400 d83d9d5c 2019-05-13 stsp return err;
5401 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5403 d83d9d5c 2019-05-13 stsp return err;
5404 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5405 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5407 d83d9d5c 2019-05-13 stsp return err;
5410 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5412 d0eebce4 2019-03-11 stsp goto done;
5414 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5417 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5419 d1c1ae5f 2019-08-12 stsp return err;
5422 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5423 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5425 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5426 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5427 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5430 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5431 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5432 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5434 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5435 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5437 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5439 d1c1ae5f 2019-08-12 stsp return err;
5441 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5443 d1c1ae5f 2019-08-12 stsp goto done;
5445 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5447 d1c1ae5f 2019-08-12 stsp if (target_ref)
5448 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5450 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5451 d0eebce4 2019-03-11 stsp return err;
5454 d0eebce4 2019-03-11 stsp static const struct got_error *
5455 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5457 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5458 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5459 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5460 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5461 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5462 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5463 b2070a3f 2020-03-22 stsp char *refname = NULL;
5465 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5466 d0eebce4 2019-03-11 stsp switch (ch) {
5468 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5471 e31abbf2 2020-03-22 stsp do_delete = 1;
5474 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5475 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5476 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5478 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5481 d0eebce4 2019-03-11 stsp do_list = 1;
5484 e31abbf2 2020-03-22 stsp symref_target = optarg;
5487 d0eebce4 2019-03-11 stsp usage_ref();
5488 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5492 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5493 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5494 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5495 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5496 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5497 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5498 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5499 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5500 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5501 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5502 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5503 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5505 d0eebce4 2019-03-11 stsp argc -= optind;
5506 d0eebce4 2019-03-11 stsp argv += optind;
5508 e31abbf2 2020-03-22 stsp if (do_list) {
5509 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5510 d0eebce4 2019-03-11 stsp usage_ref();
5511 b2070a3f 2020-03-22 stsp if (argc == 1) {
5512 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5513 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5514 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5515 b2070a3f 2020-03-22 stsp goto done;
5519 e31abbf2 2020-03-22 stsp if (argc != 1)
5520 e31abbf2 2020-03-22 stsp usage_ref();
5521 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5522 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5523 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5524 b2070a3f 2020-03-22 stsp goto done;
5528 b2070a3f 2020-03-22 stsp if (refname)
5529 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5531 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5532 e0b57350 2019-03-12 stsp if (do_list) {
5533 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5534 e0b57350 2019-03-12 stsp NULL) == -1)
5535 e0b57350 2019-03-12 stsp err(1, "pledge");
5537 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5538 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5539 e0b57350 2019-03-12 stsp err(1, "pledge");
5542 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5543 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5544 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5545 d0eebce4 2019-03-11 stsp goto done;
5548 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5549 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5550 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5551 d0eebce4 2019-03-11 stsp goto done;
5553 d0eebce4 2019-03-11 stsp error = NULL;
5554 d0eebce4 2019-03-11 stsp if (worktree) {
5555 d0eebce4 2019-03-11 stsp repo_path =
5556 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5557 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5558 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5559 d0eebce4 2019-03-11 stsp if (error)
5560 d0eebce4 2019-03-11 stsp goto done;
5562 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5563 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5564 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5565 d0eebce4 2019-03-11 stsp goto done;
5570 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5571 d0eebce4 2019-03-11 stsp if (error != NULL)
5572 d0eebce4 2019-03-11 stsp goto done;
5574 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5575 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5576 c02c541e 2019-03-29 stsp if (error)
5577 c02c541e 2019-03-29 stsp goto done;
5579 d0eebce4 2019-03-11 stsp if (do_list)
5580 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5581 e31abbf2 2020-03-22 stsp else if (do_delete)
5582 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5583 e31abbf2 2020-03-22 stsp else if (symref_target)
5584 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5586 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5587 e31abbf2 2020-03-22 stsp usage_ref();
5588 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5591 b2070a3f 2020-03-22 stsp free(refname);
5592 1d0f4054 2021-06-17 stsp if (repo) {
5593 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5594 1d0f4054 2021-06-17 stsp if (error == NULL)
5595 1d0f4054 2021-06-17 stsp error = close_err;
5597 4e759de4 2019-06-26 stsp if (worktree)
5598 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5599 4e759de4 2019-06-26 stsp free(cwd);
5600 4e759de4 2019-06-26 stsp free(repo_path);
5601 4e759de4 2019-06-26 stsp return error;
5604 4e759de4 2019-06-26 stsp __dead static void
5605 4e759de4 2019-06-26 stsp usage_branch(void)
5607 4e759de4 2019-06-26 stsp fprintf(stderr,
5608 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5609 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5613 4e759de4 2019-06-26 stsp static const struct got_error *
5614 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5615 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5617 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5618 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5619 4e99b47e 2019-10-04 stsp char *refstr;
5621 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5622 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5623 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5624 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5626 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5628 4e99b47e 2019-10-04 stsp return err;
5629 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5630 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5631 4e99b47e 2019-10-04 stsp marker = "* ";
5633 4e99b47e 2019-10-04 stsp marker = "~ ";
5637 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5638 4e99b47e 2019-10-04 stsp refname += 11;
5639 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5640 4e99b47e 2019-10-04 stsp refname += 18;
5641 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5642 34d4e04c 2021-02-08 stsp refname += 13;
5644 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5645 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5646 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5648 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5649 4e99b47e 2019-10-04 stsp free(refstr);
5650 4e99b47e 2019-10-04 stsp return NULL;
5653 4e99b47e 2019-10-04 stsp static const struct got_error *
5654 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5656 ad89fa31 2019-10-04 stsp const char *refname;
5658 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5659 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5661 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5663 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5664 ad89fa31 2019-10-04 stsp refname += 11;
5665 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5666 ad89fa31 2019-10-04 stsp refname += 18;
5668 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5670 ad89fa31 2019-10-04 stsp return NULL;
5673 ad89fa31 2019-10-04 stsp static const struct got_error *
5674 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5676 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5677 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5678 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5679 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5680 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5682 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5684 4e99b47e 2019-10-04 stsp if (worktree) {
5685 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5686 4e99b47e 2019-10-04 stsp worktree);
5688 4e99b47e 2019-10-04 stsp return err;
5690 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5691 4e99b47e 2019-10-04 stsp worktree);
5693 4e99b47e 2019-10-04 stsp return err;
5695 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5696 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5697 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5699 4e99b47e 2019-10-04 stsp return err;
5700 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5701 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5705 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5706 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5708 4e759de4 2019-06-26 stsp return err;
5710 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5711 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5713 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5715 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5716 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5718 34d4e04c 2021-02-08 stsp return err;
5720 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5721 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5723 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5725 4e759de4 2019-06-26 stsp return NULL;
5728 4e759de4 2019-06-26 stsp static const struct got_error *
5729 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5730 45cd4e47 2019-08-25 stsp const char *branch_name)
5732 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5733 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5734 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
5736 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
5737 2f1457c6 2021-08-27 stsp branch_name += 5;
5738 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
5739 2f1457c6 2021-08-27 stsp branch_name += 6;
5740 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
5741 2f1457c6 2021-08-27 stsp branch_name += 8;
5743 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5744 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5746 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
5747 2f1457c6 2021-08-27 stsp branch_name) == -1) {
5748 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
5749 4e759de4 2019-06-26 stsp goto done;
5752 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
5753 2f1457c6 2021-08-27 stsp if (err) {
5754 2f1457c6 2021-08-27 stsp const struct got_error *err2;
5755 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
5756 2f1457c6 2021-08-27 stsp goto done;
5758 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
5759 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
5760 2f1457c6 2021-08-27 stsp * our error message.
5762 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
5764 2f1457c6 2021-08-27 stsp goto done;
5765 2f1457c6 2021-08-27 stsp err = NULL;
5768 45cd4e47 2019-08-25 stsp if (worktree &&
5769 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5770 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5771 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5772 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5773 45cd4e47 2019-08-25 stsp goto done;
5776 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
5779 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5780 4e759de4 2019-06-26 stsp free(refname);
5781 2f1457c6 2021-08-27 stsp free(remote_refname);
5782 4e759de4 2019-06-26 stsp return err;
5785 4e759de4 2019-06-26 stsp static const struct got_error *
5786 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5787 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5789 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5790 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5791 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5794 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5795 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5796 d3f84d51 2019-07-11 stsp * an unintended typo.
5798 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5799 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5801 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
5802 2f1457c6 2021-08-27 stsp branch_name += 11;
5804 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5805 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5806 62d463ca 2020-10-20 naddy goto done;
5809 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5810 4e759de4 2019-06-26 stsp if (err == NULL) {
5811 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5812 4e759de4 2019-06-26 stsp goto done;
5813 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5814 4e759de4 2019-06-26 stsp goto done;
5816 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5818 4e759de4 2019-06-26 stsp goto done;
5820 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5823 4e759de4 2019-06-26 stsp got_ref_close(ref);
5824 4e759de4 2019-06-26 stsp free(base_refname);
5825 4e759de4 2019-06-26 stsp free(refname);
5826 4e759de4 2019-06-26 stsp return err;
5829 4e759de4 2019-06-26 stsp static const struct got_error *
5830 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5832 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5833 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5834 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5835 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5836 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5837 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5838 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5839 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5840 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5841 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5842 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5844 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5846 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5847 4e759de4 2019-06-26 stsp switch (ch) {
5849 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5852 4e759de4 2019-06-26 stsp delref = optarg;
5855 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5856 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5857 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5859 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5862 4e759de4 2019-06-26 stsp do_list = 1;
5865 da76fce2 2020-02-24 stsp do_update = 0;
5868 4e759de4 2019-06-26 stsp usage_branch();
5869 4e759de4 2019-06-26 stsp /* NOTREACHED */
5873 4e759de4 2019-06-26 stsp if (do_list && delref)
5874 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5876 4e759de4 2019-06-26 stsp argc -= optind;
5877 4e759de4 2019-06-26 stsp argv += optind;
5879 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5880 ad89fa31 2019-10-04 stsp do_show = 1;
5882 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5883 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5885 4e759de4 2019-06-26 stsp if (do_list || delref) {
5886 4e759de4 2019-06-26 stsp if (argc > 0)
5887 4e759de4 2019-06-26 stsp usage_branch();
5888 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5889 4e759de4 2019-06-26 stsp usage_branch();
5891 4e759de4 2019-06-26 stsp #ifndef PROFILE
5892 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5893 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5894 4e759de4 2019-06-26 stsp NULL) == -1)
5895 4e759de4 2019-06-26 stsp err(1, "pledge");
5897 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5898 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5899 4e759de4 2019-06-26 stsp err(1, "pledge");
5902 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5903 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5904 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5905 4e759de4 2019-06-26 stsp goto done;
5908 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5909 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5910 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5911 4e759de4 2019-06-26 stsp goto done;
5913 4e759de4 2019-06-26 stsp error = NULL;
5914 4e759de4 2019-06-26 stsp if (worktree) {
5915 4e759de4 2019-06-26 stsp repo_path =
5916 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5917 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5918 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5919 4e759de4 2019-06-26 stsp if (error)
5920 4e759de4 2019-06-26 stsp goto done;
5922 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5923 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5924 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5925 4e759de4 2019-06-26 stsp goto done;
5930 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5931 4e759de4 2019-06-26 stsp if (error != NULL)
5932 4e759de4 2019-06-26 stsp goto done;
5934 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5935 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5936 4e759de4 2019-06-26 stsp if (error)
5937 4e759de4 2019-06-26 stsp goto done;
5939 ad89fa31 2019-10-04 stsp if (do_show)
5940 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5941 ad89fa31 2019-10-04 stsp else if (do_list)
5942 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5943 4e759de4 2019-06-26 stsp else if (delref)
5944 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5946 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5947 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5948 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5950 84de9106 2020-12-26 stsp if (error)
5951 84de9106 2020-12-26 stsp goto done;
5952 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5953 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5954 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5955 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5956 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5957 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5958 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5959 a74f7e83 2019-11-10 stsp if (error)
5960 a74f7e83 2019-11-10 stsp goto done;
5961 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5962 da76fce2 2020-02-24 stsp if (error)
5963 da76fce2 2020-02-24 stsp goto done;
5964 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5965 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5966 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5968 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5969 da76fce2 2020-02-24 stsp if (error)
5970 da76fce2 2020-02-24 stsp goto done;
5971 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5972 da76fce2 2020-02-24 stsp worktree);
5973 da76fce2 2020-02-24 stsp if (error)
5974 da76fce2 2020-02-24 stsp goto done;
5975 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5977 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5978 da76fce2 2020-02-24 stsp goto done;
5980 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5981 da76fce2 2020-02-24 stsp free(branch_refname);
5982 da76fce2 2020-02-24 stsp if (error)
5983 da76fce2 2020-02-24 stsp goto done;
5984 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5986 da76fce2 2020-02-24 stsp if (error)
5987 da76fce2 2020-02-24 stsp goto done;
5988 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5989 da76fce2 2020-02-24 stsp commit_id);
5990 da76fce2 2020-02-24 stsp if (error)
5991 da76fce2 2020-02-24 stsp goto done;
5992 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5993 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5994 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5996 da76fce2 2020-02-24 stsp if (error)
5997 da76fce2 2020-02-24 stsp goto done;
5998 9627c110 2020-04-18 stsp if (upa.did_something)
5999 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
6000 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6005 da76fce2 2020-02-24 stsp got_ref_close(ref);
6006 1d0f4054 2021-06-17 stsp if (repo) {
6007 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6008 1d0f4054 2021-06-17 stsp if (error == NULL)
6009 1d0f4054 2021-06-17 stsp error = close_err;
6011 d0eebce4 2019-03-11 stsp if (worktree)
6012 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6013 d0eebce4 2019-03-11 stsp free(cwd);
6014 d0eebce4 2019-03-11 stsp free(repo_path);
6015 da76fce2 2020-02-24 stsp free(commit_id);
6016 da76fce2 2020-02-24 stsp free(commit_id_str);
6017 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6018 da76fce2 2020-02-24 stsp free((char *)pe->path);
6019 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6020 d00136be 2019-03-26 stsp return error;
6024 d00136be 2019-03-26 stsp __dead static void
6025 8e7bd50a 2019-08-22 stsp usage_tag(void)
6027 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6028 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6029 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6034 b8bad2ba 2019-08-23 stsp static const struct got_error *
6035 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6037 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6038 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6039 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6040 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6041 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6043 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6044 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6045 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6046 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6048 b8bad2ba 2019-08-23 stsp return err;
6049 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6052 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6055 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6056 b8bad2ba 2019-08-23 stsp free(re_id);
6059 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6060 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6063 b8bad2ba 2019-08-23 stsp while (se) {
6064 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6067 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6068 b8bad2ba 2019-08-23 stsp free(se_id);
6071 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6072 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6074 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6075 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6077 b8bad2ba 2019-08-23 stsp return err;
6078 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6081 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6086 b8bad2ba 2019-08-23 stsp return err;
6090 b8bad2ba 2019-08-23 stsp static const struct got_error *
6091 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6093 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6094 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6095 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6097 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6099 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6101 8e7bd50a 2019-08-22 stsp return err;
6103 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6104 8e7bd50a 2019-08-22 stsp const char *refname;
6105 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6106 8e7bd50a 2019-08-22 stsp char datebuf[26];
6107 d4efa91b 2020-01-14 stsp const char *tagger;
6108 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6109 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6110 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6111 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6113 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6114 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6116 8e7bd50a 2019-08-22 stsp refname += 10;
6117 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6118 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6119 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6122 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6123 8e7bd50a 2019-08-22 stsp free(refstr);
6125 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6128 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6129 d4efa91b 2020-01-14 stsp if (err) {
6130 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6134 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6135 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6136 d4efa91b 2020-01-14 stsp if (err) {
6140 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6141 d4efa91b 2020-01-14 stsp tagger_time =
6142 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6143 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6149 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6150 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6151 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6152 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6156 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6157 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6158 2417344c 2019-08-23 stsp if (datestr)
6159 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6160 d4efa91b 2020-01-14 stsp if (commit)
6161 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6163 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6164 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6165 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6168 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6169 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6172 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6173 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6176 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6177 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6184 8e7bd50a 2019-08-22 stsp free(id_str);
6185 d4efa91b 2020-01-14 stsp if (commit) {
6186 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6189 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6191 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6192 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6193 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6194 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6199 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6201 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6203 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6204 8e7bd50a 2019-08-22 stsp } while (line);
6205 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6208 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6209 8e7bd50a 2019-08-22 stsp return NULL;
6212 8e7bd50a 2019-08-22 stsp static const struct got_error *
6213 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6214 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6216 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6217 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6218 f372d5cd 2019-10-21 stsp char *editor = NULL;
6219 1601cb9f 2020-09-11 naddy int initial_content_len;
6220 8e7bd50a 2019-08-22 stsp int fd = -1;
6222 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6223 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6224 8e7bd50a 2019-08-22 stsp goto done;
6227 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6228 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6229 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6230 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6231 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6232 8e7bd50a 2019-08-22 stsp goto done;
6235 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6237 8e7bd50a 2019-08-22 stsp goto done;
6239 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6240 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6241 97972933 2020-09-11 stsp goto done;
6244 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6246 8e7bd50a 2019-08-22 stsp goto done;
6247 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6248 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6250 8e7bd50a 2019-08-22 stsp free(initial_content);
6251 8e7bd50a 2019-08-22 stsp free(template);
6252 8e7bd50a 2019-08-22 stsp free(editor);
6254 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6255 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6257 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6258 59f86c76 2020-09-11 stsp if (err == NULL)
6259 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6260 59f86c76 2020-09-11 stsp if (err) {
6261 59f86c76 2020-09-11 stsp free(*tagmsg);
6262 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6264 8e7bd50a 2019-08-22 stsp return err;
6267 8e7bd50a 2019-08-22 stsp static const struct got_error *
6268 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6269 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6271 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6272 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6273 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6274 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6275 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6276 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6277 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6278 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6280 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6283 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6284 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6285 8e7bd50a 2019-08-22 stsp * an unintended typo.
6287 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6288 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6290 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6292 8e7bd50a 2019-08-22 stsp return err;
6294 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6296 84de9106 2020-12-26 stsp goto done;
6298 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6299 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6301 8e7bd50a 2019-08-22 stsp goto done;
6303 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6305 8e7bd50a 2019-08-22 stsp goto done;
6307 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6308 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6309 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6310 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6311 62d463ca 2020-10-20 naddy goto done;
6313 8e7bd50a 2019-08-22 stsp tag_name += 10;
6314 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6315 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6316 62d463ca 2020-10-20 naddy goto done;
6319 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6320 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6321 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6322 8e7bd50a 2019-08-22 stsp goto done;
6323 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6324 8e7bd50a 2019-08-22 stsp goto done;
6326 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6327 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6328 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6329 f372d5cd 2019-10-21 stsp if (err) {
6330 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6331 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6332 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6333 8e7bd50a 2019-08-22 stsp goto done;
6337 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6338 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6339 f372d5cd 2019-10-21 stsp if (err) {
6340 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6341 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6342 8e7bd50a 2019-08-22 stsp goto done;
6345 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6346 f372d5cd 2019-10-21 stsp if (err) {
6347 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6348 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6349 8e7bd50a 2019-08-22 stsp goto done;
6352 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6353 f372d5cd 2019-10-21 stsp if (err) {
6354 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6355 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6356 f372d5cd 2019-10-21 stsp goto done;
6359 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6360 f372d5cd 2019-10-21 stsp if (err) {
6361 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6362 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6363 f372d5cd 2019-10-21 stsp goto done;
6365 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6367 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6368 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6369 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6370 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6371 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6372 f372d5cd 2019-10-21 stsp free(tag_id_str);
6374 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6375 8e7bd50a 2019-08-22 stsp free(commit_id);
6376 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6377 8e7bd50a 2019-08-22 stsp free(refname);
6378 8e7bd50a 2019-08-22 stsp free(tagmsg);
6379 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6380 aba9c984 2019-09-08 stsp free(tagger);
6381 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6382 8e7bd50a 2019-08-22 stsp return err;
6385 8e7bd50a 2019-08-22 stsp static const struct got_error *
6386 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6388 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6389 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6390 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6391 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6392 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6393 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6394 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6396 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6397 8e7bd50a 2019-08-22 stsp switch (ch) {
6399 80106605 2020-02-24 stsp commit_id_arg = optarg;
6402 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6405 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6406 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6407 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6409 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6412 8e7bd50a 2019-08-22 stsp do_list = 1;
6415 8e7bd50a 2019-08-22 stsp usage_tag();
6416 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6420 8e7bd50a 2019-08-22 stsp argc -= optind;
6421 8e7bd50a 2019-08-22 stsp argv += optind;
6423 8e7bd50a 2019-08-22 stsp if (do_list) {
6424 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6426 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6427 8e7bd50a 2019-08-22 stsp if (tagmsg)
6428 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6429 8e7bd50a 2019-08-22 stsp if (argc > 0)
6430 8e7bd50a 2019-08-22 stsp usage_tag();
6431 80106605 2020-02-24 stsp } else if (argc != 1)
6432 8e7bd50a 2019-08-22 stsp usage_tag();
6434 a2887370 2019-08-23 stsp tag_name = argv[0];
6436 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6437 8e7bd50a 2019-08-22 stsp if (do_list) {
6438 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6439 8e7bd50a 2019-08-22 stsp NULL) == -1)
6440 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6442 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6443 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6444 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6447 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6448 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6449 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6450 8e7bd50a 2019-08-22 stsp goto done;
6453 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6454 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6455 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6456 8e7bd50a 2019-08-22 stsp goto done;
6458 8e7bd50a 2019-08-22 stsp error = NULL;
6459 8e7bd50a 2019-08-22 stsp if (worktree) {
6460 8e7bd50a 2019-08-22 stsp repo_path =
6461 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6462 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6463 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6464 8e7bd50a 2019-08-22 stsp if (error)
6465 8e7bd50a 2019-08-22 stsp goto done;
6467 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6468 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6469 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6470 8e7bd50a 2019-08-22 stsp goto done;
6475 8e7bd50a 2019-08-22 stsp if (do_list) {
6476 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6477 c9956ddf 2019-09-08 stsp if (error != NULL)
6478 c9956ddf 2019-09-08 stsp goto done;
6479 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6480 8e7bd50a 2019-08-22 stsp if (error)
6481 8e7bd50a 2019-08-22 stsp goto done;
6482 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6484 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6485 c9956ddf 2019-09-08 stsp if (error)
6486 c9956ddf 2019-09-08 stsp goto done;
6487 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6488 c9956ddf 2019-09-08 stsp if (error != NULL)
6489 c9956ddf 2019-09-08 stsp goto done;
6491 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6492 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6493 8e7bd50a 2019-08-22 stsp if (error)
6494 8e7bd50a 2019-08-22 stsp goto done;
6497 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6498 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6499 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6500 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6501 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6502 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6503 8e7bd50a 2019-08-22 stsp if (error)
6504 8e7bd50a 2019-08-22 stsp goto done;
6505 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6506 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6507 8e7bd50a 2019-08-22 stsp if (error)
6508 8e7bd50a 2019-08-22 stsp goto done;
6509 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6510 8e7bd50a 2019-08-22 stsp free(commit_id);
6511 8e7bd50a 2019-08-22 stsp if (error)
6512 8e7bd50a 2019-08-22 stsp goto done;
6515 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6516 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6519 1d0f4054 2021-06-17 stsp if (repo) {
6520 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6521 1d0f4054 2021-06-17 stsp if (error == NULL)
6522 1d0f4054 2021-06-17 stsp error = close_err;
6524 8e7bd50a 2019-08-22 stsp if (worktree)
6525 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6526 8e7bd50a 2019-08-22 stsp free(cwd);
6527 8e7bd50a 2019-08-22 stsp free(repo_path);
6528 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6529 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6530 8e7bd50a 2019-08-22 stsp return error;
6533 8e7bd50a 2019-08-22 stsp __dead static void
6534 d00136be 2019-03-26 stsp usage_add(void)
6536 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6537 022fae89 2019-12-06 tracey getprogname());
6541 d00136be 2019-03-26 stsp static const struct got_error *
6542 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6544 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6546 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6547 4e68cba3 2019-11-23 stsp return NULL;
6550 4e68cba3 2019-11-23 stsp static const struct got_error *
6551 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6553 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6554 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6555 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6556 1dd54920 2019-05-11 stsp char *cwd = NULL;
6557 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6558 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6559 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6561 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6563 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6564 d00136be 2019-03-26 stsp switch (ch) {
6565 022fae89 2019-12-06 tracey case 'I':
6566 022fae89 2019-12-06 tracey no_ignores = 1;
6569 4e68cba3 2019-11-23 stsp can_recurse = 1;
6572 d00136be 2019-03-26 stsp usage_add();
6573 d00136be 2019-03-26 stsp /* NOTREACHED */
6577 d00136be 2019-03-26 stsp argc -= optind;
6578 d00136be 2019-03-26 stsp argv += optind;
6580 43012d58 2019-07-14 stsp #ifndef PROFILE
6581 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6582 43012d58 2019-07-14 stsp NULL) == -1)
6583 43012d58 2019-07-14 stsp err(1, "pledge");
6585 723c305c 2019-05-11 jcs if (argc < 1)
6586 d00136be 2019-03-26 stsp usage_add();
6588 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6589 d00136be 2019-03-26 stsp if (cwd == NULL) {
6590 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6591 d00136be 2019-03-26 stsp goto done;
6594 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6595 fa51e947 2020-03-27 stsp if (error) {
6596 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6597 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6598 d00136be 2019-03-26 stsp goto done;
6601 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6603 031a5338 2019-03-26 stsp if (error != NULL)
6604 031a5338 2019-03-26 stsp goto done;
6606 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6607 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6608 d00136be 2019-03-26 stsp if (error)
6609 d00136be 2019-03-26 stsp goto done;
6611 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6612 6d022e97 2019-08-04 stsp if (error)
6613 022fae89 2019-12-06 tracey goto done;
6615 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6616 4e68cba3 2019-11-23 stsp char *ondisk_path;
6617 4e68cba3 2019-11-23 stsp struct stat sb;
6618 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6619 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6620 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6621 62d463ca 2020-10-20 naddy pe->path) == -1) {
6622 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6623 4e68cba3 2019-11-23 stsp goto done;
6625 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6626 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6627 4e68cba3 2019-11-23 stsp free(ondisk_path);
6630 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6631 4e68cba3 2019-11-23 stsp ondisk_path);
6632 4e68cba3 2019-11-23 stsp free(ondisk_path);
6633 4e68cba3 2019-11-23 stsp goto done;
6635 4e68cba3 2019-11-23 stsp free(ondisk_path);
6636 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6637 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6638 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6639 4e68cba3 2019-11-23 stsp goto done;
6644 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6645 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6647 1d0f4054 2021-06-17 stsp if (repo) {
6648 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6649 1d0f4054 2021-06-17 stsp if (error == NULL)
6650 1d0f4054 2021-06-17 stsp error = close_err;
6652 d00136be 2019-03-26 stsp if (worktree)
6653 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6654 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6655 1dd54920 2019-05-11 stsp free((char *)pe->path);
6656 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6657 2ec1f75b 2019-03-26 stsp free(cwd);
6658 2ec1f75b 2019-03-26 stsp return error;
6661 2ec1f75b 2019-03-26 stsp __dead static void
6662 648e4ef7 2019-07-09 stsp usage_remove(void)
6664 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6665 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6669 2a06fe5f 2019-08-24 stsp static const struct got_error *
6670 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6671 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6673 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6675 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6676 2a06fe5f 2019-08-24 stsp return NULL;
6677 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6678 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6679 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6680 2a06fe5f 2019-08-24 stsp return NULL;
6683 2ec1f75b 2019-03-26 stsp static const struct got_error *
6684 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6686 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6687 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6688 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6689 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6690 17ed4618 2019-06-02 stsp char *cwd = NULL;
6691 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6692 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6693 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6695 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6697 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6698 2ec1f75b 2019-03-26 stsp switch (ch) {
6700 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6702 70e3e7f5 2019-12-13 tracey case 'k':
6703 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6705 f2a9dc41 2019-12-13 tracey case 'R':
6706 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6709 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6710 766841c2 2020-08-13 stsp switch (optarg[i]) {
6711 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6712 766841c2 2020-08-13 stsp delete_local_mods = 1;
6714 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6717 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6718 766841c2 2020-08-13 stsp optarg[i]);
6721 766841c2 2020-08-13 stsp status_codes = optarg;
6724 f2a9dc41 2019-12-13 tracey usage_remove();
6725 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6729 2ec1f75b 2019-03-26 stsp argc -= optind;
6730 2ec1f75b 2019-03-26 stsp argv += optind;
6732 43012d58 2019-07-14 stsp #ifndef PROFILE
6733 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6734 43012d58 2019-07-14 stsp NULL) == -1)
6735 43012d58 2019-07-14 stsp err(1, "pledge");
6737 17ed4618 2019-06-02 stsp if (argc < 1)
6738 648e4ef7 2019-07-09 stsp usage_remove();
6740 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6741 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6742 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6743 2ec1f75b 2019-03-26 stsp goto done;
6745 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6746 fa51e947 2020-03-27 stsp if (error) {
6747 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6748 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6749 2ec1f75b 2019-03-26 stsp goto done;
6752 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6755 2ec1f75b 2019-03-26 stsp goto done;
6757 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6758 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6759 2ec1f75b 2019-03-26 stsp if (error)
6760 2ec1f75b 2019-03-26 stsp goto done;
6762 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6763 6d022e97 2019-08-04 stsp if (error)
6764 6d022e97 2019-08-04 stsp goto done;
6766 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6767 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6768 f2a9dc41 2019-12-13 tracey struct stat sb;
6769 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6770 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6771 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6772 62d463ca 2020-10-20 naddy pe->path) == -1) {
6773 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6774 f2a9dc41 2019-12-13 tracey goto done;
6776 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6777 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6778 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6779 f2a9dc41 2019-12-13 tracey continue;
6781 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6782 f2a9dc41 2019-12-13 tracey ondisk_path);
6783 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6784 f2a9dc41 2019-12-13 tracey goto done;
6786 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6787 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6788 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6789 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6790 f2a9dc41 2019-12-13 tracey goto done;
6795 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6796 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6797 766841c2 2020-08-13 stsp repo, keep_on_disk);
6799 1d0f4054 2021-06-17 stsp if (repo) {
6800 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6801 1d0f4054 2021-06-17 stsp if (error == NULL)
6802 1d0f4054 2021-06-17 stsp error = close_err;
6804 a129376b 2019-03-28 stsp if (worktree)
6805 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6806 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6807 17ed4618 2019-06-02 stsp free((char *)pe->path);
6808 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6809 a129376b 2019-03-28 stsp free(cwd);
6810 a129376b 2019-03-28 stsp return error;
6813 a129376b 2019-03-28 stsp __dead static void
6814 a129376b 2019-03-28 stsp usage_revert(void)
6816 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6817 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6821 1ee397ad 2019-07-12 stsp static const struct got_error *
6822 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6824 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6825 fb9704af 2020-01-27 stsp return NULL;
6827 a129376b 2019-03-28 stsp while (path[0] == '/')
6829 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6830 33aa809d 2019-08-08 stsp return NULL;
6833 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6834 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6835 33aa809d 2019-08-08 stsp const char *action;
6838 33aa809d 2019-08-08 stsp static const struct got_error *
6839 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6840 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6842 33aa809d 2019-08-08 stsp char *line = NULL;
6843 33aa809d 2019-08-08 stsp size_t linesize = 0;
6844 33aa809d 2019-08-08 stsp ssize_t linelen;
6846 33aa809d 2019-08-08 stsp switch (status) {
6847 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6848 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6850 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6851 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6853 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6854 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6855 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6856 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6857 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6858 33aa809d 2019-08-08 stsp printf("%s", line);
6859 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6860 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6861 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6862 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6863 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6866 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6869 33aa809d 2019-08-08 stsp return NULL;
6872 33aa809d 2019-08-08 stsp static const struct got_error *
6873 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6874 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6876 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6877 33aa809d 2019-08-08 stsp char *line = NULL;
6878 33aa809d 2019-08-08 stsp size_t linesize = 0;
6879 33aa809d 2019-08-08 stsp ssize_t linelen;
6880 33aa809d 2019-08-08 stsp int resp = ' ';
6881 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6883 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6885 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6887 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6888 33aa809d 2019-08-08 stsp a->action);
6890 33aa809d 2019-08-08 stsp return err;
6891 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6892 33aa809d 2019-08-08 stsp if (linelen == -1) {
6893 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6894 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6895 33aa809d 2019-08-08 stsp return NULL;
6897 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6899 33aa809d 2019-08-08 stsp *nl = '\0';
6900 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6901 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6902 33aa809d 2019-08-08 stsp printf("y\n");
6903 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6904 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6905 33aa809d 2019-08-08 stsp printf("n\n");
6906 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6907 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6908 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6909 33aa809d 2019-08-08 stsp printf("q\n");
6911 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6912 33aa809d 2019-08-08 stsp free(line);
6913 33aa809d 2019-08-08 stsp return NULL;
6916 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6917 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6918 33aa809d 2019-08-08 stsp a->action);
6920 33aa809d 2019-08-08 stsp return err;
6921 33aa809d 2019-08-08 stsp resp = getchar();
6922 33aa809d 2019-08-08 stsp if (resp == '\n')
6923 33aa809d 2019-08-08 stsp resp = getchar();
6924 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6925 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6926 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6927 33aa809d 2019-08-08 stsp resp = ' ';
6929 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6930 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6931 33aa809d 2019-08-08 stsp resp = ' ';
6935 33aa809d 2019-08-08 stsp if (resp == 'y')
6936 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6937 33aa809d 2019-08-08 stsp else if (resp == 'n')
6938 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6939 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6940 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6942 1ee397ad 2019-07-12 stsp return NULL;
6946 a129376b 2019-03-28 stsp static const struct got_error *
6947 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6949 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6950 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6951 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6952 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6953 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6954 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6955 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6956 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6957 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6958 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6960 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6962 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6963 a129376b 2019-03-28 stsp switch (ch) {
6965 33aa809d 2019-08-08 stsp pflag = 1;
6968 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6971 0f6d7415 2019-08-08 stsp can_recurse = 1;
6974 a129376b 2019-03-28 stsp usage_revert();
6975 a129376b 2019-03-28 stsp /* NOTREACHED */
6979 a129376b 2019-03-28 stsp argc -= optind;
6980 a129376b 2019-03-28 stsp argv += optind;
6982 43012d58 2019-07-14 stsp #ifndef PROFILE
6983 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6984 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6985 43012d58 2019-07-14 stsp err(1, "pledge");
6987 e20a8b6f 2019-06-04 stsp if (argc < 1)
6988 a129376b 2019-03-28 stsp usage_revert();
6989 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6990 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6992 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6993 a129376b 2019-03-28 stsp if (cwd == NULL) {
6994 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6995 a129376b 2019-03-28 stsp goto done;
6997 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6998 fa51e947 2020-03-27 stsp if (error) {
6999 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7000 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7001 2ec1f75b 2019-03-26 stsp goto done;
7004 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7006 a129376b 2019-03-28 stsp if (error != NULL)
7007 a129376b 2019-03-28 stsp goto done;
7009 33aa809d 2019-08-08 stsp if (patch_script_path) {
7010 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7011 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7012 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7013 33aa809d 2019-08-08 stsp patch_script_path);
7014 33aa809d 2019-08-08 stsp goto done;
7017 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7018 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7019 a129376b 2019-03-28 stsp if (error)
7020 a129376b 2019-03-28 stsp goto done;
7022 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7023 6d022e97 2019-08-04 stsp if (error)
7024 6d022e97 2019-08-04 stsp goto done;
7026 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7027 0f6d7415 2019-08-08 stsp char *ondisk_path;
7028 0f6d7415 2019-08-08 stsp struct stat sb;
7029 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7030 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7031 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7032 62d463ca 2020-10-20 naddy pe->path) == -1) {
7033 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7034 0f6d7415 2019-08-08 stsp goto done;
7036 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7037 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7038 0f6d7415 2019-08-08 stsp free(ondisk_path);
7041 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7042 0f6d7415 2019-08-08 stsp ondisk_path);
7043 0f6d7415 2019-08-08 stsp free(ondisk_path);
7044 0f6d7415 2019-08-08 stsp goto done;
7046 0f6d7415 2019-08-08 stsp free(ondisk_path);
7047 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7048 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7049 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7050 0f6d7415 2019-08-08 stsp goto done;
7055 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7056 33aa809d 2019-08-08 stsp cpa.action = "revert";
7057 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7058 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7060 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7061 33aa809d 2019-08-08 stsp error == NULL)
7062 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7063 1d0f4054 2021-06-17 stsp if (repo) {
7064 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7065 1d0f4054 2021-06-17 stsp if (error == NULL)
7066 1d0f4054 2021-06-17 stsp error = close_err;
7068 2ec1f75b 2019-03-26 stsp if (worktree)
7069 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7070 2ec1f75b 2019-03-26 stsp free(path);
7071 d00136be 2019-03-26 stsp free(cwd);
7072 6bad629b 2019-02-04 stsp return error;
7075 c4296144 2019-05-09 stsp __dead static void
7076 c4296144 2019-05-09 stsp usage_commit(void)
7078 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7079 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7083 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7084 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7085 28cf319f 2021-01-28 stsp const char *prepared_log;
7086 28cf319f 2021-01-28 stsp int non_interactive;
7087 e2ba3d07 2019-05-13 stsp const char *editor;
7088 e0870e44 2019-05-13 stsp const char *worktree_path;
7089 76d98825 2019-06-03 stsp const char *branch_name;
7090 314a6357 2019-05-13 stsp const char *repo_path;
7091 e0870e44 2019-05-13 stsp char *logmsg_path;
7095 28cf319f 2021-01-28 stsp static const struct got_error *
7096 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7098 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7099 28cf319f 2021-01-28 stsp FILE *f = NULL;
7100 28cf319f 2021-01-28 stsp struct stat sb;
7103 28cf319f 2021-01-28 stsp *logmsg = NULL;
7104 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7106 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7107 28cf319f 2021-01-28 stsp if (f == NULL)
7108 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7110 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7111 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7112 28cf319f 2021-01-28 stsp goto done;
7114 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7115 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7116 28cf319f 2021-01-28 stsp goto done;
7119 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7120 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7121 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7122 28cf319f 2021-01-28 stsp goto done;
7125 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7126 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7127 28cf319f 2021-01-28 stsp if (ferror(f))
7128 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7130 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7131 28cf319f 2021-01-28 stsp goto done;
7133 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7135 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7136 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7137 28cf319f 2021-01-28 stsp if (err) {
7138 28cf319f 2021-01-28 stsp free(*logmsg);
7139 28cf319f 2021-01-28 stsp *logmsg = NULL;
7141 28cf319f 2021-01-28 stsp return err;
7145 33ad4cbe 2019-05-12 jcs static const struct got_error *
7146 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7149 76d98825 2019-06-03 stsp char *initial_content = NULL;
7150 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7151 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7152 e0870e44 2019-05-13 stsp char *template = NULL;
7153 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7154 1601cb9f 2020-09-11 naddy int initial_content_len;
7155 97972933 2020-09-11 stsp int fd = -1;
7156 33ad4cbe 2019-05-12 jcs size_t len;
7158 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7159 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7160 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7161 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7162 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7163 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7164 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7165 33ad4cbe 2019-05-12 jcs return NULL;
7166 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7167 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7169 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7170 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7172 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7174 28cf319f 2021-01-28 stsp goto done;
7176 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7177 28cf319f 2021-01-28 stsp char *msg;
7178 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7180 28cf319f 2021-01-28 stsp goto done;
7181 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7182 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7183 28cf319f 2021-01-28 stsp free(msg);
7184 28cf319f 2021-01-28 stsp goto done;
7186 28cf319f 2021-01-28 stsp free(msg);
7189 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7190 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7191 1601cb9f 2020-09-11 naddy a->branch_name);
7192 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7193 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7194 e0870e44 2019-05-13 stsp goto done;
7197 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7198 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7199 97972933 2020-09-11 stsp goto done;
7202 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7203 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7204 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7205 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7206 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7209 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7210 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7212 76d98825 2019-06-03 stsp free(initial_content);
7213 e0870e44 2019-05-13 stsp free(template);
7215 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7216 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7218 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7219 59f86c76 2020-09-11 stsp if (err == NULL)
7220 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7221 59f86c76 2020-09-11 stsp if (err) {
7222 59f86c76 2020-09-11 stsp free(*logmsg);
7223 59f86c76 2020-09-11 stsp *logmsg = NULL;
7225 33ad4cbe 2019-05-12 jcs return err;
7228 c4296144 2019-05-09 stsp static const struct got_error *
7229 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7231 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7232 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7233 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7234 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7235 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7236 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7237 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7238 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7239 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7240 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7241 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7242 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7244 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7245 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7247 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7248 c4296144 2019-05-09 stsp switch (ch) {
7250 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7251 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7252 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7253 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7254 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7258 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7259 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7260 c4296144 2019-05-09 stsp logmsg = optarg;
7263 28cf319f 2021-01-28 stsp non_interactive = 1;
7266 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7269 c4296144 2019-05-09 stsp usage_commit();
7270 c4296144 2019-05-09 stsp /* NOTREACHED */
7274 c4296144 2019-05-09 stsp argc -= optind;
7275 c4296144 2019-05-09 stsp argv += optind;
7277 43012d58 2019-07-14 stsp #ifndef PROFILE
7278 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7279 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7280 43012d58 2019-07-14 stsp err(1, "pledge");
7282 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7283 c4296144 2019-05-09 stsp if (cwd == NULL) {
7284 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7285 c4296144 2019-05-09 stsp goto done;
7287 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7288 fa51e947 2020-03-27 stsp if (error) {
7289 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7290 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7291 7d5807f4 2019-07-11 stsp goto done;
7294 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7295 c4296144 2019-05-09 stsp if (error)
7296 a698f62e 2019-07-25 stsp goto done;
7297 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7298 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7299 c4296144 2019-05-09 stsp goto done;
7302 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7303 916f288c 2019-07-30 stsp worktree);
7304 5c1e53bc 2019-07-28 stsp if (error)
7305 5c1e53bc 2019-07-28 stsp goto done;
7307 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7308 c9956ddf 2019-09-08 stsp if (error)
7309 c9956ddf 2019-09-08 stsp goto done;
7310 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7311 c9956ddf 2019-09-08 stsp gitconfig_path);
7312 c4296144 2019-05-09 stsp if (error != NULL)
7313 c4296144 2019-05-09 stsp goto done;
7315 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7316 aba9c984 2019-09-08 stsp if (error)
7317 aba9c984 2019-09-08 stsp return error;
7320 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7321 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7323 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7324 314a6357 2019-05-13 stsp error = get_editor(&editor);
7326 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7327 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7328 c4296144 2019-05-09 stsp if (error)
7329 c4296144 2019-05-09 stsp goto done;
7331 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7332 087fb88c 2019-08-04 stsp if (error)
7333 087fb88c 2019-08-04 stsp goto done;
7335 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7336 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7337 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7338 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7339 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7340 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7341 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7342 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7343 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7344 916f288c 2019-07-30 stsp goto done;
7346 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7348 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7349 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7350 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7351 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7352 e0870e44 2019-05-13 stsp if (error) {
7353 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7354 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7355 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7356 c4296144 2019-05-09 stsp goto done;
7359 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7360 c4296144 2019-05-09 stsp if (error)
7361 c4296144 2019-05-09 stsp goto done;
7362 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7364 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7365 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7366 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7367 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7368 7266f21f 2019-10-21 stsp error == NULL)
7369 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7370 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7371 1d0f4054 2021-06-17 stsp if (repo) {
7372 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7373 1d0f4054 2021-06-17 stsp if (error == NULL)
7374 1d0f4054 2021-06-17 stsp error = close_err;
7376 c4296144 2019-05-09 stsp if (worktree)
7377 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7378 c4296144 2019-05-09 stsp free(cwd);
7379 c4296144 2019-05-09 stsp free(id_str);
7380 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7381 e2ba3d07 2019-05-13 stsp free(editor);
7382 aba9c984 2019-09-08 stsp free(author);
7383 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7384 f8a36e22 2021-08-26 stsp return error;
7387 f8a36e22 2021-08-26 stsp __dead static void
7388 f8a36e22 2021-08-26 stsp usage_send(void)
7390 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7391 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7392 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7396 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7397 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7398 f8a36e22 2021-08-26 stsp int verbosity;
7399 f8a36e22 2021-08-26 stsp int last_ncommits;
7400 f8a36e22 2021-08-26 stsp int last_nobj_total;
7401 f8a36e22 2021-08-26 stsp int last_p_deltify;
7402 f8a36e22 2021-08-26 stsp int last_p_written;
7403 f8a36e22 2021-08-26 stsp int last_p_sent;
7404 f8a36e22 2021-08-26 stsp int printed_something;
7405 f8a36e22 2021-08-26 stsp int sent_something;
7406 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7409 f8a36e22 2021-08-26 stsp static const struct got_error *
7410 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7411 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7412 f8a36e22 2021-08-26 stsp int success)
7414 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7415 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7416 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7417 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7418 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7419 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7421 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7422 f8a36e22 2021-08-26 stsp return NULL;
7424 f8a36e22 2021-08-26 stsp if (refname) {
7425 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7427 f8a36e22 2021-08-26 stsp if (success) {
7428 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7429 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7430 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7431 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7432 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7433 f8a36e22 2021-08-26 stsp status = "deleted";
7434 27b75514 2021-08-28 stsp a->sent_something = 1;
7440 27b75514 2021-08-28 stsp if (a->printed_something)
7441 27b75514 2021-08-28 stsp putchar('\n');
7442 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7443 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7444 f8a36e22 2021-08-26 stsp return NULL;
7447 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7448 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7449 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7450 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7452 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7453 f8a36e22 2021-08-26 stsp print_searching = 1;
7454 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7457 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7458 f8a36e22 2021-08-26 stsp print_searching = 1;
7459 f8a36e22 2021-08-26 stsp print_total = 1;
7460 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7463 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7464 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7465 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7466 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7467 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7470 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7471 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7472 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7473 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7474 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7475 f8a36e22 2021-08-26 stsp print_searching = 1;
7476 f8a36e22 2021-08-26 stsp print_total = 1;
7477 f8a36e22 2021-08-26 stsp print_deltify = 1;
7480 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7481 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7482 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7483 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7484 f8a36e22 2021-08-26 stsp print_searching = 1;
7485 f8a36e22 2021-08-26 stsp print_total = 1;
7486 f8a36e22 2021-08-26 stsp print_deltify = 1;
7487 f8a36e22 2021-08-26 stsp print_written = 1;
7492 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7493 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7494 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7495 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7496 f8a36e22 2021-08-26 stsp print_searching = 1;
7497 f8a36e22 2021-08-26 stsp print_total = 1;
7498 f8a36e22 2021-08-26 stsp print_deltify = 1;
7499 f8a36e22 2021-08-26 stsp print_written = 1;
7500 f8a36e22 2021-08-26 stsp print_sent = 1;
7502 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7505 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7506 f8a36e22 2021-08-26 stsp print_sent)
7507 f8a36e22 2021-08-26 stsp printf("\r");
7508 f8a36e22 2021-08-26 stsp if (print_searching)
7509 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7510 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7511 f8a36e22 2021-08-26 stsp if (print_total)
7512 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7513 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7514 f8a36e22 2021-08-26 stsp if (print_deltify)
7515 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7516 f8a36e22 2021-08-26 stsp if (print_sent)
7517 f8a36e22 2021-08-26 stsp printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7518 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7519 f8a36e22 2021-08-26 stsp else if (print_written)
7520 f8a36e22 2021-08-26 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7521 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7522 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7523 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7524 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7525 f8a36e22 2021-08-26 stsp fflush(stdout);
7527 f8a36e22 2021-08-26 stsp return NULL;
7530 f8a36e22 2021-08-26 stsp static const struct got_error *
7531 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7533 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7534 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7535 f8a36e22 2021-08-26 stsp const char *remote_name;
7536 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7537 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7538 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7539 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7540 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7541 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7542 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7543 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7544 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7545 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7546 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7547 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7548 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7549 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7550 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7551 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7552 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7553 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7554 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7555 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7556 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7558 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7559 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7560 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7561 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7562 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7563 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7565 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7566 f8a36e22 2021-08-26 stsp switch (ch) {
7568 f8a36e22 2021-08-26 stsp send_all_branches = 1;
7571 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
7572 f8a36e22 2021-08-26 stsp if (error)
7573 f8a36e22 2021-08-26 stsp return error;
7574 f8a36e22 2021-08-26 stsp nbranches++;
7577 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
7578 f8a36e22 2021-08-26 stsp if (error)
7579 f8a36e22 2021-08-26 stsp return error;
7582 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
7585 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
7586 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7587 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
7589 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
7592 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
7593 f8a36e22 2021-08-26 stsp if (error)
7594 f8a36e22 2021-08-26 stsp return error;
7598 f8a36e22 2021-08-26 stsp send_all_tags = 1;
7601 f8a36e22 2021-08-26 stsp if (verbosity < 0)
7602 f8a36e22 2021-08-26 stsp verbosity = 0;
7603 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
7604 f8a36e22 2021-08-26 stsp verbosity++;
7607 f8a36e22 2021-08-26 stsp verbosity = -1;
7610 f8a36e22 2021-08-26 stsp usage_send();
7611 f8a36e22 2021-08-26 stsp /* NOTREACHED */
7614 f8a36e22 2021-08-26 stsp argc -= optind;
7615 f8a36e22 2021-08-26 stsp argv += optind;
7617 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
7618 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
7619 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
7620 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
7623 f8a36e22 2021-08-26 stsp if (argc == 0)
7624 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7625 f8a36e22 2021-08-26 stsp else if (argc == 1)
7626 f8a36e22 2021-08-26 stsp remote_name = argv[0];
7628 f8a36e22 2021-08-26 stsp usage_send();
7630 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
7631 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
7632 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
7633 f8a36e22 2021-08-26 stsp goto done;
7636 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7637 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
7638 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7639 f8a36e22 2021-08-26 stsp goto done;
7641 f8a36e22 2021-08-26 stsp error = NULL;
7642 f8a36e22 2021-08-26 stsp if (worktree) {
7643 f8a36e22 2021-08-26 stsp repo_path =
7644 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
7645 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7646 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7647 f8a36e22 2021-08-26 stsp if (error)
7648 f8a36e22 2021-08-26 stsp goto done;
7650 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
7651 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7652 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7653 f8a36e22 2021-08-26 stsp goto done;
7658 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
7659 f8a36e22 2021-08-26 stsp if (error)
7660 f8a36e22 2021-08-26 stsp goto done;
7662 f8a36e22 2021-08-26 stsp if (worktree) {
7663 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
7664 f8a36e22 2021-08-26 stsp if (worktree_conf) {
7665 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7666 f8a36e22 2021-08-26 stsp worktree_conf);
7667 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7668 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7669 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7675 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7676 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
7677 f8a36e22 2021-08-26 stsp if (repo_conf) {
7678 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7679 f8a36e22 2021-08-26 stsp repo_conf);
7680 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7681 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7682 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7688 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7689 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7690 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7691 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7692 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7697 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7698 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7699 f8a36e22 2021-08-26 stsp goto done;
7702 f8a36e22 2021-08-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
7703 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
7704 f8a36e22 2021-08-26 stsp if (error)
7705 f8a36e22 2021-08-26 stsp goto done;
7707 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
7708 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7709 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7710 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
7711 f8a36e22 2021-08-26 stsp err(1, "pledge");
7713 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
7714 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
7715 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7716 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7717 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
7718 f8a36e22 2021-08-26 stsp err(1, "pledge");
7720 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
7721 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
7722 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7723 f8a36e22 2021-08-26 stsp goto done;
7725 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7726 f8a36e22 2021-08-26 stsp goto done;
7729 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
7730 f8a36e22 2021-08-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
7731 f8a36e22 2021-08-26 stsp error = got_error_from_errno2("unveil",
7732 f8a36e22 2021-08-26 stsp GOT_FETCH_PATH_SSH);
7733 f8a36e22 2021-08-26 stsp goto done;
7736 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7737 f8a36e22 2021-08-26 stsp if (error)
7738 f8a36e22 2021-08-26 stsp goto done;
7740 f8a36e22 2021-08-26 stsp if (send_all_branches) {
7741 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
7742 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7743 f8a36e22 2021-08-26 stsp if (error)
7744 f8a36e22 2021-08-26 stsp goto done;
7745 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
7746 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
7747 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
7748 f8a36e22 2021-08-26 stsp branchname, NULL);
7749 f8a36e22 2021-08-26 stsp if (error)
7750 f8a36e22 2021-08-26 stsp goto done;
7751 f8a36e22 2021-08-26 stsp nbranches++;
7753 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
7754 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
7755 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
7756 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
7760 f8a36e22 2021-08-26 stsp if (send_all_tags) {
7761 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
7762 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7763 f8a36e22 2021-08-26 stsp if (error)
7764 f8a36e22 2021-08-26 stsp goto done;
7765 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
7766 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
7767 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
7768 f8a36e22 2021-08-26 stsp tagname, NULL);
7769 f8a36e22 2021-08-26 stsp if (error)
7770 f8a36e22 2021-08-26 stsp goto done;
7776 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
7777 f8a36e22 2021-08-26 stsp * with 'got send -d'.
7778 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
7780 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
7781 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7783 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
7784 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
7785 f8a36e22 2021-08-26 stsp s = strdup(branchname);
7786 f8a36e22 2021-08-26 stsp if (s == NULL) {
7787 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7788 f8a36e22 2021-08-26 stsp goto done;
7791 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7792 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
7793 f8a36e22 2021-08-26 stsp goto done;
7796 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7797 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
7799 f8a36e22 2021-08-26 stsp if (error)
7800 f8a36e22 2021-08-26 stsp goto done;
7801 f8a36e22 2021-08-26 stsp ndelete_branches++;
7804 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
7805 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
7806 f8a36e22 2021-08-26 stsp if (worktree)
7807 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
7808 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
7810 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7811 f8a36e22 2021-08-26 stsp if (error)
7812 f8a36e22 2021-08-26 stsp goto done;
7813 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
7814 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7815 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
7816 f8a36e22 2021-08-26 stsp if (error)
7817 f8a36e22 2021-08-26 stsp goto done;
7819 f8a36e22 2021-08-26 stsp ref = head_ref;
7820 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
7822 f8a36e22 2021-08-26 stsp if (error)
7823 f8a36e22 2021-08-26 stsp goto done;
7824 f8a36e22 2021-08-26 stsp nbranches++;
7827 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
7828 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7829 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
7831 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7832 f8a36e22 2021-08-26 stsp server_path, verbosity);
7833 f8a36e22 2021-08-26 stsp if (error)
7834 f8a36e22 2021-08-26 stsp goto done;
7836 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
7837 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
7838 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
7839 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
7840 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
7841 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
7842 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7843 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7844 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
7845 f8a36e22 2021-08-26 stsp if (spa.printed_something)
7846 f8a36e22 2021-08-26 stsp putchar('\n');
7847 f8a36e22 2021-08-26 stsp if (error)
7848 f8a36e22 2021-08-26 stsp goto done;
7849 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
7850 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
7852 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
7853 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
7854 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
7855 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7856 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
7858 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7859 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
7860 f8a36e22 2021-08-26 stsp if (repo) {
7861 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
7862 f8a36e22 2021-08-26 stsp if (error == NULL)
7863 f8a36e22 2021-08-26 stsp error = close_err;
7865 f8a36e22 2021-08-26 stsp if (worktree)
7866 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
7868 f8a36e22 2021-08-26 stsp got_ref_close(ref);
7869 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
7870 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
7871 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
7872 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
7873 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
7874 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
7875 f8a36e22 2021-08-26 stsp free((char *)pe->path);
7876 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
7877 f8a36e22 2021-08-26 stsp free(cwd);
7878 f8a36e22 2021-08-26 stsp free(repo_path);
7879 f8a36e22 2021-08-26 stsp free(proto);
7880 f8a36e22 2021-08-26 stsp free(host);
7881 f8a36e22 2021-08-26 stsp free(port);
7882 f8a36e22 2021-08-26 stsp free(server_path);
7883 f8a36e22 2021-08-26 stsp free(repo_name);
7884 234035bc 2019-06-01 stsp return error;
7887 234035bc 2019-06-01 stsp __dead static void
7888 234035bc 2019-06-01 stsp usage_cherrypick(void)
7890 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7894 234035bc 2019-06-01 stsp static const struct got_error *
7895 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7897 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7898 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7899 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7900 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7901 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7902 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7903 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7905 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7907 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7908 234035bc 2019-06-01 stsp switch (ch) {
7910 234035bc 2019-06-01 stsp usage_cherrypick();
7911 234035bc 2019-06-01 stsp /* NOTREACHED */
7915 234035bc 2019-06-01 stsp argc -= optind;
7916 234035bc 2019-06-01 stsp argv += optind;
7918 43012d58 2019-07-14 stsp #ifndef PROFILE
7919 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7920 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7921 43012d58 2019-07-14 stsp err(1, "pledge");
7923 234035bc 2019-06-01 stsp if (argc != 1)
7924 234035bc 2019-06-01 stsp usage_cherrypick();
7926 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7927 234035bc 2019-06-01 stsp if (cwd == NULL) {
7928 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7929 234035bc 2019-06-01 stsp goto done;
7931 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7932 fa51e947 2020-03-27 stsp if (error) {
7933 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7934 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7936 234035bc 2019-06-01 stsp goto done;
7939 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7941 234035bc 2019-06-01 stsp if (error != NULL)
7942 234035bc 2019-06-01 stsp goto done;
7944 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7945 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7946 234035bc 2019-06-01 stsp if (error)
7947 234035bc 2019-06-01 stsp goto done;
7949 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7950 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7951 234035bc 2019-06-01 stsp if (error != NULL) {
7952 234035bc 2019-06-01 stsp struct got_reference *ref;
7953 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7954 234035bc 2019-06-01 stsp goto done;
7955 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7956 234035bc 2019-06-01 stsp if (error != NULL)
7957 234035bc 2019-06-01 stsp goto done;
7958 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7959 234035bc 2019-06-01 stsp got_ref_close(ref);
7960 234035bc 2019-06-01 stsp if (error != NULL)
7961 234035bc 2019-06-01 stsp goto done;
7963 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7964 234035bc 2019-06-01 stsp if (error)
7965 234035bc 2019-06-01 stsp goto done;
7967 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7968 234035bc 2019-06-01 stsp if (error)
7969 234035bc 2019-06-01 stsp goto done;
7970 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7971 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7972 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7973 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7975 234035bc 2019-06-01 stsp if (error != NULL)
7976 234035bc 2019-06-01 stsp goto done;
7978 9627c110 2020-04-18 stsp if (upa.did_something)
7979 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7980 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7982 234035bc 2019-06-01 stsp if (commit)
7983 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7984 234035bc 2019-06-01 stsp free(commit_id_str);
7985 234035bc 2019-06-01 stsp if (worktree)
7986 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7987 1d0f4054 2021-06-17 stsp if (repo) {
7988 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7989 1d0f4054 2021-06-17 stsp if (error == NULL)
7990 1d0f4054 2021-06-17 stsp error = close_err;
7992 c4296144 2019-05-09 stsp return error;
7995 5ef14e63 2019-06-02 stsp __dead static void
7996 5ef14e63 2019-06-02 stsp usage_backout(void)
7998 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8002 5ef14e63 2019-06-02 stsp static const struct got_error *
8003 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8005 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8006 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8007 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8008 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8009 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8010 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8011 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8013 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8015 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8016 5ef14e63 2019-06-02 stsp switch (ch) {
8018 5ef14e63 2019-06-02 stsp usage_backout();
8019 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8023 5ef14e63 2019-06-02 stsp argc -= optind;
8024 5ef14e63 2019-06-02 stsp argv += optind;
8026 43012d58 2019-07-14 stsp #ifndef PROFILE
8027 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8028 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8029 43012d58 2019-07-14 stsp err(1, "pledge");
8031 5ef14e63 2019-06-02 stsp if (argc != 1)
8032 5ef14e63 2019-06-02 stsp usage_backout();
8034 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8035 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8036 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8037 5ef14e63 2019-06-02 stsp goto done;
8039 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8040 fa51e947 2020-03-27 stsp if (error) {
8041 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8042 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8043 5ef14e63 2019-06-02 stsp goto done;
8046 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8048 5ef14e63 2019-06-02 stsp if (error != NULL)
8049 5ef14e63 2019-06-02 stsp goto done;
8051 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8052 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8053 5ef14e63 2019-06-02 stsp if (error)
8054 5ef14e63 2019-06-02 stsp goto done;
8056 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8057 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8058 5ef14e63 2019-06-02 stsp if (error != NULL) {
8059 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8060 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8061 5ef14e63 2019-06-02 stsp goto done;
8062 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8063 5ef14e63 2019-06-02 stsp if (error != NULL)
8064 5ef14e63 2019-06-02 stsp goto done;
8065 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8066 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8067 5ef14e63 2019-06-02 stsp if (error != NULL)
8068 5ef14e63 2019-06-02 stsp goto done;
8070 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8071 5ef14e63 2019-06-02 stsp if (error)
8072 5ef14e63 2019-06-02 stsp goto done;
8074 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8075 5ef14e63 2019-06-02 stsp if (error)
8076 5ef14e63 2019-06-02 stsp goto done;
8077 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8078 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8079 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8080 5ef14e63 2019-06-02 stsp goto done;
8083 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8084 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8085 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8086 5ef14e63 2019-06-02 stsp if (error != NULL)
8087 5ef14e63 2019-06-02 stsp goto done;
8089 9627c110 2020-04-18 stsp if (upa.did_something)
8090 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8091 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8093 5ef14e63 2019-06-02 stsp if (commit)
8094 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8095 5ef14e63 2019-06-02 stsp free(commit_id_str);
8096 818c7501 2019-07-11 stsp if (worktree)
8097 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8098 1d0f4054 2021-06-17 stsp if (repo) {
8099 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8100 1d0f4054 2021-06-17 stsp if (error == NULL)
8101 1d0f4054 2021-06-17 stsp error = close_err;
8103 818c7501 2019-07-11 stsp return error;
8106 818c7501 2019-07-11 stsp __dead static void
8107 818c7501 2019-07-11 stsp usage_rebase(void)
8109 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8110 af54c8f8 2019-07-11 stsp getprogname());
8115 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8118 0ebf8283 2019-07-24 stsp size_t len;
8120 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8121 0ebf8283 2019-07-24 stsp if (len > limit)
8122 0ebf8283 2019-07-24 stsp len = limit;
8123 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8124 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8126 0ebf8283 2019-07-24 stsp *nl = '\0';
8129 0ebf8283 2019-07-24 stsp static const struct got_error *
8130 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8132 5943eee2 2019-08-13 stsp const struct got_error *err;
8133 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8134 5943eee2 2019-08-13 stsp const char *s;
8136 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8138 5943eee2 2019-08-13 stsp return err;
8140 5943eee2 2019-08-13 stsp s = logmsg0;
8141 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8144 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8145 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8146 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8147 5943eee2 2019-08-13 stsp goto done;
8150 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8152 5943eee2 2019-08-13 stsp free(logmsg0);
8153 a0ea4fc0 2020-02-28 stsp return err;
8156 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8157 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8159 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8160 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8161 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8163 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8165 a0ea4fc0 2020-02-28 stsp return err;
8167 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8169 a0ea4fc0 2020-02-28 stsp goto done;
8171 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8173 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8175 a0ea4fc0 2020-02-28 stsp goto done;
8177 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8179 a0ea4fc0 2020-02-28 stsp free(id_str);
8180 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8181 a0ea4fc0 2020-02-28 stsp free(logmsg);
8182 5943eee2 2019-08-13 stsp return err;
8185 818c7501 2019-07-11 stsp static const struct got_error *
8186 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8187 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8189 818c7501 2019-07-11 stsp const struct got_error *err;
8190 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8192 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8194 818c7501 2019-07-11 stsp goto done;
8196 ff0d2220 2019-07-11 stsp if (new_id) {
8197 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8199 ff0d2220 2019-07-11 stsp goto done;
8202 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8203 ff0d2220 2019-07-11 stsp if (new_id_str)
8204 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8206 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8208 0ebf8283 2019-07-24 stsp goto done;
8210 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8211 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8213 818c7501 2019-07-11 stsp free(old_id_str);
8214 818c7501 2019-07-11 stsp free(new_id_str);
8215 272a1371 2020-02-28 stsp free(logmsg);
8216 818c7501 2019-07-11 stsp return err;
8219 1ee397ad 2019-07-12 stsp static const struct got_error *
8220 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8221 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8222 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8223 e600f124 2021-03-21 stsp int create_backup)
8225 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8226 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8227 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8230 818c7501 2019-07-11 stsp static const struct got_error *
8231 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8232 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8233 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8234 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8236 ff0d2220 2019-07-11 stsp const struct got_error *error;
8237 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8238 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8240 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8241 ff0d2220 2019-07-11 stsp if (error)
8242 ff0d2220 2019-07-11 stsp return error;
8244 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8245 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8246 ff0d2220 2019-07-11 stsp if (error) {
8247 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8248 ff0d2220 2019-07-11 stsp goto done;
8249 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8251 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8252 ff0d2220 2019-07-11 stsp free(new_commit_id);
8255 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8256 ff0d2220 2019-07-11 stsp return error;
8259 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8260 64c6d990 2019-07-11 stsp const char *path_prefix;
8261 64c6d990 2019-07-11 stsp size_t len;
8262 8ca9bd68 2019-07-25 stsp int errcode;
8265 64c6d990 2019-07-11 stsp static const struct got_error *
8266 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8267 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8268 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8269 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8271 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8273 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8274 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8275 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8277 64c6d990 2019-07-11 stsp return NULL;
8280 64c6d990 2019-07-11 stsp static const struct got_error *
8281 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8282 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8283 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8285 64c6d990 2019-07-11 stsp const struct got_error *err;
8286 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8287 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8288 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8290 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8291 64c6d990 2019-07-11 stsp return NULL;
8293 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8295 64c6d990 2019-07-11 stsp goto done;
8297 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8299 64c6d990 2019-07-11 stsp goto done;
8301 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8302 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8304 64c6d990 2019-07-11 stsp goto done;
8306 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8307 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8309 64c6d990 2019-07-11 stsp goto done;
8311 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8312 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8313 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8314 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8315 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8316 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8317 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8319 64c6d990 2019-07-11 stsp if (tree1)
8320 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8321 64c6d990 2019-07-11 stsp if (tree2)
8322 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8323 64c6d990 2019-07-11 stsp if (commit)
8324 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8325 64c6d990 2019-07-11 stsp if (parent_commit)
8326 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8327 64c6d990 2019-07-11 stsp return err;
8330 ff0d2220 2019-07-11 stsp static const struct got_error *
8331 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8332 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8333 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8334 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8335 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8337 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8338 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8339 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8340 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8341 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8343 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8345 af61c510 2019-07-19 stsp return err;
8347 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8348 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8350 af61c510 2019-07-19 stsp goto done;
8351 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8352 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8353 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8354 af61c510 2019-07-19 stsp if (err) {
8355 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8356 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8357 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8358 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8359 af61c510 2019-07-19 stsp "been reached?!?");
8361 ee780d5c 2020-01-04 stsp goto done;
8363 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8364 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8366 af61c510 2019-07-19 stsp goto done;
8368 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8370 af61c510 2019-07-19 stsp goto done;
8371 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8372 af61c510 2019-07-19 stsp commit_id = parent_id;
8376 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8377 af61c510 2019-07-19 stsp return err;
8380 e600f124 2021-03-21 stsp static const struct got_error *
8381 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8383 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8384 e600f124 2021-03-21 stsp time_t committer_time;
8385 e600f124 2021-03-21 stsp struct tm tm;
8386 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8387 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8388 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8390 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8391 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8392 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8393 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8394 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8396 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8397 e600f124 2021-03-21 stsp if (author0 == NULL)
8398 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8399 e600f124 2021-03-21 stsp author = author0;
8400 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8401 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8402 e600f124 2021-03-21 stsp author = smallerthan + 1;
8403 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8405 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8407 e600f124 2021-03-21 stsp goto done;
8408 e600f124 2021-03-21 stsp logmsg = logmsg0;
8409 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8411 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8412 e600f124 2021-03-21 stsp if (newline)
8413 e600f124 2021-03-21 stsp *newline = '\0';
8415 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8416 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8417 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8419 e600f124 2021-03-21 stsp free(author0);
8420 e600f124 2021-03-21 stsp free(logmsg0);
8421 643b85bc 2021-07-16 stsp return err;
8424 643b85bc 2021-07-16 stsp static const struct got_error *
8425 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8426 643b85bc 2021-07-16 stsp struct got_repository *repo)
8428 643b85bc 2021-07-16 stsp const struct got_error *err;
8429 643b85bc 2021-07-16 stsp char *id_str;
8431 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8433 643b85bc 2021-07-16 stsp return err;
8435 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8437 643b85bc 2021-07-16 stsp goto done;
8439 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8441 643b85bc 2021-07-16 stsp free(id_str);
8442 e600f124 2021-03-21 stsp return err;
8445 e600f124 2021-03-21 stsp static const struct got_error *
8446 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8447 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8448 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8449 e600f124 2021-03-21 stsp struct got_repository *repo)
8451 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8452 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8453 e600f124 2021-03-21 stsp char *refs_str = NULL;
8454 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8455 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8456 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8457 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8458 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8459 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8460 e600f124 2021-03-21 stsp char *custom_refs_str;
8462 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8463 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8465 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8466 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8468 e600f124 2021-03-21 stsp goto done;
8470 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8472 e600f124 2021-03-21 stsp goto done;
8474 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8475 e600f124 2021-03-21 stsp if (refs) {
8476 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8478 e600f124 2021-03-21 stsp goto done;
8481 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8483 e600f124 2021-03-21 stsp goto done;
8485 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8487 e600f124 2021-03-21 stsp goto done;
8489 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8490 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8492 e600f124 2021-03-21 stsp goto done;
8494 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8495 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8496 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8497 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8498 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8499 e600f124 2021-03-21 stsp free(refs_str);
8500 e600f124 2021-03-21 stsp refs_str = NULL;
8502 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8504 e600f124 2021-03-21 stsp goto done;
8506 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8508 e600f124 2021-03-21 stsp goto done;
8510 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8512 e600f124 2021-03-21 stsp goto done;
8514 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8515 e600f124 2021-03-21 stsp if (refs) {
8516 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8518 e600f124 2021-03-21 stsp goto done;
8520 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8521 e600f124 2021-03-21 stsp yca_id_str,
8522 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8523 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8526 e600f124 2021-03-21 stsp free(custom_refs_str);
8527 e600f124 2021-03-21 stsp free(new_commit_id);
8528 e600f124 2021-03-21 stsp free(refs_str);
8529 e600f124 2021-03-21 stsp free(yca_id);
8530 e600f124 2021-03-21 stsp free(yca_id_str);
8531 e600f124 2021-03-21 stsp free(yca_brief_str);
8532 e600f124 2021-03-21 stsp if (new_commit)
8533 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8534 e600f124 2021-03-21 stsp if (yca_commit)
8535 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8537 e600f124 2021-03-21 stsp return NULL;
8540 af61c510 2019-07-19 stsp static const struct got_error *
8541 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8542 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8544 e600f124 2021-03-21 stsp const struct got_error *err;
8545 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8546 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8547 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8548 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8549 e600f124 2021-03-21 stsp char *branch_name = NULL;
8550 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8551 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8552 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8554 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8555 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8557 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8559 e600f124 2021-03-21 stsp return err;
8561 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8563 e600f124 2021-03-21 stsp goto done;
8565 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8566 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8567 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8570 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8571 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8573 e600f124 2021-03-21 stsp goto done;
8575 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8576 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8577 e600f124 2021-03-21 stsp char *slash;
8579 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8583 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8587 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8588 e600f124 2021-03-21 stsp old_commit_id);
8592 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8593 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8594 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8596 e600f124 2021-03-21 stsp while (refname[0] == '/')
8597 e600f124 2021-03-21 stsp refname++;
8599 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8600 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8601 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8604 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8605 e600f124 2021-03-21 stsp if (slash) {
8606 e600f124 2021-03-21 stsp *slash = '\0';
8607 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8610 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8611 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8612 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8613 643b85bc 2021-07-16 stsp if (delete) {
8614 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8615 643b85bc 2021-07-16 stsp old_commit_id, repo);
8617 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8618 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
8625 e600f124 2021-03-21 stsp free(old_commit_id);
8626 e600f124 2021-03-21 stsp old_commit_id = NULL;
8627 e600f124 2021-03-21 stsp free(branch_name);
8628 e600f124 2021-03-21 stsp branch_name = NULL;
8629 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8630 e600f124 2021-03-21 stsp old_commit = NULL;
8633 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8634 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8635 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8638 e600f124 2021-03-21 stsp if (refs_idmap)
8639 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8640 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8641 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8642 e600f124 2021-03-21 stsp free(old_commit_id);
8643 e600f124 2021-03-21 stsp free(branch_name);
8644 e600f124 2021-03-21 stsp if (old_commit)
8645 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8646 e600f124 2021-03-21 stsp return err;
8649 e600f124 2021-03-21 stsp static const struct got_error *
8650 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8652 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8653 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8654 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8655 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8656 818c7501 2019-07-11 stsp char *cwd = NULL;
8657 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8658 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8659 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8660 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8661 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8662 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8663 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8664 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8665 643b85bc 2021-07-16 stsp int delete_backups = 0;
8666 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8667 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8668 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8669 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8670 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8672 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8673 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8675 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8676 818c7501 2019-07-11 stsp switch (ch) {
8678 818c7501 2019-07-11 stsp abort_rebase = 1;
8681 818c7501 2019-07-11 stsp continue_rebase = 1;
8684 e600f124 2021-03-21 stsp list_backups = 1;
8687 643b85bc 2021-07-16 stsp delete_backups = 1;
8690 818c7501 2019-07-11 stsp usage_rebase();
8691 818c7501 2019-07-11 stsp /* NOTREACHED */
8695 818c7501 2019-07-11 stsp argc -= optind;
8696 818c7501 2019-07-11 stsp argv += optind;
8698 43012d58 2019-07-14 stsp #ifndef PROFILE
8699 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8700 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8701 43012d58 2019-07-14 stsp err(1, "pledge");
8703 e600f124 2021-03-21 stsp if (list_backups) {
8704 e600f124 2021-03-21 stsp if (abort_rebase)
8705 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8706 e600f124 2021-03-21 stsp if (continue_rebase)
8707 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8708 643b85bc 2021-07-16 stsp if (delete_backups)
8709 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8710 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8711 643b85bc 2021-07-16 stsp usage_rebase();
8712 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8713 643b85bc 2021-07-16 stsp if (abort_rebase)
8714 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8715 643b85bc 2021-07-16 stsp if (continue_rebase)
8716 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8717 643b85bc 2021-07-16 stsp if (list_backups)
8718 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8719 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8720 818c7501 2019-07-11 stsp usage_rebase();
8722 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8723 e600f124 2021-03-21 stsp usage_rebase();
8724 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8725 e600f124 2021-03-21 stsp if (argc != 0)
8726 e600f124 2021-03-21 stsp usage_rebase();
8727 e600f124 2021-03-21 stsp } else if (argc != 1)
8728 e600f124 2021-03-21 stsp usage_rebase();
8731 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8732 818c7501 2019-07-11 stsp if (cwd == NULL) {
8733 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8734 818c7501 2019-07-11 stsp goto done;
8736 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8737 fa51e947 2020-03-27 stsp if (error) {
8738 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8739 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8740 e600f124 2021-03-21 stsp goto done;
8742 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8743 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8744 e600f124 2021-03-21 stsp "rebase", cwd);
8745 e600f124 2021-03-21 stsp goto done;
8749 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8750 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8751 818c7501 2019-07-11 stsp if (error != NULL)
8752 818c7501 2019-07-11 stsp goto done;
8754 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8755 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8756 7ef62c4e 2020-02-24 stsp if (error)
8757 7ef62c4e 2020-02-24 stsp goto done;
8759 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8760 643b85bc 2021-07-16 stsp error = process_backup_refs(
8761 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8762 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8763 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8766 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8767 7ef62c4e 2020-02-24 stsp worktree);
8768 818c7501 2019-07-11 stsp if (error)
8769 7ef62c4e 2020-02-24 stsp goto done;
8770 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8771 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8772 818c7501 2019-07-11 stsp goto done;
8775 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8776 818c7501 2019-07-11 stsp if (error)
8777 818c7501 2019-07-11 stsp goto done;
8779 f6794adc 2019-07-23 stsp if (abort_rebase) {
8780 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8781 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8782 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8783 f6794adc 2019-07-23 stsp goto done;
8785 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8786 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8787 3e3a69f1 2019-07-25 stsp worktree, repo);
8788 818c7501 2019-07-11 stsp if (error)
8789 818c7501 2019-07-11 stsp goto done;
8790 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8791 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8792 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8793 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8794 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8795 818c7501 2019-07-11 stsp if (error)
8796 818c7501 2019-07-11 stsp goto done;
8797 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8798 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8799 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8802 818c7501 2019-07-11 stsp if (continue_rebase) {
8803 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8804 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8805 f6794adc 2019-07-23 stsp goto done;
8807 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8808 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8809 3e3a69f1 2019-07-25 stsp worktree, repo);
8810 818c7501 2019-07-11 stsp if (error)
8811 818c7501 2019-07-11 stsp goto done;
8813 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8814 01757395 2019-07-12 stsp resume_commit_id, repo);
8815 818c7501 2019-07-11 stsp if (error)
8816 818c7501 2019-07-11 stsp goto done;
8818 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8819 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8820 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8821 818c7501 2019-07-11 stsp goto done;
8824 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8825 818c7501 2019-07-11 stsp if (error != NULL)
8826 818c7501 2019-07-11 stsp goto done;
8829 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8830 ff0d2220 2019-07-11 stsp if (error)
8831 ff0d2220 2019-07-11 stsp goto done;
8833 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8834 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8836 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8837 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8838 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8839 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8840 818c7501 2019-07-11 stsp if (error)
8841 818c7501 2019-07-11 stsp goto done;
8842 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8843 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8844 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8845 818c7501 2019-07-11 stsp "with work tree's branch");
8846 818c7501 2019-07-11 stsp goto done;
8849 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8850 a51a74b3 2019-07-27 stsp if (error) {
8851 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8852 a51a74b3 2019-07-27 stsp goto done;
8853 a51a74b3 2019-07-27 stsp error = NULL;
8855 df3ed485 2021-01-31 stsp static char msg[128];
8856 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8857 df3ed485 2021-01-31 stsp "%s is already based on %s",
8858 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8859 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8860 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8861 a51a74b3 2019-07-27 stsp goto done;
8863 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8864 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8865 818c7501 2019-07-11 stsp if (error)
8866 818c7501 2019-07-11 stsp goto done;
8869 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8870 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8871 818c7501 2019-07-11 stsp if (error)
8872 818c7501 2019-07-11 stsp goto done;
8874 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8875 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
8876 fc66b545 2019-08-12 stsp if (pid == NULL) {
8877 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8878 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8879 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8880 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8881 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8882 fc66b545 2019-08-12 stsp if (error)
8883 fc66b545 2019-08-12 stsp goto done;
8884 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8885 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8886 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8889 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8890 fc66b545 2019-08-12 stsp goto done;
8892 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8893 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8894 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8895 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8896 818c7501 2019-07-11 stsp commit = NULL;
8897 818c7501 2019-07-11 stsp if (error)
8898 818c7501 2019-07-11 stsp goto done;
8900 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
8901 38b0338b 2019-11-29 stsp if (continue_rebase) {
8902 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8903 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8904 e600f124 2021-03-21 stsp create_backup);
8905 38b0338b 2019-11-29 stsp goto done;
8907 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8908 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8909 38b0338b 2019-11-29 stsp char *id_str;
8910 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8911 38b0338b 2019-11-29 stsp new_base_branch);
8912 38b0338b 2019-11-29 stsp if (error)
8913 38b0338b 2019-11-29 stsp goto done;
8914 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8915 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8916 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8917 38b0338b 2019-11-29 stsp free(id_str);
8918 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8919 38b0338b 2019-11-29 stsp new_head_commit_id);
8920 38b0338b 2019-11-29 stsp if (error)
8921 38b0338b 2019-11-29 stsp goto done;
8922 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8923 e600f124 2021-03-21 stsp create_backup = 0;
8927 818c7501 2019-07-11 stsp pid = NULL;
8928 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
8929 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8931 818c7501 2019-07-11 stsp commit_id = qid->id;
8932 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
8933 818c7501 2019-07-11 stsp pid = qid;
8935 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8936 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
8937 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
8938 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8939 818c7501 2019-07-11 stsp if (error)
8940 818c7501 2019-07-11 stsp goto done;
8942 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8943 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8944 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8946 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8947 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
8948 a0ea4fc0 2020-02-28 stsp if (error)
8949 a0ea4fc0 2020-02-28 stsp goto done;
8950 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8954 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
8955 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
8956 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8957 818c7501 2019-07-11 stsp if (error)
8958 818c7501 2019-07-11 stsp goto done;
8961 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8962 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
8963 818c7501 2019-07-11 stsp if (error)
8964 818c7501 2019-07-11 stsp goto done;
8965 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8966 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
8968 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
8969 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
8971 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
8972 818c7501 2019-07-11 stsp free(branch_head_commit_id);
8973 818c7501 2019-07-11 stsp free(resume_commit_id);
8974 818c7501 2019-07-11 stsp free(yca_id);
8975 818c7501 2019-07-11 stsp if (commit)
8976 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8977 818c7501 2019-07-11 stsp if (branch)
8978 818c7501 2019-07-11 stsp got_ref_close(branch);
8979 818c7501 2019-07-11 stsp if (new_base_branch)
8980 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
8981 818c7501 2019-07-11 stsp if (tmp_branch)
8982 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
8983 5ef14e63 2019-06-02 stsp if (worktree)
8984 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
8985 1d0f4054 2021-06-17 stsp if (repo) {
8986 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8987 1d0f4054 2021-06-17 stsp if (error == NULL)
8988 1d0f4054 2021-06-17 stsp error = close_err;
8990 5ef14e63 2019-06-02 stsp return error;
8993 0ebf8283 2019-07-24 stsp __dead static void
8994 0ebf8283 2019-07-24 stsp usage_histedit(void)
8996 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8997 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
8998 643b85bc 2021-07-16 stsp getprogname());
9002 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9003 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9004 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9005 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9006 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9008 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
9009 0ebf8283 2019-07-24 stsp unsigned char code;
9010 0ebf8283 2019-07-24 stsp const char *name;
9011 0ebf8283 2019-07-24 stsp const char *desc;
9012 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9013 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9014 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9015 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9016 82997472 2020-01-29 stsp "be used" },
9017 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9018 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9019 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9022 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9023 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9024 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9025 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9026 0ebf8283 2019-07-24 stsp char *logmsg;
9028 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9030 0ebf8283 2019-07-24 stsp static const struct got_error *
9031 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9032 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9034 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9035 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9036 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9039 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9041 0ebf8283 2019-07-24 stsp goto done;
9043 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9045 0ebf8283 2019-07-24 stsp goto done;
9047 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9049 0ebf8283 2019-07-24 stsp goto done;
9051 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9052 0ebf8283 2019-07-24 stsp if (n < 0)
9053 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9055 0ebf8283 2019-07-24 stsp if (commit)
9056 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9057 0ebf8283 2019-07-24 stsp free(id_str);
9058 0ebf8283 2019-07-24 stsp free(logmsg);
9059 0ebf8283 2019-07-24 stsp return err;
9062 0ebf8283 2019-07-24 stsp static const struct got_error *
9063 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9064 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9066 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9067 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9068 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9070 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9071 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9073 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9074 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9075 dbdddfee 2021-06-23 naddy if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9076 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9077 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9080 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9081 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9082 083957f4 2020-02-24 stsp if (n < 0) {
9083 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9089 0ebf8283 2019-07-24 stsp return err;
9092 0ebf8283 2019-07-24 stsp static const struct got_error *
9093 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9094 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9096 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9099 514f2ffe 2020-01-29 stsp char *id_str;
9100 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9102 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9103 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9105 514f2ffe 2020-01-29 stsp return err;
9107 514f2ffe 2020-01-29 stsp n = fprintf(f,
9108 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9109 514f2ffe 2020-01-29 stsp "# commit %s\n"
9110 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9111 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9112 514f2ffe 2020-01-29 stsp if (n < 0) {
9113 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9114 514f2ffe 2020-01-29 stsp goto done;
9117 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9118 514f2ffe 2020-01-29 stsp if (n < 0) {
9119 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9120 514f2ffe 2020-01-29 stsp goto done;
9123 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9124 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9125 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9126 0ebf8283 2019-07-24 stsp cmd->desc);
9127 0ebf8283 2019-07-24 stsp if (n < 0) {
9128 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9133 514f2ffe 2020-01-29 stsp free(id_str);
9134 0ebf8283 2019-07-24 stsp return err;
9137 0ebf8283 2019-07-24 stsp static const struct got_error *
9138 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9140 0ebf8283 2019-07-24 stsp static char msg[42];
9143 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9145 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9146 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9148 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9151 0ebf8283 2019-07-24 stsp static const struct got_error *
9152 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9153 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9155 0ebf8283 2019-07-24 stsp const struct got_error *err;
9156 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9157 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9159 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9161 0ebf8283 2019-07-24 stsp return err;
9163 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9165 0ebf8283 2019-07-24 stsp goto done;
9167 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9169 5943eee2 2019-08-13 stsp goto done;
9170 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9171 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9172 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9173 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9176 0ebf8283 2019-07-24 stsp if (folded_commit)
9177 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9178 0ebf8283 2019-07-24 stsp free(id_str);
9179 5943eee2 2019-08-13 stsp free(folded_logmsg);
9180 0ebf8283 2019-07-24 stsp return err;
9183 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9184 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9186 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9188 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9189 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9190 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9191 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9192 3f9de99f 2019-07-24 stsp folded = prev;
9193 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9196 0ebf8283 2019-07-24 stsp return folded;
9199 0ebf8283 2019-07-24 stsp static const struct got_error *
9200 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9201 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9203 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9204 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9205 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9206 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9207 1601cb9f 2020-09-11 naddy int logmsg_len;
9209 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9211 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9213 0ebf8283 2019-07-24 stsp return err;
9215 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9216 0ebf8283 2019-07-24 stsp if (folded) {
9217 0ebf8283 2019-07-24 stsp while (folded != hle) {
9218 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9219 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9222 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9223 0ebf8283 2019-07-24 stsp logmsg, repo);
9225 0ebf8283 2019-07-24 stsp goto done;
9226 0ebf8283 2019-07-24 stsp free(logmsg);
9227 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9228 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9232 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9234 0ebf8283 2019-07-24 stsp goto done;
9235 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9237 5943eee2 2019-08-13 stsp goto done;
9238 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9239 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9240 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9241 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9242 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9243 0ebf8283 2019-07-24 stsp goto done;
9245 0ebf8283 2019-07-24 stsp free(logmsg);
9246 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9248 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9250 0ebf8283 2019-07-24 stsp goto done;
9252 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9253 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9255 0ebf8283 2019-07-24 stsp goto done;
9257 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9258 0ebf8283 2019-07-24 stsp close(fd);
9260 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9262 0ebf8283 2019-07-24 stsp goto done;
9264 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9265 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9266 0ebf8283 2019-07-24 stsp if (err) {
9267 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9268 0ebf8283 2019-07-24 stsp goto done;
9269 71392a05 2020-12-13 stsp err = NULL;
9270 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9271 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9272 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9275 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9276 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9277 0ebf8283 2019-07-24 stsp free(logmsg_path);
9278 0ebf8283 2019-07-24 stsp free(logmsg);
9279 5943eee2 2019-08-13 stsp free(orig_logmsg);
9280 0ebf8283 2019-07-24 stsp free(editor);
9281 0ebf8283 2019-07-24 stsp if (commit)
9282 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9283 0ebf8283 2019-07-24 stsp return err;
9286 0ebf8283 2019-07-24 stsp static const struct got_error *
9287 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9288 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9290 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9291 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9292 6059809a 2020-12-17 stsp size_t i, size;
9293 0ebf8283 2019-07-24 stsp ssize_t len;
9294 6059809a 2020-12-17 stsp int lineno = 0;
9295 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9296 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9297 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9299 0ebf8283 2019-07-24 stsp for (;;) {
9300 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9301 0ebf8283 2019-07-24 stsp if (len == -1) {
9302 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9303 0ebf8283 2019-07-24 stsp if (feof(f))
9305 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9306 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9311 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9313 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9314 0ebf8283 2019-07-24 stsp free(line);
9315 0ebf8283 2019-07-24 stsp line = NULL;
9318 0ebf8283 2019-07-24 stsp cmd = NULL;
9319 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9320 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9321 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9322 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9323 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9326 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9331 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9332 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9335 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9337 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9338 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9339 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9342 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9343 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9347 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9348 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9349 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9353 0ebf8283 2019-07-24 stsp free(line);
9354 0ebf8283 2019-07-24 stsp line = NULL;
9358 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9360 0ebf8283 2019-07-24 stsp *end = '\0';
9362 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9363 0ebf8283 2019-07-24 stsp if (err) {
9364 0ebf8283 2019-07-24 stsp /* override error code */
9365 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9369 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9370 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9371 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9374 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9375 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9376 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9377 0ebf8283 2019-07-24 stsp commit_id = NULL;
9378 0ebf8283 2019-07-24 stsp free(line);
9379 0ebf8283 2019-07-24 stsp line = NULL;
9380 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9383 0ebf8283 2019-07-24 stsp free(line);
9384 0ebf8283 2019-07-24 stsp free(commit_id);
9385 0ebf8283 2019-07-24 stsp return err;
9388 0ebf8283 2019-07-24 stsp static const struct got_error *
9389 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9390 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9392 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9393 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9394 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9395 5b87815e 2020-03-05 stsp static char msg[92];
9396 bfce7f83 2019-07-27 stsp char *id_str;
9398 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9399 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9400 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9401 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9402 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9404 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9405 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9406 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9407 5b87815e 2020-03-05 stsp if (hle == hle2)
9409 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9410 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9412 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9414 5b87815e 2020-03-05 stsp return err;
9415 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9416 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9417 5b87815e 2020-03-05 stsp free(id_str);
9418 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9422 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9423 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9424 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9427 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9428 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9430 bfce7f83 2019-07-27 stsp return err;
9431 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9432 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9433 bfce7f83 2019-07-27 stsp free(id_str);
9434 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9438 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9439 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9440 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9441 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9443 bfce7f83 2019-07-27 stsp return NULL;
9446 bfce7f83 2019-07-27 stsp static const struct got_error *
9447 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9448 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9449 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9451 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9452 0ebf8283 2019-07-24 stsp char *editor;
9453 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9455 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9457 0ebf8283 2019-07-24 stsp return err;
9459 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9460 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9461 0ebf8283 2019-07-24 stsp goto done;
9464 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9465 0ebf8283 2019-07-24 stsp if (f == NULL) {
9466 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9467 0ebf8283 2019-07-24 stsp goto done;
9469 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9471 bfce7f83 2019-07-27 stsp goto done;
9473 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9475 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9476 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9477 0ebf8283 2019-07-24 stsp free(editor);
9478 0ebf8283 2019-07-24 stsp return err;
9481 0ebf8283 2019-07-24 stsp static const struct got_error *
9482 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9483 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9484 514f2ffe 2020-01-29 stsp struct got_repository *);
9486 0ebf8283 2019-07-24 stsp static const struct got_error *
9487 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9488 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9489 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
9491 0ebf8283 2019-07-24 stsp const struct got_error *err;
9492 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9493 0ebf8283 2019-07-24 stsp char *path = NULL;
9495 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9497 0ebf8283 2019-07-24 stsp return err;
9499 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
9501 0ebf8283 2019-07-24 stsp goto done;
9503 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9504 466785b9 2020-12-10 jrick fold_only, repo);
9506 0ebf8283 2019-07-24 stsp goto done;
9508 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
9509 083957f4 2020-02-24 stsp rewind(f);
9510 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9512 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9513 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9514 0ebf8283 2019-07-24 stsp goto done;
9517 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9518 083957f4 2020-02-24 stsp if (err) {
9519 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9520 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9521 083957f4 2020-02-24 stsp goto done;
9522 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9523 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9527 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9528 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9529 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9530 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9531 0ebf8283 2019-07-24 stsp free(path);
9532 0ebf8283 2019-07-24 stsp return err;
9535 0ebf8283 2019-07-24 stsp static const struct got_error *
9536 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9537 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9539 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9540 0ebf8283 2019-07-24 stsp char *path = NULL;
9541 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9542 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9543 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9545 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9547 0ebf8283 2019-07-24 stsp return err;
9549 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
9550 0ebf8283 2019-07-24 stsp if (f == NULL) {
9551 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9552 0ebf8283 2019-07-24 stsp goto done;
9554 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9555 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9560 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9561 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9562 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9563 0ebf8283 2019-07-24 stsp if (n < 0) {
9564 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9570 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9571 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9572 0ebf8283 2019-07-24 stsp free(path);
9573 0ebf8283 2019-07-24 stsp if (commit)
9574 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9575 0ebf8283 2019-07-24 stsp return err;
9579 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9581 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9583 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9584 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9585 bfce7f83 2019-07-27 stsp free(hle);
9589 0ebf8283 2019-07-24 stsp static const struct got_error *
9590 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9591 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9593 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9594 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9596 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9597 0ebf8283 2019-07-24 stsp if (f == NULL) {
9598 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9599 0ebf8283 2019-07-24 stsp goto done;
9602 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9604 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9605 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9606 0ebf8283 2019-07-24 stsp return err;
9609 0ebf8283 2019-07-24 stsp static const struct got_error *
9610 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9611 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9612 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9614 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9615 0ebf8283 2019-07-24 stsp int resp = ' ';
9617 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9618 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9619 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9620 0ebf8283 2019-07-24 stsp resp = getchar();
9621 a61a4414 2019-08-07 stsp if (resp == '\n')
9622 a61a4414 2019-08-07 stsp resp = getchar();
9623 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9624 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9625 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9627 426ebf2e 2019-07-25 stsp if (err) {
9628 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9629 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9631 bfce7f83 2019-07-27 stsp prev_err = err;
9632 426ebf2e 2019-07-25 stsp resp = ' ';
9636 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9637 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9638 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9639 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
9640 426ebf2e 2019-07-25 stsp if (err) {
9641 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9642 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9644 bfce7f83 2019-07-27 stsp prev_err = err;
9645 426ebf2e 2019-07-25 stsp resp = ' ';
9649 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9650 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9653 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9656 0ebf8283 2019-07-24 stsp return err;
9659 0ebf8283 2019-07-24 stsp static const struct got_error *
9660 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9661 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9662 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9664 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9665 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9666 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9667 3e3a69f1 2019-07-25 stsp branch, repo);
9670 0ebf8283 2019-07-24 stsp static const struct got_error *
9671 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9672 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9674 0ebf8283 2019-07-24 stsp const struct got_error *err;
9675 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9677 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9679 0ebf8283 2019-07-24 stsp goto done;
9681 0ebf8283 2019-07-24 stsp if (new_id) {
9682 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9684 0ebf8283 2019-07-24 stsp goto done;
9687 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9688 0ebf8283 2019-07-24 stsp if (new_id_str)
9689 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9691 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9692 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9693 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9694 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9695 0ebf8283 2019-07-24 stsp goto done;
9697 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9699 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9701 0ebf8283 2019-07-24 stsp goto done;
9704 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9705 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9706 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9707 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9708 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9710 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9711 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9712 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9719 0ebf8283 2019-07-24 stsp free(old_id_str);
9720 0ebf8283 2019-07-24 stsp free(new_id_str);
9721 0ebf8283 2019-07-24 stsp return err;
9724 0ebf8283 2019-07-24 stsp static const struct got_error *
9725 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9726 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9727 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9728 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9730 0ebf8283 2019-07-24 stsp const struct got_error *err;
9731 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9732 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9734 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9735 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9736 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9738 0ebf8283 2019-07-24 stsp return err;
9741 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9743 0ebf8283 2019-07-24 stsp return err;
9745 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9746 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9747 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9748 0ebf8283 2019-07-24 stsp if (err) {
9749 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9750 0ebf8283 2019-07-24 stsp goto done;
9751 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9753 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9754 0ebf8283 2019-07-24 stsp free(new_commit_id);
9757 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9758 0ebf8283 2019-07-24 stsp return err;
9761 0ebf8283 2019-07-24 stsp static const struct got_error *
9762 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9763 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9765 0ebf8283 2019-07-24 stsp const struct got_error *error;
9766 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9768 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9770 0ebf8283 2019-07-24 stsp if (error)
9771 0ebf8283 2019-07-24 stsp return error;
9773 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9774 0ebf8283 2019-07-24 stsp if (error)
9775 0ebf8283 2019-07-24 stsp return error;
9777 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9778 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9779 0ebf8283 2019-07-24 stsp return error;
9782 ab20a43a 2020-01-29 stsp static const struct got_error *
9783 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9784 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9785 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9786 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9788 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9790 ab20a43a 2020-01-29 stsp switch (status) {
9791 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9792 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9793 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9794 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9795 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9796 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9801 ab20a43a 2020-01-29 stsp switch (staged_status) {
9802 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9803 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9804 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9805 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9806 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9811 ab20a43a 2020-01-29 stsp return NULL;
9814 0ebf8283 2019-07-24 stsp static const struct got_error *
9815 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9817 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9818 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9819 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9820 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9821 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9822 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9823 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9824 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9825 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9826 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9827 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9828 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9829 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9830 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9831 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9832 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
9833 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9834 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9835 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9836 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9837 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9838 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9839 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9840 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9842 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9843 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9844 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9845 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9847 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9848 0ebf8283 2019-07-24 stsp switch (ch) {
9850 0ebf8283 2019-07-24 stsp abort_edit = 1;
9853 0ebf8283 2019-07-24 stsp continue_edit = 1;
9855 466785b9 2020-12-10 jrick case 'f':
9856 466785b9 2020-12-10 jrick fold_only = 1;
9859 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9862 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9865 e600f124 2021-03-21 stsp list_backups = 1;
9868 643b85bc 2021-07-16 stsp delete_backups = 1;
9871 0ebf8283 2019-07-24 stsp usage_histedit();
9872 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9876 0ebf8283 2019-07-24 stsp argc -= optind;
9877 0ebf8283 2019-07-24 stsp argv += optind;
9879 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9880 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9881 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9882 0ebf8283 2019-07-24 stsp err(1, "pledge");
9884 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9885 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9886 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9887 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9888 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9889 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9890 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9891 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9892 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9893 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9894 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9895 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9896 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9897 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9898 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9899 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9900 e600f124 2021-03-21 stsp if (list_backups) {
9901 e600f124 2021-03-21 stsp if (abort_edit)
9902 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9903 e600f124 2021-03-21 stsp if (continue_edit)
9904 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9905 e600f124 2021-03-21 stsp if (edit_script_path)
9906 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9907 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9908 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9909 e600f124 2021-03-21 stsp if (fold_only)
9910 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9911 643b85bc 2021-07-16 stsp if (delete_backups)
9912 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9913 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9914 e600f124 2021-03-21 stsp usage_histedit();
9915 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9916 643b85bc 2021-07-16 stsp if (abort_edit)
9917 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9918 643b85bc 2021-07-16 stsp if (continue_edit)
9919 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9920 643b85bc 2021-07-16 stsp if (edit_script_path)
9921 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
9922 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
9923 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
9924 643b85bc 2021-07-16 stsp if (fold_only)
9925 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
9926 643b85bc 2021-07-16 stsp if (list_backups)
9927 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
9928 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9929 643b85bc 2021-07-16 stsp usage_histedit();
9930 e600f124 2021-03-21 stsp } else if (argc != 0)
9931 0ebf8283 2019-07-24 stsp usage_histedit();
9934 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
9935 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
9936 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
9937 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
9938 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
9939 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
9942 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
9943 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
9944 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
9945 0ebf8283 2019-07-24 stsp goto done;
9947 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
9948 fa51e947 2020-03-27 stsp if (error) {
9949 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9950 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9951 e600f124 2021-03-21 stsp goto done;
9953 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9954 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9955 e600f124 2021-03-21 stsp "histedit", cwd);
9956 e600f124 2021-03-21 stsp goto done;
9960 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9961 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9962 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
9964 e600f124 2021-03-21 stsp if (error != NULL)
9965 e600f124 2021-03-21 stsp goto done;
9966 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9967 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9968 e600f124 2021-03-21 stsp if (error)
9969 e600f124 2021-03-21 stsp goto done;
9970 643b85bc 2021-07-16 stsp error = process_backup_refs(
9971 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9972 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9973 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9976 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9978 0ebf8283 2019-07-24 stsp if (error != NULL)
9979 0ebf8283 2019-07-24 stsp goto done;
9981 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9982 0ebf8283 2019-07-24 stsp if (error)
9983 0ebf8283 2019-07-24 stsp goto done;
9984 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
9985 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
9986 0ebf8283 2019-07-24 stsp goto done;
9989 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
9990 0ebf8283 2019-07-24 stsp if (error)
9991 0ebf8283 2019-07-24 stsp goto done;
9993 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
9994 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9995 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
9996 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
9997 083957f4 2020-02-24 stsp "before the -m option can be used");
9998 083957f4 2020-02-24 stsp goto done;
10000 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10001 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10002 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10003 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10004 466785b9 2020-12-10 jrick "before the -f option can be used");
10005 466785b9 2020-12-10 jrick goto done;
10008 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10009 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10010 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10011 3e3a69f1 2019-07-25 stsp worktree, repo);
10012 0ebf8283 2019-07-24 stsp if (error)
10013 0ebf8283 2019-07-24 stsp goto done;
10014 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10015 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10016 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10017 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
10018 0ebf8283 2019-07-24 stsp if (error)
10019 0ebf8283 2019-07-24 stsp goto done;
10020 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10021 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10022 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10023 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10024 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10025 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10026 0ebf8283 2019-07-24 stsp goto done;
10029 0ebf8283 2019-07-24 stsp if (continue_edit) {
10030 0ebf8283 2019-07-24 stsp char *path;
10032 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10033 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10034 0ebf8283 2019-07-24 stsp goto done;
10037 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10038 0ebf8283 2019-07-24 stsp if (error)
10039 0ebf8283 2019-07-24 stsp goto done;
10041 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10042 0ebf8283 2019-07-24 stsp free(path);
10043 0ebf8283 2019-07-24 stsp if (error)
10044 0ebf8283 2019-07-24 stsp goto done;
10046 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10047 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10048 3e3a69f1 2019-07-25 stsp worktree, repo);
10049 0ebf8283 2019-07-24 stsp if (error)
10050 0ebf8283 2019-07-24 stsp goto done;
10052 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10053 0ebf8283 2019-07-24 stsp if (error)
10054 0ebf8283 2019-07-24 stsp goto done;
10056 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10057 0ebf8283 2019-07-24 stsp head_commit_id);
10058 0ebf8283 2019-07-24 stsp if (error)
10059 0ebf8283 2019-07-24 stsp goto done;
10060 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10061 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10062 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10063 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10064 3aac7cf7 2019-07-25 stsp goto done;
10066 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10067 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10068 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10069 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10070 0ebf8283 2019-07-24 stsp commit = NULL;
10071 0ebf8283 2019-07-24 stsp if (error)
10072 0ebf8283 2019-07-24 stsp goto done;
10074 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10075 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10076 0ebf8283 2019-07-24 stsp goto done;
10079 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10080 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10081 0ebf8283 2019-07-24 stsp if (error != NULL)
10082 0ebf8283 2019-07-24 stsp goto done;
10084 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10085 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10086 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10087 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10088 c7d20a3f 2019-07-30 stsp goto done;
10091 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10092 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10093 bfce7f83 2019-07-27 stsp branch = NULL;
10094 0ebf8283 2019-07-24 stsp if (error)
10095 0ebf8283 2019-07-24 stsp goto done;
10097 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10098 0ebf8283 2019-07-24 stsp head_commit_id);
10099 0ebf8283 2019-07-24 stsp if (error)
10100 0ebf8283 2019-07-24 stsp goto done;
10101 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10102 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10103 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10104 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10105 3aac7cf7 2019-07-25 stsp goto done;
10107 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10108 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10109 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10110 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10111 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10112 0ebf8283 2019-07-24 stsp commit = NULL;
10113 0ebf8283 2019-07-24 stsp if (error)
10114 0ebf8283 2019-07-24 stsp goto done;
10116 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10117 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10118 c5996fff 2020-01-29 stsp goto done;
10121 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10122 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10123 bfce7f83 2019-07-27 stsp if (error)
10124 bfce7f83 2019-07-27 stsp goto done;
10126 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10127 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10128 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10129 6ba2bea2 2019-07-27 stsp if (error) {
10130 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10131 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10132 9627c110 2020-04-18 stsp update_progress, &upa);
10133 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10134 0ebf8283 2019-07-24 stsp goto done;
10137 514f2ffe 2020-01-29 stsp const char *branch_name;
10138 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10139 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10140 514f2ffe 2020-01-29 stsp branch_name += 11;
10141 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10142 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
10143 6ba2bea2 2019-07-27 stsp if (error) {
10144 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10145 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10146 9627c110 2020-04-18 stsp update_progress, &upa);
10147 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10148 0ebf8283 2019-07-24 stsp goto done;
10153 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10155 6ba2bea2 2019-07-27 stsp if (error) {
10156 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10157 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10158 9627c110 2020-04-18 stsp update_progress, &upa);
10159 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10160 0ebf8283 2019-07-24 stsp goto done;
10165 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10166 4ec14e60 2019-08-28 hiltjo if (error)
10167 0ebf8283 2019-07-24 stsp goto done;
10169 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10170 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10171 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10172 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10173 0ebf8283 2019-07-24 stsp continue;
10175 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10176 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10177 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10178 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10180 ab20a43a 2020-01-29 stsp if (error)
10181 ab20a43a 2020-01-29 stsp goto done;
10183 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10184 ab20a43a 2020-01-29 stsp int have_changes = 0;
10186 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10187 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10188 ab20a43a 2020-01-29 stsp if (error)
10189 ab20a43a 2020-01-29 stsp goto done;
10190 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10191 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10192 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10193 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10194 ab20a43a 2020-01-29 stsp if (error) {
10195 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10196 ab20a43a 2020-01-29 stsp goto done;
10197 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10198 ab20a43a 2020-01-29 stsp goto done;
10200 ab20a43a 2020-01-29 stsp if (have_changes) {
10201 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10202 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10203 ab20a43a 2020-01-29 stsp if (error)
10204 ab20a43a 2020-01-29 stsp goto done;
10206 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10207 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10208 ab20a43a 2020-01-29 stsp if (error)
10209 ab20a43a 2020-01-29 stsp goto done;
10210 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10211 ab20a43a 2020-01-29 stsp hle, NULL);
10212 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10213 ab20a43a 2020-01-29 stsp commit = NULL;
10214 ab20a43a 2020-01-29 stsp if (error)
10215 ab20a43a 2020-01-29 stsp goto done;
10218 0ebf8283 2019-07-24 stsp continue;
10221 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10222 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10223 0ebf8283 2019-07-24 stsp if (error)
10224 0ebf8283 2019-07-24 stsp goto done;
10225 0ebf8283 2019-07-24 stsp continue;
10228 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10229 0ebf8283 2019-07-24 stsp hle->commit_id);
10230 0ebf8283 2019-07-24 stsp if (error)
10231 0ebf8283 2019-07-24 stsp goto done;
10232 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10233 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10235 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10236 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10237 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10238 0ebf8283 2019-07-24 stsp if (error)
10239 0ebf8283 2019-07-24 stsp goto done;
10240 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10241 0ebf8283 2019-07-24 stsp commit = NULL;
10243 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10244 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
10245 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
10247 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10248 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
10250 a0ea4fc0 2020-02-28 stsp if (error)
10251 a0ea4fc0 2020-02-28 stsp goto done;
10252 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10256 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10257 0ebf8283 2019-07-24 stsp char *id_str;
10258 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10259 0ebf8283 2019-07-24 stsp if (error)
10260 0ebf8283 2019-07-24 stsp goto done;
10261 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10263 0ebf8283 2019-07-24 stsp free(id_str);
10264 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10265 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10266 3e3a69f1 2019-07-25 stsp fileindex);
10267 0ebf8283 2019-07-24 stsp goto done;
10270 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10271 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10272 0ebf8283 2019-07-24 stsp if (error)
10273 0ebf8283 2019-07-24 stsp goto done;
10274 0ebf8283 2019-07-24 stsp continue;
10277 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10278 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10279 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10280 0ebf8283 2019-07-24 stsp if (error)
10281 0ebf8283 2019-07-24 stsp goto done;
10284 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10285 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10286 0ebf8283 2019-07-24 stsp if (error)
10287 0ebf8283 2019-07-24 stsp goto done;
10288 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10289 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
10291 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10292 3e3a69f1 2019-07-25 stsp branch, repo);
10294 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10295 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10296 0ebf8283 2019-07-24 stsp free(head_commit_id);
10297 0ebf8283 2019-07-24 stsp free(base_commit_id);
10298 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10299 0ebf8283 2019-07-24 stsp if (commit)
10300 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10301 0ebf8283 2019-07-24 stsp if (branch)
10302 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10303 0ebf8283 2019-07-24 stsp if (tmp_branch)
10304 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10305 0ebf8283 2019-07-24 stsp if (worktree)
10306 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10307 1d0f4054 2021-06-17 stsp if (repo) {
10308 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10309 1d0f4054 2021-06-17 stsp if (error == NULL)
10310 1d0f4054 2021-06-17 stsp error = close_err;
10312 2822a352 2019-10-15 stsp return error;
10315 2822a352 2019-10-15 stsp __dead static void
10316 2822a352 2019-10-15 stsp usage_integrate(void)
10318 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10322 2822a352 2019-10-15 stsp static const struct got_error *
10323 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10325 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10326 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10327 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10328 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10329 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10330 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10331 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10332 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10334 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10336 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10337 2822a352 2019-10-15 stsp switch (ch) {
10339 2822a352 2019-10-15 stsp usage_integrate();
10340 2822a352 2019-10-15 stsp /* NOTREACHED */
10344 2822a352 2019-10-15 stsp argc -= optind;
10345 2822a352 2019-10-15 stsp argv += optind;
10347 2822a352 2019-10-15 stsp if (argc != 1)
10348 2822a352 2019-10-15 stsp usage_integrate();
10349 2822a352 2019-10-15 stsp branch_arg = argv[0];
10350 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10351 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10352 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10353 2822a352 2019-10-15 stsp err(1, "pledge");
10355 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10356 2822a352 2019-10-15 stsp if (cwd == NULL) {
10357 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10358 2822a352 2019-10-15 stsp goto done;
10361 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10362 fa51e947 2020-03-27 stsp if (error) {
10363 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10364 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10366 2822a352 2019-10-15 stsp goto done;
10369 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10370 2822a352 2019-10-15 stsp if (error)
10371 2822a352 2019-10-15 stsp goto done;
10373 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10375 2822a352 2019-10-15 stsp if (error != NULL)
10376 2822a352 2019-10-15 stsp goto done;
10378 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10379 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10380 2822a352 2019-10-15 stsp if (error)
10381 2822a352 2019-10-15 stsp goto done;
10383 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10384 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10385 2822a352 2019-10-15 stsp goto done;
10388 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10389 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10390 2822a352 2019-10-15 stsp if (error)
10391 2822a352 2019-10-15 stsp goto done;
10393 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10394 2822a352 2019-10-15 stsp if (refname == NULL) {
10395 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10396 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10397 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10398 2822a352 2019-10-15 stsp goto done;
10400 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10401 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10402 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10403 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10404 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10405 2822a352 2019-10-15 stsp goto done;
10408 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10409 2822a352 2019-10-15 stsp if (error)
10410 2822a352 2019-10-15 stsp goto done;
10412 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10413 2822a352 2019-10-15 stsp if (error)
10414 2822a352 2019-10-15 stsp goto done;
10416 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10417 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10418 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10419 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10420 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10421 2822a352 2019-10-15 stsp goto done;
10424 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10425 2822a352 2019-10-15 stsp if (error) {
10426 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10427 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10428 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10429 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10430 2822a352 2019-10-15 stsp goto done;
10433 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10434 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10435 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10436 2822a352 2019-10-15 stsp check_cancelled, NULL);
10437 2822a352 2019-10-15 stsp if (error)
10438 2822a352 2019-10-15 stsp goto done;
10440 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10441 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10443 1d0f4054 2021-06-17 stsp if (repo) {
10444 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10445 1d0f4054 2021-06-17 stsp if (error == NULL)
10446 1d0f4054 2021-06-17 stsp error = close_err;
10448 2822a352 2019-10-15 stsp if (worktree)
10449 2822a352 2019-10-15 stsp got_worktree_close(worktree);
10450 2822a352 2019-10-15 stsp free(cwd);
10451 2822a352 2019-10-15 stsp free(base_commit_id);
10452 2822a352 2019-10-15 stsp free(commit_id);
10453 2822a352 2019-10-15 stsp free(refname);
10454 2822a352 2019-10-15 stsp free(base_refname);
10455 715dc77e 2019-08-03 stsp return error;
10458 715dc77e 2019-08-03 stsp __dead static void
10459 715dc77e 2019-08-03 stsp usage_stage(void)
10461 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10462 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
10463 715dc77e 2019-08-03 stsp getprogname());
10467 715dc77e 2019-08-03 stsp static const struct got_error *
10468 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
10469 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
10470 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10471 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
10473 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
10474 408b4ebc 2019-08-03 stsp char *id_str = NULL;
10476 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
10477 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
10478 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
10479 408b4ebc 2019-08-03 stsp return NULL;
10481 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
10482 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
10483 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
10485 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
10487 408b4ebc 2019-08-03 stsp return err;
10489 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
10490 408b4ebc 2019-08-03 stsp free(id_str);
10491 dc424a06 2019-08-07 stsp return NULL;
10494 dc424a06 2019-08-07 stsp static const struct got_error *
10495 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
10497 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
10498 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
10499 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
10500 715dc77e 2019-08-03 stsp char *cwd = NULL;
10501 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
10502 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
10503 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10504 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
10505 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10506 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10508 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
10510 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10511 715dc77e 2019-08-03 stsp switch (ch) {
10512 408b4ebc 2019-08-03 stsp case 'l':
10513 408b4ebc 2019-08-03 stsp list_stage = 1;
10515 dc424a06 2019-08-07 stsp case 'p':
10516 dc424a06 2019-08-07 stsp pflag = 1;
10518 dc424a06 2019-08-07 stsp case 'F':
10519 dc424a06 2019-08-07 stsp patch_script_path = optarg;
10521 35213c7c 2020-07-23 stsp case 'S':
10522 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
10525 715dc77e 2019-08-03 stsp usage_stage();
10526 715dc77e 2019-08-03 stsp /* NOTREACHED */
10530 715dc77e 2019-08-03 stsp argc -= optind;
10531 715dc77e 2019-08-03 stsp argv += optind;
10533 715dc77e 2019-08-03 stsp #ifndef PROFILE
10534 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10535 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
10536 715dc77e 2019-08-03 stsp err(1, "pledge");
10538 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
10539 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
10540 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
10541 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
10543 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
10544 715dc77e 2019-08-03 stsp if (cwd == NULL) {
10545 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
10546 715dc77e 2019-08-03 stsp goto done;
10549 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10550 fa51e947 2020-03-27 stsp if (error) {
10551 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10552 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
10553 715dc77e 2019-08-03 stsp goto done;
10556 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10558 715dc77e 2019-08-03 stsp if (error != NULL)
10559 715dc77e 2019-08-03 stsp goto done;
10561 dc424a06 2019-08-07 stsp if (patch_script_path) {
10562 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
10563 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
10564 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
10565 dc424a06 2019-08-07 stsp patch_script_path);
10566 dc424a06 2019-08-07 stsp goto done;
10569 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10570 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
10571 715dc77e 2019-08-03 stsp if (error)
10572 715dc77e 2019-08-03 stsp goto done;
10574 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10575 6d022e97 2019-08-04 stsp if (error)
10576 6d022e97 2019-08-04 stsp goto done;
10578 6d022e97 2019-08-04 stsp if (list_stage)
10579 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
10580 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
10582 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10583 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
10584 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
10585 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
10586 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
10587 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
10590 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10591 2e1f37b0 2019-08-08 stsp error == NULL)
10592 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10593 1d0f4054 2021-06-17 stsp if (repo) {
10594 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10595 1d0f4054 2021-06-17 stsp if (error == NULL)
10596 1d0f4054 2021-06-17 stsp error = close_err;
10598 ad493afc 2019-08-03 stsp if (worktree)
10599 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
10600 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10601 ad493afc 2019-08-03 stsp free((char *)pe->path);
10602 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
10603 ad493afc 2019-08-03 stsp free(cwd);
10604 ad493afc 2019-08-03 stsp return error;
10607 ad493afc 2019-08-03 stsp __dead static void
10608 ad493afc 2019-08-03 stsp usage_unstage(void)
10610 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10611 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
10612 ad493afc 2019-08-03 stsp getprogname());
10617 ad493afc 2019-08-03 stsp static const struct got_error *
10618 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
10620 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
10621 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
10622 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
10623 ad493afc 2019-08-03 stsp char *cwd = NULL;
10624 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
10625 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
10626 9627c110 2020-04-18 stsp int ch, pflag = 0;
10627 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10628 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
10629 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10630 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10632 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
10634 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
10635 ad493afc 2019-08-03 stsp switch (ch) {
10636 2e1f37b0 2019-08-08 stsp case 'p':
10637 2e1f37b0 2019-08-08 stsp pflag = 1;
10639 2e1f37b0 2019-08-08 stsp case 'F':
10640 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
10643 ad493afc 2019-08-03 stsp usage_unstage();
10644 ad493afc 2019-08-03 stsp /* NOTREACHED */
10648 ad493afc 2019-08-03 stsp argc -= optind;
10649 ad493afc 2019-08-03 stsp argv += optind;
10651 ad493afc 2019-08-03 stsp #ifndef PROFILE
10652 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10653 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
10654 ad493afc 2019-08-03 stsp err(1, "pledge");
10656 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
10657 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
10659 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
10660 ad493afc 2019-08-03 stsp if (cwd == NULL) {
10661 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
10662 ad493afc 2019-08-03 stsp goto done;
10665 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10666 fa51e947 2020-03-27 stsp if (error) {
10667 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10668 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
10669 ad493afc 2019-08-03 stsp goto done;
10672 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10674 ad493afc 2019-08-03 stsp if (error != NULL)
10675 ad493afc 2019-08-03 stsp goto done;
10677 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
10678 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
10679 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
10680 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
10681 2e1f37b0 2019-08-08 stsp patch_script_path);
10682 2e1f37b0 2019-08-08 stsp goto done;
10686 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10687 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10688 ad493afc 2019-08-03 stsp if (error)
10689 ad493afc 2019-08-03 stsp goto done;
10691 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10692 ad493afc 2019-08-03 stsp if (error)
10693 ad493afc 2019-08-03 stsp goto done;
10695 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10696 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10697 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10698 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10699 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10700 9627c110 2020-04-18 stsp if (!error)
10701 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10703 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10704 2e1f37b0 2019-08-08 stsp error == NULL)
10705 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10706 1d0f4054 2021-06-17 stsp if (repo) {
10707 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10708 1d0f4054 2021-06-17 stsp if (error == NULL)
10709 1d0f4054 2021-06-17 stsp error = close_err;
10711 715dc77e 2019-08-03 stsp if (worktree)
10712 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10713 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10714 715dc77e 2019-08-03 stsp free((char *)pe->path);
10715 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10716 715dc77e 2019-08-03 stsp free(cwd);
10717 01073a5d 2019-08-22 stsp return error;
10720 01073a5d 2019-08-22 stsp __dead static void
10721 01073a5d 2019-08-22 stsp usage_cat(void)
10723 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10724 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10728 01073a5d 2019-08-22 stsp static const struct got_error *
10729 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10731 01073a5d 2019-08-22 stsp const struct got_error *err;
10732 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10734 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10736 01073a5d 2019-08-22 stsp return err;
10738 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10739 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10740 01073a5d 2019-08-22 stsp return err;
10743 01073a5d 2019-08-22 stsp static const struct got_error *
10744 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10746 01073a5d 2019-08-22 stsp const struct got_error *err;
10747 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10748 56e0773d 2019-11-28 stsp int nentries, i;
10750 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10752 01073a5d 2019-08-22 stsp return err;
10754 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10755 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10756 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10757 01073a5d 2019-08-22 stsp char *id_str;
10758 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10760 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10761 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10764 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10765 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10766 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10767 01073a5d 2019-08-22 stsp free(id_str);
10770 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10771 01073a5d 2019-08-22 stsp return err;
10774 01073a5d 2019-08-22 stsp static const struct got_error *
10775 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10777 01073a5d 2019-08-22 stsp const struct got_error *err;
10778 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10779 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10780 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10781 01073a5d 2019-08-22 stsp char *id_str = NULL;
10782 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10784 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10786 01073a5d 2019-08-22 stsp return err;
10788 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10790 01073a5d 2019-08-22 stsp goto done;
10792 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10793 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10794 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10795 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10796 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
10797 01073a5d 2019-08-22 stsp char *pid_str;
10798 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10800 01073a5d 2019-08-22 stsp goto done;
10801 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10802 01073a5d 2019-08-22 stsp free(pid_str);
10804 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10805 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10806 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10808 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10809 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10810 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10812 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10813 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10814 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10816 01073a5d 2019-08-22 stsp free(id_str);
10817 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10818 01073a5d 2019-08-22 stsp return err;
10821 01073a5d 2019-08-22 stsp static const struct got_error *
10822 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10824 01073a5d 2019-08-22 stsp const struct got_error *err;
10825 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10826 01073a5d 2019-08-22 stsp char *id_str = NULL;
10827 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10829 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10831 01073a5d 2019-08-22 stsp return err;
10833 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10835 01073a5d 2019-08-22 stsp goto done;
10837 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10839 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10840 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10841 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10842 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10844 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10845 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10846 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10848 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10849 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10850 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10852 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10853 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10854 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10860 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10861 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10863 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10864 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10865 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10867 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10868 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10869 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10871 01073a5d 2019-08-22 stsp free(id_str);
10872 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10873 01073a5d 2019-08-22 stsp return err;
10876 01073a5d 2019-08-22 stsp static const struct got_error *
10877 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10879 01073a5d 2019-08-22 stsp const struct got_error *error;
10880 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10881 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10882 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10883 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10884 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10885 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10886 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10888 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10890 01073a5d 2019-08-22 stsp #ifndef PROFILE
10891 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10892 01073a5d 2019-08-22 stsp NULL) == -1)
10893 01073a5d 2019-08-22 stsp err(1, "pledge");
10896 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10897 01073a5d 2019-08-22 stsp switch (ch) {
10898 896e9b6f 2019-08-26 stsp case 'c':
10899 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10901 01073a5d 2019-08-22 stsp case 'r':
10902 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10903 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10904 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10906 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10908 896e9b6f 2019-08-26 stsp case 'P':
10909 896e9b6f 2019-08-26 stsp force_path = 1;
10912 01073a5d 2019-08-22 stsp usage_cat();
10913 01073a5d 2019-08-22 stsp /* NOTREACHED */
10917 01073a5d 2019-08-22 stsp argc -= optind;
10918 01073a5d 2019-08-22 stsp argv += optind;
10920 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10921 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10922 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10923 01073a5d 2019-08-22 stsp goto done;
10925 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
10926 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10927 01073a5d 2019-08-22 stsp goto done;
10928 01073a5d 2019-08-22 stsp if (worktree) {
10929 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10930 01073a5d 2019-08-22 stsp repo_path = strdup(
10931 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
10932 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10933 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
10934 01073a5d 2019-08-22 stsp goto done;
10939 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10940 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
10941 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10942 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
10945 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
10946 01073a5d 2019-08-22 stsp free(repo_path);
10947 01073a5d 2019-08-22 stsp if (error != NULL)
10948 01073a5d 2019-08-22 stsp goto done;
10950 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10951 896e9b6f 2019-08-26 stsp if (error)
10952 896e9b6f 2019-08-26 stsp goto done;
10954 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10955 84de9106 2020-12-26 stsp if (error)
10956 84de9106 2020-12-26 stsp goto done;
10958 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
10959 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
10960 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
10961 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10962 01073a5d 2019-08-22 stsp if (error)
10963 01073a5d 2019-08-22 stsp goto done;
10965 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
10966 896e9b6f 2019-08-26 stsp if (force_path) {
10967 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
10968 896e9b6f 2019-08-26 stsp argv[i]);
10969 896e9b6f 2019-08-26 stsp if (error)
10972 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
10973 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10975 896e9b6f 2019-08-26 stsp if (error) {
10976 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10977 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
10979 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
10980 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
10981 896e9b6f 2019-08-26 stsp if (error)
10986 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
10987 01073a5d 2019-08-22 stsp if (error)
10990 01073a5d 2019-08-22 stsp switch (obj_type) {
10991 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10992 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
10994 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10995 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
10997 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10998 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11000 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11001 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11004 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11007 01073a5d 2019-08-22 stsp if (error)
11009 01073a5d 2019-08-22 stsp free(label);
11010 01073a5d 2019-08-22 stsp label = NULL;
11011 01073a5d 2019-08-22 stsp free(id);
11012 01073a5d 2019-08-22 stsp id = NULL;
11015 01073a5d 2019-08-22 stsp free(label);
11016 01073a5d 2019-08-22 stsp free(id);
11017 896e9b6f 2019-08-26 stsp free(commit_id);
11018 01073a5d 2019-08-22 stsp if (worktree)
11019 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11020 01073a5d 2019-08-22 stsp if (repo) {
11021 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11022 01073a5d 2019-08-22 stsp if (error == NULL)
11023 1d0f4054 2021-06-17 stsp error = close_err;
11025 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11026 b2118c49 2020-07-28 stsp return error;
11029 b2118c49 2020-07-28 stsp __dead static void
11030 b2118c49 2020-07-28 stsp usage_info(void)
11032 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11033 b2118c49 2020-07-28 stsp getprogname());
11037 b2118c49 2020-07-28 stsp static const struct got_error *
11038 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11039 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11040 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11042 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11043 b2118c49 2020-07-28 stsp char *id_str = NULL;
11044 b2118c49 2020-07-28 stsp char datebuf[128];
11045 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11046 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11047 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11050 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11051 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11053 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11054 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11055 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11056 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11057 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11060 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11061 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11062 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11063 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11064 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11065 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11066 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11067 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11069 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11071 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11072 b2118c49 2020-07-28 stsp if (tm == NULL)
11073 b2118c49 2020-07-28 stsp return NULL;
11074 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11075 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11076 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11078 b2118c49 2020-07-28 stsp if (blob_id) {
11079 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11081 b2118c49 2020-07-28 stsp return err;
11082 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11083 b2118c49 2020-07-28 stsp free(id_str);
11086 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11087 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11089 b2118c49 2020-07-28 stsp return err;
11090 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11091 b2118c49 2020-07-28 stsp free(id_str);
11094 b2118c49 2020-07-28 stsp if (commit_id) {
11095 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11097 b2118c49 2020-07-28 stsp return err;
11098 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11099 b2118c49 2020-07-28 stsp free(id_str);
11102 b2118c49 2020-07-28 stsp return NULL;
11105 b2118c49 2020-07-28 stsp static const struct got_error *
11106 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11108 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11109 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11110 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11111 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11112 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11113 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11114 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11116 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11118 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11119 b2118c49 2020-07-28 stsp switch (ch) {
11121 b2118c49 2020-07-28 stsp usage_info();
11122 b2118c49 2020-07-28 stsp /* NOTREACHED */
11126 b2118c49 2020-07-28 stsp argc -= optind;
11127 b2118c49 2020-07-28 stsp argv += optind;
11129 b2118c49 2020-07-28 stsp #ifndef PROFILE
11130 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11131 b2118c49 2020-07-28 stsp NULL) == -1)
11132 b2118c49 2020-07-28 stsp err(1, "pledge");
11134 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11135 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11136 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11137 b2118c49 2020-07-28 stsp goto done;
11140 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11141 b2118c49 2020-07-28 stsp if (error) {
11142 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11143 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11144 b2118c49 2020-07-28 stsp goto done;
11147 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11148 b2118c49 2020-07-28 stsp if (error)
11149 b2118c49 2020-07-28 stsp goto done;
11151 b2118c49 2020-07-28 stsp if (argc >= 1) {
11152 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
11153 b2118c49 2020-07-28 stsp worktree);
11154 b2118c49 2020-07-28 stsp if (error)
11155 b2118c49 2020-07-28 stsp goto done;
11156 b2118c49 2020-07-28 stsp show_files = 1;
11159 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
11160 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
11161 b2118c49 2020-07-28 stsp if (error)
11162 b2118c49 2020-07-28 stsp goto done;
11164 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
11165 b2118c49 2020-07-28 stsp if (error)
11166 b2118c49 2020-07-28 stsp goto done;
11168 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11169 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
11170 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
11171 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
11172 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
11173 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
11174 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
11175 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11177 b2118c49 2020-07-28 stsp if (show_files) {
11178 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11179 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11180 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
11181 b2118c49 2020-07-28 stsp continue;
11183 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
11184 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
11186 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
11187 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
11189 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
11190 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
11191 b2118c49 2020-07-28 stsp if (error)
11192 b2118c49 2020-07-28 stsp goto done;
11193 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11194 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
11195 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
11201 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
11202 b2118c49 2020-07-28 stsp free((char *)pe->path);
11203 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
11204 b2118c49 2020-07-28 stsp free(cwd);
11205 b2118c49 2020-07-28 stsp free(id_str);
11206 b2118c49 2020-07-28 stsp free(uuidstr);
11207 0ebf8283 2019-07-24 stsp return error;