2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
67 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp sigint_received = 1;
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp sigpipe_received = 1;
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 5c860e29 2018-03-12 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
86 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
115 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
116 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
117 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
118 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
123 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
124 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
126 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
127 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
129 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
131 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
132 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
133 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
135 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
139 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
141 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
143 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
144 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
145 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
146 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
150 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
151 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
152 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
155 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
156 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
157 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
158 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
159 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
170 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
173 ce5b7c56 2019-07-09 stsp static void
174 ce5b7c56 2019-07-09 stsp list_commands(void)
178 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
183 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
192 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
193 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
194 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
195 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
209 ce5b7c56 2019-07-09 stsp usage(hflag);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 1e70621d 2018-03-27 stsp optind = 0;
218 53ccebc2 2019-07-30 stsp if (Vflag) {
219 53ccebc2 2019-07-30 stsp got_version_print_str();
223 5c860e29 2018-03-12 stsp if (argc <= 0)
224 ce5b7c56 2019-07-09 stsp usage(hflag);
226 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
227 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
229 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
230 d7d4f210 2018-03-12 stsp const struct got_error *error;
232 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
234 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
239 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
241 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
242 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
245 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 70015d7a 2019-11-08 stsp !(sigint_received &&
247 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
255 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 ce5b7c56 2019-07-09 stsp list_commands();
260 4ed7e80c 2018-05-20 stsp __dead static void
261 ce5b7c56 2019-07-09 stsp usage(int hflag)
263 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 53ccebc2 2019-07-30 stsp getprogname());
266 ce5b7c56 2019-07-09 stsp list_commands();
270 0266afb7 2019-01-04 stsp static const struct got_error *
271 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
273 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
274 e2ba3d07 2019-05-13 stsp const char *editor;
276 8920fa04 2019-08-18 stsp *abspath = NULL;
278 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
279 e2ba3d07 2019-05-13 stsp if (editor == NULL)
280 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
282 0ee7065d 2019-05-13 stsp if (editor) {
283 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
285 0ee7065d 2019-05-13 stsp return err;
288 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
289 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
290 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
291 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
294 e2ba3d07 2019-05-13 stsp return NULL;
297 e2ba3d07 2019-05-13 stsp static const struct got_error *
298 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 c530dc23 2019-07-23 stsp const char *worktree_path)
301 163ce85a 2019-05-13 stsp const struct got_error *err;
303 37c06ea4 2019-07-15 stsp #ifdef PROFILE
304 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
305 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
307 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
310 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
313 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
317 163ce85a 2019-05-13 stsp if (err != NULL)
318 163ce85a 2019-05-13 stsp return err;
320 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
323 0266afb7 2019-01-04 stsp return NULL;
326 2c7829a4 2019-06-17 stsp __dead static void
327 2c7829a4 2019-06-17 stsp usage_init(void)
329 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
333 2c7829a4 2019-06-17 stsp static const struct got_error *
334 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
336 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
337 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
340 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 2c7829a4 2019-06-17 stsp switch (ch) {
343 2c7829a4 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp /* NOTREACHED */
348 2c7829a4 2019-06-17 stsp argc -= optind;
349 2c7829a4 2019-06-17 stsp argv += optind;
351 2c7829a4 2019-06-17 stsp #ifndef PROFILE
352 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 2c7829a4 2019-06-17 stsp err(1, "pledge");
355 2c7829a4 2019-06-17 stsp if (argc != 1)
356 bc20e173 2019-06-17 stsp usage_init();
358 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
359 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
360 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
362 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
364 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
365 2c7829a4 2019-06-17 stsp if (error &&
366 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
369 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
373 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
375 3ce1b845 2019-07-15 stsp free(repo_path);
376 3ce1b845 2019-07-15 stsp return error;
379 3ce1b845 2019-07-15 stsp __dead static void
380 3ce1b845 2019-07-15 stsp usage_import(void)
382 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
388 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
391 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
392 3ce1b845 2019-07-15 stsp int st = -1;
394 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
395 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
396 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
398 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
400 3ce1b845 2019-07-15 stsp goto doneediting;
402 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
403 3ce1b845 2019-07-15 stsp _exit(127);
406 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
407 3ce1b845 2019-07-15 stsp if (errno != EINTR)
410 3ce1b845 2019-07-15 stsp doneediting:
411 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
412 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
413 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
415 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
416 3ce1b845 2019-07-15 stsp errno = EINTR;
420 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
423 3ce1b845 2019-07-15 stsp static const struct got_error *
424 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 3ce1b845 2019-07-15 stsp const char *initial_content)
427 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
428 3ce1b845 2019-07-15 stsp char buf[1024];
429 3ce1b845 2019-07-15 stsp struct stat st, st2;
431 3ce1b845 2019-07-15 stsp int content_changed = 0;
432 3ce1b845 2019-07-15 stsp size_t len;
434 3ce1b845 2019-07-15 stsp *logmsg = NULL;
436 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
437 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
439 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
442 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
445 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
449 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
450 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
452 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
455 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
456 3ce1b845 2019-07-15 stsp if (fp == NULL) {
457 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
460 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 3ce1b845 2019-07-15 stsp content_changed = 1;
463 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
465 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
467 3ce1b845 2019-07-15 stsp fclose(fp);
469 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
474 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
475 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
479 3ce1b845 2019-07-15 stsp free(*logmsg);
480 3ce1b845 2019-07-15 stsp *logmsg = NULL;
482 3ce1b845 2019-07-15 stsp return err;
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
489 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
490 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
491 1601cb9f 2020-09-11 naddy int initial_content_len;
492 97972933 2020-09-11 stsp int fd = -1;
494 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
495 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 1601cb9f 2020-09-11 naddy branch_name);
497 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
498 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
500 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
505 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
510 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
512 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
514 3ce1b845 2019-07-15 stsp free(initial_content);
515 3ce1b845 2019-07-15 stsp return err;
518 3ce1b845 2019-07-15 stsp static const struct got_error *
519 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
521 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
522 3ce1b845 2019-07-15 stsp return NULL;
525 3ce1b845 2019-07-15 stsp static const struct got_error *
526 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
527 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
529 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
530 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
531 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
533 84792843 2019-08-09 stsp *author = NULL;
535 50b0790e 2020-09-11 stsp if (worktree)
536 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
537 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
540 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
541 50b0790e 2020-09-11 stsp * significant to least significant:
542 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
543 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
544 50b0790e 2020-09-11 stsp * 3) repository's git config file
545 50b0790e 2020-09-11 stsp * 4) environment variables
546 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
549 50b0790e 2020-09-11 stsp if (worktree_conf)
550 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
551 50b0790e 2020-09-11 stsp if (got_author == NULL)
552 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
553 84792843 2019-08-09 stsp if (got_author == NULL) {
554 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
555 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
556 c9956ddf 2019-09-08 stsp if (name && email) {
557 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
558 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
559 c9956ddf 2019-09-08 stsp return NULL;
562 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
563 257add31 2020-09-09 stsp if (got_author == NULL) {
564 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
565 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
567 257add31 2020-09-09 stsp if (name && email) {
568 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
570 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
571 257add31 2020-09-09 stsp return NULL;
573 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
574 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
578 aba9c984 2019-09-08 stsp *author = strdup(got_author);
579 aba9c984 2019-09-08 stsp if (*author == NULL)
580 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
583 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
584 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
586 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
587 84792843 2019-08-09 stsp got_author++;
588 aba9c984 2019-09-08 stsp if (*got_author != '<') {
589 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
592 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
593 84792843 2019-08-09 stsp got_author++;
594 aba9c984 2019-09-08 stsp if (*got_author != '@') {
595 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
598 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
599 84792843 2019-08-09 stsp got_author++;
600 84792843 2019-08-09 stsp if (*got_author != '>')
601 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
604 aba9c984 2019-09-08 stsp free(*author);
605 aba9c984 2019-09-08 stsp *author = NULL;
607 aba9c984 2019-09-08 stsp return err;
610 c9956ddf 2019-09-08 stsp static const struct got_error *
611 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
613 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
615 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
616 c9956ddf 2019-09-08 stsp if (homedir) {
617 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
618 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
621 c9956ddf 2019-09-08 stsp return NULL;
624 84792843 2019-08-09 stsp static const struct got_error *
625 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
627 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
628 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
629 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
630 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
631 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
632 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
633 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
634 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
636 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
637 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
638 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
640 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
642 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
643 3ce1b845 2019-07-15 stsp switch (ch) {
645 3ce1b845 2019-07-15 stsp branch_name = optarg;
648 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
649 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
650 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
655 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
657 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
663 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
665 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
671 b2b65d18 2019-08-22 stsp usage_import();
672 3ce1b845 2019-07-15 stsp /* NOTREACHED */
676 3ce1b845 2019-07-15 stsp argc -= optind;
677 3ce1b845 2019-07-15 stsp argv += optind;
679 3ce1b845 2019-07-15 stsp #ifndef PROFILE
680 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
682 3ce1b845 2019-07-15 stsp NULL) == -1)
683 3ce1b845 2019-07-15 stsp err(1, "pledge");
685 3ce1b845 2019-07-15 stsp if (argc != 1)
686 3ce1b845 2019-07-15 stsp usage_import();
688 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
689 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
690 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
691 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
693 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
694 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
697 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
701 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
703 aba9c984 2019-09-08 stsp return error;
706 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
707 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
708 e560b7e0 2019-11-28 stsp * an unintended typo.
710 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
711 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
713 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
714 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
718 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
719 3ce1b845 2019-07-15 stsp if (error) {
720 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
723 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
724 3ce1b845 2019-07-15 stsp "import target branch already exists");
728 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
729 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
730 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
733 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
736 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
737 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
739 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
740 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
743 8e158b01 2019-09-22 stsp free(logmsg);
744 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
745 ef293bdd 2019-10-21 stsp path_dir, refname);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
748 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
749 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
754 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
755 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
756 ef293bdd 2019-10-21 stsp if (logmsg_path)
757 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
761 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
762 ef293bdd 2019-10-21 stsp if (error) {
763 ef293bdd 2019-10-21 stsp if (logmsg_path)
764 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
768 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
769 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
770 ef293bdd 2019-10-21 stsp if (error) {
771 ef293bdd 2019-10-21 stsp if (logmsg_path)
772 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
777 ef293bdd 2019-10-21 stsp if (error) {
778 ef293bdd 2019-10-21 stsp if (logmsg_path)
779 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
784 ef293bdd 2019-10-21 stsp if (error) {
785 ef293bdd 2019-10-21 stsp if (logmsg_path)
786 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
790 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
791 ef293bdd 2019-10-21 stsp if (error) {
792 ef293bdd 2019-10-21 stsp if (logmsg_path)
793 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
797 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
798 3ce1b845 2019-07-15 stsp if (error) {
799 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
800 ef293bdd 2019-10-21 stsp if (logmsg_path)
801 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
805 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
806 3ce1b845 2019-07-15 stsp branch_ref);
807 ef293bdd 2019-10-21 stsp if (error) {
808 ef293bdd 2019-10-21 stsp if (logmsg_path)
809 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
813 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
814 ef293bdd 2019-10-21 stsp if (error) {
815 ef293bdd 2019-10-21 stsp if (logmsg_path)
816 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
822 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
824 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
825 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
826 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
827 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
828 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
829 8e158b01 2019-09-22 stsp free(logmsg);
830 ef293bdd 2019-10-21 stsp free(logmsg_path);
831 2c7829a4 2019-06-17 stsp free(repo_path);
832 3ce1b845 2019-07-15 stsp free(editor);
833 3ce1b845 2019-07-15 stsp free(refname);
834 3ce1b845 2019-07-15 stsp free(new_commit_id);
835 3ce1b845 2019-07-15 stsp free(id_str);
836 aba9c984 2019-09-08 stsp free(author);
837 c9956ddf 2019-09-08 stsp free(gitconfig_path);
838 3ce1b845 2019-07-15 stsp if (branch_ref)
839 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
840 3ce1b845 2019-07-15 stsp if (head_ref)
841 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
842 2c7829a4 2019-06-17 stsp return error;
845 93658fb9 2020-03-18 stsp __dead static void
846 93658fb9 2020-03-18 stsp usage_clone(void)
848 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
849 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
853 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
854 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
855 892ac3b6 2020-03-18 stsp int last_p_indexed;
856 892ac3b6 2020-03-18 stsp int last_p_resolved;
857 68999b92 2020-03-18 stsp int verbosity;
860 93658fb9 2020-03-18 stsp static const struct got_error *
861 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
862 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
864 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
865 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
866 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
867 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
869 68999b92 2020-03-18 stsp if (a->verbosity < 0)
870 68999b92 2020-03-18 stsp return NULL;
872 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
873 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
874 892ac3b6 2020-03-18 stsp fflush(stdout);
875 12d1281e 2020-03-19 stsp return NULL;
878 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
879 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
880 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
881 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
882 892ac3b6 2020-03-18 stsp print_size = 1;
883 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
884 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
885 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
887 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
888 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
889 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
890 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
891 892ac3b6 2020-03-18 stsp print_indexed = 1;
892 892ac3b6 2020-03-18 stsp print_size = 1;
895 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
896 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
897 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
898 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
899 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
900 892ac3b6 2020-03-18 stsp print_resolved = 1;
901 892ac3b6 2020-03-18 stsp print_indexed = 1;
902 892ac3b6 2020-03-18 stsp print_size = 1;
907 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
908 892ac3b6 2020-03-18 stsp printf("\r");
909 892ac3b6 2020-03-18 stsp if (print_size)
910 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
911 d715f13e 2020-03-19 stsp if (print_indexed)
912 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
913 d715f13e 2020-03-19 stsp if (print_resolved)
914 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
915 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
916 892ac3b6 2020-03-18 stsp fflush(stdout);
918 531c3985 2020-03-18 stsp return NULL;
921 531c3985 2020-03-18 stsp static const struct got_error *
922 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
923 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
925 4ba14133 2020-03-20 stsp const struct got_error *err;
926 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
928 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
930 4ba14133 2020-03-20 stsp return err;
932 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
933 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
934 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
935 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
936 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
938 4ba14133 2020-03-20 stsp return err;
941 4ba14133 2020-03-20 stsp static const struct got_error *
942 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
943 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
945 41b0de12 2020-03-21 stsp const struct got_error *err;
946 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
948 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
949 41b0de12 2020-03-21 stsp const char *refname = pe->path;
950 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
952 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
955 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
956 41b0de12 2020-03-21 stsp const char *refname = pe->path;
957 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
958 41b0de12 2020-03-21 stsp char *id_str;
960 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
962 41b0de12 2020-03-21 stsp return err;
963 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
964 41b0de12 2020-03-21 stsp free(id_str);
967 41b0de12 2020-03-21 stsp return NULL;
970 6338a6a1 2020-03-21 stsp static const struct got_error *
971 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
972 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
974 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
975 6338a6a1 2020-03-21 stsp struct got_reference *ref;
976 6338a6a1 2020-03-21 stsp char *id_str;
978 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
980 6338a6a1 2020-03-21 stsp return err;
982 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
986 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
987 6338a6a1 2020-03-21 stsp got_ref_close(ref);
989 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
990 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
992 6338a6a1 2020-03-21 stsp free(id_str);
993 6338a6a1 2020-03-21 stsp return err;
997 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
999 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1001 0e4002ca 2020-03-21 stsp refname += 5;
1004 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1005 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1007 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1009 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1012 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1013 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1015 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1016 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1019 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1020 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1023 0e4002ca 2020-03-21 stsp static int
1024 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1026 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1028 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1029 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1036 41b0de12 2020-03-21 stsp static const struct got_error *
1037 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1038 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1040 0e4002ca 2020-03-21 stsp const struct got_error *err;
1041 0e4002ca 2020-03-21 stsp char *remote_refname;
1043 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1044 0e4002ca 2020-03-21 stsp refname += 5;
1046 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1047 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1048 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1050 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1051 0e4002ca 2020-03-21 stsp free(remote_refname);
1052 0e4002ca 2020-03-21 stsp return err;
1055 0e4002ca 2020-03-21 stsp static const struct got_error *
1056 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1058 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1059 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1060 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1061 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1062 bb64b798 2020-03-18 stsp const char *repo_path;
1063 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1064 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1065 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1066 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1067 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1068 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1069 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1070 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1071 257add31 2020-09-09 stsp char *gitconfig_path = NULL, *gotconfig_path = NULL;
1072 257add31 2020-09-09 stsp char *gitconfig = NULL, *gotconfig = NULL;
1073 257add31 2020-09-09 stsp FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1074 b46f3e71 2020-03-18 stsp ssize_t n;
1075 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1076 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1077 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1079 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1080 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1081 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1082 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1084 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1085 93658fb9 2020-03-18 stsp switch (ch) {
1087 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1090 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1091 4ba14133 2020-03-20 stsp optarg, NULL);
1092 4ba14133 2020-03-20 stsp if (error)
1093 4ba14133 2020-03-20 stsp return error;
1096 41b0de12 2020-03-21 stsp list_refs_only = 1;
1099 469dd726 2020-03-20 stsp mirror_references = 1;
1102 68999b92 2020-03-18 stsp if (verbosity < 0)
1103 68999b92 2020-03-18 stsp verbosity = 0;
1104 68999b92 2020-03-18 stsp else if (verbosity < 3)
1105 68999b92 2020-03-18 stsp verbosity++;
1108 68999b92 2020-03-18 stsp verbosity = -1;
1111 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1112 0e4002ca 2020-03-21 stsp optarg, NULL);
1113 0e4002ca 2020-03-21 stsp if (error)
1114 0e4002ca 2020-03-21 stsp return error;
1117 93658fb9 2020-03-18 stsp usage_clone();
1121 93658fb9 2020-03-18 stsp argc -= optind;
1122 93658fb9 2020-03-18 stsp argv += optind;
1124 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1125 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1126 41b0de12 2020-03-21 stsp if (list_refs_only) {
1127 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1128 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1129 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1130 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1131 41b0de12 2020-03-21 stsp if (mirror_references)
1132 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1133 41b0de12 2020-03-21 stsp if (verbosity == -1)
1134 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1135 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1136 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1139 93658fb9 2020-03-18 stsp uri = argv[0];
1141 9df6f38b 2020-03-18 stsp if (argc == 1)
1142 93658fb9 2020-03-18 stsp dirname = NULL;
1143 9df6f38b 2020-03-18 stsp else if (argc == 2)
1144 93658fb9 2020-03-18 stsp dirname = argv[1];
1146 93658fb9 2020-03-18 stsp usage_clone();
1148 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1149 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1150 39c64a6a 2020-03-18 stsp if (error)
1151 09f63084 2020-03-20 stsp goto done;
1153 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1154 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1155 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1156 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1157 09838ffc 2020-03-18 stsp goto done;
1160 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1161 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1162 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1163 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1164 39c64a6a 2020-03-18 stsp err(1, "pledge");
1166 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1167 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1168 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1169 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1170 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1171 39c64a6a 2020-03-18 stsp err(1, "pledge");
1173 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1174 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1175 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1176 39c64a6a 2020-03-18 stsp goto done;
1178 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1179 39c64a6a 2020-03-18 stsp goto done;
1181 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1182 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1183 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1184 bb64b798 2020-03-18 stsp goto done;
1186 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1188 bb64b798 2020-03-18 stsp repo_path = dirname;
1190 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1191 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1192 41b0de12 2020-03-21 stsp if (error)
1193 41b0de12 2020-03-21 stsp goto done;
1195 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1196 41b0de12 2020-03-21 stsp if (error)
1197 41b0de12 2020-03-21 stsp goto done;
1198 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1199 41b0de12 2020-03-21 stsp if (error)
1200 41b0de12 2020-03-21 stsp goto done;
1203 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1204 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1205 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1206 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1207 ee448f5f 2020-03-18 stsp goto done;
1210 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1211 ee448f5f 2020-03-18 stsp if (error)
1212 ee448f5f 2020-03-18 stsp goto done;
1214 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1215 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1216 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1218 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1219 9c52365f 2020-03-21 stsp server_path, verbosity);
1220 39c64a6a 2020-03-18 stsp if (error)
1221 bb64b798 2020-03-18 stsp goto done;
1223 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1224 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1225 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1226 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1227 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1228 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1229 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1230 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1231 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1232 39c64a6a 2020-03-18 stsp if (error)
1233 d9b4d0c0 2020-03-18 stsp goto done;
1235 41b0de12 2020-03-21 stsp if (list_refs_only) {
1236 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1237 41b0de12 2020-03-21 stsp goto done;
1240 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1241 39c64a6a 2020-03-18 stsp if (error)
1242 d9b4d0c0 2020-03-18 stsp goto done;
1243 68999b92 2020-03-18 stsp if (verbosity >= 0)
1244 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1245 d9b4d0c0 2020-03-18 stsp free(id_str);
1247 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1248 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1249 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1250 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1251 7ebc0570 2020-03-18 stsp char *remote_refname;
1253 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1254 0e4002ca 2020-03-21 stsp !mirror_references) {
1255 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1256 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1257 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1258 0e4002ca 2020-03-21 stsp if (error)
1259 0e4002ca 2020-03-21 stsp goto done;
1263 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1264 39c64a6a 2020-03-18 stsp if (error)
1265 d9b4d0c0 2020-03-18 stsp goto done;
1267 469dd726 2020-03-20 stsp if (mirror_references)
1270 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1273 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1274 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1275 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1276 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1277 7ebc0570 2020-03-18 stsp goto done;
1279 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1280 f298ae0f 2020-03-25 stsp free(remote_refname);
1281 39c64a6a 2020-03-18 stsp if (error)
1282 d9b4d0c0 2020-03-18 stsp goto done;
1285 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1286 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1287 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1288 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1289 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1290 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1292 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1295 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1296 39c64a6a 2020-03-18 stsp if (error) {
1297 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1298 55330abe 2020-03-20 stsp error = NULL;
1301 d9b4d0c0 2020-03-18 stsp goto done;
1304 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1305 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1306 f298ae0f 2020-03-25 stsp if (error)
1307 f298ae0f 2020-03-25 stsp goto done;
1309 f298ae0f 2020-03-25 stsp if (mirror_references)
1312 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1315 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1316 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1317 f298ae0f 2020-03-25 stsp refname) == -1) {
1318 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1319 f298ae0f 2020-03-25 stsp goto done;
1321 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1322 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1323 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1324 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1325 f298ae0f 2020-03-25 stsp free(remote_refname);
1326 f298ae0f 2020-03-25 stsp goto done;
1328 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1329 f298ae0f 2020-03-25 stsp if (error) {
1330 f298ae0f 2020-03-25 stsp free(remote_refname);
1331 f298ae0f 2020-03-25 stsp free(remote_target);
1332 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1333 f298ae0f 2020-03-25 stsp error = NULL;
1336 f298ae0f 2020-03-25 stsp goto done;
1338 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1339 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1340 f298ae0f 2020-03-25 stsp free(remote_refname);
1341 f298ae0f 2020-03-25 stsp free(remote_target);
1342 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1343 39c64a6a 2020-03-18 stsp if (error)
1344 d9b4d0c0 2020-03-18 stsp goto done;
1346 4ba14133 2020-03-20 stsp if (pe == NULL) {
1348 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1349 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1350 4ba14133 2020-03-20 stsp * which could be fetched instead.
1352 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1353 4ba14133 2020-03-20 stsp const char *target = pe->path;
1354 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1356 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1357 4ba14133 2020-03-20 stsp if (error) {
1358 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1359 4ba14133 2020-03-20 stsp error = NULL;
1362 4ba14133 2020-03-20 stsp goto done;
1365 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1366 f298ae0f 2020-03-25 stsp verbosity, repo);
1367 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1368 4ba14133 2020-03-20 stsp if (error)
1369 4ba14133 2020-03-20 stsp goto done;
1374 257add31 2020-09-09 stsp /* Create got.conf(5). */
1375 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1376 257add31 2020-09-09 stsp if (gotconfig_path == NULL) {
1377 257add31 2020-09-09 stsp error = got_error_from_errno("got_repo_get_path_gotconfig");
1378 257add31 2020-09-09 stsp goto done;
1380 257add31 2020-09-09 stsp gotconfig_file = fopen(gotconfig_path, "a");
1381 257add31 2020-09-09 stsp if (gotconfig_file == NULL) {
1382 257add31 2020-09-09 stsp error = got_error_from_errno2("fopen", gotconfig_path);
1383 257add31 2020-09-09 stsp goto done;
1385 257add31 2020-09-09 stsp got_path_strip_trailing_slashes(server_path);
1386 257add31 2020-09-09 stsp if (asprintf(&gotconfig,
1387 257add31 2020-09-09 stsp "remote \"%s\" {\n"
1388 257add31 2020-09-09 stsp "\tserver %s\n"
1389 257add31 2020-09-09 stsp "\tprotocol %s\n"
1391 257add31 2020-09-09 stsp "\trepository \"%s\"\n"
1394 257add31 2020-09-09 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1395 257add31 2020-09-09 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1396 257add31 2020-09-09 stsp server_path,
1397 257add31 2020-09-09 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1398 257add31 2020-09-09 stsp error = got_error_from_errno("asprintf");
1399 257add31 2020-09-09 stsp goto done;
1401 257add31 2020-09-09 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1402 257add31 2020-09-09 stsp if (n != strlen(gotconfig)) {
1403 257add31 2020-09-09 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1404 257add31 2020-09-09 stsp goto done;
1407 257add31 2020-09-09 stsp /* Create a config file Git can understand. */
1408 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1409 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1410 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1411 659e7fbd 2020-03-20 stsp goto done;
1413 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1414 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1415 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1416 659e7fbd 2020-03-20 stsp goto done;
1418 659e7fbd 2020-03-20 stsp if (mirror_references) {
1419 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1420 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1421 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1422 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1423 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1424 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1425 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1426 659e7fbd 2020-03-20 stsp goto done;
1428 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1429 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1430 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1431 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1432 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1433 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1434 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1435 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1436 659e7fbd 2020-03-20 stsp goto done;
1439 659e7fbd 2020-03-20 stsp const char *branchname;
1442 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1443 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1445 659e7fbd 2020-03-20 stsp if (head_symref) {
1446 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1447 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1448 659e7fbd 2020-03-20 stsp branchname += 11;
1450 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1451 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1452 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1453 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1454 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1455 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1456 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1457 659e7fbd 2020-03-20 stsp branchname) == -1) {
1458 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1459 659e7fbd 2020-03-20 stsp goto done;
1462 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1463 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1464 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1465 659e7fbd 2020-03-20 stsp goto done;
1468 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1469 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1470 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1472 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1473 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1474 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1475 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1476 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1478 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1479 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1480 257add31 2020-09-09 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1481 257add31 2020-09-09 stsp error = got_error_from_errno("fclose");
1482 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1483 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1485 bb64b798 2020-03-18 stsp got_repo_close(repo);
1486 659e7fbd 2020-03-20 stsp if (head_symref)
1487 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1488 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1489 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1490 d9b4d0c0 2020-03-18 stsp free(pe->data);
1492 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1493 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1494 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1495 d9b4d0c0 2020-03-18 stsp free(pe->data);
1497 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1498 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1499 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1500 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1501 09838ffc 2020-03-18 stsp free(proto);
1502 09838ffc 2020-03-18 stsp free(host);
1503 09838ffc 2020-03-18 stsp free(port);
1504 09838ffc 2020-03-18 stsp free(server_path);
1505 09838ffc 2020-03-18 stsp free(repo_name);
1506 bb64b798 2020-03-18 stsp free(default_destdir);
1507 257add31 2020-09-09 stsp free(gotconfig);
1508 257add31 2020-09-09 stsp free(gitconfig);
1509 257add31 2020-09-09 stsp free(gotconfig_path);
1510 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1511 b46f3e71 2020-03-18 stsp free(git_url);
1512 39c64a6a 2020-03-18 stsp return error;
1515 7848a0e1 2020-03-19 stsp static const struct got_error *
1516 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1517 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1519 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1520 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1521 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1523 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1525 7848a0e1 2020-03-19 stsp goto done;
1527 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1528 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1529 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1531 88609724 2020-03-21 stsp goto done;
1532 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1533 88609724 2020-03-21 stsp goto done;
1534 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1535 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1536 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1538 db6d8ad8 2020-03-21 stsp goto done;
1541 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1542 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1543 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1544 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1545 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1547 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1549 7848a0e1 2020-03-19 stsp goto done;
1550 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1552 e8a967e0 2020-03-21 stsp goto done;
1554 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1556 7848a0e1 2020-03-19 stsp goto done;
1557 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1558 6338a6a1 2020-03-21 stsp goto done;
1560 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1562 6338a6a1 2020-03-21 stsp goto done;
1563 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1565 6338a6a1 2020-03-21 stsp goto done;
1568 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1569 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1570 6338a6a1 2020-03-21 stsp new_id_str);
1572 7848a0e1 2020-03-19 stsp free(old_id);
1573 7848a0e1 2020-03-19 stsp free(new_id_str);
1574 7848a0e1 2020-03-19 stsp return err;
1577 f1bcca34 2020-03-25 stsp static const struct got_error *
1578 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1579 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1581 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1582 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1583 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1585 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1586 bcf34b0e 2020-03-26 stsp if (err) {
1587 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1588 bcf34b0e 2020-03-26 stsp return err;
1589 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1591 bcf34b0e 2020-03-26 stsp goto done;
1593 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1595 bcf34b0e 2020-03-26 stsp goto done;
1597 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1598 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1599 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1600 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1602 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1604 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1605 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1606 bcf34b0e 2020-03-26 stsp goto done;
1608 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1609 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1611 bcf34b0e 2020-03-26 stsp goto done;
1613 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1615 bcf34b0e 2020-03-26 stsp goto done;
1617 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1618 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1619 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1623 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1624 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1625 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1626 bcf34b0e 2020-03-26 stsp err = unlock_err;
1628 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1629 f1bcca34 2020-03-25 stsp return err;
1632 2ab43947 2020-03-18 stsp __dead static void
1633 7848a0e1 2020-03-19 stsp usage_fetch(void)
1635 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1636 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1637 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1638 13f12b09 2020-03-21 stsp getprogname());
1642 7848a0e1 2020-03-19 stsp static const struct got_error *
1643 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1644 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1646 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1647 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1648 3789fd73 2020-03-26 stsp char *id_str = NULL;
1650 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1651 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1653 3789fd73 2020-03-26 stsp return err;
1654 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1655 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1656 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1657 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1660 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1662 3789fd73 2020-03-26 stsp return err;
1663 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1665 3789fd73 2020-03-26 stsp goto done;
1667 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1669 3789fd73 2020-03-26 stsp goto done;
1670 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1671 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1672 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1677 3789fd73 2020-03-26 stsp free(id_str);
1678 3789fd73 2020-03-26 stsp return NULL;
1681 3789fd73 2020-03-26 stsp static const struct got_error *
1682 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1683 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1684 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1685 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1687 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1688 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1689 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1690 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1691 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1692 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1694 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1696 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1698 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1700 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1702 3789fd73 2020-03-26 stsp goto done;
1704 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1705 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1707 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1708 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1709 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1710 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1711 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1713 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1714 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1715 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1716 3789fd73 2020-03-26 stsp goto done;
1718 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1722 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1723 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1726 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1729 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1730 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1733 3789fd73 2020-03-26 stsp if (pe != NULL)
1736 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1740 3789fd73 2020-03-26 stsp if (local_refname) {
1741 3789fd73 2020-03-26 stsp struct got_reference *ref;
1742 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1743 3789fd73 2020-03-26 stsp if (err) {
1744 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1746 3789fd73 2020-03-26 stsp free(local_refname);
1747 3789fd73 2020-03-26 stsp local_refname = NULL;
1750 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1753 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1754 3789fd73 2020-03-26 stsp got_ref_close(ref);
1755 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1756 3789fd73 2020-03-26 stsp err = unlock_err;
1760 3789fd73 2020-03-26 stsp free(local_refname);
1761 3789fd73 2020-03-26 stsp local_refname = NULL;
1765 3789fd73 2020-03-26 stsp free(remote_namespace);
1766 3789fd73 2020-03-26 stsp free(local_refname);
1767 0e4002ca 2020-03-21 stsp return err;
1770 0e4002ca 2020-03-21 stsp static const struct got_error *
1771 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1772 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1774 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1775 0e4002ca 2020-03-21 stsp char *remote_refname;
1776 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1778 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1779 0e4002ca 2020-03-21 stsp refname += 5;
1781 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1782 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1783 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1785 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1786 0e4002ca 2020-03-21 stsp if (err) {
1787 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1788 0e4002ca 2020-03-21 stsp goto done;
1789 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1791 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1792 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1793 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1794 9f142382 2020-03-21 stsp err = unlock_err;
1795 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1798 0e4002ca 2020-03-21 stsp free(remote_refname);
1799 f21ec2f0 2020-03-21 stsp return err;
1802 f21ec2f0 2020-03-21 stsp static const struct got_error *
1803 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1805 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1806 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1807 7848a0e1 2020-03-19 stsp const char *remote_name;
1808 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1809 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1810 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1811 7848a0e1 2020-03-19 stsp int nremotes;
1812 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1813 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1814 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1815 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1816 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1817 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1818 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1819 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1820 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1821 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1822 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1823 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1825 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1826 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1827 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1828 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1830 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1831 7848a0e1 2020-03-19 stsp switch (ch) {
1833 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1836 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1837 4ba14133 2020-03-20 stsp optarg, NULL);
1838 4ba14133 2020-03-20 stsp if (error)
1839 4ba14133 2020-03-20 stsp return error;
1842 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1845 41b0de12 2020-03-21 stsp list_refs_only = 1;
1848 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1849 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1850 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1852 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1855 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1858 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1859 7848a0e1 2020-03-19 stsp verbosity = 0;
1860 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1861 7848a0e1 2020-03-19 stsp verbosity++;
1864 7848a0e1 2020-03-19 stsp verbosity = -1;
1867 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1868 0e4002ca 2020-03-21 stsp optarg, NULL);
1869 0e4002ca 2020-03-21 stsp if (error)
1870 0e4002ca 2020-03-21 stsp return error;
1873 7848a0e1 2020-03-19 stsp usage_fetch();
1877 7848a0e1 2020-03-19 stsp argc -= optind;
1878 7848a0e1 2020-03-19 stsp argv += optind;
1880 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1881 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1882 41b0de12 2020-03-21 stsp if (list_refs_only) {
1883 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1884 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1885 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1886 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1887 f21ec2f0 2020-03-21 stsp if (delete_refs)
1888 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1889 41b0de12 2020-03-21 stsp if (verbosity == -1)
1890 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1893 7848a0e1 2020-03-19 stsp if (argc == 0)
1894 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1895 7848a0e1 2020-03-19 stsp else if (argc == 1)
1896 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1898 7848a0e1 2020-03-19 stsp usage_fetch();
1900 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1901 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1902 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1903 7848a0e1 2020-03-19 stsp goto done;
1906 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1907 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1908 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1909 7848a0e1 2020-03-19 stsp goto done;
1911 7848a0e1 2020-03-19 stsp error = NULL;
1912 7848a0e1 2020-03-19 stsp if (worktree) {
1913 7848a0e1 2020-03-19 stsp repo_path =
1914 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1915 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1916 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1917 7848a0e1 2020-03-19 stsp if (error)
1918 7848a0e1 2020-03-19 stsp goto done;
1920 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1921 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1922 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1923 7848a0e1 2020-03-19 stsp goto done;
1928 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1929 7848a0e1 2020-03-19 stsp if (error)
1930 7848a0e1 2020-03-19 stsp goto done;
1932 50b0790e 2020-09-11 stsp if (worktree) {
1933 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1934 50b0790e 2020-09-11 stsp if (worktree_conf) {
1935 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1936 50b0790e 2020-09-11 stsp worktree_conf);
1937 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1938 50b0790e 2020-09-11 stsp remote = &remotes[i];
1939 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1944 50b0790e 2020-09-11 stsp if (remote == NULL) {
1945 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
1946 50b0790e 2020-09-11 stsp if (repo_conf) {
1947 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1948 50b0790e 2020-09-11 stsp repo_conf);
1949 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1950 50b0790e 2020-09-11 stsp remote = &remotes[i];
1951 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1956 50b0790e 2020-09-11 stsp if (remote == NULL) {
1957 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1958 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
1959 257add31 2020-09-09 stsp remote = &remotes[i];
1960 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
1964 50b0790e 2020-09-11 stsp if (remote == NULL) {
1965 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1966 50b0790e 2020-09-11 stsp goto done;
1969 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1970 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1971 7848a0e1 2020-03-19 stsp if (error)
1972 7848a0e1 2020-03-19 stsp goto done;
1974 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1975 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1976 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1977 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1978 7848a0e1 2020-03-19 stsp err(1, "pledge");
1980 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1981 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1982 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1983 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1984 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1985 7848a0e1 2020-03-19 stsp err(1, "pledge");
1987 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1988 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1989 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1990 7848a0e1 2020-03-19 stsp goto done;
1992 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1993 7848a0e1 2020-03-19 stsp goto done;
1996 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1997 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1998 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1999 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2000 7848a0e1 2020-03-19 stsp goto done;
2003 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2004 7848a0e1 2020-03-19 stsp if (error)
2005 7848a0e1 2020-03-19 stsp goto done;
2007 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2008 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2009 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2011 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2012 9c52365f 2020-03-21 stsp server_path, verbosity);
2013 7848a0e1 2020-03-19 stsp if (error)
2014 7848a0e1 2020-03-19 stsp goto done;
2016 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2017 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2018 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2019 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2020 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2021 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2022 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2023 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2024 7848a0e1 2020-03-19 stsp if (error)
2025 7848a0e1 2020-03-19 stsp goto done;
2027 41b0de12 2020-03-21 stsp if (list_refs_only) {
2028 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2029 41b0de12 2020-03-21 stsp goto done;
2032 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2033 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2034 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2035 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2036 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2037 984065c8 2020-03-19 stsp if (error)
2038 984065c8 2020-03-19 stsp goto done;
2039 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2040 984065c8 2020-03-19 stsp free(id_str);
2041 984065c8 2020-03-19 stsp id_str = NULL;
2044 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2045 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2046 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2047 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2048 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2049 7848a0e1 2020-03-19 stsp char *remote_refname;
2051 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2052 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2053 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2054 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2055 0e4002ca 2020-03-21 stsp if (error)
2056 0e4002ca 2020-03-21 stsp goto done;
2060 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2061 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2062 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2063 7848a0e1 2020-03-19 stsp if (error) {
2064 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2065 7848a0e1 2020-03-19 stsp goto done;
2066 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2068 7848a0e1 2020-03-19 stsp if (error)
2069 7848a0e1 2020-03-19 stsp goto done;
2071 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2072 db6d8ad8 2020-03-21 stsp verbosity, repo);
2073 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2074 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2075 9f142382 2020-03-21 stsp error = unlock_err;
2076 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2077 7848a0e1 2020-03-19 stsp if (error)
2078 7848a0e1 2020-03-19 stsp goto done;
2080 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2081 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2082 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2083 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2084 7848a0e1 2020-03-19 stsp goto done;
2087 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2088 7848a0e1 2020-03-19 stsp if (error) {
2089 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2090 7848a0e1 2020-03-19 stsp goto done;
2091 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2092 688f11b3 2020-03-21 stsp verbosity, repo);
2093 7848a0e1 2020-03-19 stsp if (error)
2094 7848a0e1 2020-03-19 stsp goto done;
2096 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2097 db6d8ad8 2020-03-21 stsp verbosity, repo);
2098 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2099 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2100 9f142382 2020-03-21 stsp error = unlock_err;
2101 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2102 7848a0e1 2020-03-19 stsp if (error)
2103 7848a0e1 2020-03-19 stsp goto done;
2106 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2107 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2108 2ec30c80 2020-03-20 stsp if (error) {
2109 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2110 2ec30c80 2020-03-20 stsp goto done;
2111 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2113 2ec30c80 2020-03-20 stsp if (error)
2114 2ec30c80 2020-03-20 stsp goto done;
2116 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2117 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2118 9f142382 2020-03-21 stsp error = unlock_err;
2119 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2123 f1bcca34 2020-03-25 stsp if (delete_refs) {
2124 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2125 3789fd73 2020-03-26 stsp verbosity, repo);
2126 f1bcca34 2020-03-25 stsp if (error)
2127 f1bcca34 2020-03-25 stsp goto done;
2130 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2131 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2132 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2133 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2134 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2135 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2136 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2138 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2141 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2144 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2145 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2146 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2147 f1bcca34 2020-03-25 stsp goto done;
2149 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2150 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2151 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2152 f1bcca34 2020-03-25 stsp free(remote_refname);
2153 f1bcca34 2020-03-25 stsp goto done;
2156 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2158 f1bcca34 2020-03-25 stsp if (error) {
2159 f1bcca34 2020-03-25 stsp free(remote_refname);
2160 f1bcca34 2020-03-25 stsp free(remote_target);
2161 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2162 f1bcca34 2020-03-25 stsp error = NULL;
2165 f1bcca34 2020-03-25 stsp goto done;
2167 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2168 f1bcca34 2020-03-25 stsp verbosity, repo);
2169 f1bcca34 2020-03-25 stsp free(remote_refname);
2170 f1bcca34 2020-03-25 stsp free(remote_target);
2171 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2172 f1bcca34 2020-03-25 stsp if (error)
2173 f1bcca34 2020-03-25 stsp goto done;
2177 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2178 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2179 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2180 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2181 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2183 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2184 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2186 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2187 7848a0e1 2020-03-19 stsp if (worktree)
2188 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2189 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2190 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2191 7848a0e1 2020-03-19 stsp free(pe->data);
2193 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2194 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2195 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2196 7848a0e1 2020-03-19 stsp free(pe->data);
2198 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2199 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2200 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2201 7848a0e1 2020-03-19 stsp free(id_str);
2202 7848a0e1 2020-03-19 stsp free(cwd);
2203 7848a0e1 2020-03-19 stsp free(repo_path);
2204 7848a0e1 2020-03-19 stsp free(pack_hash);
2205 7848a0e1 2020-03-19 stsp free(proto);
2206 7848a0e1 2020-03-19 stsp free(host);
2207 7848a0e1 2020-03-19 stsp free(port);
2208 7848a0e1 2020-03-19 stsp free(server_path);
2209 7848a0e1 2020-03-19 stsp free(repo_name);
2210 7848a0e1 2020-03-19 stsp return error;
2214 7848a0e1 2020-03-19 stsp __dead static void
2215 2ab43947 2020-03-18 stsp usage_checkout(void)
2217 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2218 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2222 2ab43947 2020-03-18 stsp static void
2223 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2225 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2226 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2227 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2228 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2229 2ab43947 2020-03-18 stsp getprogname());
2232 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2233 2ab43947 2020-03-18 stsp const char *worktree_path;
2234 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2237 2ab43947 2020-03-18 stsp static const struct got_error *
2238 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2240 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2242 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2243 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2244 2ab43947 2020-03-18 stsp return NULL;
2246 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2247 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2248 2ab43947 2020-03-18 stsp return NULL;
2251 2ab43947 2020-03-18 stsp while (path[0] == '/')
2254 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2255 2ab43947 2020-03-18 stsp return NULL;
2258 2ab43947 2020-03-18 stsp static const struct got_error *
2259 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2261 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2262 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2263 2ab43947 2020-03-18 stsp return NULL;
2266 2ab43947 2020-03-18 stsp static const struct got_error *
2267 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2268 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2269 2ab43947 2020-03-18 stsp struct got_repository *repo)
2271 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2272 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2274 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2275 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2277 2ab43947 2020-03-18 stsp return err;
2279 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2280 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2283 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2284 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2286 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2288 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2296 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2297 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2298 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2300 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2301 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2302 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2303 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2304 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2305 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2307 2ab43947 2020-03-18 stsp free(yca_id);
2308 2ab43947 2020-03-18 stsp return NULL;
2311 2ab43947 2020-03-18 stsp static const struct got_error *
2312 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2313 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2314 2ab43947 2020-03-18 stsp struct got_repository *repo)
2316 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2317 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2318 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2319 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2321 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2323 2ab43947 2020-03-18 stsp goto done;
2325 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2326 2ab43947 2020-03-18 stsp is_same_branch = 1;
2327 2ab43947 2020-03-18 stsp goto done;
2329 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2330 2ab43947 2020-03-18 stsp is_same_branch = 1;
2331 2ab43947 2020-03-18 stsp goto done;
2334 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2336 2ab43947 2020-03-18 stsp goto done;
2338 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2339 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2341 2ab43947 2020-03-18 stsp goto done;
2343 2ab43947 2020-03-18 stsp for (;;) {
2344 2ab43947 2020-03-18 stsp struct got_object_id *id;
2345 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2346 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2347 2ab43947 2020-03-18 stsp if (err) {
2348 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2349 2ab43947 2020-03-18 stsp err = NULL;
2354 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2356 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2357 2ab43947 2020-03-18 stsp is_same_branch = 1;
2363 2ab43947 2020-03-18 stsp if (graph)
2364 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2365 2ab43947 2020-03-18 stsp free(head_commit_id);
2366 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2367 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2368 2ab43947 2020-03-18 stsp return err;
2371 4ed7e80c 2018-05-20 stsp static const struct got_error *
2372 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2374 4b6c9460 2020-03-05 stsp static char msg[512];
2375 4b6c9460 2020-03-05 stsp const char *branch_name;
2377 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2378 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2380 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2382 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2383 4b6c9460 2020-03-05 stsp branch_name += 11;
2385 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2386 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2387 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2388 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2389 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2390 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2392 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2395 4b6c9460 2020-03-05 stsp static const struct got_error *
2396 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2398 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2399 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2400 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2401 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2402 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2403 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2404 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2405 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2406 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2407 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2408 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2409 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2411 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2413 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2414 0bb8a95e 2018-03-12 stsp switch (ch) {
2416 08573d5b 2019-05-14 stsp branch_name = optarg;
2419 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2420 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2421 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2424 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2427 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2430 2deda0b9 2019-03-07 stsp usage_checkout();
2431 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2435 0bb8a95e 2018-03-12 stsp argc -= optind;
2436 0bb8a95e 2018-03-12 stsp argv += optind;
2438 6715a751 2018-03-16 stsp #ifndef PROFILE
2439 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2440 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2441 c09a553d 2018-03-12 stsp err(1, "pledge");
2443 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2444 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2445 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2446 76089277 2018-04-01 stsp if (repo_path == NULL)
2447 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2448 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2449 76089277 2018-04-01 stsp if (cwd == NULL) {
2450 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2451 76089277 2018-04-01 stsp goto done;
2453 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2454 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2455 230a42bd 2019-05-11 jcs if (base == NULL) {
2456 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2457 230a42bd 2019-05-11 jcs path_prefix);
2461 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2462 230a42bd 2019-05-11 jcs if (base == NULL) {
2463 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2464 230a42bd 2019-05-11 jcs repo_path);
2468 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2469 c09a553d 2018-03-12 stsp if (dotgit)
2470 c09a553d 2018-03-12 stsp *dotgit = '\0';
2471 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2472 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2473 c09a553d 2018-03-12 stsp free(cwd);
2474 76089277 2018-04-01 stsp goto done;
2476 c09a553d 2018-03-12 stsp free(cwd);
2477 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2478 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2479 76089277 2018-04-01 stsp if (repo_path == NULL) {
2480 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2481 76089277 2018-04-01 stsp goto done;
2483 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2484 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2485 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2486 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2488 b4b3a7dd 2019-07-22 stsp goto done;
2490 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2491 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2492 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2493 b4b3a7dd 2019-07-22 stsp goto done;
2497 c09a553d 2018-03-12 stsp usage_checkout();
2499 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2500 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2502 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2503 c09a553d 2018-03-12 stsp if (error != NULL)
2504 c02c541e 2019-03-29 stsp goto done;
2506 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2507 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2508 c530dc23 2019-07-23 stsp if (error) {
2509 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2510 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2511 c530dc23 2019-07-23 stsp goto done;
2512 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2513 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2514 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2515 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2516 c530dc23 2019-07-23 stsp goto done;
2520 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2521 c02c541e 2019-03-29 stsp if (error)
2522 c09a553d 2018-03-12 stsp goto done;
2524 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2525 c09a553d 2018-03-12 stsp if (error != NULL)
2526 c09a553d 2018-03-12 stsp goto done;
2528 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2529 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2530 c09a553d 2018-03-12 stsp goto done;
2532 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2533 c09a553d 2018-03-12 stsp if (error != NULL)
2534 c09a553d 2018-03-12 stsp goto done;
2536 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2537 e5dc7198 2018-12-29 stsp path_prefix);
2538 e5dc7198 2018-12-29 stsp if (error != NULL)
2539 e5dc7198 2018-12-29 stsp goto done;
2540 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2541 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2542 49520a32 2018-12-29 stsp goto done;
2545 8069f636 2019-01-12 stsp if (commit_id_str) {
2546 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2547 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2548 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2549 30837e32 2019-07-25 stsp if (error)
2550 8069f636 2019-01-12 stsp goto done;
2551 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2552 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2553 8069f636 2019-01-12 stsp if (error != NULL) {
2554 8069f636 2019-01-12 stsp free(commit_id);
2555 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2556 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2557 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2559 8069f636 2019-01-12 stsp goto done;
2561 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2562 4b6c9460 2020-03-05 stsp if (error) {
2563 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2564 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2565 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2567 45d344f6 2019-05-14 stsp goto done;
2569 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2570 8069f636 2019-01-12 stsp commit_id);
2571 8069f636 2019-01-12 stsp free(commit_id);
2572 8069f636 2019-01-12 stsp if (error)
2573 8069f636 2019-01-12 stsp goto done;
2576 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2577 f2ea84fa 2019-07-27 stsp if (error)
2578 f2ea84fa 2019-07-27 stsp goto done;
2579 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2580 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2581 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2582 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2583 c09a553d 2018-03-12 stsp if (error != NULL)
2584 c09a553d 2018-03-12 stsp goto done;
2586 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2587 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2588 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2590 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2591 8069f636 2019-01-12 stsp free(commit_id_str);
2592 76089277 2018-04-01 stsp free(repo_path);
2593 507dc3bb 2018-12-29 stsp free(worktree_path);
2594 507dc3bb 2018-12-29 stsp return error;
2597 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2598 9627c110 2020-04-18 stsp int did_something;
2599 9627c110 2020-04-18 stsp int conflicts;
2600 9627c110 2020-04-18 stsp int obstructed;
2601 9627c110 2020-04-18 stsp int not_updated;
2605 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2607 9627c110 2020-04-18 stsp if (!upa->did_something)
2610 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2611 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2612 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2613 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2614 9627c110 2020-04-18 stsp upa->obstructed);
2615 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2616 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2617 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2620 507dc3bb 2018-12-29 stsp __dead static void
2621 507dc3bb 2018-12-29 stsp usage_update(void)
2623 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2624 507dc3bb 2018-12-29 stsp getprogname());
2628 1ee397ad 2019-07-12 stsp static const struct got_error *
2629 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2631 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2633 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2634 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2635 1ee397ad 2019-07-12 stsp return NULL;
2637 9627c110 2020-04-18 stsp upa->did_something = 1;
2639 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2640 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2641 1ee397ad 2019-07-12 stsp return NULL;
2643 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2644 9627c110 2020-04-18 stsp upa->conflicts++;
2645 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2646 9627c110 2020-04-18 stsp upa->obstructed++;
2647 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2648 9627c110 2020-04-18 stsp upa->not_updated++;
2650 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2652 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2653 1ee397ad 2019-07-12 stsp return NULL;
2656 be7061eb 2018-12-30 stsp static const struct got_error *
2657 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2658 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2659 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2661 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2662 a1fb16d8 2019-05-24 stsp char *base_id_str;
2663 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2665 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2666 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2667 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2668 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2669 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2670 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2671 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2674 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2675 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2676 a1fb16d8 2019-05-24 stsp if (err) {
2677 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2678 a1fb16d8 2019-05-24 stsp return err;
2679 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2681 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2682 a1fb16d8 2019-05-24 stsp return NULL;
2684 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2685 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2686 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2688 a1fb16d8 2019-05-24 stsp return err;
2689 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2690 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2691 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2692 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2693 0ebf8283 2019-07-24 stsp return NULL;
2696 0ebf8283 2019-07-24 stsp static const struct got_error *
2697 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2699 0ebf8283 2019-07-24 stsp const struct got_error *err;
2700 0ebf8283 2019-07-24 stsp int in_progress;
2702 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2704 0ebf8283 2019-07-24 stsp return err;
2705 0ebf8283 2019-07-24 stsp if (in_progress)
2706 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2708 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2710 0ebf8283 2019-07-24 stsp return err;
2711 0ebf8283 2019-07-24 stsp if (in_progress)
2712 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2714 a1fb16d8 2019-05-24 stsp return NULL;
2717 a5edda0a 2019-07-27 stsp static const struct got_error *
2718 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2719 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2721 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2722 a5edda0a 2019-07-27 stsp char *path;
2725 a5edda0a 2019-07-27 stsp if (argc == 0) {
2726 a5edda0a 2019-07-27 stsp path = strdup("");
2727 a5edda0a 2019-07-27 stsp if (path == NULL)
2728 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2729 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2732 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2733 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2736 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2737 a5edda0a 2019-07-27 stsp if (err) {
2738 a5edda0a 2019-07-27 stsp free(path);
2743 a5edda0a 2019-07-27 stsp return err;
2746 a1fb16d8 2019-05-24 stsp static const struct got_error *
2747 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2748 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2750 fa51e947 2020-03-27 stsp const struct got_error *err;
2751 fa51e947 2020-03-27 stsp struct got_repository *repo;
2752 fa51e947 2020-03-27 stsp static char msg[512];
2754 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2756 fa51e947 2020-03-27 stsp return orig_err;
2758 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2759 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2760 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2761 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2762 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2763 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2764 fa51e947 2020-03-27 stsp got_repo_close(repo);
2765 fa51e947 2020-03-27 stsp return err;
2768 fa51e947 2020-03-27 stsp static const struct got_error *
2769 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2771 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2772 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2773 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2774 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2775 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2776 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2777 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2778 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2779 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2780 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2782 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2784 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2786 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2787 507dc3bb 2018-12-29 stsp switch (ch) {
2789 024e9686 2019-05-14 stsp branch_name = optarg;
2792 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2793 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2794 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2797 2deda0b9 2019-03-07 stsp usage_update();
2798 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2802 507dc3bb 2018-12-29 stsp argc -= optind;
2803 507dc3bb 2018-12-29 stsp argv += optind;
2805 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2806 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2807 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2808 507dc3bb 2018-12-29 stsp err(1, "pledge");
2810 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2811 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2812 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2813 c4cdcb68 2019-04-03 stsp goto done;
2815 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2816 fa51e947 2020-03-27 stsp if (error) {
2817 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2818 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2819 fa51e947 2020-03-27 stsp worktree_path);
2820 7d5807f4 2019-07-11 stsp goto done;
2823 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2824 f2ea84fa 2019-07-27 stsp if (error)
2825 f2ea84fa 2019-07-27 stsp goto done;
2827 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2829 507dc3bb 2018-12-29 stsp if (error != NULL)
2830 507dc3bb 2018-12-29 stsp goto done;
2832 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2833 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2834 087fb88c 2019-08-04 stsp if (error)
2835 087fb88c 2019-08-04 stsp goto done;
2837 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2838 0266afb7 2019-01-04 stsp if (error)
2839 0266afb7 2019-01-04 stsp goto done;
2841 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2842 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2843 024e9686 2019-05-14 stsp if (error != NULL)
2844 024e9686 2019-05-14 stsp goto done;
2845 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2846 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2847 9c4b8182 2019-01-02 stsp if (error != NULL)
2848 9c4b8182 2019-01-02 stsp goto done;
2849 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2850 507dc3bb 2018-12-29 stsp if (error != NULL)
2851 507dc3bb 2018-12-29 stsp goto done;
2853 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2854 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2855 04f57cb3 2019-07-25 stsp free(commit_id_str);
2856 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2857 30837e32 2019-07-25 stsp if (error)
2858 a297e751 2019-07-11 stsp goto done;
2859 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2860 a297e751 2019-07-11 stsp if (error)
2861 507dc3bb 2018-12-29 stsp goto done;
2864 a1fb16d8 2019-05-24 stsp if (branch_name) {
2865 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2866 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2867 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2869 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2870 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2871 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2872 024e9686 2019-05-14 stsp goto done;
2874 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2875 024e9686 2019-05-14 stsp if (error)
2876 024e9686 2019-05-14 stsp goto done;
2877 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2879 a367ff0f 2019-05-14 stsp free(head_commit_id);
2880 024e9686 2019-05-14 stsp if (error != NULL)
2881 024e9686 2019-05-14 stsp goto done;
2882 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2883 a367ff0f 2019-05-14 stsp if (error)
2884 a367ff0f 2019-05-14 stsp goto done;
2885 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2886 024e9686 2019-05-14 stsp if (error)
2887 024e9686 2019-05-14 stsp goto done;
2889 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2890 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2891 a367ff0f 2019-05-14 stsp if (error != NULL) {
2892 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2893 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2894 024e9686 2019-05-14 stsp goto done;
2896 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2897 a367ff0f 2019-05-14 stsp if (error)
2898 a367ff0f 2019-05-14 stsp goto done;
2901 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2902 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2903 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2904 507dc3bb 2018-12-29 stsp commit_id);
2905 507dc3bb 2018-12-29 stsp if (error)
2906 507dc3bb 2018-12-29 stsp goto done;
2909 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
2910 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2911 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
2912 507dc3bb 2018-12-29 stsp if (error != NULL)
2913 507dc3bb 2018-12-29 stsp goto done;
2915 9627c110 2020-04-18 stsp if (upa.did_something)
2916 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2918 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2919 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
2921 c09a553d 2018-03-12 stsp free(worktree_path);
2922 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2923 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2924 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2925 507dc3bb 2018-12-29 stsp free(commit_id);
2926 9c4b8182 2019-01-02 stsp free(commit_id_str);
2927 c09a553d 2018-03-12 stsp return error;
2930 f42b1b34 2018-03-12 stsp static const struct got_error *
2931 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2932 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2933 63035f9f 2019-10-06 stsp struct got_repository *repo)
2935 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2936 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2938 44392932 2019-08-25 stsp if (blob_id1) {
2939 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2941 44392932 2019-08-25 stsp goto done;
2944 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2946 44392932 2019-08-25 stsp goto done;
2948 44392932 2019-08-25 stsp while (path[0] == '/')
2950 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2951 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2953 44392932 2019-08-25 stsp if (blob1)
2954 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2955 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2956 44392932 2019-08-25 stsp return err;
2959 44392932 2019-08-25 stsp static const struct got_error *
2960 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2961 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2962 63035f9f 2019-10-06 stsp struct got_repository *repo)
2964 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2965 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2966 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2968 44392932 2019-08-25 stsp if (tree_id1) {
2969 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2971 44392932 2019-08-25 stsp goto done;
2974 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2976 0f2b3dca 2018-12-22 stsp goto done;
2978 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2979 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2980 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2981 44392932 2019-08-25 stsp while (path[0] == '/')
2983 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2984 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2986 79109fed 2018-03-27 stsp if (tree1)
2987 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2988 366e0a5f 2019-10-10 stsp if (tree2)
2989 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2990 44392932 2019-08-25 stsp return err;
2993 44392932 2019-08-25 stsp static const struct got_error *
2994 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2995 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
2997 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
2998 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2999 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3000 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3002 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3003 0208f208 2020-05-05 stsp if (qid != NULL) {
3004 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3005 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3008 0208f208 2020-05-05 stsp return err;
3010 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3011 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3015 0208f208 2020-05-05 stsp if (tree_id1) {
3016 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3018 0208f208 2020-05-05 stsp goto done;
3021 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3022 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3024 0208f208 2020-05-05 stsp goto done;
3026 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3027 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3029 0208f208 2020-05-05 stsp if (tree1)
3030 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3031 0208f208 2020-05-05 stsp if (tree2)
3032 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3033 0208f208 2020-05-05 stsp return err;
3036 0208f208 2020-05-05 stsp static const struct got_error *
3037 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3038 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3040 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3041 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3042 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3043 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3044 44392932 2019-08-25 stsp struct got_object_qid *qid;
3046 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3047 44392932 2019-08-25 stsp if (qid != NULL) {
3048 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3051 44392932 2019-08-25 stsp return err;
3054 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3055 44392932 2019-08-25 stsp int obj_type;
3056 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3058 44392932 2019-08-25 stsp goto done;
3059 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3060 44392932 2019-08-25 stsp if (err) {
3061 44392932 2019-08-25 stsp free(obj_id2);
3062 44392932 2019-08-25 stsp goto done;
3064 44392932 2019-08-25 stsp if (pcommit) {
3065 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3066 44392932 2019-08-25 stsp qid->id, path);
3067 44392932 2019-08-25 stsp if (err) {
3068 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3069 2e8c69d1 2020-05-04 stsp free(obj_id2);
3070 2e8c69d1 2020-05-04 stsp goto done;
3073 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3074 2e8c69d1 2020-05-04 stsp if (err) {
3075 2e8c69d1 2020-05-04 stsp free(obj_id2);
3076 2e8c69d1 2020-05-04 stsp goto done;
3080 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3081 44392932 2019-08-25 stsp if (err) {
3082 44392932 2019-08-25 stsp free(obj_id2);
3083 44392932 2019-08-25 stsp goto done;
3085 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3086 44392932 2019-08-25 stsp switch (obj_type) {
3087 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3088 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3091 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3092 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3096 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3099 44392932 2019-08-25 stsp free(obj_id1);
3100 44392932 2019-08-25 stsp free(obj_id2);
3102 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3103 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3105 44392932 2019-08-25 stsp goto done;
3106 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3107 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3109 44392932 2019-08-25 stsp goto done;
3110 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3111 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3114 0f2b3dca 2018-12-22 stsp free(id_str1);
3115 0f2b3dca 2018-12-22 stsp free(id_str2);
3116 44392932 2019-08-25 stsp if (pcommit)
3117 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3118 79109fed 2018-03-27 stsp return err;
3121 4bb494d5 2018-06-16 stsp static char *
3122 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3124 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3125 09867e48 2019-08-13 stsp char *p, *s;
3127 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3128 09867e48 2019-08-13 stsp if (tm == NULL)
3129 09867e48 2019-08-13 stsp return NULL;
3130 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3131 09867e48 2019-08-13 stsp if (s == NULL)
3132 09867e48 2019-08-13 stsp return NULL;
3133 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3135 6c281f94 2018-06-11 stsp *p = '\0';
3139 6841bf13 2019-11-29 kn static const struct got_error *
3140 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3141 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3143 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3144 6841bf13 2019-11-29 kn regmatch_t regmatch;
3145 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3147 6841bf13 2019-11-29 kn *have_match = 0;
3149 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3153 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3157 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3158 6841bf13 2019-11-29 kn *have_match = 1;
3160 6841bf13 2019-11-29 kn free(id_str);
3161 6841bf13 2019-11-29 kn free(logmsg);
3165 0208f208 2020-05-05 stsp static void
3166 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3167 0208f208 2020-05-05 stsp regex_t *regex)
3169 0208f208 2020-05-05 stsp regmatch_t regmatch;
3170 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3172 0208f208 2020-05-05 stsp *have_match = 0;
3174 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3175 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3176 0208f208 2020-05-05 stsp *have_match = 1;
3182 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3184 79109fed 2018-03-27 stsp static const struct got_error *
3185 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3186 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3187 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3188 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3190 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3191 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3192 6c281f94 2018-06-11 stsp char datebuf[26];
3193 45d799e2 2018-12-23 stsp time_t committer_time;
3194 45d799e2 2018-12-23 stsp const char *author, *committer;
3195 199a4027 2019-02-02 stsp char *refs_str = NULL;
3196 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3198 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3200 199a4027 2019-02-02 stsp const char *name;
3201 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3204 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3205 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3207 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3208 199a4027 2019-02-02 stsp name += 5;
3209 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3211 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3212 e34f9ed6 2019-02-02 stsp name += 6;
3213 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3214 141c2bff 2019-02-04 stsp name += 8;
3215 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3216 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3219 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3220 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3221 5d844a1e 2019-08-13 stsp if (err) {
3222 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3223 5d844a1e 2019-08-13 stsp return err;
3224 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3225 5d844a1e 2019-08-13 stsp err = NULL;
3226 5d844a1e 2019-08-13 stsp tag = NULL;
3229 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3230 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3232 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3233 a436ad14 2019-08-13 stsp if (cmp != 0)
3235 199a4027 2019-02-02 stsp s = refs_str;
3236 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3237 199a4027 2019-02-02 stsp name) == -1) {
3238 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3240 34ca4898 2019-08-13 stsp return err;
3244 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3246 8bf5b3c9 2018-03-17 stsp return err;
3248 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3249 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3250 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3251 832c249c 2018-06-10 stsp free(id_str);
3252 d3d493d7 2019-02-21 stsp id_str = NULL;
3253 d3d493d7 2019-02-21 stsp free(refs_str);
3254 d3d493d7 2019-02-21 stsp refs_str = NULL;
3255 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3256 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3257 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3258 09867e48 2019-08-13 stsp if (datestr)
3259 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3260 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3261 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3262 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3263 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3264 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3265 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3266 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3267 3fe1abad 2018-06-10 stsp int n = 1;
3268 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3269 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3270 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3272 a0603db2 2018-06-10 stsp return err;
3273 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3274 a0603db2 2018-06-10 stsp free(id_str);
3278 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3280 5943eee2 2019-08-13 stsp return err;
3282 621015ac 2018-07-23 stsp logmsg = logmsg0;
3284 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3286 832c249c 2018-06-10 stsp printf(" %s\n", line);
3287 832c249c 2018-06-10 stsp } while (line);
3288 621015ac 2018-07-23 stsp free(logmsg0);
3290 0208f208 2020-05-05 stsp if (changed_paths) {
3291 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3292 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3293 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3294 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3296 0208f208 2020-05-05 stsp printf("\n");
3298 971751ac 2018-03-27 stsp if (show_patch) {
3299 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3300 971751ac 2018-03-27 stsp if (err == 0)
3301 971751ac 2018-03-27 stsp printf("\n");
3304 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3305 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3306 07862c20 2018-09-15 stsp return err;
3309 f42b1b34 2018-03-12 stsp static const struct got_error *
3310 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3311 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3312 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3313 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3315 f42b1b34 2018-03-12 stsp const struct got_error *err;
3316 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3317 6841bf13 2019-11-29 kn regex_t regex;
3318 6841bf13 2019-11-29 kn int have_match;
3319 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3320 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3321 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3322 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3323 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3325 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3326 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3328 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3329 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3330 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3332 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3334 f42b1b34 2018-03-12 stsp return err;
3335 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3336 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3338 fcc85cad 2018-07-22 stsp goto done;
3340 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3342 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3345 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3346 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3347 372ccdbb 2018-06-10 stsp if (err) {
3348 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3349 9ba79e04 2018-06-11 stsp err = NULL;
3352 b43fbaa0 2018-06-11 stsp if (id == NULL)
3355 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3359 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3360 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3365 6841bf13 2019-11-29 kn if (search_pattern) {
3366 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3368 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3371 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3372 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3373 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3374 6841bf13 2019-11-29 kn if (have_match == 0) {
3375 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3376 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3377 0208f208 2020-05-05 stsp free((char *)pe->path);
3378 0208f208 2020-05-05 stsp free(pe->data);
3380 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3385 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3386 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3389 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3390 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3392 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3393 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3394 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3395 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3399 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3400 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3403 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3404 0208f208 2020-05-05 stsp free((char *)pe->path);
3405 0208f208 2020-05-05 stsp free(pe->data);
3407 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3409 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3410 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3411 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3414 502b9684 2020-07-31 stsp if (show_changed_paths) {
3415 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3416 502b9684 2020-07-31 stsp commit, repo);
3420 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3421 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3422 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3423 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3426 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3427 502b9684 2020-07-31 stsp free((char *)pe->path);
3428 502b9684 2020-07-31 stsp free(pe->data);
3430 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3434 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3435 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3436 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3437 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3439 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3440 0208f208 2020-05-05 stsp free((char *)pe->path);
3441 0208f208 2020-05-05 stsp free(pe->data);
3443 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3444 6841bf13 2019-11-29 kn if (search_pattern)
3445 6841bf13 2019-11-29 kn regfree(®ex);
3446 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3447 f42b1b34 2018-03-12 stsp return err;
3450 4ed7e80c 2018-05-20 stsp __dead static void
3451 6f3d1eb0 2018-03-12 stsp usage_log(void)
3453 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3454 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3455 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3459 b1ebc001 2019-08-13 stsp static int
3460 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3462 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3463 b1ebc001 2019-08-13 stsp long long n;
3464 b1ebc001 2019-08-13 stsp const char *errstr;
3466 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3467 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3469 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3470 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3475 d1fe46f9 2020-04-18 stsp static const struct got_error *
3476 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3477 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3479 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3480 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3482 d1fe46f9 2020-04-18 stsp *id = NULL;
3484 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3485 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3486 d1fe46f9 2020-04-18 stsp int obj_type;
3487 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3488 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3490 d1fe46f9 2020-04-18 stsp return err;
3491 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3493 d1fe46f9 2020-04-18 stsp return err;
3494 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3495 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3496 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3498 d1fe46f9 2020-04-18 stsp return err;
3499 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3500 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3501 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3502 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3504 d1fe46f9 2020-04-18 stsp free(*id);
3505 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3506 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3507 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3508 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3509 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3510 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3512 d1fe46f9 2020-04-18 stsp return err;
3513 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3514 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3516 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3517 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3520 d1fe46f9 2020-04-18 stsp return err;
3523 4ed7e80c 2018-05-20 stsp static const struct got_error *
3524 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3526 f42b1b34 2018-03-12 stsp const struct got_error *error;
3527 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3528 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3529 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3530 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3531 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3532 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3533 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3534 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3535 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3536 64a96a6d 2018-04-01 stsp const char *errstr;
3537 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3539 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3541 6715a751 2018-03-16 stsp #ifndef PROFILE
3542 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3545 f42b1b34 2018-03-12 stsp err(1, "pledge");
3548 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3550 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3551 79109fed 2018-03-27 stsp switch (ch) {
3553 79109fed 2018-03-27 stsp show_patch = 1;
3556 0208f208 2020-05-05 stsp show_changed_paths = 1;
3559 d142fc45 2018-04-01 stsp start_commit = optarg;
3562 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3564 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3565 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3568 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3569 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3570 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3573 48c8c60d 2020-01-27 stsp log_branches = 1;
3576 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3577 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3578 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3580 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3583 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3586 6841bf13 2019-11-29 kn search_pattern = optarg;
3589 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3592 2deda0b9 2019-03-07 stsp usage_log();
3593 79109fed 2018-03-27 stsp /* NOTREACHED */
3597 79109fed 2018-03-27 stsp argc -= optind;
3598 79109fed 2018-03-27 stsp argv += optind;
3600 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3601 dc1edbfa 2019-11-29 kn diff_context = 3;
3602 dc1edbfa 2019-11-29 kn else if (!show_patch)
3603 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3605 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3606 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3607 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3608 04ca23f4 2018-07-16 stsp goto done;
3611 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3612 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3613 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3614 50f2fada 2020-04-24 stsp goto done;
3615 50f2fada 2020-04-24 stsp error = NULL;
3618 e7301579 2019-03-18 stsp if (argc == 0) {
3619 cbd1af7a 2019-03-18 stsp path = strdup("");
3620 e7301579 2019-03-18 stsp if (path == NULL) {
3621 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3622 cbd1af7a 2019-03-18 stsp goto done;
3624 e7301579 2019-03-18 stsp } else if (argc == 1) {
3625 e7301579 2019-03-18 stsp if (worktree) {
3626 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3628 e7301579 2019-03-18 stsp if (error)
3629 e7301579 2019-03-18 stsp goto done;
3631 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3632 e7301579 2019-03-18 stsp if (path == NULL) {
3633 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3634 e7301579 2019-03-18 stsp goto done;
3638 cbd1af7a 2019-03-18 stsp usage_log();
3640 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3641 5486daa2 2019-05-11 stsp repo_path = worktree ?
3642 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3644 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3645 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3646 cffc0aa4 2019-02-05 stsp goto done;
3649 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3650 d7d4f210 2018-03-12 stsp if (error != NULL)
3651 04ca23f4 2018-07-16 stsp goto done;
3653 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3654 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3655 c02c541e 2019-03-29 stsp if (error)
3656 c02c541e 2019-03-29 stsp goto done;
3658 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3659 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3660 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3661 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3662 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3663 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3664 3235492e 2018-04-01 stsp if (error != NULL)
3665 d1fe46f9 2020-04-18 stsp goto done;
3666 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3667 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3668 3235492e 2018-04-01 stsp if (error != NULL)
3669 d1fe46f9 2020-04-18 stsp goto done;
3670 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3671 d1fe46f9 2020-04-18 stsp start_id);
3672 d1fe46f9 2020-04-18 stsp if (error != NULL)
3673 d1fe46f9 2020-04-18 stsp goto done;
3674 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3676 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3677 d1fe46f9 2020-04-18 stsp if (error != NULL)
3678 d1fe46f9 2020-04-18 stsp goto done;
3680 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3681 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3682 d1fe46f9 2020-04-18 stsp if (error != NULL)
3683 d1fe46f9 2020-04-18 stsp goto done;
3686 deeabeae 2019-08-27 stsp if (worktree) {
3687 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3689 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3690 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3691 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3692 deeabeae 2019-08-27 stsp goto done;
3694 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3697 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3698 04ca23f4 2018-07-16 stsp if (error != NULL)
3699 04ca23f4 2018-07-16 stsp goto done;
3700 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3701 04ca23f4 2018-07-16 stsp free(path);
3702 04ca23f4 2018-07-16 stsp path = in_repo_path;
3705 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3706 199a4027 2019-02-02 stsp if (error)
3707 199a4027 2019-02-02 stsp goto done;
3709 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3710 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3711 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3713 04ca23f4 2018-07-16 stsp free(path);
3714 04ca23f4 2018-07-16 stsp free(repo_path);
3715 04ca23f4 2018-07-16 stsp free(cwd);
3716 cffc0aa4 2019-02-05 stsp if (worktree)
3717 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3718 ad242220 2018-09-08 stsp if (repo) {
3719 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3720 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3721 ad242220 2018-09-08 stsp if (error == NULL)
3722 ad242220 2018-09-08 stsp error = repo_error;
3724 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3725 8bf5b3c9 2018-03-17 stsp return error;
3728 4ed7e80c 2018-05-20 stsp __dead static void
3729 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3731 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3732 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3736 b72f483a 2019-02-05 stsp struct print_diff_arg {
3737 b72f483a 2019-02-05 stsp struct got_repository *repo;
3738 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3739 b72f483a 2019-02-05 stsp int diff_context;
3740 3fc0c068 2019-02-10 stsp const char *id_str;
3741 3fc0c068 2019-02-10 stsp int header_shown;
3742 98eaaa12 2019-08-03 stsp int diff_staged;
3743 63035f9f 2019-10-06 stsp int ignore_whitespace;
3747 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3748 39449a05 2020-07-23 stsp * it as content to the diff engine.
3750 39449a05 2020-07-23 stsp static const struct got_error *
3751 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3752 39449a05 2020-07-23 stsp const char *abspath)
3754 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3755 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3756 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3760 39449a05 2020-07-23 stsp if (dirfd != -1) {
3761 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3762 39449a05 2020-07-23 stsp if (target_len == -1)
3763 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3765 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3766 39449a05 2020-07-23 stsp if (target_len == -1)
3767 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3770 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3771 39449a05 2020-07-23 stsp if (*fd == -1)
3772 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3774 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3775 39449a05 2020-07-23 stsp if (outlen == -1) {
3776 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3777 39449a05 2020-07-23 stsp goto done;
3780 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3781 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3782 39449a05 2020-07-23 stsp goto done;
3785 39449a05 2020-07-23 stsp if (err) {
3786 39449a05 2020-07-23 stsp close(*fd);
3789 39449a05 2020-07-23 stsp return err;
3792 4ed7e80c 2018-05-20 stsp static const struct got_error *
3793 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3794 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3795 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3796 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3798 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3799 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3800 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3801 12463d8b 2019-12-13 stsp int fd = -1;
3802 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3803 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3804 b72f483a 2019-02-05 stsp struct stat sb;
3806 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3807 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3808 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3809 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3810 98eaaa12 2019-08-03 stsp return NULL;
3812 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3813 98eaaa12 2019-08-03 stsp return NULL;
3814 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3815 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3816 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3817 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3818 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3819 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3820 98eaaa12 2019-08-03 stsp return NULL;
3823 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3824 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3825 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3826 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3827 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3830 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3831 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3832 98eaaa12 2019-08-03 stsp switch (staged_status) {
3833 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3834 98eaaa12 2019-08-03 stsp label1 = path;
3835 98eaaa12 2019-08-03 stsp label2 = path;
3837 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3838 98eaaa12 2019-08-03 stsp label2 = path;
3840 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3841 98eaaa12 2019-08-03 stsp label1 = path;
3844 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3846 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3847 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3848 63035f9f 2019-10-06 stsp a->repo, stdout);
3851 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3852 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3853 4ce46740 2019-08-08 stsp char *id_str;
3854 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3857 4ce46740 2019-08-08 stsp goto done;
3858 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3860 4ce46740 2019-08-08 stsp goto done;
3861 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3862 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3863 4ce46740 2019-08-08 stsp free(id_str);
3864 4ce46740 2019-08-08 stsp goto done;
3866 4ce46740 2019-08-08 stsp free(id_str);
3867 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3868 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3870 4ce46740 2019-08-08 stsp goto done;
3873 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3874 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3875 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3877 2ec1f75b 2019-03-26 stsp goto done;
3880 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3881 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3882 12463d8b 2019-12-13 stsp if (fd == -1) {
3883 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3884 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
3886 39449a05 2020-07-23 stsp goto done;
3888 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3889 39449a05 2020-07-23 stsp de_name, abspath);
3891 39449a05 2020-07-23 stsp goto done;
3894 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3895 12463d8b 2019-12-13 stsp if (fd == -1) {
3896 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3897 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
3899 39449a05 2020-07-23 stsp goto done;
3901 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3902 39449a05 2020-07-23 stsp de_name, abspath);
3904 39449a05 2020-07-23 stsp goto done;
3907 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3908 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3909 2ec1f75b 2019-03-26 stsp goto done;
3911 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3912 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3913 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3914 2ec1f75b 2019-03-26 stsp goto done;
3918 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3920 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3921 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3923 b72f483a 2019-02-05 stsp if (blob1)
3924 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3925 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3926 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3927 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3928 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3929 b72f483a 2019-02-05 stsp free(abspath);
3930 927df6b7 2019-02-10 stsp return err;
3933 927df6b7 2019-02-10 stsp static const struct got_error *
3934 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3936 b00d56cd 2018-04-01 stsp const struct got_error *error;
3937 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3938 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3939 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3940 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3941 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3942 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3943 15a94983 2018-12-23 stsp int type1, type2;
3944 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3945 c0cc5c62 2018-10-18 stsp const char *errstr;
3946 927df6b7 2019-02-10 stsp char *path = NULL;
3948 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3949 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3950 25eccc22 2019-01-04 stsp NULL) == -1)
3951 b00d56cd 2018-04-01 stsp err(1, "pledge");
3954 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3955 b00d56cd 2018-04-01 stsp switch (ch) {
3957 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3959 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3960 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3963 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3964 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3965 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3967 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3970 98eaaa12 2019-08-03 stsp diff_staged = 1;
3973 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3976 2deda0b9 2019-03-07 stsp usage_diff();
3977 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3981 b00d56cd 2018-04-01 stsp argc -= optind;
3982 b00d56cd 2018-04-01 stsp argv += optind;
3984 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3985 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3986 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3987 b72f483a 2019-02-05 stsp goto done;
3989 927df6b7 2019-02-10 stsp if (argc <= 1) {
3990 b72f483a 2019-02-05 stsp if (repo_path)
3992 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3993 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3994 fa51e947 2020-03-27 stsp if (error) {
3995 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3996 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
3998 1f03b8da 2020-03-20 stsp goto done;
4000 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4001 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4002 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4003 927df6b7 2019-02-10 stsp goto done;
4005 927df6b7 2019-02-10 stsp if (argc == 1) {
4006 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4008 927df6b7 2019-02-10 stsp if (error)
4009 927df6b7 2019-02-10 stsp goto done;
4011 927df6b7 2019-02-10 stsp path = strdup("");
4012 927df6b7 2019-02-10 stsp if (path == NULL) {
4013 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4014 927df6b7 2019-02-10 stsp goto done;
4017 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4018 98eaaa12 2019-08-03 stsp if (diff_staged)
4019 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4020 98eaaa12 2019-08-03 stsp "objects in repository");
4021 15a94983 2018-12-23 stsp id_str1 = argv[0];
4022 15a94983 2018-12-23 stsp id_str2 = argv[1];
4023 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4024 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4025 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4026 30db809c 2019-06-05 stsp goto done;
4027 1f03b8da 2020-03-20 stsp if (worktree) {
4028 1f03b8da 2020-03-20 stsp repo_path = strdup(
4029 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4030 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4031 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4032 1f03b8da 2020-03-20 stsp goto done;
4035 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4036 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4037 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4038 1f03b8da 2020-03-20 stsp goto done;
4043 b00d56cd 2018-04-01 stsp usage_diff();
4045 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4046 76089277 2018-04-01 stsp free(repo_path);
4047 b00d56cd 2018-04-01 stsp if (error != NULL)
4048 b00d56cd 2018-04-01 stsp goto done;
4050 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4051 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4052 c02c541e 2019-03-29 stsp if (error)
4053 c02c541e 2019-03-29 stsp goto done;
4055 30db809c 2019-06-05 stsp if (argc <= 1) {
4056 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4057 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4058 b72f483a 2019-02-05 stsp char *id_str;
4060 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4062 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4063 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4064 b72f483a 2019-02-05 stsp if (error)
4065 b72f483a 2019-02-05 stsp goto done;
4066 b72f483a 2019-02-05 stsp arg.repo = repo;
4067 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4068 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4069 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4070 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4071 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4072 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4074 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4075 72ea6654 2019-07-27 stsp if (error)
4076 72ea6654 2019-07-27 stsp goto done;
4078 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4079 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4080 b72f483a 2019-02-05 stsp free(id_str);
4081 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4082 b72f483a 2019-02-05 stsp goto done;
4085 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4086 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4087 0adb7196 2019-08-11 stsp if (error)
4088 0adb7196 2019-08-11 stsp goto done;
4090 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4091 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4092 0adb7196 2019-08-11 stsp if (error)
4093 0adb7196 2019-08-11 stsp goto done;
4095 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4096 15a94983 2018-12-23 stsp if (error)
4097 15a94983 2018-12-23 stsp goto done;
4099 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4100 15a94983 2018-12-23 stsp if (error)
4101 15a94983 2018-12-23 stsp goto done;
4103 15a94983 2018-12-23 stsp if (type1 != type2) {
4104 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4105 b00d56cd 2018-04-01 stsp goto done;
4108 15a94983 2018-12-23 stsp switch (type1) {
4109 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4110 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4111 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4113 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4114 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4115 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4117 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4118 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4119 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4120 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4123 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4126 5e70831e 2019-05-28 stsp free(label1);
4127 5e70831e 2019-05-28 stsp free(label2);
4128 15a94983 2018-12-23 stsp free(id1);
4129 15a94983 2018-12-23 stsp free(id2);
4130 927df6b7 2019-02-10 stsp free(path);
4131 b72f483a 2019-02-05 stsp if (worktree)
4132 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4133 ad242220 2018-09-08 stsp if (repo) {
4134 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4135 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4136 ad242220 2018-09-08 stsp if (error == NULL)
4137 ad242220 2018-09-08 stsp error = repo_error;
4139 b00d56cd 2018-04-01 stsp return error;
4142 404c43c4 2018-06-21 stsp __dead static void
4143 404c43c4 2018-06-21 stsp usage_blame(void)
4145 9270e621 2019-02-05 stsp fprintf(stderr,
4146 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4147 404c43c4 2018-06-21 stsp getprogname());
4151 28315671 2019-08-14 stsp struct blame_line {
4152 28315671 2019-08-14 stsp int annotated;
4153 28315671 2019-08-14 stsp char *id_str;
4154 82f6abb8 2019-08-14 stsp char *committer;
4155 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4158 28315671 2019-08-14 stsp struct blame_cb_args {
4159 28315671 2019-08-14 stsp struct blame_line *lines;
4160 28315671 2019-08-14 stsp int nlines;
4161 7ef28ff8 2019-08-14 stsp int nlines_prec;
4162 28315671 2019-08-14 stsp int lineno_cur;
4163 28315671 2019-08-14 stsp off_t *line_offsets;
4165 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4168 404c43c4 2018-06-21 stsp static const struct got_error *
4169 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4171 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4172 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4173 28315671 2019-08-14 stsp struct blame_line *bline;
4174 82f6abb8 2019-08-14 stsp char *line = NULL;
4175 28315671 2019-08-14 stsp size_t linesize = 0;
4176 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4177 28315671 2019-08-14 stsp off_t offset;
4178 bcb49d15 2019-08-14 stsp struct tm tm;
4179 bcb49d15 2019-08-14 stsp time_t committer_time;
4181 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4182 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4183 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4185 28315671 2019-08-14 stsp if (sigint_received)
4186 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4188 2839f8b9 2019-08-18 stsp if (lineno == -1)
4189 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4191 28315671 2019-08-14 stsp /* Annotate this line. */
4192 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4193 28315671 2019-08-14 stsp if (bline->annotated)
4194 28315671 2019-08-14 stsp return NULL;
4195 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4197 28315671 2019-08-14 stsp return err;
4199 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4201 82f6abb8 2019-08-14 stsp goto done;
4203 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4204 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4205 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4206 bcb49d15 2019-08-14 stsp goto done;
4209 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4210 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4211 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4212 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4213 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4214 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4215 82f6abb8 2019-08-14 stsp goto done;
4217 28315671 2019-08-14 stsp bline->annotated = 1;
4219 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4220 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4221 28315671 2019-08-14 stsp if (!bline->annotated)
4222 82f6abb8 2019-08-14 stsp goto done;
4224 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4225 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4226 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4227 82f6abb8 2019-08-14 stsp goto done;
4230 28315671 2019-08-14 stsp while (bline->annotated) {
4231 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4232 82f6abb8 2019-08-14 stsp size_t len;
4234 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4235 28315671 2019-08-14 stsp if (ferror(a->f))
4236 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4240 82f6abb8 2019-08-14 stsp committer = bline->committer;
4241 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4242 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4243 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4244 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4246 82f6abb8 2019-08-14 stsp *at = '\0';
4247 82f6abb8 2019-08-14 stsp len = strlen(committer);
4248 82f6abb8 2019-08-14 stsp if (len >= 9)
4249 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4251 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4253 28315671 2019-08-14 stsp *nl = '\0';
4254 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4255 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4257 28315671 2019-08-14 stsp a->lineno_cur++;
4258 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4261 82f6abb8 2019-08-14 stsp if (commit)
4262 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4263 28315671 2019-08-14 stsp free(line);
4264 28315671 2019-08-14 stsp return err;
4267 28315671 2019-08-14 stsp static const struct got_error *
4268 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4270 404c43c4 2018-06-21 stsp const struct got_error *error;
4271 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4272 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4273 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4274 0587e10c 2020-07-23 stsp char *link_target = NULL;
4275 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4276 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4277 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4278 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4279 28315671 2019-08-14 stsp struct blame_cb_args bca;
4280 28315671 2019-08-14 stsp int ch, obj_type, i;
4281 b02560ec 2019-08-19 stsp size_t filesize;
4283 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4285 404c43c4 2018-06-21 stsp #ifndef PROFILE
4286 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4287 36e2fb66 2019-01-04 stsp NULL) == -1)
4288 404c43c4 2018-06-21 stsp err(1, "pledge");
4291 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4292 404c43c4 2018-06-21 stsp switch (ch) {
4294 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4297 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4298 66bea077 2018-08-02 stsp if (repo_path == NULL)
4299 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4301 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4304 2deda0b9 2019-03-07 stsp usage_blame();
4305 404c43c4 2018-06-21 stsp /* NOTREACHED */
4309 404c43c4 2018-06-21 stsp argc -= optind;
4310 404c43c4 2018-06-21 stsp argv += optind;
4312 a39318fd 2018-08-02 stsp if (argc == 1)
4313 404c43c4 2018-06-21 stsp path = argv[0];
4315 404c43c4 2018-06-21 stsp usage_blame();
4317 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4318 66bea077 2018-08-02 stsp if (cwd == NULL) {
4319 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4320 66bea077 2018-08-02 stsp goto done;
4322 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4323 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4324 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4325 66bea077 2018-08-02 stsp goto done;
4327 0c06baac 2019-02-05 stsp error = NULL;
4328 0c06baac 2019-02-05 stsp if (worktree) {
4329 0c06baac 2019-02-05 stsp repo_path =
4330 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4331 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4332 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4333 1f03b8da 2020-03-20 stsp if (error)
4334 1f03b8da 2020-03-20 stsp goto done;
4337 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4338 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4339 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4340 0c06baac 2019-02-05 stsp goto done;
4345 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4346 c02c541e 2019-03-29 stsp if (error != NULL)
4347 36e2fb66 2019-01-04 stsp goto done;
4349 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4350 c02c541e 2019-03-29 stsp if (error)
4351 404c43c4 2018-06-21 stsp goto done;
4353 0c06baac 2019-02-05 stsp if (worktree) {
4354 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4355 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4356 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4357 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4358 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4359 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4360 6efaaa2d 2019-02-05 stsp path) == -1) {
4361 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4362 0c06baac 2019-02-05 stsp goto done;
4364 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4367 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4369 0c06baac 2019-02-05 stsp if (error)
4370 66bea077 2018-08-02 stsp goto done;
4372 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4373 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4374 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4375 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4376 404c43c4 2018-06-21 stsp if (error != NULL)
4377 66bea077 2018-08-02 stsp goto done;
4378 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4379 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4380 404c43c4 2018-06-21 stsp if (error != NULL)
4381 66bea077 2018-08-02 stsp goto done;
4383 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4384 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4385 30837e32 2019-07-25 stsp if (error)
4386 66bea077 2018-08-02 stsp goto done;
4389 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4390 0587e10c 2020-07-23 stsp commit_id, repo);
4391 0587e10c 2020-07-23 stsp if (error)
4392 0587e10c 2020-07-23 stsp goto done;
4394 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4395 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4396 28315671 2019-08-14 stsp if (error)
4397 28315671 2019-08-14 stsp goto done;
4399 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4400 28315671 2019-08-14 stsp if (error)
4401 28315671 2019-08-14 stsp goto done;
4403 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4404 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4405 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4406 28315671 2019-08-14 stsp goto done;
4409 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4410 28315671 2019-08-14 stsp if (error)
4411 28315671 2019-08-14 stsp goto done;
4412 28315671 2019-08-14 stsp bca.f = got_opentemp();
4413 28315671 2019-08-14 stsp if (bca.f == NULL) {
4414 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4415 28315671 2019-08-14 stsp goto done;
4417 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4418 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4419 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4420 28315671 2019-08-14 stsp goto done;
4422 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4423 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4424 b02560ec 2019-08-19 stsp bca.nlines--;
4426 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4427 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4428 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4429 28315671 2019-08-14 stsp goto done;
4431 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4432 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4433 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4434 7ef28ff8 2019-08-14 stsp while (i > 0) {
4436 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4438 82f6abb8 2019-08-14 stsp bca.repo = repo;
4440 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4441 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4443 66bea077 2018-08-02 stsp free(in_repo_path);
4444 0587e10c 2020-07-23 stsp free(link_target);
4445 66bea077 2018-08-02 stsp free(repo_path);
4446 66bea077 2018-08-02 stsp free(cwd);
4447 404c43c4 2018-06-21 stsp free(commit_id);
4448 28315671 2019-08-14 stsp free(obj_id);
4450 28315671 2019-08-14 stsp got_object_blob_close(blob);
4451 0c06baac 2019-02-05 stsp if (worktree)
4452 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4453 ad242220 2018-09-08 stsp if (repo) {
4454 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4455 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4456 ad242220 2018-09-08 stsp if (error == NULL)
4457 ad242220 2018-09-08 stsp error = repo_error;
4459 3affba96 2019-09-22 stsp if (bca.lines) {
4460 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4461 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4462 3affba96 2019-09-22 stsp free(bline->id_str);
4463 3affba96 2019-09-22 stsp free(bline->committer);
4465 3affba96 2019-09-22 stsp free(bca.lines);
4467 28315671 2019-08-14 stsp free(bca.line_offsets);
4468 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4469 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4470 404c43c4 2018-06-21 stsp return error;
4473 5de5890b 2018-10-18 stsp __dead static void
4474 5de5890b 2018-10-18 stsp usage_tree(void)
4476 c1669e2e 2019-01-09 stsp fprintf(stderr,
4477 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4478 5de5890b 2018-10-18 stsp getprogname());
4482 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4483 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4484 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4486 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4487 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4488 848d6979 2019-08-12 stsp const char *modestr = "";
4489 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4490 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4492 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4493 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4496 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4497 63c5ca5d 2019-08-24 stsp modestr = "$";
4498 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4501 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4503 0d6c6ee3 2020-05-20 stsp return err;
4504 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4505 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4506 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4509 848d6979 2019-08-12 stsp modestr = "@";
4511 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4512 848d6979 2019-08-12 stsp modestr = "/";
4513 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4514 848d6979 2019-08-12 stsp modestr = "*";
4516 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4517 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4518 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4520 0d6c6ee3 2020-05-20 stsp free(link_target);
4521 0d6c6ee3 2020-05-20 stsp return NULL;
4524 5de5890b 2018-10-18 stsp static const struct got_error *
4525 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4526 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4527 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4529 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4530 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4531 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4532 56e0773d 2019-11-28 stsp int nentries, i;
4534 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4536 5de5890b 2018-10-18 stsp goto done;
4538 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4540 5de5890b 2018-10-18 stsp goto done;
4541 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4542 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4543 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4544 5de5890b 2018-10-18 stsp char *id = NULL;
4546 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4549 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4550 5de5890b 2018-10-18 stsp if (show_ids) {
4551 5de5890b 2018-10-18 stsp char *id_str;
4552 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4553 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4555 5de5890b 2018-10-18 stsp goto done;
4556 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4557 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4558 5de5890b 2018-10-18 stsp free(id_str);
4559 5de5890b 2018-10-18 stsp goto done;
4561 5de5890b 2018-10-18 stsp free(id_str);
4563 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4566 0d6c6ee3 2020-05-20 stsp goto done;
4568 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4569 c1669e2e 2019-01-09 stsp char *child_path;
4570 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4571 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4572 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4573 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4574 c1669e2e 2019-01-09 stsp goto done;
4576 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4577 c1669e2e 2019-01-09 stsp root_path, repo);
4578 c1669e2e 2019-01-09 stsp free(child_path);
4580 c1669e2e 2019-01-09 stsp goto done;
4585 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4586 5de5890b 2018-10-18 stsp free(tree_id);
4587 5de5890b 2018-10-18 stsp return err;
4590 5de5890b 2018-10-18 stsp static const struct got_error *
4591 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4593 5de5890b 2018-10-18 stsp const struct got_error *error;
4594 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4595 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4596 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4597 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4598 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4599 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4600 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4603 5de5890b 2018-10-18 stsp #ifndef PROFILE
4604 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4605 0f8d269b 2019-01-04 stsp NULL) == -1)
4606 5de5890b 2018-10-18 stsp err(1, "pledge");
4609 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4610 5de5890b 2018-10-18 stsp switch (ch) {
4612 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4615 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4616 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4617 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4619 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4622 5de5890b 2018-10-18 stsp show_ids = 1;
4625 c1669e2e 2019-01-09 stsp recurse = 1;
4628 2deda0b9 2019-03-07 stsp usage_tree();
4629 5de5890b 2018-10-18 stsp /* NOTREACHED */
4633 5de5890b 2018-10-18 stsp argc -= optind;
4634 5de5890b 2018-10-18 stsp argv += optind;
4636 5de5890b 2018-10-18 stsp if (argc == 1)
4637 5de5890b 2018-10-18 stsp path = argv[0];
4638 5de5890b 2018-10-18 stsp else if (argc > 1)
4639 5de5890b 2018-10-18 stsp usage_tree();
4641 7a2c19d6 2019-02-05 stsp path = NULL;
4643 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4644 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4645 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4646 9bf7a39b 2019-02-05 stsp goto done;
4648 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4649 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4650 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4651 7a2c19d6 2019-02-05 stsp goto done;
4653 7a2c19d6 2019-02-05 stsp error = NULL;
4654 7a2c19d6 2019-02-05 stsp if (worktree) {
4655 7a2c19d6 2019-02-05 stsp repo_path =
4656 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4657 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4658 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4659 7a2c19d6 2019-02-05 stsp if (error)
4660 7a2c19d6 2019-02-05 stsp goto done;
4662 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4663 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4664 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4665 7a2c19d6 2019-02-05 stsp goto done;
4670 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4671 5de5890b 2018-10-18 stsp if (error != NULL)
4672 c02c541e 2019-03-29 stsp goto done;
4674 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4675 c02c541e 2019-03-29 stsp if (error)
4676 5de5890b 2018-10-18 stsp goto done;
4678 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4679 9bf7a39b 2019-02-05 stsp if (worktree) {
4680 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4681 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4682 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4683 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4684 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4685 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4686 9bf7a39b 2019-02-05 stsp goto done;
4688 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4690 9bf7a39b 2019-02-05 stsp if (error)
4691 9bf7a39b 2019-02-05 stsp goto done;
4693 9bf7a39b 2019-02-05 stsp path = "/";
4695 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4696 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4697 9bf7a39b 2019-02-05 stsp if (error != NULL)
4698 9bf7a39b 2019-02-05 stsp goto done;
4701 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4702 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4703 4e0a20a4 2020-03-23 tracey if (worktree)
4704 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4706 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4707 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4708 5de5890b 2018-10-18 stsp if (error != NULL)
4709 5de5890b 2018-10-18 stsp goto done;
4710 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4711 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4712 5de5890b 2018-10-18 stsp if (error != NULL)
4713 5de5890b 2018-10-18 stsp goto done;
4715 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4716 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4717 30837e32 2019-07-25 stsp if (error)
4718 5de5890b 2018-10-18 stsp goto done;
4721 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4722 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4724 5de5890b 2018-10-18 stsp free(in_repo_path);
4725 5de5890b 2018-10-18 stsp free(repo_path);
4726 5de5890b 2018-10-18 stsp free(cwd);
4727 5de5890b 2018-10-18 stsp free(commit_id);
4728 7a2c19d6 2019-02-05 stsp if (worktree)
4729 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4730 5de5890b 2018-10-18 stsp if (repo) {
4731 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4732 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4733 5de5890b 2018-10-18 stsp if (error == NULL)
4734 5de5890b 2018-10-18 stsp error = repo_error;
4736 5de5890b 2018-10-18 stsp return error;
4739 6bad629b 2019-02-04 stsp __dead static void
4740 6bad629b 2019-02-04 stsp usage_status(void)
4742 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4743 081470ac 2020-08-13 stsp getprogname());
4747 b72f483a 2019-02-05 stsp static const struct got_error *
4748 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4749 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4750 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4751 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4753 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4754 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4755 081470ac 2020-08-13 stsp if (arg) {
4756 081470ac 2020-08-13 stsp char *status_codes = arg;
4757 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4759 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4760 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4761 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4764 081470ac 2020-08-13 stsp if (i == ncodes)
4765 081470ac 2020-08-13 stsp return NULL;
4767 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4768 b72f483a 2019-02-05 stsp return NULL;
4771 6bad629b 2019-02-04 stsp static const struct got_error *
4772 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4774 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4775 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4776 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4777 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4778 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4779 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4780 081470ac 2020-08-13 stsp int ch, i;
4782 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4784 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4785 6bad629b 2019-02-04 stsp switch (ch) {
4787 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4788 081470ac 2020-08-13 stsp switch (optarg[i]) {
4789 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4790 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4791 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4792 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4793 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4794 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4795 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4796 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4797 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4800 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4801 081470ac 2020-08-13 stsp optarg[i]);
4804 081470ac 2020-08-13 stsp status_codes = optarg;
4807 2deda0b9 2019-03-07 stsp usage_status();
4808 6bad629b 2019-02-04 stsp /* NOTREACHED */
4812 6bad629b 2019-02-04 stsp argc -= optind;
4813 6bad629b 2019-02-04 stsp argv += optind;
4815 6bad629b 2019-02-04 stsp #ifndef PROFILE
4816 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4817 6bad629b 2019-02-04 stsp NULL) == -1)
4818 6bad629b 2019-02-04 stsp err(1, "pledge");
4820 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4821 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4822 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4823 927df6b7 2019-02-10 stsp goto done;
4826 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4827 fa51e947 2020-03-27 stsp if (error) {
4828 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4829 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4830 a5edda0a 2019-07-27 stsp goto done;
4833 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4835 6bad629b 2019-02-04 stsp if (error != NULL)
4836 6bad629b 2019-02-04 stsp goto done;
4838 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4839 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4840 087fb88c 2019-08-04 stsp if (error)
4841 087fb88c 2019-08-04 stsp goto done;
4843 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4844 6bad629b 2019-02-04 stsp if (error)
4845 6bad629b 2019-02-04 stsp goto done;
4847 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4848 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4850 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4851 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4852 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4853 927df6b7 2019-02-10 stsp free(cwd);
4854 d0eebce4 2019-03-11 stsp return error;
4857 d0eebce4 2019-03-11 stsp __dead static void
4858 d0eebce4 2019-03-11 stsp usage_ref(void)
4860 d0eebce4 2019-03-11 stsp fprintf(stderr,
4861 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4862 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4863 d0eebce4 2019-03-11 stsp getprogname());
4867 d0eebce4 2019-03-11 stsp static const struct got_error *
4868 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4870 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4871 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4872 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4874 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4875 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4877 d0eebce4 2019-03-11 stsp return err;
4879 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4880 d0eebce4 2019-03-11 stsp char *refstr;
4881 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4882 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4883 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4884 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4885 d0eebce4 2019-03-11 stsp free(refstr);
4888 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4889 d0eebce4 2019-03-11 stsp return NULL;
4892 d0eebce4 2019-03-11 stsp static const struct got_error *
4893 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4895 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4896 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4898 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4900 d0eebce4 2019-03-11 stsp return err;
4902 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4903 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4904 d0eebce4 2019-03-11 stsp return err;
4907 d0eebce4 2019-03-11 stsp static const struct got_error *
4908 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4910 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4911 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4912 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4915 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4916 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4917 d1644381 2019-07-14 stsp * an unintended typo.
4919 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4920 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4922 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4924 d83d9d5c 2019-05-13 stsp if (err) {
4925 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4927 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4928 d83d9d5c 2019-05-13 stsp return err;
4929 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4931 d83d9d5c 2019-05-13 stsp return err;
4932 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4933 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4935 d83d9d5c 2019-05-13 stsp return err;
4938 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4940 d0eebce4 2019-03-11 stsp goto done;
4942 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4945 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4947 d1c1ae5f 2019-08-12 stsp return err;
4950 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4951 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4953 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4954 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4955 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4958 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4959 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4960 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4962 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4963 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4965 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4967 d1c1ae5f 2019-08-12 stsp return err;
4969 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4971 d1c1ae5f 2019-08-12 stsp goto done;
4973 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4975 d1c1ae5f 2019-08-12 stsp if (target_ref)
4976 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4978 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4979 d0eebce4 2019-03-11 stsp return err;
4982 d0eebce4 2019-03-11 stsp static const struct got_error *
4983 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4985 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4986 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4987 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4988 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4989 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4990 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4991 b2070a3f 2020-03-22 stsp char *refname = NULL;
4993 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4994 d0eebce4 2019-03-11 stsp switch (ch) {
4996 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4999 e31abbf2 2020-03-22 stsp do_delete = 1;
5002 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5003 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5004 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5006 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5009 d0eebce4 2019-03-11 stsp do_list = 1;
5012 e31abbf2 2020-03-22 stsp symref_target = optarg;
5015 d0eebce4 2019-03-11 stsp usage_ref();
5016 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5020 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5021 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5022 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5023 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5024 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5025 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5026 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5027 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5028 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5029 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5030 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5031 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5033 d0eebce4 2019-03-11 stsp argc -= optind;
5034 d0eebce4 2019-03-11 stsp argv += optind;
5036 e31abbf2 2020-03-22 stsp if (do_list) {
5037 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5038 d0eebce4 2019-03-11 stsp usage_ref();
5039 b2070a3f 2020-03-22 stsp if (argc == 1) {
5040 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5041 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5042 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5043 b2070a3f 2020-03-22 stsp goto done;
5047 e31abbf2 2020-03-22 stsp if (argc != 1)
5048 e31abbf2 2020-03-22 stsp usage_ref();
5049 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5050 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5051 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5052 b2070a3f 2020-03-22 stsp goto done;
5056 b2070a3f 2020-03-22 stsp if (refname)
5057 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5059 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5060 e0b57350 2019-03-12 stsp if (do_list) {
5061 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5062 e0b57350 2019-03-12 stsp NULL) == -1)
5063 e0b57350 2019-03-12 stsp err(1, "pledge");
5065 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5066 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5067 e0b57350 2019-03-12 stsp err(1, "pledge");
5070 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5071 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5072 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5073 d0eebce4 2019-03-11 stsp goto done;
5076 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5077 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5078 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5079 d0eebce4 2019-03-11 stsp goto done;
5081 d0eebce4 2019-03-11 stsp error = NULL;
5082 d0eebce4 2019-03-11 stsp if (worktree) {
5083 d0eebce4 2019-03-11 stsp repo_path =
5084 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5085 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5086 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5087 d0eebce4 2019-03-11 stsp if (error)
5088 d0eebce4 2019-03-11 stsp goto done;
5090 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5091 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5092 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5093 d0eebce4 2019-03-11 stsp goto done;
5098 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5099 d0eebce4 2019-03-11 stsp if (error != NULL)
5100 d0eebce4 2019-03-11 stsp goto done;
5102 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5103 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5104 c02c541e 2019-03-29 stsp if (error)
5105 c02c541e 2019-03-29 stsp goto done;
5107 d0eebce4 2019-03-11 stsp if (do_list)
5108 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5109 e31abbf2 2020-03-22 stsp else if (do_delete)
5110 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5111 e31abbf2 2020-03-22 stsp else if (symref_target)
5112 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5114 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5115 e31abbf2 2020-03-22 stsp usage_ref();
5116 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5119 b2070a3f 2020-03-22 stsp free(refname);
5121 4e759de4 2019-06-26 stsp got_repo_close(repo);
5122 4e759de4 2019-06-26 stsp if (worktree)
5123 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5124 4e759de4 2019-06-26 stsp free(cwd);
5125 4e759de4 2019-06-26 stsp free(repo_path);
5126 4e759de4 2019-06-26 stsp return error;
5129 4e759de4 2019-06-26 stsp __dead static void
5130 4e759de4 2019-06-26 stsp usage_branch(void)
5132 4e759de4 2019-06-26 stsp fprintf(stderr,
5133 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5134 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5138 4e759de4 2019-06-26 stsp static const struct got_error *
5139 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5140 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5142 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5143 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5144 4e99b47e 2019-10-04 stsp char *refstr;
5146 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5147 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5148 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5149 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5151 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5153 4e99b47e 2019-10-04 stsp return err;
5154 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5155 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5156 4e99b47e 2019-10-04 stsp marker = "* ";
5158 4e99b47e 2019-10-04 stsp marker = "~ ";
5162 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5163 4e99b47e 2019-10-04 stsp refname += 11;
5164 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5165 4e99b47e 2019-10-04 stsp refname += 18;
5167 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5168 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5169 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5171 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5172 4e99b47e 2019-10-04 stsp free(refstr);
5173 4e99b47e 2019-10-04 stsp return NULL;
5176 4e99b47e 2019-10-04 stsp static const struct got_error *
5177 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5179 ad89fa31 2019-10-04 stsp const char *refname;
5181 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5182 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5184 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5186 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5187 ad89fa31 2019-10-04 stsp refname += 11;
5188 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5189 ad89fa31 2019-10-04 stsp refname += 18;
5191 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5193 ad89fa31 2019-10-04 stsp return NULL;
5196 ad89fa31 2019-10-04 stsp static const struct got_error *
5197 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5199 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5200 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5201 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5202 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5203 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5205 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5207 4e99b47e 2019-10-04 stsp if (worktree) {
5208 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5209 4e99b47e 2019-10-04 stsp worktree);
5211 4e99b47e 2019-10-04 stsp return err;
5213 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5214 4e99b47e 2019-10-04 stsp worktree);
5216 4e99b47e 2019-10-04 stsp return err;
5218 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5219 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5220 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5222 4e99b47e 2019-10-04 stsp return err;
5223 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5224 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5228 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5229 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5231 4e759de4 2019-06-26 stsp return err;
5233 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5234 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5236 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5237 4e759de4 2019-06-26 stsp return NULL;
5240 4e759de4 2019-06-26 stsp static const struct got_error *
5241 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5242 45cd4e47 2019-08-25 stsp const char *branch_name)
5244 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5245 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5246 4e759de4 2019-06-26 stsp char *refname;
5248 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5249 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5251 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5253 4e759de4 2019-06-26 stsp goto done;
5255 45cd4e47 2019-08-25 stsp if (worktree &&
5256 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5257 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5258 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5259 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5260 45cd4e47 2019-08-25 stsp goto done;
5263 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5266 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5267 4e759de4 2019-06-26 stsp free(refname);
5268 4e759de4 2019-06-26 stsp return err;
5271 4e759de4 2019-06-26 stsp static const struct got_error *
5272 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5273 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5275 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5276 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5277 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5280 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5281 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5282 d3f84d51 2019-07-11 stsp * an unintended typo.
5284 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5285 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5287 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5288 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5289 4e759de4 2019-06-26 stsp goto done;
5292 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5293 4e759de4 2019-06-26 stsp if (err == NULL) {
5294 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5295 4e759de4 2019-06-26 stsp goto done;
5296 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5297 4e759de4 2019-06-26 stsp goto done;
5299 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5301 4e759de4 2019-06-26 stsp goto done;
5303 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5306 4e759de4 2019-06-26 stsp got_ref_close(ref);
5307 4e759de4 2019-06-26 stsp free(base_refname);
5308 4e759de4 2019-06-26 stsp free(refname);
5309 4e759de4 2019-06-26 stsp return err;
5312 4e759de4 2019-06-26 stsp static const struct got_error *
5313 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5315 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5316 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5317 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5318 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5319 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5320 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5321 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5322 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5323 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5324 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5325 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5327 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5329 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5330 4e759de4 2019-06-26 stsp switch (ch) {
5332 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5335 4e759de4 2019-06-26 stsp delref = optarg;
5338 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5339 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5340 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5342 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5345 4e759de4 2019-06-26 stsp do_list = 1;
5348 da76fce2 2020-02-24 stsp do_update = 0;
5351 4e759de4 2019-06-26 stsp usage_branch();
5352 4e759de4 2019-06-26 stsp /* NOTREACHED */
5356 4e759de4 2019-06-26 stsp if (do_list && delref)
5357 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5359 4e759de4 2019-06-26 stsp argc -= optind;
5360 4e759de4 2019-06-26 stsp argv += optind;
5362 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5363 ad89fa31 2019-10-04 stsp do_show = 1;
5365 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5366 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5368 4e759de4 2019-06-26 stsp if (do_list || delref) {
5369 4e759de4 2019-06-26 stsp if (argc > 0)
5370 4e759de4 2019-06-26 stsp usage_branch();
5371 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5372 4e759de4 2019-06-26 stsp usage_branch();
5374 4e759de4 2019-06-26 stsp #ifndef PROFILE
5375 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5376 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5377 4e759de4 2019-06-26 stsp NULL) == -1)
5378 4e759de4 2019-06-26 stsp err(1, "pledge");
5380 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5381 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5382 4e759de4 2019-06-26 stsp err(1, "pledge");
5385 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5386 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5387 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5388 4e759de4 2019-06-26 stsp goto done;
5391 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5392 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5393 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5394 4e759de4 2019-06-26 stsp goto done;
5396 4e759de4 2019-06-26 stsp error = NULL;
5397 4e759de4 2019-06-26 stsp if (worktree) {
5398 4e759de4 2019-06-26 stsp repo_path =
5399 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5400 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5401 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5402 4e759de4 2019-06-26 stsp if (error)
5403 4e759de4 2019-06-26 stsp goto done;
5405 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5406 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5407 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5408 4e759de4 2019-06-26 stsp goto done;
5413 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5414 4e759de4 2019-06-26 stsp if (error != NULL)
5415 4e759de4 2019-06-26 stsp goto done;
5417 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5418 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5419 4e759de4 2019-06-26 stsp if (error)
5420 4e759de4 2019-06-26 stsp goto done;
5422 ad89fa31 2019-10-04 stsp if (do_show)
5423 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5424 ad89fa31 2019-10-04 stsp else if (do_list)
5425 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5426 4e759de4 2019-06-26 stsp else if (delref)
5427 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5429 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5430 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5431 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5432 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5433 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5434 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5435 a74f7e83 2019-11-10 stsp if (error)
5436 a74f7e83 2019-11-10 stsp goto done;
5437 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5438 da76fce2 2020-02-24 stsp if (error)
5439 da76fce2 2020-02-24 stsp goto done;
5440 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5441 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5442 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5444 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5445 da76fce2 2020-02-24 stsp if (error)
5446 da76fce2 2020-02-24 stsp goto done;
5447 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5448 da76fce2 2020-02-24 stsp worktree);
5449 da76fce2 2020-02-24 stsp if (error)
5450 da76fce2 2020-02-24 stsp goto done;
5451 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5453 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5454 da76fce2 2020-02-24 stsp goto done;
5456 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5457 da76fce2 2020-02-24 stsp free(branch_refname);
5458 da76fce2 2020-02-24 stsp if (error)
5459 da76fce2 2020-02-24 stsp goto done;
5460 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5462 da76fce2 2020-02-24 stsp if (error)
5463 da76fce2 2020-02-24 stsp goto done;
5464 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5465 da76fce2 2020-02-24 stsp commit_id);
5466 da76fce2 2020-02-24 stsp if (error)
5467 da76fce2 2020-02-24 stsp goto done;
5468 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5469 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5470 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5472 da76fce2 2020-02-24 stsp if (error)
5473 da76fce2 2020-02-24 stsp goto done;
5474 9627c110 2020-04-18 stsp if (upa.did_something)
5475 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5476 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5481 da76fce2 2020-02-24 stsp got_ref_close(ref);
5483 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5484 d0eebce4 2019-03-11 stsp if (worktree)
5485 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5486 d0eebce4 2019-03-11 stsp free(cwd);
5487 d0eebce4 2019-03-11 stsp free(repo_path);
5488 da76fce2 2020-02-24 stsp free(commit_id);
5489 da76fce2 2020-02-24 stsp free(commit_id_str);
5490 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5491 da76fce2 2020-02-24 stsp free((char *)pe->path);
5492 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5493 d00136be 2019-03-26 stsp return error;
5497 d00136be 2019-03-26 stsp __dead static void
5498 8e7bd50a 2019-08-22 stsp usage_tag(void)
5500 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5501 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5502 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5507 b8bad2ba 2019-08-23 stsp static const struct got_error *
5508 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5510 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5511 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5512 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5513 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5514 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5516 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5517 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5518 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5519 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5521 b8bad2ba 2019-08-23 stsp return err;
5522 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5525 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5528 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5529 b8bad2ba 2019-08-23 stsp free(re_id);
5532 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5533 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5536 b8bad2ba 2019-08-23 stsp while (se) {
5537 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5540 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5541 b8bad2ba 2019-08-23 stsp free(se_id);
5544 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5545 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5547 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5548 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5550 b8bad2ba 2019-08-23 stsp return err;
5551 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5554 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5559 b8bad2ba 2019-08-23 stsp return err;
5563 b8bad2ba 2019-08-23 stsp static const struct got_error *
5564 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5566 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5567 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5568 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5570 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5572 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5574 8e7bd50a 2019-08-22 stsp return err;
5576 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5577 8e7bd50a 2019-08-22 stsp const char *refname;
5578 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5579 8e7bd50a 2019-08-22 stsp char datebuf[26];
5580 d4efa91b 2020-01-14 stsp const char *tagger;
5581 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5582 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5583 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5584 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5586 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5587 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5589 8e7bd50a 2019-08-22 stsp refname += 10;
5590 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5591 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5592 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5595 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5596 8e7bd50a 2019-08-22 stsp free(refstr);
5598 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5601 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5602 d4efa91b 2020-01-14 stsp if (err) {
5603 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5607 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5608 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5609 d4efa91b 2020-01-14 stsp if (err) {
5613 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5614 d4efa91b 2020-01-14 stsp tagger_time =
5615 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5616 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5622 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5623 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5624 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5625 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5629 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5630 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5631 2417344c 2019-08-23 stsp if (datestr)
5632 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5633 d4efa91b 2020-01-14 stsp if (commit)
5634 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5636 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5637 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5638 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5641 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5642 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5645 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5646 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5649 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5650 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5657 8e7bd50a 2019-08-22 stsp free(id_str);
5658 d4efa91b 2020-01-14 stsp if (commit) {
5659 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5662 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5664 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5665 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5666 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5667 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5672 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5674 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5676 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5677 8e7bd50a 2019-08-22 stsp } while (line);
5678 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5681 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5682 8e7bd50a 2019-08-22 stsp return NULL;
5685 8e7bd50a 2019-08-22 stsp static const struct got_error *
5686 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5687 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5689 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5690 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5691 f372d5cd 2019-10-21 stsp char *editor = NULL;
5692 1601cb9f 2020-09-11 naddy int initial_content_len;
5693 8e7bd50a 2019-08-22 stsp int fd = -1;
5695 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5696 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5697 8e7bd50a 2019-08-22 stsp goto done;
5700 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5701 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5702 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5703 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5704 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5705 8e7bd50a 2019-08-22 stsp goto done;
5708 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5710 8e7bd50a 2019-08-22 stsp goto done;
5712 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5713 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5714 97972933 2020-09-11 stsp goto done;
5717 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5719 8e7bd50a 2019-08-22 stsp goto done;
5720 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5722 8e7bd50a 2019-08-22 stsp free(initial_content);
5723 8e7bd50a 2019-08-22 stsp free(template);
5724 8e7bd50a 2019-08-22 stsp free(editor);
5726 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5727 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5729 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5730 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5731 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5732 8e7bd50a 2019-08-22 stsp if (err) {
5733 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5734 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5737 8e7bd50a 2019-08-22 stsp return err;
5740 8e7bd50a 2019-08-22 stsp static const struct got_error *
5741 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5742 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5744 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5745 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5746 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5747 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5748 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5749 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5750 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5753 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5754 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5755 8e7bd50a 2019-08-22 stsp * an unintended typo.
5757 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5758 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5760 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5762 8e7bd50a 2019-08-22 stsp return err;
5764 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5765 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5767 8e7bd50a 2019-08-22 stsp goto done;
5769 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5771 8e7bd50a 2019-08-22 stsp goto done;
5773 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5774 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5775 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5776 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5777 8e7bd50a 2019-08-22 stsp goto done;
5779 8e7bd50a 2019-08-22 stsp tag_name += 10;
5780 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5781 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5782 8e7bd50a 2019-08-22 stsp goto done;
5785 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5786 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5787 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5788 8e7bd50a 2019-08-22 stsp goto done;
5789 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5790 8e7bd50a 2019-08-22 stsp goto done;
5792 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5793 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5794 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5795 f372d5cd 2019-10-21 stsp if (err) {
5796 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5797 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5798 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5799 8e7bd50a 2019-08-22 stsp goto done;
5803 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5804 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5805 f372d5cd 2019-10-21 stsp if (err) {
5806 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5807 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5808 8e7bd50a 2019-08-22 stsp goto done;
5811 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5812 f372d5cd 2019-10-21 stsp if (err) {
5813 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5814 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5815 8e7bd50a 2019-08-22 stsp goto done;
5818 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5819 f372d5cd 2019-10-21 stsp if (err) {
5820 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5821 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5822 f372d5cd 2019-10-21 stsp goto done;
5825 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5826 f372d5cd 2019-10-21 stsp if (err) {
5827 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5828 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5829 f372d5cd 2019-10-21 stsp goto done;
5831 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5833 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5834 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5835 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5836 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5837 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5838 f372d5cd 2019-10-21 stsp free(tag_id_str);
5840 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5841 8e7bd50a 2019-08-22 stsp free(commit_id);
5842 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5843 8e7bd50a 2019-08-22 stsp free(refname);
5844 8e7bd50a 2019-08-22 stsp free(tagmsg);
5845 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5846 aba9c984 2019-09-08 stsp free(tagger);
5847 8e7bd50a 2019-08-22 stsp return err;
5850 8e7bd50a 2019-08-22 stsp static const struct got_error *
5851 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5853 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5854 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5855 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5856 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5857 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5858 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5859 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5861 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5862 8e7bd50a 2019-08-22 stsp switch (ch) {
5864 80106605 2020-02-24 stsp commit_id_arg = optarg;
5867 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5870 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5871 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5872 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5874 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5877 8e7bd50a 2019-08-22 stsp do_list = 1;
5880 8e7bd50a 2019-08-22 stsp usage_tag();
5881 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5885 8e7bd50a 2019-08-22 stsp argc -= optind;
5886 8e7bd50a 2019-08-22 stsp argv += optind;
5888 8e7bd50a 2019-08-22 stsp if (do_list) {
5889 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5891 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5892 8e7bd50a 2019-08-22 stsp if (tagmsg)
5893 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5894 8e7bd50a 2019-08-22 stsp if (argc > 0)
5895 8e7bd50a 2019-08-22 stsp usage_tag();
5896 80106605 2020-02-24 stsp } else if (argc != 1)
5897 8e7bd50a 2019-08-22 stsp usage_tag();
5899 a2887370 2019-08-23 stsp tag_name = argv[0];
5901 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5902 8e7bd50a 2019-08-22 stsp if (do_list) {
5903 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5904 8e7bd50a 2019-08-22 stsp NULL) == -1)
5905 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5907 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5908 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5909 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5912 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5913 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5914 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5915 8e7bd50a 2019-08-22 stsp goto done;
5918 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5919 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5920 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5921 8e7bd50a 2019-08-22 stsp goto done;
5923 8e7bd50a 2019-08-22 stsp error = NULL;
5924 8e7bd50a 2019-08-22 stsp if (worktree) {
5925 8e7bd50a 2019-08-22 stsp repo_path =
5926 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5927 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5928 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5929 8e7bd50a 2019-08-22 stsp if (error)
5930 8e7bd50a 2019-08-22 stsp goto done;
5932 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5933 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5934 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5935 8e7bd50a 2019-08-22 stsp goto done;
5940 8e7bd50a 2019-08-22 stsp if (do_list) {
5941 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5942 c9956ddf 2019-09-08 stsp if (error != NULL)
5943 c9956ddf 2019-09-08 stsp goto done;
5944 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5945 8e7bd50a 2019-08-22 stsp if (error)
5946 8e7bd50a 2019-08-22 stsp goto done;
5947 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5949 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5950 c9956ddf 2019-09-08 stsp if (error)
5951 c9956ddf 2019-09-08 stsp goto done;
5952 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5953 c9956ddf 2019-09-08 stsp if (error != NULL)
5954 c9956ddf 2019-09-08 stsp goto done;
5956 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5957 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5958 8e7bd50a 2019-08-22 stsp if (error)
5959 8e7bd50a 2019-08-22 stsp goto done;
5962 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5963 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5964 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5965 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5966 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5967 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5968 8e7bd50a 2019-08-22 stsp if (error)
5969 8e7bd50a 2019-08-22 stsp goto done;
5970 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5971 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5972 8e7bd50a 2019-08-22 stsp if (error)
5973 8e7bd50a 2019-08-22 stsp goto done;
5974 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5975 8e7bd50a 2019-08-22 stsp free(commit_id);
5976 8e7bd50a 2019-08-22 stsp if (error)
5977 8e7bd50a 2019-08-22 stsp goto done;
5980 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
5981 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5985 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5986 8e7bd50a 2019-08-22 stsp if (worktree)
5987 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5988 8e7bd50a 2019-08-22 stsp free(cwd);
5989 8e7bd50a 2019-08-22 stsp free(repo_path);
5990 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5991 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5992 8e7bd50a 2019-08-22 stsp return error;
5995 8e7bd50a 2019-08-22 stsp __dead static void
5996 d00136be 2019-03-26 stsp usage_add(void)
5998 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5999 022fae89 2019-12-06 tracey getprogname());
6003 d00136be 2019-03-26 stsp static const struct got_error *
6004 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6006 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6008 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6009 4e68cba3 2019-11-23 stsp return NULL;
6012 4e68cba3 2019-11-23 stsp static const struct got_error *
6013 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6015 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6016 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6017 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6018 1dd54920 2019-05-11 stsp char *cwd = NULL;
6019 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6020 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6021 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6023 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6025 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6026 d00136be 2019-03-26 stsp switch (ch) {
6027 022fae89 2019-12-06 tracey case 'I':
6028 022fae89 2019-12-06 tracey no_ignores = 1;
6031 4e68cba3 2019-11-23 stsp can_recurse = 1;
6034 d00136be 2019-03-26 stsp usage_add();
6035 d00136be 2019-03-26 stsp /* NOTREACHED */
6039 d00136be 2019-03-26 stsp argc -= optind;
6040 d00136be 2019-03-26 stsp argv += optind;
6042 43012d58 2019-07-14 stsp #ifndef PROFILE
6043 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6044 43012d58 2019-07-14 stsp NULL) == -1)
6045 43012d58 2019-07-14 stsp err(1, "pledge");
6047 723c305c 2019-05-11 jcs if (argc < 1)
6048 d00136be 2019-03-26 stsp usage_add();
6050 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6051 d00136be 2019-03-26 stsp if (cwd == NULL) {
6052 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6053 d00136be 2019-03-26 stsp goto done;
6056 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6057 fa51e947 2020-03-27 stsp if (error) {
6058 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6059 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6060 d00136be 2019-03-26 stsp goto done;
6063 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6065 031a5338 2019-03-26 stsp if (error != NULL)
6066 031a5338 2019-03-26 stsp goto done;
6068 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6069 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6070 d00136be 2019-03-26 stsp if (error)
6071 d00136be 2019-03-26 stsp goto done;
6073 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6074 6d022e97 2019-08-04 stsp if (error)
6075 022fae89 2019-12-06 tracey goto done;
6077 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6078 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6079 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6080 6d022e97 2019-08-04 stsp goto done;
6084 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6085 4e68cba3 2019-11-23 stsp char *ondisk_path;
6086 4e68cba3 2019-11-23 stsp struct stat sb;
6087 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6088 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6089 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6090 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6091 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6092 4e68cba3 2019-11-23 stsp goto done;
6094 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6095 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6096 4e68cba3 2019-11-23 stsp free(ondisk_path);
6099 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6100 4e68cba3 2019-11-23 stsp ondisk_path);
6101 4e68cba3 2019-11-23 stsp free(ondisk_path);
6102 4e68cba3 2019-11-23 stsp goto done;
6104 4e68cba3 2019-11-23 stsp free(ondisk_path);
6105 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6106 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6107 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6108 4e68cba3 2019-11-23 stsp goto done;
6113 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6114 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6117 031a5338 2019-03-26 stsp got_repo_close(repo);
6118 d00136be 2019-03-26 stsp if (worktree)
6119 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6120 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6121 1dd54920 2019-05-11 stsp free((char *)pe->path);
6122 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6123 2ec1f75b 2019-03-26 stsp free(cwd);
6124 2ec1f75b 2019-03-26 stsp return error;
6127 2ec1f75b 2019-03-26 stsp __dead static void
6128 648e4ef7 2019-07-09 stsp usage_remove(void)
6130 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6131 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6135 2a06fe5f 2019-08-24 stsp static const struct got_error *
6136 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6137 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6139 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6141 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6142 2a06fe5f 2019-08-24 stsp return NULL;
6143 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6144 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6145 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6146 2a06fe5f 2019-08-24 stsp return NULL;
6149 2ec1f75b 2019-03-26 stsp static const struct got_error *
6150 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6152 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6153 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6154 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6155 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6156 17ed4618 2019-06-02 stsp char *cwd = NULL;
6157 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6158 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6159 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6161 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6163 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6164 2ec1f75b 2019-03-26 stsp switch (ch) {
6166 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6168 70e3e7f5 2019-12-13 tracey case 'k':
6169 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6171 f2a9dc41 2019-12-13 tracey case 'R':
6172 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6175 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6176 766841c2 2020-08-13 stsp switch (optarg[i]) {
6177 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6178 766841c2 2020-08-13 stsp delete_local_mods = 1;
6180 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6183 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6184 766841c2 2020-08-13 stsp optarg[i]);
6187 766841c2 2020-08-13 stsp status_codes = optarg;
6190 f2a9dc41 2019-12-13 tracey usage_remove();
6191 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6195 2ec1f75b 2019-03-26 stsp argc -= optind;
6196 2ec1f75b 2019-03-26 stsp argv += optind;
6198 43012d58 2019-07-14 stsp #ifndef PROFILE
6199 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6200 43012d58 2019-07-14 stsp NULL) == -1)
6201 43012d58 2019-07-14 stsp err(1, "pledge");
6203 17ed4618 2019-06-02 stsp if (argc < 1)
6204 648e4ef7 2019-07-09 stsp usage_remove();
6206 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6207 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6208 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6209 2ec1f75b 2019-03-26 stsp goto done;
6211 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6212 fa51e947 2020-03-27 stsp if (error) {
6213 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6214 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6215 2ec1f75b 2019-03-26 stsp goto done;
6218 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6221 2ec1f75b 2019-03-26 stsp goto done;
6223 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6224 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6225 2ec1f75b 2019-03-26 stsp if (error)
6226 2ec1f75b 2019-03-26 stsp goto done;
6228 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6229 6d022e97 2019-08-04 stsp if (error)
6230 6d022e97 2019-08-04 stsp goto done;
6232 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6233 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6234 f2a9dc41 2019-12-13 tracey struct stat sb;
6235 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6236 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6237 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6238 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6239 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6240 f2a9dc41 2019-12-13 tracey goto done;
6242 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6243 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6244 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6245 f2a9dc41 2019-12-13 tracey continue;
6247 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6248 f2a9dc41 2019-12-13 tracey ondisk_path);
6249 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6250 f2a9dc41 2019-12-13 tracey goto done;
6252 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6253 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6254 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6255 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6256 f2a9dc41 2019-12-13 tracey goto done;
6261 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6262 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6263 766841c2 2020-08-13 stsp repo, keep_on_disk);
6266 a129376b 2019-03-28 stsp got_repo_close(repo);
6267 a129376b 2019-03-28 stsp if (worktree)
6268 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6269 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6270 17ed4618 2019-06-02 stsp free((char *)pe->path);
6271 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6272 a129376b 2019-03-28 stsp free(cwd);
6273 a129376b 2019-03-28 stsp return error;
6276 a129376b 2019-03-28 stsp __dead static void
6277 a129376b 2019-03-28 stsp usage_revert(void)
6279 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6280 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6284 1ee397ad 2019-07-12 stsp static const struct got_error *
6285 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6287 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6288 fb9704af 2020-01-27 stsp return NULL;
6290 a129376b 2019-03-28 stsp while (path[0] == '/')
6292 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6293 33aa809d 2019-08-08 stsp return NULL;
6296 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6297 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6298 33aa809d 2019-08-08 stsp const char *action;
6301 33aa809d 2019-08-08 stsp static const struct got_error *
6302 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6303 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6305 33aa809d 2019-08-08 stsp char *line = NULL;
6306 33aa809d 2019-08-08 stsp size_t linesize = 0;
6307 33aa809d 2019-08-08 stsp ssize_t linelen;
6309 33aa809d 2019-08-08 stsp switch (status) {
6310 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6311 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6313 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6314 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6316 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6317 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6318 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6319 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6320 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6321 33aa809d 2019-08-08 stsp printf("%s", line);
6322 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6323 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6324 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6325 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6326 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6329 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6332 33aa809d 2019-08-08 stsp return NULL;
6335 33aa809d 2019-08-08 stsp static const struct got_error *
6336 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6337 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6339 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6340 33aa809d 2019-08-08 stsp char *line = NULL;
6341 33aa809d 2019-08-08 stsp size_t linesize = 0;
6342 33aa809d 2019-08-08 stsp ssize_t linelen;
6343 33aa809d 2019-08-08 stsp int resp = ' ';
6344 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6346 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6348 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6350 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6351 33aa809d 2019-08-08 stsp a->action);
6353 33aa809d 2019-08-08 stsp return err;
6354 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6355 33aa809d 2019-08-08 stsp if (linelen == -1) {
6356 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6357 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6358 33aa809d 2019-08-08 stsp return NULL;
6360 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6362 33aa809d 2019-08-08 stsp *nl = '\0';
6363 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6364 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6365 33aa809d 2019-08-08 stsp printf("y\n");
6366 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6367 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6368 33aa809d 2019-08-08 stsp printf("n\n");
6369 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6370 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6371 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6372 33aa809d 2019-08-08 stsp printf("q\n");
6374 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6375 33aa809d 2019-08-08 stsp free(line);
6376 33aa809d 2019-08-08 stsp return NULL;
6379 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6380 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6381 33aa809d 2019-08-08 stsp a->action);
6383 33aa809d 2019-08-08 stsp return err;
6384 33aa809d 2019-08-08 stsp resp = getchar();
6385 33aa809d 2019-08-08 stsp if (resp == '\n')
6386 33aa809d 2019-08-08 stsp resp = getchar();
6387 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6388 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6389 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6390 33aa809d 2019-08-08 stsp resp = ' ';
6392 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6393 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6394 33aa809d 2019-08-08 stsp resp = ' ';
6398 33aa809d 2019-08-08 stsp if (resp == 'y')
6399 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6400 33aa809d 2019-08-08 stsp else if (resp == 'n')
6401 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6402 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6403 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6405 1ee397ad 2019-07-12 stsp return NULL;
6409 a129376b 2019-03-28 stsp static const struct got_error *
6410 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6412 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6413 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6414 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6415 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6416 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6417 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6418 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6419 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6420 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6421 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6423 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6425 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6426 a129376b 2019-03-28 stsp switch (ch) {
6428 33aa809d 2019-08-08 stsp pflag = 1;
6431 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6434 0f6d7415 2019-08-08 stsp can_recurse = 1;
6437 a129376b 2019-03-28 stsp usage_revert();
6438 a129376b 2019-03-28 stsp /* NOTREACHED */
6442 a129376b 2019-03-28 stsp argc -= optind;
6443 a129376b 2019-03-28 stsp argv += optind;
6445 43012d58 2019-07-14 stsp #ifndef PROFILE
6446 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6447 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6448 43012d58 2019-07-14 stsp err(1, "pledge");
6450 e20a8b6f 2019-06-04 stsp if (argc < 1)
6451 a129376b 2019-03-28 stsp usage_revert();
6452 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6453 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6455 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6456 a129376b 2019-03-28 stsp if (cwd == NULL) {
6457 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6458 a129376b 2019-03-28 stsp goto done;
6460 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6461 fa51e947 2020-03-27 stsp if (error) {
6462 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6463 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6464 2ec1f75b 2019-03-26 stsp goto done;
6467 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6469 a129376b 2019-03-28 stsp if (error != NULL)
6470 a129376b 2019-03-28 stsp goto done;
6472 33aa809d 2019-08-08 stsp if (patch_script_path) {
6473 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6474 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6475 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6476 33aa809d 2019-08-08 stsp patch_script_path);
6477 33aa809d 2019-08-08 stsp goto done;
6480 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6481 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6482 a129376b 2019-03-28 stsp if (error)
6483 a129376b 2019-03-28 stsp goto done;
6485 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6486 6d022e97 2019-08-04 stsp if (error)
6487 6d022e97 2019-08-04 stsp goto done;
6489 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6490 0f6d7415 2019-08-08 stsp char *ondisk_path;
6491 0f6d7415 2019-08-08 stsp struct stat sb;
6492 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6493 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6494 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6495 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6496 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6497 0f6d7415 2019-08-08 stsp goto done;
6499 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6500 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6501 0f6d7415 2019-08-08 stsp free(ondisk_path);
6504 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6505 0f6d7415 2019-08-08 stsp ondisk_path);
6506 0f6d7415 2019-08-08 stsp free(ondisk_path);
6507 0f6d7415 2019-08-08 stsp goto done;
6509 0f6d7415 2019-08-08 stsp free(ondisk_path);
6510 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6511 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6512 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6513 0f6d7415 2019-08-08 stsp goto done;
6518 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6519 33aa809d 2019-08-08 stsp cpa.action = "revert";
6520 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6521 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6523 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6524 33aa809d 2019-08-08 stsp error == NULL)
6525 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6527 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6528 2ec1f75b 2019-03-26 stsp if (worktree)
6529 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6530 2ec1f75b 2019-03-26 stsp free(path);
6531 d00136be 2019-03-26 stsp free(cwd);
6532 6bad629b 2019-02-04 stsp return error;
6535 c4296144 2019-05-09 stsp __dead static void
6536 c4296144 2019-05-09 stsp usage_commit(void)
6538 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6539 5c1e53bc 2019-07-28 stsp getprogname());
6543 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6544 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6545 e2ba3d07 2019-05-13 stsp const char *editor;
6546 e0870e44 2019-05-13 stsp const char *worktree_path;
6547 76d98825 2019-06-03 stsp const char *branch_name;
6548 314a6357 2019-05-13 stsp const char *repo_path;
6549 e0870e44 2019-05-13 stsp char *logmsg_path;
6553 33ad4cbe 2019-05-12 jcs static const struct got_error *
6554 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6557 76d98825 2019-06-03 stsp char *initial_content = NULL;
6558 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6559 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6560 e0870e44 2019-05-13 stsp char *template = NULL;
6561 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6562 1601cb9f 2020-09-11 naddy int initial_content_len;
6563 97972933 2020-09-11 stsp int fd = -1;
6564 33ad4cbe 2019-05-12 jcs size_t len;
6566 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6567 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6568 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6569 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6570 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6571 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6572 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6573 33ad4cbe 2019-05-12 jcs return NULL;
6576 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6577 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6579 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6580 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6581 1601cb9f 2020-09-11 naddy a->branch_name);
6582 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6583 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6585 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6587 e0870e44 2019-05-13 stsp goto done;
6589 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6590 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6591 97972933 2020-09-11 stsp goto done;
6594 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6595 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6596 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6597 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6598 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6601 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6603 76d98825 2019-06-03 stsp free(initial_content);
6604 e0870e44 2019-05-13 stsp free(template);
6606 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6607 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6609 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6610 3ce1b845 2019-07-15 stsp if (err == NULL) {
6611 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6612 3ce1b845 2019-07-15 stsp if (err) {
6613 3ce1b845 2019-07-15 stsp free(*logmsg);
6614 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6617 33ad4cbe 2019-05-12 jcs return err;
6620 c4296144 2019-05-09 stsp static const struct got_error *
6621 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6623 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6624 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6625 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6626 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6627 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6628 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6629 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6630 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6631 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6632 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6633 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6635 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6636 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6638 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6639 c4296144 2019-05-09 stsp switch (ch) {
6641 c4296144 2019-05-09 stsp logmsg = optarg;
6644 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6647 c4296144 2019-05-09 stsp usage_commit();
6648 c4296144 2019-05-09 stsp /* NOTREACHED */
6652 c4296144 2019-05-09 stsp argc -= optind;
6653 c4296144 2019-05-09 stsp argv += optind;
6655 43012d58 2019-07-14 stsp #ifndef PROFILE
6656 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6657 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6658 43012d58 2019-07-14 stsp err(1, "pledge");
6660 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6661 c4296144 2019-05-09 stsp if (cwd == NULL) {
6662 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6663 c4296144 2019-05-09 stsp goto done;
6665 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6666 fa51e947 2020-03-27 stsp if (error) {
6667 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6668 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6669 7d5807f4 2019-07-11 stsp goto done;
6672 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6673 c4296144 2019-05-09 stsp if (error)
6674 a698f62e 2019-07-25 stsp goto done;
6675 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6676 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6677 c4296144 2019-05-09 stsp goto done;
6680 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6681 916f288c 2019-07-30 stsp worktree);
6682 5c1e53bc 2019-07-28 stsp if (error)
6683 5c1e53bc 2019-07-28 stsp goto done;
6685 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6686 c9956ddf 2019-09-08 stsp if (error)
6687 c9956ddf 2019-09-08 stsp goto done;
6688 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6689 c9956ddf 2019-09-08 stsp gitconfig_path);
6690 c4296144 2019-05-09 stsp if (error != NULL)
6691 c4296144 2019-05-09 stsp goto done;
6693 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6694 aba9c984 2019-09-08 stsp if (error)
6695 aba9c984 2019-09-08 stsp return error;
6698 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6699 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6701 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6702 314a6357 2019-05-13 stsp error = get_editor(&editor);
6704 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6705 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6706 c4296144 2019-05-09 stsp if (error)
6707 c4296144 2019-05-09 stsp goto done;
6709 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6710 087fb88c 2019-08-04 stsp if (error)
6711 087fb88c 2019-08-04 stsp goto done;
6713 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6714 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6715 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6716 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6717 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6718 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6719 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6720 916f288c 2019-07-30 stsp goto done;
6722 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6724 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6725 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6726 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6727 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6728 e0870e44 2019-05-13 stsp if (error) {
6729 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6730 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6731 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6732 c4296144 2019-05-09 stsp goto done;
6735 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6736 c4296144 2019-05-09 stsp if (error)
6737 c4296144 2019-05-09 stsp goto done;
6738 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6740 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6741 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6742 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6743 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6744 7266f21f 2019-10-21 stsp error == NULL)
6745 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6746 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6748 c4296144 2019-05-09 stsp got_repo_close(repo);
6749 c4296144 2019-05-09 stsp if (worktree)
6750 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6751 c4296144 2019-05-09 stsp free(cwd);
6752 c4296144 2019-05-09 stsp free(id_str);
6753 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6754 e2ba3d07 2019-05-13 stsp free(editor);
6755 aba9c984 2019-09-08 stsp free(author);
6756 234035bc 2019-06-01 stsp return error;
6759 234035bc 2019-06-01 stsp __dead static void
6760 234035bc 2019-06-01 stsp usage_cherrypick(void)
6762 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6766 234035bc 2019-06-01 stsp static const struct got_error *
6767 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6769 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6770 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6771 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6772 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6773 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6774 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6775 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6776 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6778 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6780 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6781 234035bc 2019-06-01 stsp switch (ch) {
6783 234035bc 2019-06-01 stsp usage_cherrypick();
6784 234035bc 2019-06-01 stsp /* NOTREACHED */
6788 234035bc 2019-06-01 stsp argc -= optind;
6789 234035bc 2019-06-01 stsp argv += optind;
6791 43012d58 2019-07-14 stsp #ifndef PROFILE
6792 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6793 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6794 43012d58 2019-07-14 stsp err(1, "pledge");
6796 234035bc 2019-06-01 stsp if (argc != 1)
6797 234035bc 2019-06-01 stsp usage_cherrypick();
6799 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6800 234035bc 2019-06-01 stsp if (cwd == NULL) {
6801 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6802 234035bc 2019-06-01 stsp goto done;
6804 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6805 fa51e947 2020-03-27 stsp if (error) {
6806 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6807 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6809 234035bc 2019-06-01 stsp goto done;
6812 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6814 234035bc 2019-06-01 stsp if (error != NULL)
6815 234035bc 2019-06-01 stsp goto done;
6817 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6818 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6819 234035bc 2019-06-01 stsp if (error)
6820 234035bc 2019-06-01 stsp goto done;
6822 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6823 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6824 234035bc 2019-06-01 stsp if (error != NULL) {
6825 234035bc 2019-06-01 stsp struct got_reference *ref;
6826 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6827 234035bc 2019-06-01 stsp goto done;
6828 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6829 234035bc 2019-06-01 stsp if (error != NULL)
6830 234035bc 2019-06-01 stsp goto done;
6831 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6832 234035bc 2019-06-01 stsp got_ref_close(ref);
6833 234035bc 2019-06-01 stsp if (error != NULL)
6834 234035bc 2019-06-01 stsp goto done;
6836 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6837 234035bc 2019-06-01 stsp if (error)
6838 234035bc 2019-06-01 stsp goto done;
6840 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6841 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6842 234035bc 2019-06-01 stsp if (error != NULL)
6843 234035bc 2019-06-01 stsp goto done;
6845 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6846 234035bc 2019-06-01 stsp if (error) {
6847 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6848 234035bc 2019-06-01 stsp goto done;
6849 234035bc 2019-06-01 stsp error = NULL;
6851 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6852 234035bc 2019-06-01 stsp goto done;
6855 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6856 234035bc 2019-06-01 stsp if (error)
6857 234035bc 2019-06-01 stsp goto done;
6858 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6859 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6860 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6861 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6863 234035bc 2019-06-01 stsp if (error != NULL)
6864 234035bc 2019-06-01 stsp goto done;
6866 9627c110 2020-04-18 stsp if (upa.did_something)
6867 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6868 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6870 234035bc 2019-06-01 stsp if (commit)
6871 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6872 234035bc 2019-06-01 stsp free(commit_id_str);
6873 234035bc 2019-06-01 stsp if (head_ref)
6874 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6875 234035bc 2019-06-01 stsp if (worktree)
6876 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6878 234035bc 2019-06-01 stsp got_repo_close(repo);
6879 c4296144 2019-05-09 stsp return error;
6882 5ef14e63 2019-06-02 stsp __dead static void
6883 5ef14e63 2019-06-02 stsp usage_backout(void)
6885 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6889 5ef14e63 2019-06-02 stsp static const struct got_error *
6890 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6892 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6893 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6894 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6895 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6896 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6897 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6898 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6899 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6901 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6903 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6904 5ef14e63 2019-06-02 stsp switch (ch) {
6906 5ef14e63 2019-06-02 stsp usage_backout();
6907 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6911 5ef14e63 2019-06-02 stsp argc -= optind;
6912 5ef14e63 2019-06-02 stsp argv += optind;
6914 43012d58 2019-07-14 stsp #ifndef PROFILE
6915 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6916 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6917 43012d58 2019-07-14 stsp err(1, "pledge");
6919 5ef14e63 2019-06-02 stsp if (argc != 1)
6920 5ef14e63 2019-06-02 stsp usage_backout();
6922 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6923 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6924 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6925 5ef14e63 2019-06-02 stsp goto done;
6927 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6928 fa51e947 2020-03-27 stsp if (error) {
6929 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6930 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6931 5ef14e63 2019-06-02 stsp goto done;
6934 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6936 5ef14e63 2019-06-02 stsp if (error != NULL)
6937 5ef14e63 2019-06-02 stsp goto done;
6939 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6940 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6941 5ef14e63 2019-06-02 stsp if (error)
6942 5ef14e63 2019-06-02 stsp goto done;
6944 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6945 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6946 5ef14e63 2019-06-02 stsp if (error != NULL) {
6947 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6948 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6949 5ef14e63 2019-06-02 stsp goto done;
6950 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6951 5ef14e63 2019-06-02 stsp if (error != NULL)
6952 5ef14e63 2019-06-02 stsp goto done;
6953 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6954 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6955 5ef14e63 2019-06-02 stsp if (error != NULL)
6956 5ef14e63 2019-06-02 stsp goto done;
6958 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6959 5ef14e63 2019-06-02 stsp if (error)
6960 5ef14e63 2019-06-02 stsp goto done;
6962 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6963 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6964 5ef14e63 2019-06-02 stsp if (error != NULL)
6965 5ef14e63 2019-06-02 stsp goto done;
6967 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6968 5ef14e63 2019-06-02 stsp if (error)
6969 5ef14e63 2019-06-02 stsp goto done;
6971 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6972 5ef14e63 2019-06-02 stsp if (error)
6973 5ef14e63 2019-06-02 stsp goto done;
6974 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6975 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6976 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6977 5ef14e63 2019-06-02 stsp goto done;
6980 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6981 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6982 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
6983 5ef14e63 2019-06-02 stsp if (error != NULL)
6984 5ef14e63 2019-06-02 stsp goto done;
6986 9627c110 2020-04-18 stsp if (upa.did_something)
6987 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6988 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6990 5ef14e63 2019-06-02 stsp if (commit)
6991 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6992 5ef14e63 2019-06-02 stsp free(commit_id_str);
6993 5ef14e63 2019-06-02 stsp if (head_ref)
6994 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6995 818c7501 2019-07-11 stsp if (worktree)
6996 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6998 818c7501 2019-07-11 stsp got_repo_close(repo);
6999 818c7501 2019-07-11 stsp return error;
7002 818c7501 2019-07-11 stsp __dead static void
7003 818c7501 2019-07-11 stsp usage_rebase(void)
7005 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7006 af54c8f8 2019-07-11 stsp getprogname());
7011 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7014 0ebf8283 2019-07-24 stsp size_t len;
7016 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7017 0ebf8283 2019-07-24 stsp if (len > limit)
7018 0ebf8283 2019-07-24 stsp len = limit;
7019 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7020 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7022 0ebf8283 2019-07-24 stsp *nl = '\0';
7025 0ebf8283 2019-07-24 stsp static const struct got_error *
7026 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7028 5943eee2 2019-08-13 stsp const struct got_error *err;
7029 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7030 5943eee2 2019-08-13 stsp const char *s;
7032 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7034 5943eee2 2019-08-13 stsp return err;
7036 5943eee2 2019-08-13 stsp s = logmsg0;
7037 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7040 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7041 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7042 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7043 5943eee2 2019-08-13 stsp goto done;
7046 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7048 5943eee2 2019-08-13 stsp free(logmsg0);
7049 a0ea4fc0 2020-02-28 stsp return err;
7052 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7053 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7055 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7056 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7057 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7059 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7061 a0ea4fc0 2020-02-28 stsp return err;
7063 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7065 a0ea4fc0 2020-02-28 stsp goto done;
7067 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7069 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7071 a0ea4fc0 2020-02-28 stsp goto done;
7073 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7075 a0ea4fc0 2020-02-28 stsp free(id_str);
7076 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7077 a0ea4fc0 2020-02-28 stsp free(logmsg);
7078 5943eee2 2019-08-13 stsp return err;
7081 818c7501 2019-07-11 stsp static const struct got_error *
7082 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7083 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7085 818c7501 2019-07-11 stsp const struct got_error *err;
7086 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7088 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7090 818c7501 2019-07-11 stsp goto done;
7092 ff0d2220 2019-07-11 stsp if (new_id) {
7093 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7095 ff0d2220 2019-07-11 stsp goto done;
7098 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7099 ff0d2220 2019-07-11 stsp if (new_id_str)
7100 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7102 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7104 0ebf8283 2019-07-24 stsp goto done;
7106 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7107 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7109 818c7501 2019-07-11 stsp free(old_id_str);
7110 818c7501 2019-07-11 stsp free(new_id_str);
7111 272a1371 2020-02-28 stsp free(logmsg);
7112 818c7501 2019-07-11 stsp return err;
7115 1ee397ad 2019-07-12 stsp static const struct got_error *
7116 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7117 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7118 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7120 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7121 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7122 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7125 818c7501 2019-07-11 stsp static const struct got_error *
7126 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7127 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7128 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7129 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7131 ff0d2220 2019-07-11 stsp const struct got_error *error;
7132 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7133 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7135 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7136 ff0d2220 2019-07-11 stsp if (error)
7137 ff0d2220 2019-07-11 stsp return error;
7139 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7140 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7141 ff0d2220 2019-07-11 stsp if (error) {
7142 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7143 ff0d2220 2019-07-11 stsp goto done;
7144 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7146 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7147 ff0d2220 2019-07-11 stsp free(new_commit_id);
7150 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7151 ff0d2220 2019-07-11 stsp return error;
7154 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7155 64c6d990 2019-07-11 stsp const char *path_prefix;
7156 64c6d990 2019-07-11 stsp size_t len;
7157 8ca9bd68 2019-07-25 stsp int errcode;
7160 64c6d990 2019-07-11 stsp static const struct got_error *
7161 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7162 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7163 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7164 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7166 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7168 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7169 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7170 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7172 64c6d990 2019-07-11 stsp return NULL;
7175 64c6d990 2019-07-11 stsp static const struct got_error *
7176 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7177 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7178 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7180 64c6d990 2019-07-11 stsp const struct got_error *err;
7181 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7182 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7183 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7185 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7186 64c6d990 2019-07-11 stsp return NULL;
7188 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7190 64c6d990 2019-07-11 stsp goto done;
7192 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7194 64c6d990 2019-07-11 stsp goto done;
7196 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7197 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7199 64c6d990 2019-07-11 stsp goto done;
7201 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7202 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7204 64c6d990 2019-07-11 stsp goto done;
7206 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7207 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7208 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7209 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7210 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7211 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7212 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7214 64c6d990 2019-07-11 stsp if (tree1)
7215 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7216 64c6d990 2019-07-11 stsp if (tree2)
7217 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7218 64c6d990 2019-07-11 stsp if (commit)
7219 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7220 64c6d990 2019-07-11 stsp if (parent_commit)
7221 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7222 64c6d990 2019-07-11 stsp return err;
7225 ff0d2220 2019-07-11 stsp static const struct got_error *
7226 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7227 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7228 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7229 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7230 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7232 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7233 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7234 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7235 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7236 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7238 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7240 af61c510 2019-07-19 stsp return err;
7242 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7243 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7245 af61c510 2019-07-19 stsp goto done;
7246 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7247 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7248 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7249 af61c510 2019-07-19 stsp if (err) {
7250 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7251 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7252 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7253 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7254 af61c510 2019-07-19 stsp "been reached?!?");
7256 ee780d5c 2020-01-04 stsp goto done;
7258 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7259 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7261 af61c510 2019-07-19 stsp goto done;
7263 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7265 af61c510 2019-07-19 stsp goto done;
7266 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7267 af61c510 2019-07-19 stsp commit_id = parent_id;
7271 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7272 af61c510 2019-07-19 stsp return err;
7275 af61c510 2019-07-19 stsp static const struct got_error *
7276 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7278 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7279 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7280 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7281 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7282 818c7501 2019-07-11 stsp char *cwd = NULL;
7283 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7284 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7285 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7286 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7287 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7288 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7289 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7290 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7291 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7292 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7293 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7294 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7295 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7297 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7298 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7300 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7301 818c7501 2019-07-11 stsp switch (ch) {
7303 818c7501 2019-07-11 stsp abort_rebase = 1;
7306 818c7501 2019-07-11 stsp continue_rebase = 1;
7309 818c7501 2019-07-11 stsp usage_rebase();
7310 818c7501 2019-07-11 stsp /* NOTREACHED */
7314 818c7501 2019-07-11 stsp argc -= optind;
7315 818c7501 2019-07-11 stsp argv += optind;
7317 43012d58 2019-07-14 stsp #ifndef PROFILE
7318 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7319 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7320 43012d58 2019-07-14 stsp err(1, "pledge");
7322 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7323 818c7501 2019-07-11 stsp usage_rebase();
7324 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7325 818c7501 2019-07-11 stsp if (argc != 0)
7326 818c7501 2019-07-11 stsp usage_rebase();
7327 818c7501 2019-07-11 stsp } else if (argc != 1)
7328 818c7501 2019-07-11 stsp usage_rebase();
7330 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7331 818c7501 2019-07-11 stsp if (cwd == NULL) {
7332 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7333 818c7501 2019-07-11 stsp goto done;
7335 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7336 fa51e947 2020-03-27 stsp if (error) {
7337 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7338 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7339 818c7501 2019-07-11 stsp goto done;
7342 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7344 818c7501 2019-07-11 stsp if (error != NULL)
7345 818c7501 2019-07-11 stsp goto done;
7347 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7348 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7349 7ef62c4e 2020-02-24 stsp if (error)
7350 7ef62c4e 2020-02-24 stsp goto done;
7352 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7353 7ef62c4e 2020-02-24 stsp worktree);
7354 818c7501 2019-07-11 stsp if (error)
7355 7ef62c4e 2020-02-24 stsp goto done;
7356 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7357 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7358 818c7501 2019-07-11 stsp goto done;
7361 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7362 818c7501 2019-07-11 stsp if (error)
7363 818c7501 2019-07-11 stsp goto done;
7365 f6794adc 2019-07-23 stsp if (abort_rebase) {
7366 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7367 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7368 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7369 f6794adc 2019-07-23 stsp goto done;
7371 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7372 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7373 3e3a69f1 2019-07-25 stsp worktree, repo);
7374 818c7501 2019-07-11 stsp if (error)
7375 818c7501 2019-07-11 stsp goto done;
7376 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7377 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7378 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7379 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7380 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7381 818c7501 2019-07-11 stsp if (error)
7382 818c7501 2019-07-11 stsp goto done;
7383 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7384 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7385 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7388 818c7501 2019-07-11 stsp if (continue_rebase) {
7389 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7390 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7391 f6794adc 2019-07-23 stsp goto done;
7393 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7394 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7395 3e3a69f1 2019-07-25 stsp worktree, repo);
7396 818c7501 2019-07-11 stsp if (error)
7397 818c7501 2019-07-11 stsp goto done;
7399 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7400 01757395 2019-07-12 stsp resume_commit_id, repo);
7401 818c7501 2019-07-11 stsp if (error)
7402 818c7501 2019-07-11 stsp goto done;
7404 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7405 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7406 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7407 818c7501 2019-07-11 stsp goto done;
7410 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7411 818c7501 2019-07-11 stsp if (error != NULL)
7412 818c7501 2019-07-11 stsp goto done;
7415 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7416 ff0d2220 2019-07-11 stsp if (error)
7417 ff0d2220 2019-07-11 stsp goto done;
7419 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7420 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7422 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7423 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7424 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7425 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7426 818c7501 2019-07-11 stsp if (error)
7427 818c7501 2019-07-11 stsp goto done;
7428 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7429 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7430 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7431 818c7501 2019-07-11 stsp "with work tree's branch");
7432 818c7501 2019-07-11 stsp goto done;
7435 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7436 a51a74b3 2019-07-27 stsp if (error) {
7437 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7438 a51a74b3 2019-07-27 stsp goto done;
7439 a51a74b3 2019-07-27 stsp error = NULL;
7441 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7442 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7443 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7444 a51a74b3 2019-07-27 stsp goto done;
7446 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7447 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7448 818c7501 2019-07-11 stsp if (error)
7449 818c7501 2019-07-11 stsp goto done;
7452 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7453 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7454 818c7501 2019-07-11 stsp if (error)
7455 818c7501 2019-07-11 stsp goto done;
7457 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7458 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7459 fc66b545 2019-08-12 stsp if (pid == NULL) {
7460 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7461 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7462 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7463 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7464 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7465 fc66b545 2019-08-12 stsp if (error)
7466 fc66b545 2019-08-12 stsp goto done;
7467 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7468 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7469 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7472 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7473 fc66b545 2019-08-12 stsp goto done;
7475 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7476 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7477 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7478 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7479 818c7501 2019-07-11 stsp commit = NULL;
7480 818c7501 2019-07-11 stsp if (error)
7481 818c7501 2019-07-11 stsp goto done;
7483 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7484 38b0338b 2019-11-29 stsp if (continue_rebase) {
7485 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7486 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7487 38b0338b 2019-11-29 stsp goto done;
7489 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7490 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7491 38b0338b 2019-11-29 stsp char *id_str;
7492 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7493 38b0338b 2019-11-29 stsp new_base_branch);
7494 38b0338b 2019-11-29 stsp if (error)
7495 38b0338b 2019-11-29 stsp goto done;
7496 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7497 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7498 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7499 38b0338b 2019-11-29 stsp free(id_str);
7500 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7501 38b0338b 2019-11-29 stsp new_head_commit_id);
7502 38b0338b 2019-11-29 stsp if (error)
7503 38b0338b 2019-11-29 stsp goto done;
7507 818c7501 2019-07-11 stsp pid = NULL;
7508 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7509 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7511 818c7501 2019-07-11 stsp commit_id = qid->id;
7512 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7513 818c7501 2019-07-11 stsp pid = qid;
7515 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7516 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7517 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7518 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7519 818c7501 2019-07-11 stsp if (error)
7520 818c7501 2019-07-11 stsp goto done;
7522 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7523 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7524 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7526 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7527 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7528 a0ea4fc0 2020-02-28 stsp if (error)
7529 a0ea4fc0 2020-02-28 stsp goto done;
7530 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7534 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7535 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7536 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7537 818c7501 2019-07-11 stsp if (error)
7538 818c7501 2019-07-11 stsp goto done;
7541 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7542 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7543 818c7501 2019-07-11 stsp if (error)
7544 818c7501 2019-07-11 stsp goto done;
7545 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7546 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7548 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7549 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7551 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7552 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7553 818c7501 2019-07-11 stsp free(resume_commit_id);
7554 818c7501 2019-07-11 stsp free(yca_id);
7555 818c7501 2019-07-11 stsp if (commit)
7556 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7557 818c7501 2019-07-11 stsp if (branch)
7558 818c7501 2019-07-11 stsp got_ref_close(branch);
7559 818c7501 2019-07-11 stsp if (new_base_branch)
7560 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7561 818c7501 2019-07-11 stsp if (tmp_branch)
7562 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7563 5ef14e63 2019-06-02 stsp if (worktree)
7564 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7566 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7567 5ef14e63 2019-06-02 stsp return error;
7570 0ebf8283 2019-07-24 stsp __dead static void
7571 0ebf8283 2019-07-24 stsp usage_histedit(void)
7573 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7574 0ebf8283 2019-07-24 stsp getprogname());
7578 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7579 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7580 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7581 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7582 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7584 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7585 0ebf8283 2019-07-24 stsp unsigned char code;
7586 0ebf8283 2019-07-24 stsp const char *name;
7587 0ebf8283 2019-07-24 stsp const char *desc;
7588 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7589 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7590 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7591 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7592 82997472 2020-01-29 stsp "be used" },
7593 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7594 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7595 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7598 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7599 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7600 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7601 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7602 0ebf8283 2019-07-24 stsp char *logmsg;
7604 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7606 0ebf8283 2019-07-24 stsp static const struct got_error *
7607 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7608 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7610 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7611 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7612 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7615 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7617 0ebf8283 2019-07-24 stsp goto done;
7619 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7621 0ebf8283 2019-07-24 stsp goto done;
7623 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7625 0ebf8283 2019-07-24 stsp goto done;
7627 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7628 0ebf8283 2019-07-24 stsp if (n < 0)
7629 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7631 0ebf8283 2019-07-24 stsp if (commit)
7632 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7633 0ebf8283 2019-07-24 stsp free(id_str);
7634 0ebf8283 2019-07-24 stsp free(logmsg);
7635 0ebf8283 2019-07-24 stsp return err;
7638 0ebf8283 2019-07-24 stsp static const struct got_error *
7639 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7640 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7642 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7643 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7645 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7646 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7648 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7649 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7653 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7654 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7655 083957f4 2020-02-24 stsp if (n < 0) {
7656 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7662 0ebf8283 2019-07-24 stsp return err;
7665 0ebf8283 2019-07-24 stsp static const struct got_error *
7666 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7667 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7669 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7671 514f2ffe 2020-01-29 stsp char *id_str;
7672 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7674 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7675 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7677 514f2ffe 2020-01-29 stsp return err;
7679 514f2ffe 2020-01-29 stsp n = fprintf(f,
7680 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7681 514f2ffe 2020-01-29 stsp "# commit %s\n"
7682 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7683 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7684 514f2ffe 2020-01-29 stsp if (n < 0) {
7685 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7686 514f2ffe 2020-01-29 stsp goto done;
7689 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7690 514f2ffe 2020-01-29 stsp if (n < 0) {
7691 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7692 514f2ffe 2020-01-29 stsp goto done;
7695 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7696 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7697 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7698 0ebf8283 2019-07-24 stsp cmd->desc);
7699 0ebf8283 2019-07-24 stsp if (n < 0) {
7700 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7705 514f2ffe 2020-01-29 stsp free(id_str);
7706 0ebf8283 2019-07-24 stsp return err;
7709 0ebf8283 2019-07-24 stsp static const struct got_error *
7710 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7712 0ebf8283 2019-07-24 stsp static char msg[42];
7715 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7717 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7718 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7720 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7723 0ebf8283 2019-07-24 stsp static const struct got_error *
7724 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7725 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7727 0ebf8283 2019-07-24 stsp const struct got_error *err;
7728 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7729 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7731 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7733 0ebf8283 2019-07-24 stsp return err;
7735 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7737 0ebf8283 2019-07-24 stsp goto done;
7739 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7741 5943eee2 2019-08-13 stsp goto done;
7742 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7743 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7744 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7745 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7748 0ebf8283 2019-07-24 stsp if (folded_commit)
7749 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7750 0ebf8283 2019-07-24 stsp free(id_str);
7751 5943eee2 2019-08-13 stsp free(folded_logmsg);
7752 0ebf8283 2019-07-24 stsp return err;
7755 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7756 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7758 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7760 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7761 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7762 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7763 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7764 3f9de99f 2019-07-24 stsp folded = prev;
7765 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7768 0ebf8283 2019-07-24 stsp return folded;
7771 0ebf8283 2019-07-24 stsp static const struct got_error *
7772 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7773 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7775 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7776 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7777 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7778 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7779 1601cb9f 2020-09-11 naddy int logmsg_len;
7781 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7783 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7785 0ebf8283 2019-07-24 stsp return err;
7787 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7788 0ebf8283 2019-07-24 stsp if (folded) {
7789 0ebf8283 2019-07-24 stsp while (folded != hle) {
7790 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7791 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7794 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7795 0ebf8283 2019-07-24 stsp logmsg, repo);
7797 0ebf8283 2019-07-24 stsp goto done;
7798 0ebf8283 2019-07-24 stsp free(logmsg);
7799 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7800 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7804 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7806 0ebf8283 2019-07-24 stsp goto done;
7807 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7809 5943eee2 2019-08-13 stsp goto done;
7810 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7811 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7812 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7813 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7814 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7815 0ebf8283 2019-07-24 stsp goto done;
7817 0ebf8283 2019-07-24 stsp free(logmsg);
7818 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7820 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7822 0ebf8283 2019-07-24 stsp goto done;
7824 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7825 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7827 0ebf8283 2019-07-24 stsp goto done;
7829 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7830 0ebf8283 2019-07-24 stsp close(fd);
7832 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7834 0ebf8283 2019-07-24 stsp goto done;
7836 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7837 0ebf8283 2019-07-24 stsp if (err) {
7838 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7839 0ebf8283 2019-07-24 stsp goto done;
7840 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7843 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7844 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7845 0ebf8283 2019-07-24 stsp free(logmsg_path);
7846 0ebf8283 2019-07-24 stsp free(logmsg);
7847 5943eee2 2019-08-13 stsp free(orig_logmsg);
7848 0ebf8283 2019-07-24 stsp free(editor);
7849 0ebf8283 2019-07-24 stsp if (commit)
7850 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7851 0ebf8283 2019-07-24 stsp return err;
7854 0ebf8283 2019-07-24 stsp static const struct got_error *
7855 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7856 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7858 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7859 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7860 0ebf8283 2019-07-24 stsp size_t size;
7861 0ebf8283 2019-07-24 stsp ssize_t len;
7862 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7863 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7864 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7865 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7867 0ebf8283 2019-07-24 stsp for (;;) {
7868 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7869 0ebf8283 2019-07-24 stsp if (len == -1) {
7870 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7871 0ebf8283 2019-07-24 stsp if (feof(f))
7873 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7874 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7879 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7881 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7882 0ebf8283 2019-07-24 stsp free(line);
7883 0ebf8283 2019-07-24 stsp line = NULL;
7886 0ebf8283 2019-07-24 stsp cmd = NULL;
7887 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7888 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7889 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7890 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7891 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7894 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7899 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7900 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7903 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7905 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7906 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7907 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7910 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7911 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7915 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7916 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7917 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7921 0ebf8283 2019-07-24 stsp free(line);
7922 0ebf8283 2019-07-24 stsp line = NULL;
7926 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7928 0ebf8283 2019-07-24 stsp *end = '\0';
7930 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7931 0ebf8283 2019-07-24 stsp if (err) {
7932 0ebf8283 2019-07-24 stsp /* override error code */
7933 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7937 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7938 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7939 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7942 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7943 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7944 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7945 0ebf8283 2019-07-24 stsp commit_id = NULL;
7946 0ebf8283 2019-07-24 stsp free(line);
7947 0ebf8283 2019-07-24 stsp line = NULL;
7948 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7951 0ebf8283 2019-07-24 stsp free(line);
7952 0ebf8283 2019-07-24 stsp free(commit_id);
7953 0ebf8283 2019-07-24 stsp return err;
7956 0ebf8283 2019-07-24 stsp static const struct got_error *
7957 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7958 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7960 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7961 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7962 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7963 5b87815e 2020-03-05 stsp static char msg[92];
7964 bfce7f83 2019-07-27 stsp char *id_str;
7966 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7967 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7968 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7969 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7970 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7972 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7973 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7974 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7975 5b87815e 2020-03-05 stsp if (hle == hle2)
7977 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7978 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7980 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7982 5b87815e 2020-03-05 stsp return err;
7983 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7984 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7985 5b87815e 2020-03-05 stsp free(id_str);
7986 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7990 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7991 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7992 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7995 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7996 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7998 bfce7f83 2019-07-27 stsp return err;
7999 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8000 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8001 bfce7f83 2019-07-27 stsp free(id_str);
8002 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8006 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8007 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8008 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8009 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8011 bfce7f83 2019-07-27 stsp return NULL;
8014 bfce7f83 2019-07-27 stsp static const struct got_error *
8015 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8016 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8017 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8019 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8020 0ebf8283 2019-07-24 stsp char *editor;
8021 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8023 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8025 0ebf8283 2019-07-24 stsp return err;
8027 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8028 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8029 0ebf8283 2019-07-24 stsp goto done;
8032 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8033 0ebf8283 2019-07-24 stsp if (f == NULL) {
8034 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8035 0ebf8283 2019-07-24 stsp goto done;
8037 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8039 bfce7f83 2019-07-27 stsp goto done;
8041 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8043 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8044 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8045 0ebf8283 2019-07-24 stsp free(editor);
8046 0ebf8283 2019-07-24 stsp return err;
8049 0ebf8283 2019-07-24 stsp static const struct got_error *
8050 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8051 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8052 514f2ffe 2020-01-29 stsp struct got_repository *);
8054 0ebf8283 2019-07-24 stsp static const struct got_error *
8055 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8056 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8057 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8059 0ebf8283 2019-07-24 stsp const struct got_error *err;
8060 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8061 0ebf8283 2019-07-24 stsp char *path = NULL;
8063 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8065 0ebf8283 2019-07-24 stsp return err;
8067 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8069 0ebf8283 2019-07-24 stsp goto done;
8071 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8073 0ebf8283 2019-07-24 stsp goto done;
8075 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8076 083957f4 2020-02-24 stsp rewind(f);
8077 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8079 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8080 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8081 0ebf8283 2019-07-24 stsp goto done;
8084 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8085 083957f4 2020-02-24 stsp if (err) {
8086 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8087 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8088 083957f4 2020-02-24 stsp goto done;
8089 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8090 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8094 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8095 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8096 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8097 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8098 0ebf8283 2019-07-24 stsp free(path);
8099 0ebf8283 2019-07-24 stsp return err;
8102 0ebf8283 2019-07-24 stsp static const struct got_error *
8103 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8104 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8106 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8107 0ebf8283 2019-07-24 stsp char *path = NULL;
8108 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8109 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8110 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8112 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8114 0ebf8283 2019-07-24 stsp return err;
8116 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8117 0ebf8283 2019-07-24 stsp if (f == NULL) {
8118 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8119 0ebf8283 2019-07-24 stsp goto done;
8121 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8122 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8127 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8128 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8129 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8130 0ebf8283 2019-07-24 stsp if (n < 0) {
8131 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8137 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8138 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8139 0ebf8283 2019-07-24 stsp free(path);
8140 0ebf8283 2019-07-24 stsp if (commit)
8141 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8142 0ebf8283 2019-07-24 stsp return err;
8146 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8148 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8150 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8151 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8152 bfce7f83 2019-07-27 stsp free(hle);
8156 0ebf8283 2019-07-24 stsp static const struct got_error *
8157 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8158 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8160 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8161 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8163 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8164 0ebf8283 2019-07-24 stsp if (f == NULL) {
8165 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8166 0ebf8283 2019-07-24 stsp goto done;
8169 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8171 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8172 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8173 0ebf8283 2019-07-24 stsp return err;
8176 0ebf8283 2019-07-24 stsp static const struct got_error *
8177 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8178 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8179 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8181 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8182 0ebf8283 2019-07-24 stsp int resp = ' ';
8184 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8185 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8186 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8187 0ebf8283 2019-07-24 stsp resp = getchar();
8188 a61a4414 2019-08-07 stsp if (resp == '\n')
8189 a61a4414 2019-08-07 stsp resp = getchar();
8190 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8191 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8192 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8194 426ebf2e 2019-07-25 stsp if (err) {
8195 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8196 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8198 bfce7f83 2019-07-27 stsp prev_err = err;
8199 426ebf2e 2019-07-25 stsp resp = ' ';
8203 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8204 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8205 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8206 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8207 426ebf2e 2019-07-25 stsp if (err) {
8208 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8209 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8211 bfce7f83 2019-07-27 stsp prev_err = err;
8212 426ebf2e 2019-07-25 stsp resp = ' ';
8216 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8217 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8220 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8223 0ebf8283 2019-07-24 stsp return err;
8226 0ebf8283 2019-07-24 stsp static const struct got_error *
8227 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8228 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8229 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8231 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8232 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8233 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8234 3e3a69f1 2019-07-25 stsp branch, repo);
8237 0ebf8283 2019-07-24 stsp static const struct got_error *
8238 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8239 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8241 0ebf8283 2019-07-24 stsp const struct got_error *err;
8242 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8244 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8246 0ebf8283 2019-07-24 stsp goto done;
8248 0ebf8283 2019-07-24 stsp if (new_id) {
8249 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8251 0ebf8283 2019-07-24 stsp goto done;
8254 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8255 0ebf8283 2019-07-24 stsp if (new_id_str)
8256 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8258 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8259 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8260 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8261 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8262 0ebf8283 2019-07-24 stsp goto done;
8264 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8266 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8268 0ebf8283 2019-07-24 stsp goto done;
8271 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8272 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8273 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8274 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8275 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8277 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8278 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8279 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8286 0ebf8283 2019-07-24 stsp free(old_id_str);
8287 0ebf8283 2019-07-24 stsp free(new_id_str);
8288 0ebf8283 2019-07-24 stsp return err;
8291 0ebf8283 2019-07-24 stsp static const struct got_error *
8292 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8293 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8294 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8295 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8297 0ebf8283 2019-07-24 stsp const struct got_error *err;
8298 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8299 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8301 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8302 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8303 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8305 0ebf8283 2019-07-24 stsp return err;
8308 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8310 0ebf8283 2019-07-24 stsp return err;
8312 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8313 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8314 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8315 0ebf8283 2019-07-24 stsp if (err) {
8316 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8317 0ebf8283 2019-07-24 stsp goto done;
8318 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8320 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8321 0ebf8283 2019-07-24 stsp free(new_commit_id);
8324 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8325 0ebf8283 2019-07-24 stsp return err;
8328 0ebf8283 2019-07-24 stsp static const struct got_error *
8329 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8330 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8332 0ebf8283 2019-07-24 stsp const struct got_error *error;
8333 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8335 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8337 0ebf8283 2019-07-24 stsp if (error)
8338 0ebf8283 2019-07-24 stsp return error;
8340 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8341 0ebf8283 2019-07-24 stsp if (error)
8342 0ebf8283 2019-07-24 stsp return error;
8344 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8345 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8346 0ebf8283 2019-07-24 stsp return error;
8349 ab20a43a 2020-01-29 stsp static const struct got_error *
8350 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8351 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8352 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8353 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8355 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8357 ab20a43a 2020-01-29 stsp switch (status) {
8358 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8359 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8360 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8361 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8362 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8363 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8368 ab20a43a 2020-01-29 stsp switch (staged_status) {
8369 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8370 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8371 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8372 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8373 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8378 ab20a43a 2020-01-29 stsp return NULL;
8381 0ebf8283 2019-07-24 stsp static const struct got_error *
8382 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8384 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8385 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8386 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8387 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8388 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8389 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8390 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8391 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8392 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8393 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8394 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8395 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8396 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8397 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8398 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8399 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8400 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8401 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8402 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8403 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8404 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8405 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8406 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8408 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8409 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8410 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8411 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8413 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8414 0ebf8283 2019-07-24 stsp switch (ch) {
8416 0ebf8283 2019-07-24 stsp abort_edit = 1;
8419 0ebf8283 2019-07-24 stsp continue_edit = 1;
8422 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8425 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8428 0ebf8283 2019-07-24 stsp usage_histedit();
8429 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8433 0ebf8283 2019-07-24 stsp argc -= optind;
8434 0ebf8283 2019-07-24 stsp argv += optind;
8436 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8437 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8438 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8439 0ebf8283 2019-07-24 stsp err(1, "pledge");
8441 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8442 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8443 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8444 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8445 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8446 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8447 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8448 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8449 0ebf8283 2019-07-24 stsp if (argc != 0)
8450 0ebf8283 2019-07-24 stsp usage_histedit();
8453 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8454 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8455 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8456 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8457 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8458 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8461 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8462 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8463 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8464 0ebf8283 2019-07-24 stsp goto done;
8466 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8467 fa51e947 2020-03-27 stsp if (error) {
8468 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8469 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8470 0ebf8283 2019-07-24 stsp goto done;
8473 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8475 0ebf8283 2019-07-24 stsp if (error != NULL)
8476 0ebf8283 2019-07-24 stsp goto done;
8478 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8479 0ebf8283 2019-07-24 stsp if (error)
8480 0ebf8283 2019-07-24 stsp goto done;
8481 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8482 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8483 0ebf8283 2019-07-24 stsp goto done;
8486 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8487 0ebf8283 2019-07-24 stsp if (error)
8488 0ebf8283 2019-07-24 stsp goto done;
8490 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8491 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8492 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8493 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8494 083957f4 2020-02-24 stsp "before the -m option can be used");
8495 083957f4 2020-02-24 stsp goto done;
8498 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8499 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8500 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8501 3e3a69f1 2019-07-25 stsp worktree, repo);
8502 0ebf8283 2019-07-24 stsp if (error)
8503 0ebf8283 2019-07-24 stsp goto done;
8504 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8505 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8506 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8507 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8508 0ebf8283 2019-07-24 stsp if (error)
8509 0ebf8283 2019-07-24 stsp goto done;
8510 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8511 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8512 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8513 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8514 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8515 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8516 0ebf8283 2019-07-24 stsp goto done;
8519 0ebf8283 2019-07-24 stsp if (continue_edit) {
8520 0ebf8283 2019-07-24 stsp char *path;
8522 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8523 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8524 0ebf8283 2019-07-24 stsp goto done;
8527 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8528 0ebf8283 2019-07-24 stsp if (error)
8529 0ebf8283 2019-07-24 stsp goto done;
8531 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8532 0ebf8283 2019-07-24 stsp free(path);
8533 0ebf8283 2019-07-24 stsp if (error)
8534 0ebf8283 2019-07-24 stsp goto done;
8536 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8537 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8538 3e3a69f1 2019-07-25 stsp worktree, repo);
8539 0ebf8283 2019-07-24 stsp if (error)
8540 0ebf8283 2019-07-24 stsp goto done;
8542 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8543 0ebf8283 2019-07-24 stsp if (error)
8544 0ebf8283 2019-07-24 stsp goto done;
8546 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8547 0ebf8283 2019-07-24 stsp head_commit_id);
8548 0ebf8283 2019-07-24 stsp if (error)
8549 0ebf8283 2019-07-24 stsp goto done;
8550 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8551 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8552 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8553 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8554 3aac7cf7 2019-07-25 stsp goto done;
8556 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8557 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8558 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8559 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8560 0ebf8283 2019-07-24 stsp commit = NULL;
8561 0ebf8283 2019-07-24 stsp if (error)
8562 0ebf8283 2019-07-24 stsp goto done;
8564 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8565 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8566 0ebf8283 2019-07-24 stsp goto done;
8569 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8570 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8571 0ebf8283 2019-07-24 stsp if (error != NULL)
8572 0ebf8283 2019-07-24 stsp goto done;
8574 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8575 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8576 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8577 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8578 c7d20a3f 2019-07-30 stsp goto done;
8581 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8582 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8583 bfce7f83 2019-07-27 stsp branch = NULL;
8584 0ebf8283 2019-07-24 stsp if (error)
8585 0ebf8283 2019-07-24 stsp goto done;
8587 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8588 0ebf8283 2019-07-24 stsp head_commit_id);
8589 0ebf8283 2019-07-24 stsp if (error)
8590 0ebf8283 2019-07-24 stsp goto done;
8591 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8592 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8593 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8594 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8595 3aac7cf7 2019-07-25 stsp goto done;
8597 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8598 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8599 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8600 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8601 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8602 0ebf8283 2019-07-24 stsp commit = NULL;
8603 0ebf8283 2019-07-24 stsp if (error)
8604 0ebf8283 2019-07-24 stsp goto done;
8606 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8607 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8608 c5996fff 2020-01-29 stsp goto done;
8611 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8612 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8613 bfce7f83 2019-07-27 stsp if (error)
8614 bfce7f83 2019-07-27 stsp goto done;
8616 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8617 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8618 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8619 6ba2bea2 2019-07-27 stsp if (error) {
8620 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8621 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8622 9627c110 2020-04-18 stsp update_progress, &upa);
8623 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8624 0ebf8283 2019-07-24 stsp goto done;
8627 514f2ffe 2020-01-29 stsp const char *branch_name;
8628 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8629 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8630 514f2ffe 2020-01-29 stsp branch_name += 11;
8631 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8632 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8633 6ba2bea2 2019-07-27 stsp if (error) {
8634 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8635 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8636 9627c110 2020-04-18 stsp update_progress, &upa);
8637 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8638 0ebf8283 2019-07-24 stsp goto done;
8643 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8645 6ba2bea2 2019-07-27 stsp if (error) {
8646 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8647 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8648 9627c110 2020-04-18 stsp update_progress, &upa);
8649 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8650 0ebf8283 2019-07-24 stsp goto done;
8655 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8656 4ec14e60 2019-08-28 hiltjo if (error)
8657 0ebf8283 2019-07-24 stsp goto done;
8659 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8660 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8661 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8662 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8665 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8666 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8667 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8668 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8670 ab20a43a 2020-01-29 stsp if (error)
8671 ab20a43a 2020-01-29 stsp goto done;
8673 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8674 ab20a43a 2020-01-29 stsp int have_changes = 0;
8676 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8677 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8678 ab20a43a 2020-01-29 stsp if (error)
8679 ab20a43a 2020-01-29 stsp goto done;
8680 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8681 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8682 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8683 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8684 ab20a43a 2020-01-29 stsp if (error) {
8685 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8686 ab20a43a 2020-01-29 stsp goto done;
8687 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8688 ab20a43a 2020-01-29 stsp goto done;
8690 ab20a43a 2020-01-29 stsp if (have_changes) {
8691 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8692 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8693 ab20a43a 2020-01-29 stsp if (error)
8694 ab20a43a 2020-01-29 stsp goto done;
8696 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8697 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8698 ab20a43a 2020-01-29 stsp if (error)
8699 ab20a43a 2020-01-29 stsp goto done;
8700 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8701 ab20a43a 2020-01-29 stsp hle, NULL);
8702 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8703 ab20a43a 2020-01-29 stsp commit = NULL;
8704 ab20a43a 2020-01-29 stsp if (error)
8705 ab20a43a 2020-01-29 stsp goto done;
8711 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8712 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8713 0ebf8283 2019-07-24 stsp if (error)
8714 0ebf8283 2019-07-24 stsp goto done;
8718 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8719 0ebf8283 2019-07-24 stsp hle->commit_id);
8720 0ebf8283 2019-07-24 stsp if (error)
8721 0ebf8283 2019-07-24 stsp goto done;
8722 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8723 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8725 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8726 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8727 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8728 0ebf8283 2019-07-24 stsp if (error)
8729 0ebf8283 2019-07-24 stsp goto done;
8730 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8731 0ebf8283 2019-07-24 stsp commit = NULL;
8733 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8734 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8735 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8737 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8738 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8740 a0ea4fc0 2020-02-28 stsp if (error)
8741 a0ea4fc0 2020-02-28 stsp goto done;
8742 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8746 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8747 0ebf8283 2019-07-24 stsp char *id_str;
8748 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8749 0ebf8283 2019-07-24 stsp if (error)
8750 0ebf8283 2019-07-24 stsp goto done;
8751 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8753 0ebf8283 2019-07-24 stsp free(id_str);
8754 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8755 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8756 3e3a69f1 2019-07-25 stsp fileindex);
8757 0ebf8283 2019-07-24 stsp goto done;
8760 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8761 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8762 0ebf8283 2019-07-24 stsp if (error)
8763 0ebf8283 2019-07-24 stsp goto done;
8767 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8768 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8769 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8770 0ebf8283 2019-07-24 stsp if (error)
8771 0ebf8283 2019-07-24 stsp goto done;
8774 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8775 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8776 0ebf8283 2019-07-24 stsp if (error)
8777 0ebf8283 2019-07-24 stsp goto done;
8778 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8779 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8781 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8782 3e3a69f1 2019-07-25 stsp branch, repo);
8784 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8785 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8786 0ebf8283 2019-07-24 stsp free(head_commit_id);
8787 0ebf8283 2019-07-24 stsp free(base_commit_id);
8788 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8789 0ebf8283 2019-07-24 stsp if (commit)
8790 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8791 0ebf8283 2019-07-24 stsp if (branch)
8792 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8793 0ebf8283 2019-07-24 stsp if (tmp_branch)
8794 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8795 0ebf8283 2019-07-24 stsp if (worktree)
8796 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8798 2822a352 2019-10-15 stsp got_repo_close(repo);
8799 2822a352 2019-10-15 stsp return error;
8802 2822a352 2019-10-15 stsp __dead static void
8803 2822a352 2019-10-15 stsp usage_integrate(void)
8805 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8809 2822a352 2019-10-15 stsp static const struct got_error *
8810 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8812 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8813 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8814 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8815 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8816 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8817 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8818 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8819 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8821 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8823 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8824 2822a352 2019-10-15 stsp switch (ch) {
8826 2822a352 2019-10-15 stsp usage_integrate();
8827 2822a352 2019-10-15 stsp /* NOTREACHED */
8831 2822a352 2019-10-15 stsp argc -= optind;
8832 2822a352 2019-10-15 stsp argv += optind;
8834 2822a352 2019-10-15 stsp if (argc != 1)
8835 2822a352 2019-10-15 stsp usage_integrate();
8836 2822a352 2019-10-15 stsp branch_arg = argv[0];
8838 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8839 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8840 2822a352 2019-10-15 stsp err(1, "pledge");
8842 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8843 2822a352 2019-10-15 stsp if (cwd == NULL) {
8844 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8845 2822a352 2019-10-15 stsp goto done;
8848 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8849 fa51e947 2020-03-27 stsp if (error) {
8850 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8851 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8853 2822a352 2019-10-15 stsp goto done;
8856 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8857 2822a352 2019-10-15 stsp if (error)
8858 2822a352 2019-10-15 stsp goto done;
8860 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8862 2822a352 2019-10-15 stsp if (error != NULL)
8863 2822a352 2019-10-15 stsp goto done;
8865 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8866 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8867 2822a352 2019-10-15 stsp if (error)
8868 2822a352 2019-10-15 stsp goto done;
8870 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8871 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8872 2822a352 2019-10-15 stsp goto done;
8875 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8876 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8877 2822a352 2019-10-15 stsp if (error)
8878 2822a352 2019-10-15 stsp goto done;
8880 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8881 2822a352 2019-10-15 stsp if (refname == NULL) {
8882 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8883 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8884 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8885 2822a352 2019-10-15 stsp goto done;
8887 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8888 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8889 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8890 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8891 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8892 2822a352 2019-10-15 stsp goto done;
8895 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8896 2822a352 2019-10-15 stsp if (error)
8897 2822a352 2019-10-15 stsp goto done;
8899 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8900 2822a352 2019-10-15 stsp if (error)
8901 2822a352 2019-10-15 stsp goto done;
8903 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8904 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8905 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8906 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8907 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8908 2822a352 2019-10-15 stsp goto done;
8911 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8912 2822a352 2019-10-15 stsp if (error) {
8913 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8914 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8915 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8916 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8917 2822a352 2019-10-15 stsp goto done;
8920 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8921 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8922 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
8923 2822a352 2019-10-15 stsp check_cancelled, NULL);
8924 2822a352 2019-10-15 stsp if (error)
8925 2822a352 2019-10-15 stsp goto done;
8927 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8928 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8931 715dc77e 2019-08-03 stsp got_repo_close(repo);
8932 2822a352 2019-10-15 stsp if (worktree)
8933 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8934 2822a352 2019-10-15 stsp free(cwd);
8935 2822a352 2019-10-15 stsp free(base_commit_id);
8936 2822a352 2019-10-15 stsp free(commit_id);
8937 2822a352 2019-10-15 stsp free(refname);
8938 2822a352 2019-10-15 stsp free(base_refname);
8939 715dc77e 2019-08-03 stsp return error;
8942 715dc77e 2019-08-03 stsp __dead static void
8943 715dc77e 2019-08-03 stsp usage_stage(void)
8945 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8946 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
8947 715dc77e 2019-08-03 stsp getprogname());
8951 715dc77e 2019-08-03 stsp static const struct got_error *
8952 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8953 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8954 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8955 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8957 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8958 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8960 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8961 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8962 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8963 408b4ebc 2019-08-03 stsp return NULL;
8965 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8966 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8967 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8969 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8971 408b4ebc 2019-08-03 stsp return err;
8973 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8974 408b4ebc 2019-08-03 stsp free(id_str);
8975 dc424a06 2019-08-07 stsp return NULL;
8978 dc424a06 2019-08-07 stsp static const struct got_error *
8979 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8981 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8982 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8983 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8984 715dc77e 2019-08-03 stsp char *cwd = NULL;
8985 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8986 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8987 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8988 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8989 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8990 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8992 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8994 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8995 715dc77e 2019-08-03 stsp switch (ch) {
8997 408b4ebc 2019-08-03 stsp list_stage = 1;
9000 dc424a06 2019-08-07 stsp pflag = 1;
9003 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9006 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9009 715dc77e 2019-08-03 stsp usage_stage();
9010 715dc77e 2019-08-03 stsp /* NOTREACHED */
9014 715dc77e 2019-08-03 stsp argc -= optind;
9015 715dc77e 2019-08-03 stsp argv += optind;
9017 715dc77e 2019-08-03 stsp #ifndef PROFILE
9018 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9019 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9020 715dc77e 2019-08-03 stsp err(1, "pledge");
9022 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9023 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9024 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9025 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9027 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9028 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9029 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9030 715dc77e 2019-08-03 stsp goto done;
9033 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9034 fa51e947 2020-03-27 stsp if (error) {
9035 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9036 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9037 715dc77e 2019-08-03 stsp goto done;
9040 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9042 715dc77e 2019-08-03 stsp if (error != NULL)
9043 715dc77e 2019-08-03 stsp goto done;
9045 dc424a06 2019-08-07 stsp if (patch_script_path) {
9046 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9047 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9048 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9049 dc424a06 2019-08-07 stsp patch_script_path);
9050 dc424a06 2019-08-07 stsp goto done;
9053 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9054 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9055 715dc77e 2019-08-03 stsp if (error)
9056 715dc77e 2019-08-03 stsp goto done;
9058 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9059 6d022e97 2019-08-04 stsp if (error)
9060 6d022e97 2019-08-04 stsp goto done;
9062 6d022e97 2019-08-04 stsp if (list_stage)
9063 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9064 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9066 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9067 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9068 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9069 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9070 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9071 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9074 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9075 2e1f37b0 2019-08-08 stsp error == NULL)
9076 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9078 ad493afc 2019-08-03 stsp got_repo_close(repo);
9079 ad493afc 2019-08-03 stsp if (worktree)
9080 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9081 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9082 ad493afc 2019-08-03 stsp free((char *)pe->path);
9083 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9084 ad493afc 2019-08-03 stsp free(cwd);
9085 ad493afc 2019-08-03 stsp return error;
9088 ad493afc 2019-08-03 stsp __dead static void
9089 ad493afc 2019-08-03 stsp usage_unstage(void)
9091 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9092 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9093 ad493afc 2019-08-03 stsp getprogname());
9098 ad493afc 2019-08-03 stsp static const struct got_error *
9099 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9101 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9102 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9103 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9104 ad493afc 2019-08-03 stsp char *cwd = NULL;
9105 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9106 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9107 9627c110 2020-04-18 stsp int ch, pflag = 0;
9108 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9109 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9110 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9111 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9113 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9115 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9116 ad493afc 2019-08-03 stsp switch (ch) {
9118 2e1f37b0 2019-08-08 stsp pflag = 1;
9121 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9124 ad493afc 2019-08-03 stsp usage_unstage();
9125 ad493afc 2019-08-03 stsp /* NOTREACHED */
9129 ad493afc 2019-08-03 stsp argc -= optind;
9130 ad493afc 2019-08-03 stsp argv += optind;
9132 ad493afc 2019-08-03 stsp #ifndef PROFILE
9133 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9134 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9135 ad493afc 2019-08-03 stsp err(1, "pledge");
9137 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9138 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9140 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9141 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9142 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9143 ad493afc 2019-08-03 stsp goto done;
9146 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9147 fa51e947 2020-03-27 stsp if (error) {
9148 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9149 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9150 ad493afc 2019-08-03 stsp goto done;
9153 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9155 ad493afc 2019-08-03 stsp if (error != NULL)
9156 ad493afc 2019-08-03 stsp goto done;
9158 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9159 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9160 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9161 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9162 2e1f37b0 2019-08-08 stsp patch_script_path);
9163 2e1f37b0 2019-08-08 stsp goto done;
9167 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9168 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9169 ad493afc 2019-08-03 stsp if (error)
9170 ad493afc 2019-08-03 stsp goto done;
9172 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9173 ad493afc 2019-08-03 stsp if (error)
9174 ad493afc 2019-08-03 stsp goto done;
9176 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9177 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9178 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9179 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9180 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9181 9627c110 2020-04-18 stsp if (!error)
9182 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9184 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9185 2e1f37b0 2019-08-08 stsp error == NULL)
9186 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9188 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9189 715dc77e 2019-08-03 stsp if (worktree)
9190 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9191 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9192 715dc77e 2019-08-03 stsp free((char *)pe->path);
9193 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9194 715dc77e 2019-08-03 stsp free(cwd);
9195 01073a5d 2019-08-22 stsp return error;
9198 01073a5d 2019-08-22 stsp __dead static void
9199 01073a5d 2019-08-22 stsp usage_cat(void)
9201 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9202 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9206 01073a5d 2019-08-22 stsp static const struct got_error *
9207 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9209 01073a5d 2019-08-22 stsp const struct got_error *err;
9210 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9212 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9214 01073a5d 2019-08-22 stsp return err;
9216 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9217 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9218 01073a5d 2019-08-22 stsp return err;
9221 01073a5d 2019-08-22 stsp static const struct got_error *
9222 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9224 01073a5d 2019-08-22 stsp const struct got_error *err;
9225 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9226 56e0773d 2019-11-28 stsp int nentries, i;
9228 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9230 01073a5d 2019-08-22 stsp return err;
9232 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9233 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9234 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9235 01073a5d 2019-08-22 stsp char *id_str;
9236 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9238 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9239 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9242 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9243 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9244 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9245 01073a5d 2019-08-22 stsp free(id_str);
9248 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9249 01073a5d 2019-08-22 stsp return err;
9252 01073a5d 2019-08-22 stsp static const struct got_error *
9253 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9255 01073a5d 2019-08-22 stsp const struct got_error *err;
9256 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9257 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9258 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9259 01073a5d 2019-08-22 stsp char *id_str = NULL;
9260 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9262 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9264 01073a5d 2019-08-22 stsp return err;
9266 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9268 01073a5d 2019-08-22 stsp goto done;
9270 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9271 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9272 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9273 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9274 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9275 01073a5d 2019-08-22 stsp char *pid_str;
9276 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9278 01073a5d 2019-08-22 stsp goto done;
9279 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9280 01073a5d 2019-08-22 stsp free(pid_str);
9282 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9283 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9284 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9286 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9287 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9288 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9290 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9291 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9292 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9294 01073a5d 2019-08-22 stsp free(id_str);
9295 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9296 01073a5d 2019-08-22 stsp return err;
9299 01073a5d 2019-08-22 stsp static const struct got_error *
9300 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9302 01073a5d 2019-08-22 stsp const struct got_error *err;
9303 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9304 01073a5d 2019-08-22 stsp char *id_str = NULL;
9305 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9307 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9309 01073a5d 2019-08-22 stsp return err;
9311 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9313 01073a5d 2019-08-22 stsp goto done;
9315 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9317 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9318 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9319 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9320 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9322 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9323 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9324 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9326 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9327 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9328 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9330 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9331 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9332 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9338 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9339 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9341 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9342 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9343 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9345 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9346 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9347 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9349 01073a5d 2019-08-22 stsp free(id_str);
9350 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9351 01073a5d 2019-08-22 stsp return err;
9354 01073a5d 2019-08-22 stsp static const struct got_error *
9355 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9357 01073a5d 2019-08-22 stsp const struct got_error *error;
9358 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9359 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9360 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9361 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9362 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9363 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9365 01073a5d 2019-08-22 stsp #ifndef PROFILE
9366 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9367 01073a5d 2019-08-22 stsp NULL) == -1)
9368 01073a5d 2019-08-22 stsp err(1, "pledge");
9371 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9372 01073a5d 2019-08-22 stsp switch (ch) {
9374 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9377 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9378 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9379 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9381 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9384 896e9b6f 2019-08-26 stsp force_path = 1;
9387 01073a5d 2019-08-22 stsp usage_cat();
9388 01073a5d 2019-08-22 stsp /* NOTREACHED */
9392 01073a5d 2019-08-22 stsp argc -= optind;
9393 01073a5d 2019-08-22 stsp argv += optind;
9395 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9396 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9397 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9398 01073a5d 2019-08-22 stsp goto done;
9400 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9401 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9402 01073a5d 2019-08-22 stsp goto done;
9403 01073a5d 2019-08-22 stsp if (worktree) {
9404 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9405 01073a5d 2019-08-22 stsp repo_path = strdup(
9406 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9407 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9408 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9409 01073a5d 2019-08-22 stsp goto done;
9414 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9415 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9416 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9417 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9420 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9421 01073a5d 2019-08-22 stsp free(repo_path);
9422 01073a5d 2019-08-22 stsp if (error != NULL)
9423 01073a5d 2019-08-22 stsp goto done;
9425 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9426 896e9b6f 2019-08-26 stsp if (error)
9427 896e9b6f 2019-08-26 stsp goto done;
9429 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9430 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9431 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9432 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9433 01073a5d 2019-08-22 stsp if (error)
9434 01073a5d 2019-08-22 stsp goto done;
9436 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9437 896e9b6f 2019-08-26 stsp if (force_path) {
9438 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9440 896e9b6f 2019-08-26 stsp if (error)
9443 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9444 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9445 896e9b6f 2019-08-26 stsp if (error) {
9446 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9447 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9449 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9450 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9451 896e9b6f 2019-08-26 stsp if (error)
9456 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9457 01073a5d 2019-08-22 stsp if (error)
9460 01073a5d 2019-08-22 stsp switch (obj_type) {
9461 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9462 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9464 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9465 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9467 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9468 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9470 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9471 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9474 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9477 01073a5d 2019-08-22 stsp if (error)
9479 01073a5d 2019-08-22 stsp free(label);
9480 01073a5d 2019-08-22 stsp label = NULL;
9482 01073a5d 2019-08-22 stsp id = NULL;
9485 01073a5d 2019-08-22 stsp free(label);
9487 896e9b6f 2019-08-26 stsp free(commit_id);
9488 01073a5d 2019-08-22 stsp if (worktree)
9489 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9490 01073a5d 2019-08-22 stsp if (repo) {
9491 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9492 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9493 01073a5d 2019-08-22 stsp if (error == NULL)
9494 01073a5d 2019-08-22 stsp error = repo_error;
9496 b2118c49 2020-07-28 stsp return error;
9499 b2118c49 2020-07-28 stsp __dead static void
9500 b2118c49 2020-07-28 stsp usage_info(void)
9502 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9503 b2118c49 2020-07-28 stsp getprogname());
9507 b2118c49 2020-07-28 stsp static const struct got_error *
9508 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9509 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9510 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9512 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9513 b2118c49 2020-07-28 stsp char *id_str = NULL;
9514 b2118c49 2020-07-28 stsp char datebuf[128];
9515 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9516 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9517 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9520 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9521 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9523 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9524 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9525 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9526 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9527 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9530 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9531 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9532 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9533 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9534 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9535 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9536 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9537 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9539 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9541 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9542 b2118c49 2020-07-28 stsp if (tm == NULL)
9543 b2118c49 2020-07-28 stsp return NULL;
9544 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9545 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9546 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9548 b2118c49 2020-07-28 stsp if (blob_id) {
9549 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9551 b2118c49 2020-07-28 stsp return err;
9552 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9553 b2118c49 2020-07-28 stsp free(id_str);
9556 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9557 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9559 b2118c49 2020-07-28 stsp return err;
9560 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9561 b2118c49 2020-07-28 stsp free(id_str);
9564 b2118c49 2020-07-28 stsp if (commit_id) {
9565 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9567 b2118c49 2020-07-28 stsp return err;
9568 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9569 b2118c49 2020-07-28 stsp free(id_str);
9572 b2118c49 2020-07-28 stsp return NULL;
9575 b2118c49 2020-07-28 stsp static const struct got_error *
9576 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9578 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9579 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9580 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9581 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9582 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9583 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9584 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9586 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9588 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9589 b2118c49 2020-07-28 stsp switch (ch) {
9591 b2118c49 2020-07-28 stsp usage_info();
9592 b2118c49 2020-07-28 stsp /* NOTREACHED */
9596 b2118c49 2020-07-28 stsp argc -= optind;
9597 b2118c49 2020-07-28 stsp argv += optind;
9599 b2118c49 2020-07-28 stsp #ifndef PROFILE
9600 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9601 b2118c49 2020-07-28 stsp NULL) == -1)
9602 b2118c49 2020-07-28 stsp err(1, "pledge");
9604 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9605 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9606 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9607 b2118c49 2020-07-28 stsp goto done;
9610 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9611 b2118c49 2020-07-28 stsp if (error) {
9612 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9613 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9614 b2118c49 2020-07-28 stsp goto done;
9617 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9618 b2118c49 2020-07-28 stsp if (error)
9619 b2118c49 2020-07-28 stsp goto done;
9621 b2118c49 2020-07-28 stsp if (argc >= 1) {
9622 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9623 b2118c49 2020-07-28 stsp worktree);
9624 b2118c49 2020-07-28 stsp if (error)
9625 b2118c49 2020-07-28 stsp goto done;
9626 b2118c49 2020-07-28 stsp show_files = 1;
9629 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9630 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9631 b2118c49 2020-07-28 stsp if (error)
9632 b2118c49 2020-07-28 stsp goto done;
9634 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9635 b2118c49 2020-07-28 stsp if (error)
9636 b2118c49 2020-07-28 stsp goto done;
9638 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9639 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9640 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9641 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9642 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9643 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9644 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9645 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9647 b2118c49 2020-07-28 stsp if (show_files) {
9648 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9649 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9650 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9653 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9654 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9656 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9657 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9659 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9660 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9661 b2118c49 2020-07-28 stsp if (error)
9662 b2118c49 2020-07-28 stsp goto done;
9663 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9664 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9665 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9671 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9672 b2118c49 2020-07-28 stsp free((char *)pe->path);
9673 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9674 b2118c49 2020-07-28 stsp free(cwd);
9675 b2118c49 2020-07-28 stsp free(id_str);
9676 b2118c49 2020-07-28 stsp free(uuidstr);
9677 0ebf8283 2019-07-24 stsp return error;