Blame


1 5c860e29 2018-03-12 stsp /*
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>
5 5c860e29 2018-03-12 stsp *
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.
9 5c860e29 2018-03-12 stsp *
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.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
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>
24 f42b1b34 2018-03-12 stsp
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>
42 5c860e29 2018-03-12 stsp
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"
58 5c860e29 2018-03-12 stsp
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 5c860e29 2018-03-12 stsp #endif
62 99437157 2018-11-11 stsp
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;
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static void
67 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp {
69 99437157 2018-11-11 stsp sigint_received = 1;
70 99437157 2018-11-11 stsp }
71 99437157 2018-11-11 stsp
72 99437157 2018-11-11 stsp static void
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp {
75 99437157 2018-11-11 stsp sigpipe_received = 1;
76 99437157 2018-11-11 stsp }
77 5c860e29 2018-03-12 stsp
78 99437157 2018-11-11 stsp
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;
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
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);
114 5c860e29 2018-03-12 stsp
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 *[]);
142 5c860e29 2018-03-12 stsp
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, "" },
171 5c860e29 2018-03-12 stsp };
172 ce5b7c56 2019-07-09 stsp
173 ce5b7c56 2019-07-09 stsp static void
174 ce5b7c56 2019-07-09 stsp list_commands(void)
175 ce5b7c56 2019-07-09 stsp {
176 ce5b7c56 2019-07-09 stsp int i;
177 ce5b7c56 2019-07-09 stsp
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);
182 ce5b7c56 2019-07-09 stsp }
183 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
184 ce5b7c56 2019-07-09 stsp }
185 5c860e29 2018-03-12 stsp
186 5c860e29 2018-03-12 stsp int
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
188 5c860e29 2018-03-12 stsp {
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
191 5c860e29 2018-03-12 stsp int ch;
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}
196 83cd27f8 2020-01-13 stsp };
197 5c860e29 2018-03-12 stsp
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
199 5c860e29 2018-03-12 stsp
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
202 1b6b95a8 2018-03-12 stsp case 'h':
203 1b6b95a8 2018-03-12 stsp hflag = 1;
204 53ccebc2 2019-07-30 stsp break;
205 53ccebc2 2019-07-30 stsp case 'V':
206 53ccebc2 2019-07-30 stsp Vflag = 1;
207 1b6b95a8 2018-03-12 stsp break;
208 5c860e29 2018-03-12 stsp default:
209 ce5b7c56 2019-07-09 stsp usage(hflag);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
211 5c860e29 2018-03-12 stsp }
212 5c860e29 2018-03-12 stsp }
213 5c860e29 2018-03-12 stsp
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 1e70621d 2018-03-27 stsp optind = 0;
217 53ccebc2 2019-07-30 stsp
218 53ccebc2 2019-07-30 stsp if (Vflag) {
219 53ccebc2 2019-07-30 stsp got_version_print_str();
220 53ccebc2 2019-07-30 stsp return 1;
221 53ccebc2 2019-07-30 stsp }
222 5c860e29 2018-03-12 stsp
223 5c860e29 2018-03-12 stsp if (argc <= 0)
224 ce5b7c56 2019-07-09 stsp usage(hflag);
225 5c860e29 2018-03-12 stsp
226 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
227 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
228 99437157 2018-11-11 stsp
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;
231 d7d4f210 2018-03-12 stsp
232 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
233 5c860e29 2018-03-12 stsp
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)
236 5c860e29 2018-03-12 stsp continue;
237 5c860e29 2018-03-12 stsp
238 1b6b95a8 2018-03-12 stsp if (hflag)
239 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
240 1b6b95a8 2018-03-12 stsp
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);
249 d7d4f210 2018-03-12 stsp return 1;
250 d7d4f210 2018-03-12 stsp }
251 d7d4f210 2018-03-12 stsp
252 d7d4f210 2018-03-12 stsp return 0;
253 5c860e29 2018-03-12 stsp }
254 5c860e29 2018-03-12 stsp
255 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 ce5b7c56 2019-07-09 stsp list_commands();
257 5c860e29 2018-03-12 stsp return 1;
258 5c860e29 2018-03-12 stsp }
259 5c860e29 2018-03-12 stsp
260 4ed7e80c 2018-05-20 stsp __dead static void
261 ce5b7c56 2019-07-09 stsp usage(int hflag)
262 5c860e29 2018-03-12 stsp {
263 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 53ccebc2 2019-07-30 stsp getprogname());
265 ce5b7c56 2019-07-09 stsp if (hflag)
266 ce5b7c56 2019-07-09 stsp list_commands();
267 5c860e29 2018-03-12 stsp exit(1);
268 5c860e29 2018-03-12 stsp }
269 5c860e29 2018-03-12 stsp
270 0266afb7 2019-01-04 stsp static const struct got_error *
271 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
272 e2ba3d07 2019-05-13 stsp {
273 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
274 e2ba3d07 2019-05-13 stsp const char *editor;
275 8920fa04 2019-08-18 stsp
276 8920fa04 2019-08-18 stsp *abspath = NULL;
277 e2ba3d07 2019-05-13 stsp
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");
281 e2ba3d07 2019-05-13 stsp
282 0ee7065d 2019-05-13 stsp if (editor) {
283 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
284 0ee7065d 2019-05-13 stsp if (err)
285 0ee7065d 2019-05-13 stsp return err;
286 0ee7065d 2019-05-13 stsp }
287 e2ba3d07 2019-05-13 stsp
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");
292 0ee7065d 2019-05-13 stsp }
293 0ee7065d 2019-05-13 stsp
294 e2ba3d07 2019-05-13 stsp return NULL;
295 e2ba3d07 2019-05-13 stsp }
296 e2ba3d07 2019-05-13 stsp
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)
300 0266afb7 2019-01-04 stsp {
301 163ce85a 2019-05-13 stsp const struct got_error *err;
302 0266afb7 2019-01-04 stsp
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");
306 37c06ea4 2019-07-15 stsp #endif
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);
309 0266afb7 2019-01-04 stsp
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);
312 0266afb7 2019-01-04 stsp
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);
315 0266afb7 2019-01-04 stsp
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;
319 0266afb7 2019-01-04 stsp
320 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp return NULL;
324 2c7829a4 2019-06-17 stsp }
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp __dead static void
327 2c7829a4 2019-06-17 stsp usage_init(void)
328 2c7829a4 2019-06-17 stsp {
329 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
330 2c7829a4 2019-06-17 stsp exit(1);
331 2c7829a4 2019-06-17 stsp }
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp static const struct got_error *
334 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
335 2c7829a4 2019-06-17 stsp {
336 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
337 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
338 2c7829a4 2019-06-17 stsp int ch;
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 2c7829a4 2019-06-17 stsp switch (ch) {
342 2c7829a4 2019-06-17 stsp default:
343 2c7829a4 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp /* NOTREACHED */
345 2c7829a4 2019-06-17 stsp }
346 2c7829a4 2019-06-17 stsp }
347 2c7829a4 2019-06-17 stsp
348 2c7829a4 2019-06-17 stsp argc -= optind;
349 2c7829a4 2019-06-17 stsp argv += optind;
350 2c7829a4 2019-06-17 stsp
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");
354 2c7829a4 2019-06-17 stsp #endif
355 2c7829a4 2019-06-17 stsp if (argc != 1)
356 bc20e173 2019-06-17 stsp usage_init();
357 2c7829a4 2019-06-17 stsp
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");
361 2c7829a4 2019-06-17 stsp
362 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
363 2c7829a4 2019-06-17 stsp
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))
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
370 2c7829a4 2019-06-17 stsp if (error)
371 2c7829a4 2019-06-17 stsp goto done;
372 2c7829a4 2019-06-17 stsp
373 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
374 3ce1b845 2019-07-15 stsp done:
375 3ce1b845 2019-07-15 stsp free(repo_path);
376 3ce1b845 2019-07-15 stsp return error;
377 3ce1b845 2019-07-15 stsp }
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp __dead static void
380 3ce1b845 2019-07-15 stsp usage_import(void)
381 3ce1b845 2019-07-15 stsp {
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());
384 3ce1b845 2019-07-15 stsp exit(1);
385 3ce1b845 2019-07-15 stsp }
386 3ce1b845 2019-07-15 stsp
387 3ce1b845 2019-07-15 stsp int
388 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
389 3ce1b845 2019-07-15 stsp {
390 3ce1b845 2019-07-15 stsp pid_t pid;
391 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
392 3ce1b845 2019-07-15 stsp int st = -1;
393 3ce1b845 2019-07-15 stsp
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);
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
399 3ce1b845 2019-07-15 stsp case -1:
400 3ce1b845 2019-07-15 stsp goto doneediting;
401 3ce1b845 2019-07-15 stsp case 0:
402 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
403 3ce1b845 2019-07-15 stsp _exit(127);
404 3ce1b845 2019-07-15 stsp }
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
407 3ce1b845 2019-07-15 stsp if (errno != EINTR)
408 3ce1b845 2019-07-15 stsp break;
409 3ce1b845 2019-07-15 stsp
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);
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
416 3ce1b845 2019-07-15 stsp errno = EINTR;
417 3ce1b845 2019-07-15 stsp return -1;
418 3ce1b845 2019-07-15 stsp }
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
421 3ce1b845 2019-07-15 stsp }
422 3ce1b845 2019-07-15 stsp
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)
426 3ce1b845 2019-07-15 stsp {
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;
430 3ce1b845 2019-07-15 stsp FILE *fp;
431 3ce1b845 2019-07-15 stsp int content_changed = 0;
432 3ce1b845 2019-07-15 stsp size_t len;
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp *logmsg = NULL;
435 3ce1b845 2019-07-15 stsp
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);
438 3ce1b845 2019-07-15 stsp
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");
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
444 3ce1b845 2019-07-15 stsp
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");
448 3ce1b845 2019-07-15 stsp
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';
453 3ce1b845 2019-07-15 stsp len = 0;
454 3ce1b845 2019-07-15 stsp
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");
458 3ce1b845 2019-07-15 stsp goto done;
459 3ce1b845 2019-07-15 stsp }
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);
466 3ce1b845 2019-07-15 stsp }
467 3ce1b845 2019-07-15 stsp fclose(fp);
468 3ce1b845 2019-07-15 stsp
469 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
471 3ce1b845 2019-07-15 stsp len--;
472 3ce1b845 2019-07-15 stsp }
473 3ce1b845 2019-07-15 stsp
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");
477 3ce1b845 2019-07-15 stsp done:
478 3ce1b845 2019-07-15 stsp if (err) {
479 3ce1b845 2019-07-15 stsp free(*logmsg);
480 3ce1b845 2019-07-15 stsp *logmsg = NULL;
481 3ce1b845 2019-07-15 stsp }
482 3ce1b845 2019-07-15 stsp return err;
483 3ce1b845 2019-07-15 stsp }
484 3ce1b845 2019-07-15 stsp
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)
488 3ce1b845 2019-07-15 stsp {
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;
493 3ce1b845 2019-07-15 stsp
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");
499 3ce1b845 2019-07-15 stsp
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");
502 3ce1b845 2019-07-15 stsp if (err)
503 3ce1b845 2019-07-15 stsp goto done;
504 3ce1b845 2019-07-15 stsp
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);
507 97972933 2020-09-11 stsp goto done;
508 97972933 2020-09-11 stsp }
509 3ce1b845 2019-07-15 stsp
510 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
511 3ce1b845 2019-07-15 stsp done:
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 59f86c76 2020-09-11 stsp if (err) {
516 59f86c76 2020-09-11 stsp free(*logmsg_path);
517 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
518 59f86c76 2020-09-11 stsp }
519 3ce1b845 2019-07-15 stsp return err;
520 3ce1b845 2019-07-15 stsp }
521 3ce1b845 2019-07-15 stsp
522 3ce1b845 2019-07-15 stsp static const struct got_error *
523 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
524 3ce1b845 2019-07-15 stsp {
525 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
526 3ce1b845 2019-07-15 stsp return NULL;
527 3ce1b845 2019-07-15 stsp }
528 3ce1b845 2019-07-15 stsp
529 3ce1b845 2019-07-15 stsp static const struct got_error *
530 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
531 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
532 84792843 2019-08-09 stsp {
533 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
534 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
535 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
536 84792843 2019-08-09 stsp
537 84792843 2019-08-09 stsp *author = NULL;
538 aba9c984 2019-09-08 stsp
539 50b0790e 2020-09-11 stsp if (worktree)
540 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
541 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
542 50b0790e 2020-09-11 stsp
543 50b0790e 2020-09-11 stsp /*
544 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
545 50b0790e 2020-09-11 stsp * significant to least significant:
546 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
547 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
548 50b0790e 2020-09-11 stsp * 3) repository's git config file
549 50b0790e 2020-09-11 stsp * 4) environment variables
550 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
551 50b0790e 2020-09-11 stsp */
552 50b0790e 2020-09-11 stsp
553 50b0790e 2020-09-11 stsp if (worktree_conf)
554 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
555 50b0790e 2020-09-11 stsp if (got_author == NULL)
556 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
557 84792843 2019-08-09 stsp if (got_author == NULL) {
558 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
559 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
560 c9956ddf 2019-09-08 stsp if (name && email) {
561 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
562 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
563 c9956ddf 2019-09-08 stsp return NULL;
564 c9956ddf 2019-09-08 stsp }
565 257add31 2020-09-09 stsp
566 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
567 257add31 2020-09-09 stsp if (got_author == NULL) {
568 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
569 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
570 257add31 2020-09-09 stsp repo);
571 257add31 2020-09-09 stsp if (name && email) {
572 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
573 257add31 2020-09-09 stsp == -1)
574 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
575 257add31 2020-09-09 stsp return NULL;
576 257add31 2020-09-09 stsp }
577 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
578 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
579 257add31 2020-09-09 stsp }
580 84792843 2019-08-09 stsp }
581 84792843 2019-08-09 stsp
582 aba9c984 2019-09-08 stsp *author = strdup(got_author);
583 aba9c984 2019-09-08 stsp if (*author == NULL)
584 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
585 84792843 2019-08-09 stsp
586 84792843 2019-08-09 stsp /*
587 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
588 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
589 84792843 2019-08-09 stsp */
590 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
591 84792843 2019-08-09 stsp got_author++;
592 aba9c984 2019-09-08 stsp if (*got_author != '<') {
593 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
594 aba9c984 2019-09-08 stsp goto done;
595 aba9c984 2019-09-08 stsp }
596 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
597 84792843 2019-08-09 stsp got_author++;
598 aba9c984 2019-09-08 stsp if (*got_author != '@') {
599 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
600 aba9c984 2019-09-08 stsp goto done;
601 aba9c984 2019-09-08 stsp }
602 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
603 84792843 2019-08-09 stsp got_author++;
604 84792843 2019-08-09 stsp if (*got_author != '>')
605 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
606 aba9c984 2019-09-08 stsp done:
607 aba9c984 2019-09-08 stsp if (err) {
608 aba9c984 2019-09-08 stsp free(*author);
609 aba9c984 2019-09-08 stsp *author = NULL;
610 aba9c984 2019-09-08 stsp }
611 aba9c984 2019-09-08 stsp return err;
612 c9956ddf 2019-09-08 stsp }
613 c9956ddf 2019-09-08 stsp
614 c9956ddf 2019-09-08 stsp static const struct got_error *
615 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
616 c9956ddf 2019-09-08 stsp {
617 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
618 c9956ddf 2019-09-08 stsp
619 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
620 c9956ddf 2019-09-08 stsp if (homedir) {
621 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
623 c9956ddf 2019-09-08 stsp
624 c9956ddf 2019-09-08 stsp }
625 c9956ddf 2019-09-08 stsp return NULL;
626 84792843 2019-08-09 stsp }
627 84792843 2019-08-09 stsp
628 84792843 2019-08-09 stsp static const struct got_error *
629 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
630 3ce1b845 2019-07-15 stsp {
631 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
632 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
635 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
637 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
639 3ce1b845 2019-07-15 stsp int ch;
640 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
641 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
642 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
643 3ce1b845 2019-07-15 stsp
644 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
645 3ce1b845 2019-07-15 stsp
646 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
647 3ce1b845 2019-07-15 stsp switch (ch) {
648 3ce1b845 2019-07-15 stsp case 'b':
649 3ce1b845 2019-07-15 stsp branch_name = optarg;
650 3ce1b845 2019-07-15 stsp break;
651 3ce1b845 2019-07-15 stsp case 'm':
652 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
653 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
654 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
655 3ce1b845 2019-07-15 stsp goto done;
656 3ce1b845 2019-07-15 stsp }
657 3ce1b845 2019-07-15 stsp break;
658 3ce1b845 2019-07-15 stsp case 'r':
659 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
660 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
661 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
662 9ba1d308 2019-10-21 stsp optarg);
663 3ce1b845 2019-07-15 stsp goto done;
664 3ce1b845 2019-07-15 stsp }
665 3ce1b845 2019-07-15 stsp break;
666 3ce1b845 2019-07-15 stsp case 'I':
667 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
668 3ce1b845 2019-07-15 stsp break;
669 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
670 3ce1b845 2019-07-15 stsp NULL);
671 3ce1b845 2019-07-15 stsp if (error)
672 3ce1b845 2019-07-15 stsp goto done;
673 3ce1b845 2019-07-15 stsp break;
674 3ce1b845 2019-07-15 stsp default:
675 b2b65d18 2019-08-22 stsp usage_import();
676 3ce1b845 2019-07-15 stsp /* NOTREACHED */
677 3ce1b845 2019-07-15 stsp }
678 3ce1b845 2019-07-15 stsp }
679 3ce1b845 2019-07-15 stsp
680 3ce1b845 2019-07-15 stsp argc -= optind;
681 3ce1b845 2019-07-15 stsp argv += optind;
682 3ce1b845 2019-07-15 stsp
683 3ce1b845 2019-07-15 stsp #ifndef PROFILE
684 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
685 aba9c984 2019-09-08 stsp "unveil",
686 3ce1b845 2019-07-15 stsp NULL) == -1)
687 3ce1b845 2019-07-15 stsp err(1, "pledge");
688 3ce1b845 2019-07-15 stsp #endif
689 3ce1b845 2019-07-15 stsp if (argc != 1)
690 3ce1b845 2019-07-15 stsp usage_import();
691 2c7829a4 2019-06-17 stsp
692 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
693 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
694 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
695 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
696 3ce1b845 2019-07-15 stsp }
697 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
698 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
699 c9956ddf 2019-09-08 stsp if (error)
700 c9956ddf 2019-09-08 stsp goto done;
701 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
702 3ce1b845 2019-07-15 stsp if (error)
703 3ce1b845 2019-07-15 stsp goto done;
704 aba9c984 2019-09-08 stsp
705 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
706 aba9c984 2019-09-08 stsp if (error)
707 aba9c984 2019-09-08 stsp return error;
708 e560b7e0 2019-11-28 stsp
709 e560b7e0 2019-11-28 stsp /*
710 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
711 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
712 e560b7e0 2019-11-28 stsp * an unintended typo.
713 e560b7e0 2019-11-28 stsp */
714 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
715 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
716 3ce1b845 2019-07-15 stsp
717 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
719 3ce1b845 2019-07-15 stsp goto done;
720 3ce1b845 2019-07-15 stsp }
721 3ce1b845 2019-07-15 stsp
722 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
723 3ce1b845 2019-07-15 stsp if (error) {
724 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
725 3ce1b845 2019-07-15 stsp goto done;
726 3ce1b845 2019-07-15 stsp } else {
727 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 3ce1b845 2019-07-15 stsp "import target branch already exists");
729 3ce1b845 2019-07-15 stsp goto done;
730 3ce1b845 2019-07-15 stsp }
731 3ce1b845 2019-07-15 stsp
732 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
733 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
734 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
735 3ce1b845 2019-07-15 stsp goto done;
736 3ce1b845 2019-07-15 stsp }
737 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
738 3ce1b845 2019-07-15 stsp
739 3ce1b845 2019-07-15 stsp /*
740 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
741 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
742 3ce1b845 2019-07-15 stsp */
743 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
744 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
745 3ce1b845 2019-07-15 stsp if (error)
746 3ce1b845 2019-07-15 stsp goto done;
747 8e158b01 2019-09-22 stsp free(logmsg);
748 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
749 ef293bdd 2019-10-21 stsp path_dir, refname);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
752 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
753 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
754 3ce1b845 2019-07-15 stsp goto done;
755 ef293bdd 2019-10-21 stsp }
756 3ce1b845 2019-07-15 stsp }
757 3ce1b845 2019-07-15 stsp
758 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
759 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
760 ef293bdd 2019-10-21 stsp if (logmsg_path)
761 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
762 3ce1b845 2019-07-15 stsp goto done;
763 ef293bdd 2019-10-21 stsp }
764 3ce1b845 2019-07-15 stsp
765 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
766 ef293bdd 2019-10-21 stsp if (error) {
767 ef293bdd 2019-10-21 stsp if (logmsg_path)
768 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
769 ef293bdd 2019-10-21 stsp goto done;
770 ef293bdd 2019-10-21 stsp }
771 ef293bdd 2019-10-21 stsp
772 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
774 ef293bdd 2019-10-21 stsp if (error) {
775 ef293bdd 2019-10-21 stsp if (logmsg_path)
776 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
777 3ce1b845 2019-07-15 stsp goto done;
778 ef293bdd 2019-10-21 stsp }
779 3ce1b845 2019-07-15 stsp
780 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
781 ef293bdd 2019-10-21 stsp if (error) {
782 ef293bdd 2019-10-21 stsp if (logmsg_path)
783 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
784 3ce1b845 2019-07-15 stsp goto done;
785 ef293bdd 2019-10-21 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
788 ef293bdd 2019-10-21 stsp if (error) {
789 ef293bdd 2019-10-21 stsp if (logmsg_path)
790 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
791 3ce1b845 2019-07-15 stsp goto done;
792 ef293bdd 2019-10-21 stsp }
793 3ce1b845 2019-07-15 stsp
794 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
795 ef293bdd 2019-10-21 stsp if (error) {
796 ef293bdd 2019-10-21 stsp if (logmsg_path)
797 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
798 3ce1b845 2019-07-15 stsp goto done;
799 ef293bdd 2019-10-21 stsp }
800 3ce1b845 2019-07-15 stsp
801 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
802 3ce1b845 2019-07-15 stsp if (error) {
803 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
804 ef293bdd 2019-10-21 stsp if (logmsg_path)
805 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
806 3ce1b845 2019-07-15 stsp goto done;
807 ef293bdd 2019-10-21 stsp }
808 3ce1b845 2019-07-15 stsp
809 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
810 3ce1b845 2019-07-15 stsp branch_ref);
811 ef293bdd 2019-10-21 stsp if (error) {
812 ef293bdd 2019-10-21 stsp if (logmsg_path)
813 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
814 3ce1b845 2019-07-15 stsp goto done;
815 ef293bdd 2019-10-21 stsp }
816 3ce1b845 2019-07-15 stsp
817 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
818 ef293bdd 2019-10-21 stsp if (error) {
819 ef293bdd 2019-10-21 stsp if (logmsg_path)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 3ce1b845 2019-07-15 stsp goto done;
822 ef293bdd 2019-10-21 stsp }
823 3ce1b845 2019-07-15 stsp }
824 3ce1b845 2019-07-15 stsp
825 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
826 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
827 2c7829a4 2019-06-17 stsp done:
828 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
829 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
830 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
831 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
833 8e158b01 2019-09-22 stsp free(logmsg);
834 ef293bdd 2019-10-21 stsp free(logmsg_path);
835 2c7829a4 2019-06-17 stsp free(repo_path);
836 3ce1b845 2019-07-15 stsp free(editor);
837 3ce1b845 2019-07-15 stsp free(refname);
838 3ce1b845 2019-07-15 stsp free(new_commit_id);
839 3ce1b845 2019-07-15 stsp free(id_str);
840 aba9c984 2019-09-08 stsp free(author);
841 c9956ddf 2019-09-08 stsp free(gitconfig_path);
842 3ce1b845 2019-07-15 stsp if (branch_ref)
843 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
844 3ce1b845 2019-07-15 stsp if (head_ref)
845 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
846 2c7829a4 2019-06-17 stsp return error;
847 93658fb9 2020-03-18 stsp }
848 93658fb9 2020-03-18 stsp
849 93658fb9 2020-03-18 stsp __dead static void
850 93658fb9 2020-03-18 stsp usage_clone(void)
851 93658fb9 2020-03-18 stsp {
852 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
854 93658fb9 2020-03-18 stsp exit(1);
855 93658fb9 2020-03-18 stsp }
856 892ac3b6 2020-03-18 stsp
857 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
858 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
859 892ac3b6 2020-03-18 stsp int last_p_indexed;
860 892ac3b6 2020-03-18 stsp int last_p_resolved;
861 68999b92 2020-03-18 stsp int verbosity;
862 892ac3b6 2020-03-18 stsp };
863 93658fb9 2020-03-18 stsp
864 93658fb9 2020-03-18 stsp static const struct got_error *
865 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
866 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
867 531c3985 2020-03-18 stsp {
868 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
869 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
870 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
871 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
872 b2409d58 2020-03-18 stsp
873 68999b92 2020-03-18 stsp if (a->verbosity < 0)
874 68999b92 2020-03-18 stsp return NULL;
875 68999b92 2020-03-18 stsp
876 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
877 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
878 892ac3b6 2020-03-18 stsp fflush(stdout);
879 12d1281e 2020-03-19 stsp return NULL;
880 b2409d58 2020-03-18 stsp }
881 b2409d58 2020-03-18 stsp
882 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
883 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
884 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
885 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
886 892ac3b6 2020-03-18 stsp print_size = 1;
887 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
888 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
889 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
890 892ac3b6 2020-03-18 stsp }
891 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
892 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
893 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
894 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
895 892ac3b6 2020-03-18 stsp print_indexed = 1;
896 892ac3b6 2020-03-18 stsp print_size = 1;
897 892ac3b6 2020-03-18 stsp }
898 61cc1a7a 2020-03-18 stsp }
899 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
900 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
901 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
902 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
903 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
904 892ac3b6 2020-03-18 stsp print_resolved = 1;
905 892ac3b6 2020-03-18 stsp print_indexed = 1;
906 892ac3b6 2020-03-18 stsp print_size = 1;
907 892ac3b6 2020-03-18 stsp }
908 61cc1a7a 2020-03-18 stsp }
909 3168e5da 2020-09-10 stsp
910 d2cdc636 2020-03-18 stsp }
911 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
912 892ac3b6 2020-03-18 stsp printf("\r");
913 892ac3b6 2020-03-18 stsp if (print_size)
914 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
915 d715f13e 2020-03-19 stsp if (print_indexed)
916 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
917 d715f13e 2020-03-19 stsp if (print_resolved)
918 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
919 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
920 892ac3b6 2020-03-18 stsp fflush(stdout);
921 892ac3b6 2020-03-18 stsp
922 531c3985 2020-03-18 stsp return NULL;
923 531c3985 2020-03-18 stsp }
924 531c3985 2020-03-18 stsp
925 531c3985 2020-03-18 stsp static const struct got_error *
926 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
927 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
928 4ba14133 2020-03-20 stsp {
929 4ba14133 2020-03-20 stsp const struct got_error *err;
930 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
931 4ba14133 2020-03-20 stsp
932 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
933 4ba14133 2020-03-20 stsp if (err)
934 4ba14133 2020-03-20 stsp return err;
935 4ba14133 2020-03-20 stsp
936 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
937 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
938 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
939 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
940 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
941 6338a6a1 2020-03-21 stsp }
942 4ba14133 2020-03-20 stsp return err;
943 4ba14133 2020-03-20 stsp }
944 4ba14133 2020-03-20 stsp
945 4ba14133 2020-03-20 stsp static const struct got_error *
946 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
947 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
948 41b0de12 2020-03-21 stsp {
949 41b0de12 2020-03-21 stsp const struct got_error *err;
950 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
951 41b0de12 2020-03-21 stsp
952 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
953 41b0de12 2020-03-21 stsp const char *refname = pe->path;
954 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
955 41b0de12 2020-03-21 stsp
956 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
957 41b0de12 2020-03-21 stsp }
958 41b0de12 2020-03-21 stsp
959 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
960 41b0de12 2020-03-21 stsp const char *refname = pe->path;
961 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
962 41b0de12 2020-03-21 stsp char *id_str;
963 41b0de12 2020-03-21 stsp
964 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
965 41b0de12 2020-03-21 stsp if (err)
966 41b0de12 2020-03-21 stsp return err;
967 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
968 41b0de12 2020-03-21 stsp free(id_str);
969 41b0de12 2020-03-21 stsp }
970 41b0de12 2020-03-21 stsp
971 41b0de12 2020-03-21 stsp return NULL;
972 6338a6a1 2020-03-21 stsp }
973 6338a6a1 2020-03-21 stsp
974 6338a6a1 2020-03-21 stsp static const struct got_error *
975 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
976 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
977 6338a6a1 2020-03-21 stsp {
978 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
979 6338a6a1 2020-03-21 stsp struct got_reference *ref;
980 6338a6a1 2020-03-21 stsp char *id_str;
981 6338a6a1 2020-03-21 stsp
982 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
983 6338a6a1 2020-03-21 stsp if (err)
984 6338a6a1 2020-03-21 stsp return err;
985 6338a6a1 2020-03-21 stsp
986 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
987 6338a6a1 2020-03-21 stsp if (err)
988 6338a6a1 2020-03-21 stsp goto done;
989 6338a6a1 2020-03-21 stsp
990 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
991 6338a6a1 2020-03-21 stsp got_ref_close(ref);
992 6338a6a1 2020-03-21 stsp
993 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
994 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
995 6338a6a1 2020-03-21 stsp done:
996 6338a6a1 2020-03-21 stsp free(id_str);
997 6338a6a1 2020-03-21 stsp return err;
998 0e4002ca 2020-03-21 stsp }
999 0e4002ca 2020-03-21 stsp
1000 0e4002ca 2020-03-21 stsp static int
1001 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1002 0e4002ca 2020-03-21 stsp {
1003 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1004 0e4002ca 2020-03-21 stsp return 0;
1005 0e4002ca 2020-03-21 stsp refname += 5;
1006 0e4002ca 2020-03-21 stsp
1007 0e4002ca 2020-03-21 stsp /*
1008 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1009 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1010 0e4002ca 2020-03-21 stsp */
1011 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1012 0e4002ca 2020-03-21 stsp return 0;
1013 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1014 0e4002ca 2020-03-21 stsp return 0;
1015 0e4002ca 2020-03-21 stsp
1016 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1017 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1018 0e4002ca 2020-03-21 stsp
1019 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1020 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1021 0e4002ca 2020-03-21 stsp return 1;
1022 0e4002ca 2020-03-21 stsp
1023 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1024 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1025 41b0de12 2020-03-21 stsp }
1026 41b0de12 2020-03-21 stsp
1027 0e4002ca 2020-03-21 stsp static int
1028 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1029 0e4002ca 2020-03-21 stsp {
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1031 0e4002ca 2020-03-21 stsp
1032 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1033 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1034 0e4002ca 2020-03-21 stsp return 1;
1035 0e4002ca 2020-03-21 stsp }
1036 0e4002ca 2020-03-21 stsp
1037 0e4002ca 2020-03-21 stsp return 0;
1038 0e4002ca 2020-03-21 stsp }
1039 0e4002ca 2020-03-21 stsp
1040 41b0de12 2020-03-21 stsp static const struct got_error *
1041 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1042 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1043 0e4002ca 2020-03-21 stsp {
1044 0e4002ca 2020-03-21 stsp const struct got_error *err;
1045 0e4002ca 2020-03-21 stsp char *remote_refname;
1046 0e4002ca 2020-03-21 stsp
1047 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1048 0e4002ca 2020-03-21 stsp refname += 5;
1049 0e4002ca 2020-03-21 stsp
1050 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1051 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1052 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1053 0e4002ca 2020-03-21 stsp
1054 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1055 0e4002ca 2020-03-21 stsp free(remote_refname);
1056 0e4002ca 2020-03-21 stsp return err;
1057 0e4002ca 2020-03-21 stsp }
1058 0e4002ca 2020-03-21 stsp
1059 0e4002ca 2020-03-21 stsp static const struct got_error *
1060 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1061 93658fb9 2020-03-18 stsp {
1062 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1063 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1064 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1065 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1066 bb64b798 2020-03-18 stsp const char *repo_path;
1067 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1068 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1069 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1070 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1071 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1072 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1073 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1074 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1075 257add31 2020-09-09 stsp char *gitconfig_path = NULL, *gotconfig_path = NULL;
1076 257add31 2020-09-09 stsp char *gitconfig = NULL, *gotconfig = NULL;
1077 257add31 2020-09-09 stsp FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1078 b46f3e71 2020-03-18 stsp ssize_t n;
1079 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1080 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1081 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1082 93658fb9 2020-03-18 stsp
1083 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1084 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1085 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1086 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1087 d9b4d0c0 2020-03-18 stsp
1088 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1089 93658fb9 2020-03-18 stsp switch (ch) {
1090 659e7fbd 2020-03-20 stsp case 'a':
1091 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1092 4ba14133 2020-03-20 stsp break;
1093 4ba14133 2020-03-20 stsp case 'b':
1094 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1095 4ba14133 2020-03-20 stsp optarg, NULL);
1096 4ba14133 2020-03-20 stsp if (error)
1097 4ba14133 2020-03-20 stsp return error;
1098 659e7fbd 2020-03-20 stsp break;
1099 41b0de12 2020-03-21 stsp case 'l':
1100 41b0de12 2020-03-21 stsp list_refs_only = 1;
1101 41b0de12 2020-03-21 stsp break;
1102 469dd726 2020-03-20 stsp case 'm':
1103 469dd726 2020-03-20 stsp mirror_references = 1;
1104 469dd726 2020-03-20 stsp break;
1105 68999b92 2020-03-18 stsp case 'v':
1106 68999b92 2020-03-18 stsp if (verbosity < 0)
1107 68999b92 2020-03-18 stsp verbosity = 0;
1108 68999b92 2020-03-18 stsp else if (verbosity < 3)
1109 68999b92 2020-03-18 stsp verbosity++;
1110 68999b92 2020-03-18 stsp break;
1111 68999b92 2020-03-18 stsp case 'q':
1112 68999b92 2020-03-18 stsp verbosity = -1;
1113 68999b92 2020-03-18 stsp break;
1114 0e4002ca 2020-03-21 stsp case 'R':
1115 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1116 0e4002ca 2020-03-21 stsp optarg, NULL);
1117 0e4002ca 2020-03-21 stsp if (error)
1118 0e4002ca 2020-03-21 stsp return error;
1119 0e4002ca 2020-03-21 stsp break;
1120 93658fb9 2020-03-18 stsp default:
1121 93658fb9 2020-03-18 stsp usage_clone();
1122 93658fb9 2020-03-18 stsp break;
1123 93658fb9 2020-03-18 stsp }
1124 93658fb9 2020-03-18 stsp }
1125 93658fb9 2020-03-18 stsp argc -= optind;
1126 93658fb9 2020-03-18 stsp argv += optind;
1127 39c64a6a 2020-03-18 stsp
1128 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1129 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1130 41b0de12 2020-03-21 stsp if (list_refs_only) {
1131 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1132 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1133 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1134 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1135 41b0de12 2020-03-21 stsp if (mirror_references)
1136 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1137 41b0de12 2020-03-21 stsp if (verbosity == -1)
1138 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1139 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1140 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1141 41b0de12 2020-03-21 stsp }
1142 4ba14133 2020-03-20 stsp
1143 93658fb9 2020-03-18 stsp uri = argv[0];
1144 9df6f38b 2020-03-18 stsp
1145 9df6f38b 2020-03-18 stsp if (argc == 1)
1146 93658fb9 2020-03-18 stsp dirname = NULL;
1147 9df6f38b 2020-03-18 stsp else if (argc == 2)
1148 93658fb9 2020-03-18 stsp dirname = argv[1];
1149 93658fb9 2020-03-18 stsp else
1150 93658fb9 2020-03-18 stsp usage_clone();
1151 09838ffc 2020-03-18 stsp
1152 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1153 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1154 39c64a6a 2020-03-18 stsp if (error)
1155 09f63084 2020-03-20 stsp goto done;
1156 09f63084 2020-03-20 stsp
1157 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1158 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1159 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1160 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1161 09838ffc 2020-03-18 stsp goto done;
1162 09f63084 2020-03-20 stsp }
1163 09838ffc 2020-03-18 stsp
1164 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1165 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1166 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1167 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1168 39c64a6a 2020-03-18 stsp err(1, "pledge");
1169 b46f3e71 2020-03-18 stsp #endif
1170 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1171 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1172 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1173 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1174 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1175 39c64a6a 2020-03-18 stsp err(1, "pledge");
1176 b46f3e71 2020-03-18 stsp #endif
1177 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1178 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1179 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1180 39c64a6a 2020-03-18 stsp goto done;
1181 39c64a6a 2020-03-18 stsp } else {
1182 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1183 39c64a6a 2020-03-18 stsp goto done;
1184 39c64a6a 2020-03-18 stsp }
1185 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1186 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1187 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1188 bb64b798 2020-03-18 stsp goto done;
1189 bb64b798 2020-03-18 stsp }
1190 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1191 bb64b798 2020-03-18 stsp } else
1192 bb64b798 2020-03-18 stsp repo_path = dirname;
1193 bb64b798 2020-03-18 stsp
1194 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1195 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1196 41b0de12 2020-03-21 stsp if (error)
1197 41b0de12 2020-03-21 stsp goto done;
1198 bb64b798 2020-03-18 stsp
1199 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1200 41b0de12 2020-03-21 stsp if (error)
1201 41b0de12 2020-03-21 stsp goto done;
1202 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1203 41b0de12 2020-03-21 stsp if (error)
1204 41b0de12 2020-03-21 stsp goto done;
1205 41b0de12 2020-03-21 stsp }
1206 bb64b798 2020-03-18 stsp
1207 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1208 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1209 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1210 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1211 ee448f5f 2020-03-18 stsp goto done;
1212 ee448f5f 2020-03-18 stsp }
1213 ee448f5f 2020-03-18 stsp }
1214 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1215 ee448f5f 2020-03-18 stsp if (error)
1216 ee448f5f 2020-03-18 stsp goto done;
1217 f79e6490 2020-04-19 stsp
1218 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1219 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1220 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1221 ee448f5f 2020-03-18 stsp
1222 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1223 9c52365f 2020-03-21 stsp server_path, verbosity);
1224 39c64a6a 2020-03-18 stsp if (error)
1225 bb64b798 2020-03-18 stsp goto done;
1226 bb64b798 2020-03-18 stsp
1227 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1228 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1229 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1230 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1231 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1232 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1233 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1234 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1235 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1236 39c64a6a 2020-03-18 stsp if (error)
1237 d9b4d0c0 2020-03-18 stsp goto done;
1238 d9b4d0c0 2020-03-18 stsp
1239 41b0de12 2020-03-21 stsp if (list_refs_only) {
1240 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1241 41b0de12 2020-03-21 stsp goto done;
1242 41b0de12 2020-03-21 stsp }
1243 41b0de12 2020-03-21 stsp
1244 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1245 39c64a6a 2020-03-18 stsp if (error)
1246 d9b4d0c0 2020-03-18 stsp goto done;
1247 68999b92 2020-03-18 stsp if (verbosity >= 0)
1248 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1249 d9b4d0c0 2020-03-18 stsp free(id_str);
1250 d9b4d0c0 2020-03-18 stsp
1251 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1253 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1254 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1255 7ebc0570 2020-03-18 stsp char *remote_refname;
1256 0e4002ca 2020-03-21 stsp
1257 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1258 0e4002ca 2020-03-21 stsp !mirror_references) {
1259 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1260 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1261 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1262 0e4002ca 2020-03-21 stsp if (error)
1263 0e4002ca 2020-03-21 stsp goto done;
1264 0e4002ca 2020-03-21 stsp continue;
1265 0e4002ca 2020-03-21 stsp }
1266 668a20f6 2020-03-18 stsp
1267 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1268 39c64a6a 2020-03-18 stsp if (error)
1269 d9b4d0c0 2020-03-18 stsp goto done;
1270 d9b4d0c0 2020-03-18 stsp
1271 469dd726 2020-03-20 stsp if (mirror_references)
1272 469dd726 2020-03-20 stsp continue;
1273 469dd726 2020-03-20 stsp
1274 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1275 7ebc0570 2020-03-18 stsp continue;
1276 7ebc0570 2020-03-18 stsp
1277 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1278 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1279 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1280 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1281 7ebc0570 2020-03-18 stsp goto done;
1282 7ebc0570 2020-03-18 stsp }
1283 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1284 f298ae0f 2020-03-25 stsp free(remote_refname);
1285 39c64a6a 2020-03-18 stsp if (error)
1286 d9b4d0c0 2020-03-18 stsp goto done;
1287 d9b4d0c0 2020-03-18 stsp }
1288 d9b4d0c0 2020-03-18 stsp
1289 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1290 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1291 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1292 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1293 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1294 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1295 d9b4d0c0 2020-03-18 stsp
1296 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1297 d9b4d0c0 2020-03-18 stsp continue;
1298 d9b4d0c0 2020-03-18 stsp
1299 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1300 39c64a6a 2020-03-18 stsp if (error) {
1301 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1302 55330abe 2020-03-20 stsp error = NULL;
1303 d9b4d0c0 2020-03-18 stsp continue;
1304 55330abe 2020-03-20 stsp }
1305 d9b4d0c0 2020-03-18 stsp goto done;
1306 d9b4d0c0 2020-03-18 stsp }
1307 d9b4d0c0 2020-03-18 stsp
1308 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1309 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1310 f298ae0f 2020-03-25 stsp if (error)
1311 f298ae0f 2020-03-25 stsp goto done;
1312 f298ae0f 2020-03-25 stsp
1313 f298ae0f 2020-03-25 stsp if (mirror_references)
1314 f298ae0f 2020-03-25 stsp continue;
1315 f298ae0f 2020-03-25 stsp
1316 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1317 f298ae0f 2020-03-25 stsp continue;
1318 f298ae0f 2020-03-25 stsp
1319 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1320 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1321 f298ae0f 2020-03-25 stsp refname) == -1) {
1322 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1323 f298ae0f 2020-03-25 stsp goto done;
1324 f298ae0f 2020-03-25 stsp }
1325 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1326 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1327 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1328 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1329 f298ae0f 2020-03-25 stsp free(remote_refname);
1330 f298ae0f 2020-03-25 stsp goto done;
1331 f298ae0f 2020-03-25 stsp }
1332 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1333 f298ae0f 2020-03-25 stsp if (error) {
1334 f298ae0f 2020-03-25 stsp free(remote_refname);
1335 f298ae0f 2020-03-25 stsp free(remote_target);
1336 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1337 f298ae0f 2020-03-25 stsp error = NULL;
1338 f298ae0f 2020-03-25 stsp continue;
1339 f298ae0f 2020-03-25 stsp }
1340 f298ae0f 2020-03-25 stsp goto done;
1341 f298ae0f 2020-03-25 stsp }
1342 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1343 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1344 f298ae0f 2020-03-25 stsp free(remote_refname);
1345 f298ae0f 2020-03-25 stsp free(remote_target);
1346 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1347 39c64a6a 2020-03-18 stsp if (error)
1348 d9b4d0c0 2020-03-18 stsp goto done;
1349 4ba14133 2020-03-20 stsp }
1350 4ba14133 2020-03-20 stsp if (pe == NULL) {
1351 4ba14133 2020-03-20 stsp /*
1352 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1353 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1354 4ba14133 2020-03-20 stsp * which could be fetched instead.
1355 4ba14133 2020-03-20 stsp */
1356 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1357 4ba14133 2020-03-20 stsp const char *target = pe->path;
1358 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1359 d9b4d0c0 2020-03-18 stsp
1360 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1361 4ba14133 2020-03-20 stsp if (error) {
1362 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1363 4ba14133 2020-03-20 stsp error = NULL;
1364 4ba14133 2020-03-20 stsp continue;
1365 4ba14133 2020-03-20 stsp }
1366 4ba14133 2020-03-20 stsp goto done;
1367 4ba14133 2020-03-20 stsp }
1368 4ba14133 2020-03-20 stsp
1369 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1370 f298ae0f 2020-03-25 stsp verbosity, repo);
1371 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1372 4ba14133 2020-03-20 stsp if (error)
1373 4ba14133 2020-03-20 stsp goto done;
1374 4ba14133 2020-03-20 stsp break;
1375 4ba14133 2020-03-20 stsp }
1376 659e7fbd 2020-03-20 stsp }
1377 659e7fbd 2020-03-20 stsp
1378 257add31 2020-09-09 stsp /* Create got.conf(5). */
1379 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1380 257add31 2020-09-09 stsp if (gotconfig_path == NULL) {
1381 257add31 2020-09-09 stsp error = got_error_from_errno("got_repo_get_path_gotconfig");
1382 257add31 2020-09-09 stsp goto done;
1383 257add31 2020-09-09 stsp }
1384 257add31 2020-09-09 stsp gotconfig_file = fopen(gotconfig_path, "a");
1385 257add31 2020-09-09 stsp if (gotconfig_file == NULL) {
1386 257add31 2020-09-09 stsp error = got_error_from_errno2("fopen", gotconfig_path);
1387 257add31 2020-09-09 stsp goto done;
1388 257add31 2020-09-09 stsp }
1389 257add31 2020-09-09 stsp got_path_strip_trailing_slashes(server_path);
1390 257add31 2020-09-09 stsp if (asprintf(&gotconfig,
1391 257add31 2020-09-09 stsp "remote \"%s\" {\n"
1392 257add31 2020-09-09 stsp "\tserver %s\n"
1393 257add31 2020-09-09 stsp "\tprotocol %s\n"
1394 257add31 2020-09-09 stsp "%s%s%s"
1395 257add31 2020-09-09 stsp "\trepository \"%s\"\n"
1396 257add31 2020-09-09 stsp "%s"
1397 257add31 2020-09-09 stsp "}\n",
1398 257add31 2020-09-09 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1399 257add31 2020-09-09 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1400 257add31 2020-09-09 stsp server_path,
1401 257add31 2020-09-09 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1402 257add31 2020-09-09 stsp error = got_error_from_errno("asprintf");
1403 257add31 2020-09-09 stsp goto done;
1404 257add31 2020-09-09 stsp }
1405 257add31 2020-09-09 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1406 257add31 2020-09-09 stsp if (n != strlen(gotconfig)) {
1407 257add31 2020-09-09 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1408 257add31 2020-09-09 stsp goto done;
1409 257add31 2020-09-09 stsp }
1410 257add31 2020-09-09 stsp
1411 257add31 2020-09-09 stsp /* Create a config file Git can understand. */
1412 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1413 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1414 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1415 659e7fbd 2020-03-20 stsp goto done;
1416 659e7fbd 2020-03-20 stsp }
1417 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1418 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1419 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1420 659e7fbd 2020-03-20 stsp goto done;
1421 b46f3e71 2020-03-18 stsp }
1422 659e7fbd 2020-03-20 stsp if (mirror_references) {
1423 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1424 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1425 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1426 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1427 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1428 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1429 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1430 659e7fbd 2020-03-20 stsp goto done;
1431 659e7fbd 2020-03-20 stsp }
1432 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1433 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1434 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1435 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1436 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1437 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1438 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1439 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1440 659e7fbd 2020-03-20 stsp goto done;
1441 659e7fbd 2020-03-20 stsp }
1442 659e7fbd 2020-03-20 stsp } else {
1443 659e7fbd 2020-03-20 stsp const char *branchname;
1444 d715f13e 2020-03-19 stsp
1445 659e7fbd 2020-03-20 stsp /*
1446 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1447 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1448 659e7fbd 2020-03-20 stsp */
1449 659e7fbd 2020-03-20 stsp if (head_symref) {
1450 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1451 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1452 659e7fbd 2020-03-20 stsp branchname += 11;
1453 659e7fbd 2020-03-20 stsp } else
1454 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1455 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1456 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1457 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1458 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1459 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1460 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1461 659e7fbd 2020-03-20 stsp branchname) == -1) {
1462 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1463 659e7fbd 2020-03-20 stsp goto done;
1464 659e7fbd 2020-03-20 stsp }
1465 659e7fbd 2020-03-20 stsp }
1466 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1467 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1468 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1469 659e7fbd 2020-03-20 stsp goto done;
1470 659e7fbd 2020-03-20 stsp }
1471 659e7fbd 2020-03-20 stsp
1472 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1473 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1474 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1475 09838ffc 2020-03-18 stsp done:
1476 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1477 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1478 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1479 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1480 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1481 9c52365f 2020-03-21 stsp }
1482 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1483 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1484 257add31 2020-09-09 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1485 257add31 2020-09-09 stsp error = got_error_from_errno("fclose");
1486 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1487 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1488 bb64b798 2020-03-18 stsp if (repo)
1489 bb64b798 2020-03-18 stsp got_repo_close(repo);
1490 659e7fbd 2020-03-20 stsp if (head_symref)
1491 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1492 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1493 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1494 d9b4d0c0 2020-03-18 stsp free(pe->data);
1495 d9b4d0c0 2020-03-18 stsp }
1496 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1497 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1498 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1499 d9b4d0c0 2020-03-18 stsp free(pe->data);
1500 d9b4d0c0 2020-03-18 stsp }
1501 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1502 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1503 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1504 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1505 09838ffc 2020-03-18 stsp free(proto);
1506 09838ffc 2020-03-18 stsp free(host);
1507 09838ffc 2020-03-18 stsp free(port);
1508 09838ffc 2020-03-18 stsp free(server_path);
1509 09838ffc 2020-03-18 stsp free(repo_name);
1510 bb64b798 2020-03-18 stsp free(default_destdir);
1511 257add31 2020-09-09 stsp free(gotconfig);
1512 257add31 2020-09-09 stsp free(gitconfig);
1513 257add31 2020-09-09 stsp free(gotconfig_path);
1514 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1515 b46f3e71 2020-03-18 stsp free(git_url);
1516 39c64a6a 2020-03-18 stsp return error;
1517 7848a0e1 2020-03-19 stsp }
1518 7848a0e1 2020-03-19 stsp
1519 7848a0e1 2020-03-19 stsp static const struct got_error *
1520 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1521 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1522 7848a0e1 2020-03-19 stsp {
1523 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1524 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1525 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1526 7848a0e1 2020-03-19 stsp
1527 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1528 7848a0e1 2020-03-19 stsp if (err)
1529 7848a0e1 2020-03-19 stsp goto done;
1530 7848a0e1 2020-03-19 stsp
1531 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1532 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1533 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1534 88609724 2020-03-21 stsp if (err)
1535 88609724 2020-03-21 stsp goto done;
1536 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1537 88609724 2020-03-21 stsp goto done;
1538 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1539 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1540 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1541 db6d8ad8 2020-03-21 stsp }
1542 db6d8ad8 2020-03-21 stsp goto done;
1543 db6d8ad8 2020-03-21 stsp }
1544 db6d8ad8 2020-03-21 stsp
1545 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1546 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1547 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1548 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1549 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1550 688f11b3 2020-03-21 stsp }
1551 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1552 7848a0e1 2020-03-19 stsp if (err)
1553 7848a0e1 2020-03-19 stsp goto done;
1554 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1555 e8a967e0 2020-03-21 stsp if (err)
1556 e8a967e0 2020-03-21 stsp goto done;
1557 7848a0e1 2020-03-19 stsp } else {
1558 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1559 7848a0e1 2020-03-19 stsp if (err)
1560 7848a0e1 2020-03-19 stsp goto done;
1561 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1562 6338a6a1 2020-03-21 stsp goto done;
1563 6338a6a1 2020-03-21 stsp
1564 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1565 6338a6a1 2020-03-21 stsp if (err)
1566 6338a6a1 2020-03-21 stsp goto done;
1567 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1568 6338a6a1 2020-03-21 stsp if (err)
1569 6338a6a1 2020-03-21 stsp goto done;
1570 7848a0e1 2020-03-19 stsp }
1571 6338a6a1 2020-03-21 stsp
1572 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1573 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1574 6338a6a1 2020-03-21 stsp new_id_str);
1575 7848a0e1 2020-03-19 stsp done:
1576 7848a0e1 2020-03-19 stsp free(old_id);
1577 7848a0e1 2020-03-19 stsp free(new_id_str);
1578 7848a0e1 2020-03-19 stsp return err;
1579 2ab43947 2020-03-18 stsp }
1580 f1bcca34 2020-03-25 stsp
1581 f1bcca34 2020-03-25 stsp static const struct got_error *
1582 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1583 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1584 f1bcca34 2020-03-25 stsp {
1585 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1586 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1587 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1588 f1bcca34 2020-03-25 stsp
1589 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1590 bcf34b0e 2020-03-26 stsp if (err) {
1591 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1592 bcf34b0e 2020-03-26 stsp return err;
1593 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1594 bcf34b0e 2020-03-26 stsp if (err)
1595 bcf34b0e 2020-03-26 stsp goto done;
1596 2ab43947 2020-03-18 stsp
1597 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1598 bcf34b0e 2020-03-26 stsp if (err)
1599 bcf34b0e 2020-03-26 stsp goto done;
1600 f1bcca34 2020-03-25 stsp
1601 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1602 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1603 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1604 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1605 bcf34b0e 2020-03-26 stsp } else {
1606 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1607 f1bcca34 2020-03-25 stsp
1608 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1609 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1610 bcf34b0e 2020-03-26 stsp goto done;
1611 bcf34b0e 2020-03-26 stsp
1612 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1613 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1614 bcf34b0e 2020-03-26 stsp if (err)
1615 bcf34b0e 2020-03-26 stsp goto done;
1616 bcf34b0e 2020-03-26 stsp
1617 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1618 bcf34b0e 2020-03-26 stsp if (err)
1619 bcf34b0e 2020-03-26 stsp goto done;
1620 bcf34b0e 2020-03-26 stsp
1621 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1622 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1623 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1624 bcf34b0e 2020-03-26 stsp
1625 bcf34b0e 2020-03-26 stsp }
1626 f1bcca34 2020-03-25 stsp done:
1627 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1628 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1629 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1630 bcf34b0e 2020-03-26 stsp err = unlock_err;
1631 bcf34b0e 2020-03-26 stsp }
1632 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1633 f1bcca34 2020-03-25 stsp return err;
1634 f1bcca34 2020-03-25 stsp }
1635 f1bcca34 2020-03-25 stsp
1636 2ab43947 2020-03-18 stsp __dead static void
1637 7848a0e1 2020-03-19 stsp usage_fetch(void)
1638 7848a0e1 2020-03-19 stsp {
1639 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1640 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1641 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1642 13f12b09 2020-03-21 stsp getprogname());
1643 7848a0e1 2020-03-19 stsp exit(1);
1644 7848a0e1 2020-03-19 stsp }
1645 7848a0e1 2020-03-19 stsp
1646 7848a0e1 2020-03-19 stsp static const struct got_error *
1647 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1648 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1649 f21ec2f0 2020-03-21 stsp {
1650 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1651 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1652 3789fd73 2020-03-26 stsp char *id_str = NULL;
1653 3789fd73 2020-03-26 stsp
1654 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1655 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1656 3789fd73 2020-03-26 stsp if (err)
1657 3789fd73 2020-03-26 stsp return err;
1658 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1659 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1660 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1661 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1662 3789fd73 2020-03-26 stsp }
1663 3789fd73 2020-03-26 stsp } else {
1664 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1665 3789fd73 2020-03-26 stsp if (err)
1666 3789fd73 2020-03-26 stsp return err;
1667 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1668 3789fd73 2020-03-26 stsp if (err)
1669 3789fd73 2020-03-26 stsp goto done;
1670 3168e5da 2020-09-10 stsp
1671 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1672 3789fd73 2020-03-26 stsp if (err)
1673 3789fd73 2020-03-26 stsp goto done;
1674 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1675 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1676 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1677 3789fd73 2020-03-26 stsp }
1678 3789fd73 2020-03-26 stsp }
1679 3789fd73 2020-03-26 stsp done:
1680 3789fd73 2020-03-26 stsp free(id);
1681 3789fd73 2020-03-26 stsp free(id_str);
1682 3789fd73 2020-03-26 stsp return NULL;
1683 3789fd73 2020-03-26 stsp }
1684 3789fd73 2020-03-26 stsp
1685 3789fd73 2020-03-26 stsp static const struct got_error *
1686 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1687 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1688 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1689 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1690 3789fd73 2020-03-26 stsp {
1691 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1692 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1693 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1694 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1695 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1696 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1697 f21ec2f0 2020-03-21 stsp
1698 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1699 f21ec2f0 2020-03-21 stsp
1700 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1701 3789fd73 2020-03-26 stsp == -1)
1702 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1703 3789fd73 2020-03-26 stsp
1704 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1705 f21ec2f0 2020-03-21 stsp if (err)
1706 3789fd73 2020-03-26 stsp goto done;
1707 f21ec2f0 2020-03-21 stsp
1708 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1709 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1710 f21ec2f0 2020-03-21 stsp
1711 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1712 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1713 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1714 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1715 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1716 3789fd73 2020-03-26 stsp continue;
1717 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1718 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1719 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1720 3789fd73 2020-03-26 stsp goto done;
1721 3789fd73 2020-03-26 stsp }
1722 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1723 3789fd73 2020-03-26 stsp continue;
1724 3789fd73 2020-03-26 stsp }
1725 f21ec2f0 2020-03-21 stsp
1726 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1727 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1728 f21ec2f0 2020-03-21 stsp break;
1729 f21ec2f0 2020-03-21 stsp }
1730 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1731 f21ec2f0 2020-03-21 stsp continue;
1732 f21ec2f0 2020-03-21 stsp
1733 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1734 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1735 3789fd73 2020-03-26 stsp break;
1736 3789fd73 2020-03-26 stsp }
1737 3789fd73 2020-03-26 stsp if (pe != NULL)
1738 3789fd73 2020-03-26 stsp continue;
1739 f21ec2f0 2020-03-21 stsp
1740 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1741 f21ec2f0 2020-03-21 stsp if (err)
1742 f21ec2f0 2020-03-21 stsp break;
1743 3789fd73 2020-03-26 stsp
1744 3789fd73 2020-03-26 stsp if (local_refname) {
1745 3789fd73 2020-03-26 stsp struct got_reference *ref;
1746 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1747 3789fd73 2020-03-26 stsp if (err) {
1748 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1749 3789fd73 2020-03-26 stsp break;
1750 3789fd73 2020-03-26 stsp free(local_refname);
1751 3789fd73 2020-03-26 stsp local_refname = NULL;
1752 3789fd73 2020-03-26 stsp continue;
1753 3789fd73 2020-03-26 stsp }
1754 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1755 3789fd73 2020-03-26 stsp if (err)
1756 3789fd73 2020-03-26 stsp break;
1757 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1758 3789fd73 2020-03-26 stsp got_ref_close(ref);
1759 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1760 3789fd73 2020-03-26 stsp err = unlock_err;
1761 3789fd73 2020-03-26 stsp break;
1762 3789fd73 2020-03-26 stsp }
1763 3789fd73 2020-03-26 stsp
1764 3789fd73 2020-03-26 stsp free(local_refname);
1765 3789fd73 2020-03-26 stsp local_refname = NULL;
1766 6338a6a1 2020-03-21 stsp }
1767 f21ec2f0 2020-03-21 stsp }
1768 3789fd73 2020-03-26 stsp done:
1769 3789fd73 2020-03-26 stsp free(remote_namespace);
1770 3789fd73 2020-03-26 stsp free(local_refname);
1771 0e4002ca 2020-03-21 stsp return err;
1772 0e4002ca 2020-03-21 stsp }
1773 0e4002ca 2020-03-21 stsp
1774 0e4002ca 2020-03-21 stsp static const struct got_error *
1775 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1776 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1777 0e4002ca 2020-03-21 stsp {
1778 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1779 0e4002ca 2020-03-21 stsp char *remote_refname;
1780 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1781 0e4002ca 2020-03-21 stsp
1782 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1783 0e4002ca 2020-03-21 stsp refname += 5;
1784 0e4002ca 2020-03-21 stsp
1785 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1786 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1787 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1788 f21ec2f0 2020-03-21 stsp
1789 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1790 0e4002ca 2020-03-21 stsp if (err) {
1791 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1792 0e4002ca 2020-03-21 stsp goto done;
1793 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1794 0e4002ca 2020-03-21 stsp } else {
1795 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1796 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1797 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1798 9f142382 2020-03-21 stsp err = unlock_err;
1799 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1800 0e4002ca 2020-03-21 stsp }
1801 0e4002ca 2020-03-21 stsp done:
1802 0e4002ca 2020-03-21 stsp free(remote_refname);
1803 f21ec2f0 2020-03-21 stsp return err;
1804 f21ec2f0 2020-03-21 stsp }
1805 f21ec2f0 2020-03-21 stsp
1806 f21ec2f0 2020-03-21 stsp static const struct got_error *
1807 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1808 7848a0e1 2020-03-19 stsp {
1809 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1810 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1811 7848a0e1 2020-03-19 stsp const char *remote_name;
1812 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1813 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1814 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1815 7848a0e1 2020-03-19 stsp int nremotes;
1816 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1817 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1818 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1819 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1820 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1821 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1822 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1823 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1824 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1825 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1826 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1827 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1828 7848a0e1 2020-03-19 stsp
1829 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1830 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1831 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1832 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1833 7848a0e1 2020-03-19 stsp
1834 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1835 7848a0e1 2020-03-19 stsp switch (ch) {
1836 659e7fbd 2020-03-20 stsp case 'a':
1837 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1838 4ba14133 2020-03-20 stsp break;
1839 4ba14133 2020-03-20 stsp case 'b':
1840 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1841 4ba14133 2020-03-20 stsp optarg, NULL);
1842 4ba14133 2020-03-20 stsp if (error)
1843 4ba14133 2020-03-20 stsp return error;
1844 41b0de12 2020-03-21 stsp break;
1845 f21ec2f0 2020-03-21 stsp case 'd':
1846 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1847 f21ec2f0 2020-03-21 stsp break;
1848 41b0de12 2020-03-21 stsp case 'l':
1849 41b0de12 2020-03-21 stsp list_refs_only = 1;
1850 659e7fbd 2020-03-20 stsp break;
1851 7848a0e1 2020-03-19 stsp case 'r':
1852 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1853 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1854 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1855 7848a0e1 2020-03-19 stsp optarg);
1856 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1857 7848a0e1 2020-03-19 stsp break;
1858 db6d8ad8 2020-03-21 stsp case 't':
1859 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1860 db6d8ad8 2020-03-21 stsp break;
1861 7848a0e1 2020-03-19 stsp case 'v':
1862 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1863 7848a0e1 2020-03-19 stsp verbosity = 0;
1864 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1865 7848a0e1 2020-03-19 stsp verbosity++;
1866 7848a0e1 2020-03-19 stsp break;
1867 7848a0e1 2020-03-19 stsp case 'q':
1868 7848a0e1 2020-03-19 stsp verbosity = -1;
1869 7848a0e1 2020-03-19 stsp break;
1870 0e4002ca 2020-03-21 stsp case 'R':
1871 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1872 0e4002ca 2020-03-21 stsp optarg, NULL);
1873 0e4002ca 2020-03-21 stsp if (error)
1874 0e4002ca 2020-03-21 stsp return error;
1875 0e4002ca 2020-03-21 stsp break;
1876 7848a0e1 2020-03-19 stsp default:
1877 7848a0e1 2020-03-19 stsp usage_fetch();
1878 7848a0e1 2020-03-19 stsp break;
1879 7848a0e1 2020-03-19 stsp }
1880 7848a0e1 2020-03-19 stsp }
1881 7848a0e1 2020-03-19 stsp argc -= optind;
1882 7848a0e1 2020-03-19 stsp argv += optind;
1883 7848a0e1 2020-03-19 stsp
1884 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1885 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1886 41b0de12 2020-03-21 stsp if (list_refs_only) {
1887 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1888 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1889 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1890 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1891 f21ec2f0 2020-03-21 stsp if (delete_refs)
1892 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1893 41b0de12 2020-03-21 stsp if (verbosity == -1)
1894 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1895 41b0de12 2020-03-21 stsp }
1896 4ba14133 2020-03-20 stsp
1897 7848a0e1 2020-03-19 stsp if (argc == 0)
1898 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1899 7848a0e1 2020-03-19 stsp else if (argc == 1)
1900 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1901 7848a0e1 2020-03-19 stsp else
1902 7848a0e1 2020-03-19 stsp usage_fetch();
1903 7848a0e1 2020-03-19 stsp
1904 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1905 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1906 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1907 7848a0e1 2020-03-19 stsp goto done;
1908 7848a0e1 2020-03-19 stsp }
1909 7848a0e1 2020-03-19 stsp
1910 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1911 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1912 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 7848a0e1 2020-03-19 stsp goto done;
1914 7848a0e1 2020-03-19 stsp else
1915 7848a0e1 2020-03-19 stsp error = NULL;
1916 7848a0e1 2020-03-19 stsp if (worktree) {
1917 7848a0e1 2020-03-19 stsp repo_path =
1918 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1919 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1920 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1921 7848a0e1 2020-03-19 stsp if (error)
1922 7848a0e1 2020-03-19 stsp goto done;
1923 7848a0e1 2020-03-19 stsp } else {
1924 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1925 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1926 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1927 7848a0e1 2020-03-19 stsp goto done;
1928 7848a0e1 2020-03-19 stsp }
1929 7848a0e1 2020-03-19 stsp }
1930 7848a0e1 2020-03-19 stsp }
1931 7848a0e1 2020-03-19 stsp
1932 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1933 7848a0e1 2020-03-19 stsp if (error)
1934 7848a0e1 2020-03-19 stsp goto done;
1935 7848a0e1 2020-03-19 stsp
1936 50b0790e 2020-09-11 stsp if (worktree) {
1937 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1938 50b0790e 2020-09-11 stsp if (worktree_conf) {
1939 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1940 50b0790e 2020-09-11 stsp worktree_conf);
1941 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1942 50b0790e 2020-09-11 stsp remote = &remotes[i];
1943 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1944 50b0790e 2020-09-11 stsp break;
1945 50b0790e 2020-09-11 stsp }
1946 50b0790e 2020-09-11 stsp }
1947 7848a0e1 2020-03-19 stsp }
1948 50b0790e 2020-09-11 stsp if (remote == NULL) {
1949 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
1950 50b0790e 2020-09-11 stsp if (repo_conf) {
1951 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1952 50b0790e 2020-09-11 stsp repo_conf);
1953 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1954 50b0790e 2020-09-11 stsp remote = &remotes[i];
1955 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1956 50b0790e 2020-09-11 stsp break;
1957 50b0790e 2020-09-11 stsp }
1958 50b0790e 2020-09-11 stsp }
1959 50b0790e 2020-09-11 stsp }
1960 50b0790e 2020-09-11 stsp if (remote == NULL) {
1961 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1962 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
1963 257add31 2020-09-09 stsp remote = &remotes[i];
1964 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
1965 257add31 2020-09-09 stsp break;
1966 257add31 2020-09-09 stsp }
1967 7848a0e1 2020-03-19 stsp }
1968 50b0790e 2020-09-11 stsp if (remote == NULL) {
1969 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1970 50b0790e 2020-09-11 stsp goto done;
1971 50b0790e 2020-09-11 stsp }
1972 7848a0e1 2020-03-19 stsp
1973 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1974 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1975 7848a0e1 2020-03-19 stsp if (error)
1976 7848a0e1 2020-03-19 stsp goto done;
1977 7848a0e1 2020-03-19 stsp
1978 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1979 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1980 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1981 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1982 7848a0e1 2020-03-19 stsp err(1, "pledge");
1983 7848a0e1 2020-03-19 stsp #endif
1984 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1985 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1986 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1987 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1988 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1989 7848a0e1 2020-03-19 stsp err(1, "pledge");
1990 7848a0e1 2020-03-19 stsp #endif
1991 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1992 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1993 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1994 7848a0e1 2020-03-19 stsp goto done;
1995 7848a0e1 2020-03-19 stsp } else {
1996 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1997 7848a0e1 2020-03-19 stsp goto done;
1998 7848a0e1 2020-03-19 stsp }
1999 7848a0e1 2020-03-19 stsp
2000 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2001 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2002 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2003 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2004 7848a0e1 2020-03-19 stsp goto done;
2005 7848a0e1 2020-03-19 stsp }
2006 7848a0e1 2020-03-19 stsp }
2007 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2008 7848a0e1 2020-03-19 stsp if (error)
2009 7848a0e1 2020-03-19 stsp goto done;
2010 f79e6490 2020-04-19 stsp
2011 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2012 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2013 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2014 7848a0e1 2020-03-19 stsp
2015 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2016 9c52365f 2020-03-21 stsp server_path, verbosity);
2017 7848a0e1 2020-03-19 stsp if (error)
2018 7848a0e1 2020-03-19 stsp goto done;
2019 7848a0e1 2020-03-19 stsp
2020 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2021 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2022 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2023 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2024 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2025 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2026 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2027 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2028 7848a0e1 2020-03-19 stsp if (error)
2029 7848a0e1 2020-03-19 stsp goto done;
2030 7848a0e1 2020-03-19 stsp
2031 41b0de12 2020-03-21 stsp if (list_refs_only) {
2032 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2033 41b0de12 2020-03-21 stsp goto done;
2034 41b0de12 2020-03-21 stsp }
2035 41b0de12 2020-03-21 stsp
2036 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2037 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2038 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2039 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2040 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2041 984065c8 2020-03-19 stsp if (error)
2042 984065c8 2020-03-19 stsp goto done;
2043 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2044 984065c8 2020-03-19 stsp free(id_str);
2045 984065c8 2020-03-19 stsp id_str = NULL;
2046 984065c8 2020-03-19 stsp }
2047 7848a0e1 2020-03-19 stsp
2048 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2049 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2050 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2051 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2052 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2053 7848a0e1 2020-03-19 stsp char *remote_refname;
2054 7848a0e1 2020-03-19 stsp
2055 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2056 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2057 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2058 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2059 0e4002ca 2020-03-21 stsp if (error)
2060 0e4002ca 2020-03-21 stsp goto done;
2061 0e4002ca 2020-03-21 stsp continue;
2062 0e4002ca 2020-03-21 stsp }
2063 0e4002ca 2020-03-21 stsp
2064 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2065 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2066 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2067 7848a0e1 2020-03-19 stsp if (error) {
2068 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2069 7848a0e1 2020-03-19 stsp goto done;
2070 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2071 6338a6a1 2020-03-21 stsp repo);
2072 7848a0e1 2020-03-19 stsp if (error)
2073 7848a0e1 2020-03-19 stsp goto done;
2074 7848a0e1 2020-03-19 stsp } else {
2075 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2076 db6d8ad8 2020-03-21 stsp verbosity, repo);
2077 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2078 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2079 9f142382 2020-03-21 stsp error = unlock_err;
2080 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2081 7848a0e1 2020-03-19 stsp if (error)
2082 7848a0e1 2020-03-19 stsp goto done;
2083 7848a0e1 2020-03-19 stsp }
2084 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2085 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2086 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2087 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2088 7848a0e1 2020-03-19 stsp goto done;
2089 7848a0e1 2020-03-19 stsp }
2090 7848a0e1 2020-03-19 stsp
2091 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2092 7848a0e1 2020-03-19 stsp if (error) {
2093 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2094 7848a0e1 2020-03-19 stsp goto done;
2095 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2096 688f11b3 2020-03-21 stsp verbosity, repo);
2097 7848a0e1 2020-03-19 stsp if (error)
2098 7848a0e1 2020-03-19 stsp goto done;
2099 7848a0e1 2020-03-19 stsp } else {
2100 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2101 db6d8ad8 2020-03-21 stsp verbosity, repo);
2102 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2103 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2104 9f142382 2020-03-21 stsp error = unlock_err;
2105 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2106 7848a0e1 2020-03-19 stsp if (error)
2107 7848a0e1 2020-03-19 stsp goto done;
2108 7848a0e1 2020-03-19 stsp }
2109 2ec30c80 2020-03-20 stsp
2110 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2111 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2112 2ec30c80 2020-03-20 stsp if (error) {
2113 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2114 2ec30c80 2020-03-20 stsp goto done;
2115 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2116 6338a6a1 2020-03-21 stsp repo);
2117 2ec30c80 2020-03-20 stsp if (error)
2118 2ec30c80 2020-03-20 stsp goto done;
2119 9f142382 2020-03-21 stsp } else {
2120 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2121 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2122 9f142382 2020-03-21 stsp error = unlock_err;
2123 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2124 9f142382 2020-03-21 stsp }
2125 7848a0e1 2020-03-19 stsp }
2126 7848a0e1 2020-03-19 stsp }
2127 f1bcca34 2020-03-25 stsp if (delete_refs) {
2128 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2129 3789fd73 2020-03-26 stsp verbosity, repo);
2130 f1bcca34 2020-03-25 stsp if (error)
2131 f1bcca34 2020-03-25 stsp goto done;
2132 f1bcca34 2020-03-25 stsp }
2133 f1bcca34 2020-03-25 stsp
2134 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2135 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2136 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2137 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2138 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2139 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2140 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2141 f1bcca34 2020-03-25 stsp
2142 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2143 f1bcca34 2020-03-25 stsp continue;
2144 f1bcca34 2020-03-25 stsp
2145 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2146 f1bcca34 2020-03-25 stsp continue;
2147 f1bcca34 2020-03-25 stsp
2148 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2149 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2150 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2151 f1bcca34 2020-03-25 stsp goto done;
2152 f1bcca34 2020-03-25 stsp }
2153 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2154 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2155 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2156 f1bcca34 2020-03-25 stsp free(remote_refname);
2157 f1bcca34 2020-03-25 stsp goto done;
2158 f1bcca34 2020-03-25 stsp }
2159 f1bcca34 2020-03-25 stsp
2160 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2161 f1bcca34 2020-03-25 stsp 0);
2162 f1bcca34 2020-03-25 stsp if (error) {
2163 f1bcca34 2020-03-25 stsp free(remote_refname);
2164 f1bcca34 2020-03-25 stsp free(remote_target);
2165 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2166 f1bcca34 2020-03-25 stsp error = NULL;
2167 f1bcca34 2020-03-25 stsp continue;
2168 f1bcca34 2020-03-25 stsp }
2169 f1bcca34 2020-03-25 stsp goto done;
2170 f1bcca34 2020-03-25 stsp }
2171 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2172 f1bcca34 2020-03-25 stsp verbosity, repo);
2173 f1bcca34 2020-03-25 stsp free(remote_refname);
2174 f1bcca34 2020-03-25 stsp free(remote_target);
2175 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2176 f1bcca34 2020-03-25 stsp if (error)
2177 f1bcca34 2020-03-25 stsp goto done;
2178 f1bcca34 2020-03-25 stsp }
2179 f1bcca34 2020-03-25 stsp }
2180 7848a0e1 2020-03-19 stsp done:
2181 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2182 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2183 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2184 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2185 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2186 9c52365f 2020-03-21 stsp }
2187 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2188 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2189 7848a0e1 2020-03-19 stsp if (repo)
2190 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2191 7848a0e1 2020-03-19 stsp if (worktree)
2192 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2193 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2194 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2195 7848a0e1 2020-03-19 stsp free(pe->data);
2196 7848a0e1 2020-03-19 stsp }
2197 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2198 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2199 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2200 7848a0e1 2020-03-19 stsp free(pe->data);
2201 7848a0e1 2020-03-19 stsp }
2202 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2203 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2204 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2205 7848a0e1 2020-03-19 stsp free(id_str);
2206 7848a0e1 2020-03-19 stsp free(cwd);
2207 7848a0e1 2020-03-19 stsp free(repo_path);
2208 7848a0e1 2020-03-19 stsp free(pack_hash);
2209 7848a0e1 2020-03-19 stsp free(proto);
2210 7848a0e1 2020-03-19 stsp free(host);
2211 7848a0e1 2020-03-19 stsp free(port);
2212 7848a0e1 2020-03-19 stsp free(server_path);
2213 7848a0e1 2020-03-19 stsp free(repo_name);
2214 7848a0e1 2020-03-19 stsp return error;
2215 7848a0e1 2020-03-19 stsp }
2216 7848a0e1 2020-03-19 stsp
2217 7848a0e1 2020-03-19 stsp
2218 7848a0e1 2020-03-19 stsp __dead static void
2219 2ab43947 2020-03-18 stsp usage_checkout(void)
2220 2ab43947 2020-03-18 stsp {
2221 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2222 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2223 2ab43947 2020-03-18 stsp exit(1);
2224 2ab43947 2020-03-18 stsp }
2225 2ab43947 2020-03-18 stsp
2226 2ab43947 2020-03-18 stsp static void
2227 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2228 2ab43947 2020-03-18 stsp {
2229 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2230 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2231 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2232 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2233 2ab43947 2020-03-18 stsp getprogname());
2234 2ab43947 2020-03-18 stsp }
2235 2ab43947 2020-03-18 stsp
2236 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2237 2ab43947 2020-03-18 stsp const char *worktree_path;
2238 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2239 2ab43947 2020-03-18 stsp };
2240 2ab43947 2020-03-18 stsp
2241 2ab43947 2020-03-18 stsp static const struct got_error *
2242 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2243 2ab43947 2020-03-18 stsp {
2244 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2245 2ab43947 2020-03-18 stsp
2246 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2247 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2248 2ab43947 2020-03-18 stsp return NULL;
2249 2ab43947 2020-03-18 stsp
2250 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2251 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2252 2ab43947 2020-03-18 stsp return NULL;
2253 2ab43947 2020-03-18 stsp }
2254 2ab43947 2020-03-18 stsp
2255 2ab43947 2020-03-18 stsp while (path[0] == '/')
2256 2ab43947 2020-03-18 stsp path++;
2257 2ab43947 2020-03-18 stsp
2258 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2259 2ab43947 2020-03-18 stsp return NULL;
2260 2ab43947 2020-03-18 stsp }
2261 2ab43947 2020-03-18 stsp
2262 2ab43947 2020-03-18 stsp static const struct got_error *
2263 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2264 2ab43947 2020-03-18 stsp {
2265 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2266 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2267 2ab43947 2020-03-18 stsp return NULL;
2268 2ab43947 2020-03-18 stsp }
2269 2ab43947 2020-03-18 stsp
2270 2ab43947 2020-03-18 stsp static const struct got_error *
2271 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2272 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2273 2ab43947 2020-03-18 stsp struct got_repository *repo)
2274 2ab43947 2020-03-18 stsp {
2275 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2276 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2277 2ab43947 2020-03-18 stsp
2278 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2279 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2280 2ab43947 2020-03-18 stsp if (err)
2281 2ab43947 2020-03-18 stsp return err;
2282 2ab43947 2020-03-18 stsp
2283 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2284 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2285 2ab43947 2020-03-18 stsp
2286 2ab43947 2020-03-18 stsp /*
2287 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2288 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2289 2ab43947 2020-03-18 stsp *
2290 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2291 2ab43947 2020-03-18 stsp *
2292 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2293 2ab43947 2020-03-18 stsp * \ /
2294 2ab43947 2020-03-18 stsp * C E
2295 2ab43947 2020-03-18 stsp * \ /
2296 2ab43947 2020-03-18 stsp * B (yca)
2297 2ab43947 2020-03-18 stsp * |
2298 2ab43947 2020-03-18 stsp * A
2299 2ab43947 2020-03-18 stsp *
2300 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2301 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2302 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2303 2ab43947 2020-03-18 stsp */
2304 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2305 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2306 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2307 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2308 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2309 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2310 2ab43947 2020-03-18 stsp
2311 2ab43947 2020-03-18 stsp free(yca_id);
2312 2ab43947 2020-03-18 stsp return NULL;
2313 2ab43947 2020-03-18 stsp }
2314 2ab43947 2020-03-18 stsp
2315 2ab43947 2020-03-18 stsp static const struct got_error *
2316 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2317 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2318 2ab43947 2020-03-18 stsp struct got_repository *repo)
2319 2ab43947 2020-03-18 stsp {
2320 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2321 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2322 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2323 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2324 2ab43947 2020-03-18 stsp
2325 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2326 2ab43947 2020-03-18 stsp if (err)
2327 2ab43947 2020-03-18 stsp goto done;
2328 2ab43947 2020-03-18 stsp
2329 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2330 2ab43947 2020-03-18 stsp is_same_branch = 1;
2331 2ab43947 2020-03-18 stsp goto done;
2332 2ab43947 2020-03-18 stsp }
2333 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2334 2ab43947 2020-03-18 stsp is_same_branch = 1;
2335 2ab43947 2020-03-18 stsp goto done;
2336 2ab43947 2020-03-18 stsp }
2337 2ab43947 2020-03-18 stsp
2338 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2339 2ab43947 2020-03-18 stsp if (err)
2340 2ab43947 2020-03-18 stsp goto done;
2341 2ab43947 2020-03-18 stsp
2342 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2343 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2344 2ab43947 2020-03-18 stsp if (err)
2345 2ab43947 2020-03-18 stsp goto done;
2346 2ab43947 2020-03-18 stsp
2347 2ab43947 2020-03-18 stsp for (;;) {
2348 2ab43947 2020-03-18 stsp struct got_object_id *id;
2349 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2350 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2351 2ab43947 2020-03-18 stsp if (err) {
2352 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2353 2ab43947 2020-03-18 stsp err = NULL;
2354 2ab43947 2020-03-18 stsp break;
2355 2ab43947 2020-03-18 stsp }
2356 2ab43947 2020-03-18 stsp
2357 2ab43947 2020-03-18 stsp if (id) {
2358 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2359 2ab43947 2020-03-18 stsp break;
2360 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2361 2ab43947 2020-03-18 stsp is_same_branch = 1;
2362 2ab43947 2020-03-18 stsp break;
2363 2ab43947 2020-03-18 stsp }
2364 2ab43947 2020-03-18 stsp }
2365 2ab43947 2020-03-18 stsp }
2366 2ab43947 2020-03-18 stsp done:
2367 2ab43947 2020-03-18 stsp if (graph)
2368 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2369 2ab43947 2020-03-18 stsp free(head_commit_id);
2370 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2371 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2372 2ab43947 2020-03-18 stsp return err;
2373 a367ff0f 2019-05-14 stsp }
2374 8069f636 2019-01-12 stsp
2375 4ed7e80c 2018-05-20 stsp static const struct got_error *
2376 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2377 4b6c9460 2020-03-05 stsp {
2378 4b6c9460 2020-03-05 stsp static char msg[512];
2379 4b6c9460 2020-03-05 stsp const char *branch_name;
2380 4b6c9460 2020-03-05 stsp
2381 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2382 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2383 4b6c9460 2020-03-05 stsp else
2384 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2385 4b6c9460 2020-03-05 stsp
2386 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2387 4b6c9460 2020-03-05 stsp branch_name += 11;
2388 4b6c9460 2020-03-05 stsp
2389 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2390 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2391 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2392 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2393 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2394 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2395 4b6c9460 2020-03-05 stsp
2396 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2397 4b6c9460 2020-03-05 stsp }
2398 4b6c9460 2020-03-05 stsp
2399 4b6c9460 2020-03-05 stsp static const struct got_error *
2400 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2401 c09a553d 2018-03-12 stsp {
2402 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2403 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2404 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2405 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2406 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2407 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2408 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2409 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2410 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2411 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2412 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2413 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2414 f2ea84fa 2019-07-27 stsp
2415 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2416 c09a553d 2018-03-12 stsp
2417 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2418 0bb8a95e 2018-03-12 stsp switch (ch) {
2419 08573d5b 2019-05-14 stsp case 'b':
2420 08573d5b 2019-05-14 stsp branch_name = optarg;
2421 08573d5b 2019-05-14 stsp break;
2422 8069f636 2019-01-12 stsp case 'c':
2423 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2424 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2425 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2426 bb51a5b4 2020-01-13 stsp break;
2427 bb51a5b4 2020-01-13 stsp case 'E':
2428 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2429 8069f636 2019-01-12 stsp break;
2430 0bb8a95e 2018-03-12 stsp case 'p':
2431 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2432 0bb8a95e 2018-03-12 stsp break;
2433 0bb8a95e 2018-03-12 stsp default:
2434 2deda0b9 2019-03-07 stsp usage_checkout();
2435 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2436 0bb8a95e 2018-03-12 stsp }
2437 0bb8a95e 2018-03-12 stsp }
2438 0bb8a95e 2018-03-12 stsp
2439 0bb8a95e 2018-03-12 stsp argc -= optind;
2440 0bb8a95e 2018-03-12 stsp argv += optind;
2441 0bb8a95e 2018-03-12 stsp
2442 6715a751 2018-03-16 stsp #ifndef PROFILE
2443 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2444 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2445 c09a553d 2018-03-12 stsp err(1, "pledge");
2446 6715a751 2018-03-16 stsp #endif
2447 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2448 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2449 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2450 76089277 2018-04-01 stsp if (repo_path == NULL)
2451 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2452 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2453 76089277 2018-04-01 stsp if (cwd == NULL) {
2454 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2455 76089277 2018-04-01 stsp goto done;
2456 76089277 2018-04-01 stsp }
2457 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2458 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2459 230a42bd 2019-05-11 jcs if (base == NULL) {
2460 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2461 230a42bd 2019-05-11 jcs path_prefix);
2462 230a42bd 2019-05-11 jcs goto done;
2463 230a42bd 2019-05-11 jcs }
2464 230a42bd 2019-05-11 jcs } else {
2465 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2466 230a42bd 2019-05-11 jcs if (base == NULL) {
2467 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2468 230a42bd 2019-05-11 jcs repo_path);
2469 230a42bd 2019-05-11 jcs goto done;
2470 230a42bd 2019-05-11 jcs }
2471 76089277 2018-04-01 stsp }
2472 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2473 c09a553d 2018-03-12 stsp if (dotgit)
2474 c09a553d 2018-03-12 stsp *dotgit = '\0';
2475 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2476 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2477 c09a553d 2018-03-12 stsp free(cwd);
2478 76089277 2018-04-01 stsp goto done;
2479 c09a553d 2018-03-12 stsp }
2480 c09a553d 2018-03-12 stsp free(cwd);
2481 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2482 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2483 76089277 2018-04-01 stsp if (repo_path == NULL) {
2484 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2485 76089277 2018-04-01 stsp goto done;
2486 76089277 2018-04-01 stsp }
2487 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2488 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2489 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2490 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2491 b4b3a7dd 2019-07-22 stsp argv[1]);
2492 b4b3a7dd 2019-07-22 stsp goto done;
2493 b4b3a7dd 2019-07-22 stsp }
2494 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2495 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2496 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2497 b4b3a7dd 2019-07-22 stsp goto done;
2498 b4b3a7dd 2019-07-22 stsp }
2499 76089277 2018-04-01 stsp }
2500 c09a553d 2018-03-12 stsp } else
2501 c09a553d 2018-03-12 stsp usage_checkout();
2502 c09a553d 2018-03-12 stsp
2503 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2504 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2505 13bfb272 2019-05-10 jcs
2506 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2507 c09a553d 2018-03-12 stsp if (error != NULL)
2508 c02c541e 2019-03-29 stsp goto done;
2509 c02c541e 2019-03-29 stsp
2510 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2511 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2512 c530dc23 2019-07-23 stsp if (error) {
2513 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2514 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2515 c530dc23 2019-07-23 stsp goto done;
2516 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2517 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2518 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2519 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2520 c530dc23 2019-07-23 stsp goto done;
2521 c530dc23 2019-07-23 stsp }
2522 c530dc23 2019-07-23 stsp }
2523 c530dc23 2019-07-23 stsp
2524 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2525 c02c541e 2019-03-29 stsp if (error)
2526 c09a553d 2018-03-12 stsp goto done;
2527 8069f636 2019-01-12 stsp
2528 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2529 c09a553d 2018-03-12 stsp if (error != NULL)
2530 c09a553d 2018-03-12 stsp goto done;
2531 c09a553d 2018-03-12 stsp
2532 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2533 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2534 c09a553d 2018-03-12 stsp goto done;
2535 c09a553d 2018-03-12 stsp
2536 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2537 c09a553d 2018-03-12 stsp if (error != NULL)
2538 c09a553d 2018-03-12 stsp goto done;
2539 c09a553d 2018-03-12 stsp
2540 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2541 e5dc7198 2018-12-29 stsp path_prefix);
2542 e5dc7198 2018-12-29 stsp if (error != NULL)
2543 e5dc7198 2018-12-29 stsp goto done;
2544 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2545 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2546 49520a32 2018-12-29 stsp goto done;
2547 49520a32 2018-12-29 stsp }
2548 49520a32 2018-12-29 stsp
2549 8069f636 2019-01-12 stsp if (commit_id_str) {
2550 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2551 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2552 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2553 30837e32 2019-07-25 stsp if (error)
2554 8069f636 2019-01-12 stsp goto done;
2555 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2556 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2557 8069f636 2019-01-12 stsp if (error != NULL) {
2558 8069f636 2019-01-12 stsp free(commit_id);
2559 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2560 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2561 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2562 4b6c9460 2020-03-05 stsp }
2563 8069f636 2019-01-12 stsp goto done;
2564 8069f636 2019-01-12 stsp }
2565 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2566 4b6c9460 2020-03-05 stsp if (error) {
2567 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2568 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2569 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2570 4b6c9460 2020-03-05 stsp }
2571 45d344f6 2019-05-14 stsp goto done;
2572 4b6c9460 2020-03-05 stsp }
2573 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2574 8069f636 2019-01-12 stsp commit_id);
2575 8069f636 2019-01-12 stsp free(commit_id);
2576 8069f636 2019-01-12 stsp if (error)
2577 8069f636 2019-01-12 stsp goto done;
2578 8069f636 2019-01-12 stsp }
2579 8069f636 2019-01-12 stsp
2580 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2581 f2ea84fa 2019-07-27 stsp if (error)
2582 f2ea84fa 2019-07-27 stsp goto done;
2583 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2584 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2585 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2586 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2587 c09a553d 2018-03-12 stsp if (error != NULL)
2588 c09a553d 2018-03-12 stsp goto done;
2589 c09a553d 2018-03-12 stsp
2590 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2591 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2592 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2593 c09a553d 2018-03-12 stsp done:
2594 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2595 8069f636 2019-01-12 stsp free(commit_id_str);
2596 76089277 2018-04-01 stsp free(repo_path);
2597 507dc3bb 2018-12-29 stsp free(worktree_path);
2598 507dc3bb 2018-12-29 stsp return error;
2599 9627c110 2020-04-18 stsp }
2600 9627c110 2020-04-18 stsp
2601 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2602 9627c110 2020-04-18 stsp int did_something;
2603 9627c110 2020-04-18 stsp int conflicts;
2604 9627c110 2020-04-18 stsp int obstructed;
2605 9627c110 2020-04-18 stsp int not_updated;
2606 9627c110 2020-04-18 stsp };
2607 9627c110 2020-04-18 stsp
2608 9627c110 2020-04-18 stsp void
2609 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2610 9627c110 2020-04-18 stsp {
2611 9627c110 2020-04-18 stsp if (!upa->did_something)
2612 9627c110 2020-04-18 stsp return;
2613 9627c110 2020-04-18 stsp
2614 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2615 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2616 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2617 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2618 9627c110 2020-04-18 stsp upa->obstructed);
2619 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2620 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2621 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2622 507dc3bb 2018-12-29 stsp }
2623 507dc3bb 2018-12-29 stsp
2624 507dc3bb 2018-12-29 stsp __dead static void
2625 507dc3bb 2018-12-29 stsp usage_update(void)
2626 507dc3bb 2018-12-29 stsp {
2627 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2628 507dc3bb 2018-12-29 stsp getprogname());
2629 507dc3bb 2018-12-29 stsp exit(1);
2630 507dc3bb 2018-12-29 stsp }
2631 507dc3bb 2018-12-29 stsp
2632 1ee397ad 2019-07-12 stsp static const struct got_error *
2633 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2634 507dc3bb 2018-12-29 stsp {
2635 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2636 784955db 2019-01-12 stsp
2637 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2638 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2639 1ee397ad 2019-07-12 stsp return NULL;
2640 507dc3bb 2018-12-29 stsp
2641 9627c110 2020-04-18 stsp upa->did_something = 1;
2642 a484d721 2019-06-10 stsp
2643 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2644 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2645 1ee397ad 2019-07-12 stsp return NULL;
2646 a484d721 2019-06-10 stsp
2647 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2648 9627c110 2020-04-18 stsp upa->conflicts++;
2649 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2650 9627c110 2020-04-18 stsp upa->obstructed++;
2651 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2652 9627c110 2020-04-18 stsp upa->not_updated++;
2653 9627c110 2020-04-18 stsp
2654 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2655 507dc3bb 2018-12-29 stsp path++;
2656 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2657 1ee397ad 2019-07-12 stsp return NULL;
2658 be7061eb 2018-12-30 stsp }
2659 be7061eb 2018-12-30 stsp
2660 be7061eb 2018-12-30 stsp static const struct got_error *
2661 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2662 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2663 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2664 a1fb16d8 2019-05-24 stsp {
2665 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2666 a1fb16d8 2019-05-24 stsp char *base_id_str;
2667 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2668 a1fb16d8 2019-05-24 stsp
2669 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2670 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2671 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2672 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2673 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2674 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2675 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2676 a1fb16d8 2019-05-24 stsp }
2677 a1fb16d8 2019-05-24 stsp
2678 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2679 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2680 a1fb16d8 2019-05-24 stsp if (err) {
2681 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2682 a1fb16d8 2019-05-24 stsp return err;
2683 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2684 a1fb16d8 2019-05-24 stsp }
2685 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2686 a1fb16d8 2019-05-24 stsp return NULL;
2687 a1fb16d8 2019-05-24 stsp
2688 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2689 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2690 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2691 a1fb16d8 2019-05-24 stsp if (err)
2692 a1fb16d8 2019-05-24 stsp return err;
2693 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2694 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2695 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2696 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2697 0ebf8283 2019-07-24 stsp return NULL;
2698 0ebf8283 2019-07-24 stsp }
2699 0ebf8283 2019-07-24 stsp
2700 0ebf8283 2019-07-24 stsp static const struct got_error *
2701 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2702 0ebf8283 2019-07-24 stsp {
2703 0ebf8283 2019-07-24 stsp const struct got_error *err;
2704 0ebf8283 2019-07-24 stsp int in_progress;
2705 0ebf8283 2019-07-24 stsp
2706 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2707 0ebf8283 2019-07-24 stsp if (err)
2708 0ebf8283 2019-07-24 stsp return err;
2709 0ebf8283 2019-07-24 stsp if (in_progress)
2710 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2711 0ebf8283 2019-07-24 stsp
2712 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2713 0ebf8283 2019-07-24 stsp if (err)
2714 0ebf8283 2019-07-24 stsp return err;
2715 0ebf8283 2019-07-24 stsp if (in_progress)
2716 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2717 0ebf8283 2019-07-24 stsp
2718 a1fb16d8 2019-05-24 stsp return NULL;
2719 a5edda0a 2019-07-27 stsp }
2720 a5edda0a 2019-07-27 stsp
2721 a5edda0a 2019-07-27 stsp static const struct got_error *
2722 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2723 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2724 a5edda0a 2019-07-27 stsp {
2725 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2726 a5edda0a 2019-07-27 stsp char *path;
2727 a5edda0a 2019-07-27 stsp int i;
2728 a5edda0a 2019-07-27 stsp
2729 a5edda0a 2019-07-27 stsp if (argc == 0) {
2730 a5edda0a 2019-07-27 stsp path = strdup("");
2731 a5edda0a 2019-07-27 stsp if (path == NULL)
2732 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2733 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2734 a5edda0a 2019-07-27 stsp }
2735 a5edda0a 2019-07-27 stsp
2736 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2737 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2738 a5edda0a 2019-07-27 stsp if (err)
2739 a5edda0a 2019-07-27 stsp break;
2740 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2741 a5edda0a 2019-07-27 stsp if (err) {
2742 a5edda0a 2019-07-27 stsp free(path);
2743 a5edda0a 2019-07-27 stsp break;
2744 a5edda0a 2019-07-27 stsp }
2745 a5edda0a 2019-07-27 stsp }
2746 a5edda0a 2019-07-27 stsp
2747 a5edda0a 2019-07-27 stsp return err;
2748 a1fb16d8 2019-05-24 stsp }
2749 a1fb16d8 2019-05-24 stsp
2750 a1fb16d8 2019-05-24 stsp static const struct got_error *
2751 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2752 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2753 fa51e947 2020-03-27 stsp {
2754 fa51e947 2020-03-27 stsp const struct got_error *err;
2755 fa51e947 2020-03-27 stsp struct got_repository *repo;
2756 fa51e947 2020-03-27 stsp static char msg[512];
2757 fa51e947 2020-03-27 stsp
2758 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2759 fa51e947 2020-03-27 stsp if (err)
2760 fa51e947 2020-03-27 stsp return orig_err;
2761 fa51e947 2020-03-27 stsp
2762 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2763 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2764 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2765 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2766 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2767 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2768 fa51e947 2020-03-27 stsp got_repo_close(repo);
2769 fa51e947 2020-03-27 stsp return err;
2770 fa51e947 2020-03-27 stsp }
2771 fa51e947 2020-03-27 stsp
2772 fa51e947 2020-03-27 stsp static const struct got_error *
2773 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2774 507dc3bb 2018-12-29 stsp {
2775 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2776 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2777 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2778 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2779 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2780 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2781 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2782 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2783 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2784 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2785 9627c110 2020-04-18 stsp int ch;
2786 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2787 507dc3bb 2018-12-29 stsp
2788 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2789 f2ea84fa 2019-07-27 stsp
2790 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2791 507dc3bb 2018-12-29 stsp switch (ch) {
2792 024e9686 2019-05-14 stsp case 'b':
2793 024e9686 2019-05-14 stsp branch_name = optarg;
2794 024e9686 2019-05-14 stsp break;
2795 507dc3bb 2018-12-29 stsp case 'c':
2796 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2797 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2798 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2799 507dc3bb 2018-12-29 stsp break;
2800 507dc3bb 2018-12-29 stsp default:
2801 2deda0b9 2019-03-07 stsp usage_update();
2802 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2803 507dc3bb 2018-12-29 stsp }
2804 507dc3bb 2018-12-29 stsp }
2805 507dc3bb 2018-12-29 stsp
2806 507dc3bb 2018-12-29 stsp argc -= optind;
2807 507dc3bb 2018-12-29 stsp argv += optind;
2808 507dc3bb 2018-12-29 stsp
2809 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2810 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2811 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2812 507dc3bb 2018-12-29 stsp err(1, "pledge");
2813 507dc3bb 2018-12-29 stsp #endif
2814 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2815 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2816 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2817 c4cdcb68 2019-04-03 stsp goto done;
2818 c4cdcb68 2019-04-03 stsp }
2819 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2820 fa51e947 2020-03-27 stsp if (error) {
2821 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2822 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2823 fa51e947 2020-03-27 stsp worktree_path);
2824 7d5807f4 2019-07-11 stsp goto done;
2825 fa51e947 2020-03-27 stsp }
2826 7d5807f4 2019-07-11 stsp
2827 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2828 f2ea84fa 2019-07-27 stsp if (error)
2829 f2ea84fa 2019-07-27 stsp goto done;
2830 507dc3bb 2018-12-29 stsp
2831 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2832 c9956ddf 2019-09-08 stsp NULL);
2833 507dc3bb 2018-12-29 stsp if (error != NULL)
2834 507dc3bb 2018-12-29 stsp goto done;
2835 507dc3bb 2018-12-29 stsp
2836 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2837 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2838 087fb88c 2019-08-04 stsp if (error)
2839 087fb88c 2019-08-04 stsp goto done;
2840 087fb88c 2019-08-04 stsp
2841 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2842 0266afb7 2019-01-04 stsp if (error)
2843 0266afb7 2019-01-04 stsp goto done;
2844 0266afb7 2019-01-04 stsp
2845 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2846 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2847 024e9686 2019-05-14 stsp if (error != NULL)
2848 024e9686 2019-05-14 stsp goto done;
2849 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2850 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2851 9c4b8182 2019-01-02 stsp if (error != NULL)
2852 9c4b8182 2019-01-02 stsp goto done;
2853 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2854 507dc3bb 2018-12-29 stsp if (error != NULL)
2855 507dc3bb 2018-12-29 stsp goto done;
2856 507dc3bb 2018-12-29 stsp } else {
2857 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2858 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2859 04f57cb3 2019-07-25 stsp free(commit_id_str);
2860 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2861 30837e32 2019-07-25 stsp if (error)
2862 a297e751 2019-07-11 stsp goto done;
2863 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2864 a297e751 2019-07-11 stsp if (error)
2865 507dc3bb 2018-12-29 stsp goto done;
2866 507dc3bb 2018-12-29 stsp }
2867 35c965b2 2018-12-29 stsp
2868 a1fb16d8 2019-05-24 stsp if (branch_name) {
2869 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2870 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2871 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2872 f2ea84fa 2019-07-27 stsp continue;
2873 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2874 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2875 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2876 024e9686 2019-05-14 stsp goto done;
2877 024e9686 2019-05-14 stsp }
2878 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2879 024e9686 2019-05-14 stsp if (error)
2880 024e9686 2019-05-14 stsp goto done;
2881 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2882 3aef623b 2019-10-15 stsp repo);
2883 a367ff0f 2019-05-14 stsp free(head_commit_id);
2884 024e9686 2019-05-14 stsp if (error != NULL)
2885 024e9686 2019-05-14 stsp goto done;
2886 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2887 a367ff0f 2019-05-14 stsp if (error)
2888 a367ff0f 2019-05-14 stsp goto done;
2889 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2890 024e9686 2019-05-14 stsp if (error)
2891 024e9686 2019-05-14 stsp goto done;
2892 024e9686 2019-05-14 stsp } else {
2893 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2894 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2895 a367ff0f 2019-05-14 stsp if (error != NULL) {
2896 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2897 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2898 024e9686 2019-05-14 stsp goto done;
2899 a367ff0f 2019-05-14 stsp }
2900 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2901 a367ff0f 2019-05-14 stsp if (error)
2902 a367ff0f 2019-05-14 stsp goto done;
2903 024e9686 2019-05-14 stsp }
2904 507dc3bb 2018-12-29 stsp
2905 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2906 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2907 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2908 507dc3bb 2018-12-29 stsp commit_id);
2909 507dc3bb 2018-12-29 stsp if (error)
2910 507dc3bb 2018-12-29 stsp goto done;
2911 507dc3bb 2018-12-29 stsp }
2912 507dc3bb 2018-12-29 stsp
2913 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
2914 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2915 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
2916 507dc3bb 2018-12-29 stsp if (error != NULL)
2917 507dc3bb 2018-12-29 stsp goto done;
2918 9c4b8182 2019-01-02 stsp
2919 9627c110 2020-04-18 stsp if (upa.did_something)
2920 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2921 784955db 2019-01-12 stsp else
2922 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2923 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
2924 507dc3bb 2018-12-29 stsp done:
2925 c09a553d 2018-03-12 stsp free(worktree_path);
2926 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2927 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2928 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2929 507dc3bb 2018-12-29 stsp free(commit_id);
2930 9c4b8182 2019-01-02 stsp free(commit_id_str);
2931 c09a553d 2018-03-12 stsp return error;
2932 c09a553d 2018-03-12 stsp }
2933 c09a553d 2018-03-12 stsp
2934 f42b1b34 2018-03-12 stsp static const struct got_error *
2935 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2936 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2937 63035f9f 2019-10-06 stsp struct got_repository *repo)
2938 5c860e29 2018-03-12 stsp {
2939 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2940 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2941 79109fed 2018-03-27 stsp
2942 44392932 2019-08-25 stsp if (blob_id1) {
2943 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2944 44392932 2019-08-25 stsp if (err)
2945 44392932 2019-08-25 stsp goto done;
2946 44392932 2019-08-25 stsp }
2947 79109fed 2018-03-27 stsp
2948 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2949 44392932 2019-08-25 stsp if (err)
2950 44392932 2019-08-25 stsp goto done;
2951 79109fed 2018-03-27 stsp
2952 44392932 2019-08-25 stsp while (path[0] == '/')
2953 44392932 2019-08-25 stsp path++;
2954 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2955 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2956 44392932 2019-08-25 stsp done:
2957 44392932 2019-08-25 stsp if (blob1)
2958 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2959 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2960 44392932 2019-08-25 stsp return err;
2961 44392932 2019-08-25 stsp }
2962 44392932 2019-08-25 stsp
2963 44392932 2019-08-25 stsp static const struct got_error *
2964 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2965 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2966 63035f9f 2019-10-06 stsp struct got_repository *repo)
2967 44392932 2019-08-25 stsp {
2968 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2969 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2970 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2971 44392932 2019-08-25 stsp
2972 44392932 2019-08-25 stsp if (tree_id1) {
2973 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2974 79109fed 2018-03-27 stsp if (err)
2975 44392932 2019-08-25 stsp goto done;
2976 79109fed 2018-03-27 stsp }
2977 79109fed 2018-03-27 stsp
2978 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2979 0f2b3dca 2018-12-22 stsp if (err)
2980 0f2b3dca 2018-12-22 stsp goto done;
2981 0f2b3dca 2018-12-22 stsp
2982 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2983 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2984 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2985 44392932 2019-08-25 stsp while (path[0] == '/')
2986 44392932 2019-08-25 stsp path++;
2987 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2988 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2989 0f2b3dca 2018-12-22 stsp done:
2990 79109fed 2018-03-27 stsp if (tree1)
2991 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2992 366e0a5f 2019-10-10 stsp if (tree2)
2993 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2994 44392932 2019-08-25 stsp return err;
2995 44392932 2019-08-25 stsp }
2996 44392932 2019-08-25 stsp
2997 44392932 2019-08-25 stsp static const struct got_error *
2998 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2999 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3000 0208f208 2020-05-05 stsp {
3001 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3002 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3003 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3004 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3005 0208f208 2020-05-05 stsp
3006 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3007 0208f208 2020-05-05 stsp if (qid != NULL) {
3008 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3009 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3010 0208f208 2020-05-05 stsp qid->id);
3011 0208f208 2020-05-05 stsp if (err)
3012 0208f208 2020-05-05 stsp return err;
3013 0208f208 2020-05-05 stsp
3014 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3015 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3016 0208f208 2020-05-05 stsp
3017 0208f208 2020-05-05 stsp }
3018 0208f208 2020-05-05 stsp
3019 0208f208 2020-05-05 stsp if (tree_id1) {
3020 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3021 0208f208 2020-05-05 stsp if (err)
3022 0208f208 2020-05-05 stsp goto done;
3023 0208f208 2020-05-05 stsp }
3024 0208f208 2020-05-05 stsp
3025 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3026 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3027 0208f208 2020-05-05 stsp if (err)
3028 0208f208 2020-05-05 stsp goto done;
3029 0208f208 2020-05-05 stsp
3030 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3031 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3032 0208f208 2020-05-05 stsp done:
3033 0208f208 2020-05-05 stsp if (tree1)
3034 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3035 0208f208 2020-05-05 stsp if (tree2)
3036 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3037 0208f208 2020-05-05 stsp return err;
3038 0208f208 2020-05-05 stsp }
3039 0208f208 2020-05-05 stsp
3040 0208f208 2020-05-05 stsp static const struct got_error *
3041 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3042 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3043 44392932 2019-08-25 stsp {
3044 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3045 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3046 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3047 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3048 44392932 2019-08-25 stsp struct got_object_qid *qid;
3049 44392932 2019-08-25 stsp
3050 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3051 44392932 2019-08-25 stsp if (qid != NULL) {
3052 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3053 44392932 2019-08-25 stsp qid->id);
3054 44392932 2019-08-25 stsp if (err)
3055 44392932 2019-08-25 stsp return err;
3056 44392932 2019-08-25 stsp }
3057 44392932 2019-08-25 stsp
3058 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3059 44392932 2019-08-25 stsp int obj_type;
3060 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3061 44392932 2019-08-25 stsp if (err)
3062 44392932 2019-08-25 stsp goto done;
3063 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3064 44392932 2019-08-25 stsp if (err) {
3065 44392932 2019-08-25 stsp free(obj_id2);
3066 44392932 2019-08-25 stsp goto done;
3067 44392932 2019-08-25 stsp }
3068 44392932 2019-08-25 stsp if (pcommit) {
3069 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3070 44392932 2019-08-25 stsp qid->id, path);
3071 44392932 2019-08-25 stsp if (err) {
3072 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3073 2e8c69d1 2020-05-04 stsp free(obj_id2);
3074 2e8c69d1 2020-05-04 stsp goto done;
3075 2e8c69d1 2020-05-04 stsp }
3076 2e8c69d1 2020-05-04 stsp } else {
3077 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3078 2e8c69d1 2020-05-04 stsp if (err) {
3079 2e8c69d1 2020-05-04 stsp free(obj_id2);
3080 2e8c69d1 2020-05-04 stsp goto done;
3081 2e8c69d1 2020-05-04 stsp }
3082 44392932 2019-08-25 stsp }
3083 44392932 2019-08-25 stsp }
3084 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3085 44392932 2019-08-25 stsp if (err) {
3086 44392932 2019-08-25 stsp free(obj_id2);
3087 44392932 2019-08-25 stsp goto done;
3088 44392932 2019-08-25 stsp }
3089 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3090 44392932 2019-08-25 stsp switch (obj_type) {
3091 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3092 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3093 63035f9f 2019-10-06 stsp 0, repo);
3094 44392932 2019-08-25 stsp break;
3095 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3096 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3097 63035f9f 2019-10-06 stsp 0, repo);
3098 44392932 2019-08-25 stsp break;
3099 44392932 2019-08-25 stsp default:
3100 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3101 44392932 2019-08-25 stsp break;
3102 44392932 2019-08-25 stsp }
3103 44392932 2019-08-25 stsp free(obj_id1);
3104 44392932 2019-08-25 stsp free(obj_id2);
3105 44392932 2019-08-25 stsp } else {
3106 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3107 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3108 44392932 2019-08-25 stsp if (err)
3109 44392932 2019-08-25 stsp goto done;
3110 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3111 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3112 44392932 2019-08-25 stsp if (err)
3113 44392932 2019-08-25 stsp goto done;
3114 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3115 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3116 44392932 2019-08-25 stsp }
3117 44392932 2019-08-25 stsp done:
3118 0f2b3dca 2018-12-22 stsp free(id_str1);
3119 0f2b3dca 2018-12-22 stsp free(id_str2);
3120 44392932 2019-08-25 stsp if (pcommit)
3121 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3122 79109fed 2018-03-27 stsp return err;
3123 79109fed 2018-03-27 stsp }
3124 79109fed 2018-03-27 stsp
3125 4bb494d5 2018-06-16 stsp static char *
3126 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3127 6c281f94 2018-06-11 stsp {
3128 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3129 09867e48 2019-08-13 stsp char *p, *s;
3130 09867e48 2019-08-13 stsp
3131 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3132 09867e48 2019-08-13 stsp if (tm == NULL)
3133 09867e48 2019-08-13 stsp return NULL;
3134 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3135 09867e48 2019-08-13 stsp if (s == NULL)
3136 09867e48 2019-08-13 stsp return NULL;
3137 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3138 6c281f94 2018-06-11 stsp if (p)
3139 6c281f94 2018-06-11 stsp *p = '\0';
3140 6c281f94 2018-06-11 stsp return s;
3141 6c281f94 2018-06-11 stsp }
3142 dc424a06 2019-08-07 stsp
3143 6841bf13 2019-11-29 kn static const struct got_error *
3144 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3145 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3146 6841bf13 2019-11-29 kn {
3147 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3148 6841bf13 2019-11-29 kn regmatch_t regmatch;
3149 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3150 6841bf13 2019-11-29 kn
3151 6841bf13 2019-11-29 kn *have_match = 0;
3152 6841bf13 2019-11-29 kn
3153 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3154 6841bf13 2019-11-29 kn if (err)
3155 6841bf13 2019-11-29 kn return err;
3156 6841bf13 2019-11-29 kn
3157 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3158 6841bf13 2019-11-29 kn if (err)
3159 6841bf13 2019-11-29 kn goto done;
3160 6841bf13 2019-11-29 kn
3161 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3162 6841bf13 2019-11-29 kn *have_match = 1;
3163 6841bf13 2019-11-29 kn done:
3164 6841bf13 2019-11-29 kn free(id_str);
3165 6841bf13 2019-11-29 kn free(logmsg);
3166 6841bf13 2019-11-29 kn return err;
3167 6841bf13 2019-11-29 kn }
3168 6841bf13 2019-11-29 kn
3169 0208f208 2020-05-05 stsp static void
3170 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3171 0208f208 2020-05-05 stsp regex_t *regex)
3172 0208f208 2020-05-05 stsp {
3173 0208f208 2020-05-05 stsp regmatch_t regmatch;
3174 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3175 0208f208 2020-05-05 stsp
3176 0208f208 2020-05-05 stsp *have_match = 0;
3177 0208f208 2020-05-05 stsp
3178 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3179 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3180 0208f208 2020-05-05 stsp *have_match = 1;
3181 0208f208 2020-05-05 stsp break;
3182 0208f208 2020-05-05 stsp }
3183 0208f208 2020-05-05 stsp }
3184 0208f208 2020-05-05 stsp }
3185 0208f208 2020-05-05 stsp
3186 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3187 6c281f94 2018-06-11 stsp
3188 79109fed 2018-03-27 stsp static const struct got_error *
3189 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3190 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3191 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3192 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3193 79109fed 2018-03-27 stsp {
3194 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3195 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3196 6c281f94 2018-06-11 stsp char datebuf[26];
3197 45d799e2 2018-12-23 stsp time_t committer_time;
3198 45d799e2 2018-12-23 stsp const char *author, *committer;
3199 199a4027 2019-02-02 stsp char *refs_str = NULL;
3200 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3201 5c860e29 2018-03-12 stsp
3202 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3203 199a4027 2019-02-02 stsp char *s;
3204 199a4027 2019-02-02 stsp const char *name;
3205 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3206 a436ad14 2019-08-13 stsp int cmp;
3207 a436ad14 2019-08-13 stsp
3208 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3209 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3210 d9498b20 2019-02-05 stsp continue;
3211 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3212 199a4027 2019-02-02 stsp name += 5;
3213 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3214 7143d404 2019-03-12 stsp continue;
3215 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3216 e34f9ed6 2019-02-02 stsp name += 6;
3217 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3218 141c2bff 2019-02-04 stsp name += 8;
3219 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3220 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3221 4343a07f 2020-04-24 stsp continue;
3222 4343a07f 2020-04-24 stsp }
3223 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3224 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3225 5d844a1e 2019-08-13 stsp if (err) {
3226 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3227 5d844a1e 2019-08-13 stsp return err;
3228 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3229 5d844a1e 2019-08-13 stsp err = NULL;
3230 5d844a1e 2019-08-13 stsp tag = NULL;
3231 5d844a1e 2019-08-13 stsp }
3232 a436ad14 2019-08-13 stsp }
3233 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3234 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3235 a436ad14 2019-08-13 stsp if (tag)
3236 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3237 a436ad14 2019-08-13 stsp if (cmp != 0)
3238 a436ad14 2019-08-13 stsp continue;
3239 199a4027 2019-02-02 stsp s = refs_str;
3240 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3241 199a4027 2019-02-02 stsp name) == -1) {
3242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3243 199a4027 2019-02-02 stsp free(s);
3244 34ca4898 2019-08-13 stsp return err;
3245 199a4027 2019-02-02 stsp }
3246 199a4027 2019-02-02 stsp free(s);
3247 199a4027 2019-02-02 stsp }
3248 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3249 8bf5b3c9 2018-03-17 stsp if (err)
3250 8bf5b3c9 2018-03-17 stsp return err;
3251 788c352e 2018-06-16 stsp
3252 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3253 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3254 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3255 832c249c 2018-06-10 stsp free(id_str);
3256 d3d493d7 2019-02-21 stsp id_str = NULL;
3257 d3d493d7 2019-02-21 stsp free(refs_str);
3258 d3d493d7 2019-02-21 stsp refs_str = NULL;
3259 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3260 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3261 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3262 09867e48 2019-08-13 stsp if (datestr)
3263 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3264 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3265 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3266 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3267 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3268 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3269 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3270 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3271 3fe1abad 2018-06-10 stsp int n = 1;
3272 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3273 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3274 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3275 a0603db2 2018-06-10 stsp if (err)
3276 a0603db2 2018-06-10 stsp return err;
3277 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3278 a0603db2 2018-06-10 stsp free(id_str);
3279 a0603db2 2018-06-10 stsp }
3280 a0603db2 2018-06-10 stsp }
3281 832c249c 2018-06-10 stsp
3282 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3283 5943eee2 2019-08-13 stsp if (err)
3284 5943eee2 2019-08-13 stsp return err;
3285 8bf5b3c9 2018-03-17 stsp
3286 621015ac 2018-07-23 stsp logmsg = logmsg0;
3287 832c249c 2018-06-10 stsp do {
3288 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3289 832c249c 2018-06-10 stsp if (line)
3290 832c249c 2018-06-10 stsp printf(" %s\n", line);
3291 832c249c 2018-06-10 stsp } while (line);
3292 621015ac 2018-07-23 stsp free(logmsg0);
3293 832c249c 2018-06-10 stsp
3294 0208f208 2020-05-05 stsp if (changed_paths) {
3295 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3296 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3297 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3298 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3299 0208f208 2020-05-05 stsp }
3300 0208f208 2020-05-05 stsp printf("\n");
3301 0208f208 2020-05-05 stsp }
3302 971751ac 2018-03-27 stsp if (show_patch) {
3303 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3304 971751ac 2018-03-27 stsp if (err == 0)
3305 971751ac 2018-03-27 stsp printf("\n");
3306 971751ac 2018-03-27 stsp }
3307 07862c20 2018-09-15 stsp
3308 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3309 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3310 07862c20 2018-09-15 stsp return err;
3311 f42b1b34 2018-03-12 stsp }
3312 5c860e29 2018-03-12 stsp
3313 f42b1b34 2018-03-12 stsp static const struct got_error *
3314 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3315 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3316 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3317 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3318 f42b1b34 2018-03-12 stsp {
3319 f42b1b34 2018-03-12 stsp const struct got_error *err;
3320 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3321 6841bf13 2019-11-29 kn regex_t regex;
3322 6841bf13 2019-11-29 kn int have_match;
3323 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3324 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3325 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3326 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3327 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3328 dbec59df 2020-04-18 stsp
3329 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3330 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3331 372ccdbb 2018-06-10 stsp
3332 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3333 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3334 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3335 6841bf13 2019-11-29 kn
3336 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3337 f42b1b34 2018-03-12 stsp if (err)
3338 f42b1b34 2018-03-12 stsp return err;
3339 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3340 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3341 372ccdbb 2018-06-10 stsp if (err)
3342 fcc85cad 2018-07-22 stsp goto done;
3343 656b1f76 2019-05-11 jcs for (;;) {
3344 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3345 8bf5b3c9 2018-03-17 stsp
3346 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3347 84453469 2018-11-11 stsp break;
3348 84453469 2018-11-11 stsp
3349 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3350 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3351 372ccdbb 2018-06-10 stsp if (err) {
3352 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3353 9ba79e04 2018-06-11 stsp err = NULL;
3354 ee780d5c 2020-01-04 stsp break;
3355 7e665116 2018-04-02 stsp }
3356 b43fbaa0 2018-06-11 stsp if (id == NULL)
3357 7e665116 2018-04-02 stsp break;
3358 b43fbaa0 2018-06-11 stsp
3359 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3360 b43fbaa0 2018-06-11 stsp if (err)
3361 fcc85cad 2018-07-22 stsp break;
3362 6841bf13 2019-11-29 kn
3363 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3364 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3365 0208f208 2020-05-05 stsp if (err)
3366 0208f208 2020-05-05 stsp break;
3367 0208f208 2020-05-05 stsp }
3368 0208f208 2020-05-05 stsp
3369 6841bf13 2019-11-29 kn if (search_pattern) {
3370 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3371 6841bf13 2019-11-29 kn if (err) {
3372 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3373 6841bf13 2019-11-29 kn break;
3374 6841bf13 2019-11-29 kn }
3375 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3376 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3377 0208f208 2020-05-05 stsp &changed_paths, &regex);
3378 6841bf13 2019-11-29 kn if (have_match == 0) {
3379 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3380 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3381 0208f208 2020-05-05 stsp free((char *)pe->path);
3382 0208f208 2020-05-05 stsp free(pe->data);
3383 0208f208 2020-05-05 stsp }
3384 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3385 6841bf13 2019-11-29 kn continue;
3386 6841bf13 2019-11-29 kn }
3387 6841bf13 2019-11-29 kn }
3388 6841bf13 2019-11-29 kn
3389 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3390 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3391 dbec59df 2020-04-18 stsp if (err)
3392 dbec59df 2020-04-18 stsp break;
3393 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3394 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3395 dbec59df 2020-04-18 stsp } else {
3396 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3397 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3398 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3399 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3400 dbec59df 2020-04-18 stsp if (err)
3401 dbec59df 2020-04-18 stsp break;
3402 dbec59df 2020-04-18 stsp }
3403 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3404 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3405 7e665116 2018-04-02 stsp break;
3406 0208f208 2020-05-05 stsp
3407 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3408 0208f208 2020-05-05 stsp free((char *)pe->path);
3409 0208f208 2020-05-05 stsp free(pe->data);
3410 0208f208 2020-05-05 stsp }
3411 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3412 31cedeaf 2018-09-15 stsp }
3413 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3414 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3415 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3416 dbec59df 2020-04-18 stsp if (err)
3417 dbec59df 2020-04-18 stsp break;
3418 502b9684 2020-07-31 stsp if (show_changed_paths) {
3419 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3420 502b9684 2020-07-31 stsp commit, repo);
3421 502b9684 2020-07-31 stsp if (err)
3422 502b9684 2020-07-31 stsp break;
3423 502b9684 2020-07-31 stsp }
3424 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3425 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3426 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3427 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3428 dbec59df 2020-04-18 stsp if (err)
3429 dbec59df 2020-04-18 stsp break;
3430 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3431 502b9684 2020-07-31 stsp free((char *)pe->path);
3432 502b9684 2020-07-31 stsp free(pe->data);
3433 502b9684 2020-07-31 stsp }
3434 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3435 dbec59df 2020-04-18 stsp }
3436 dbec59df 2020-04-18 stsp }
3437 fcc85cad 2018-07-22 stsp done:
3438 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3439 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3440 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3441 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3442 dbec59df 2020-04-18 stsp }
3443 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3444 0208f208 2020-05-05 stsp free((char *)pe->path);
3445 0208f208 2020-05-05 stsp free(pe->data);
3446 0208f208 2020-05-05 stsp }
3447 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3448 6841bf13 2019-11-29 kn if (search_pattern)
3449 6841bf13 2019-11-29 kn regfree(&regex);
3450 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3451 f42b1b34 2018-03-12 stsp return err;
3452 f42b1b34 2018-03-12 stsp }
3453 5c860e29 2018-03-12 stsp
3454 4ed7e80c 2018-05-20 stsp __dead static void
3455 6f3d1eb0 2018-03-12 stsp usage_log(void)
3456 6f3d1eb0 2018-03-12 stsp {
3457 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3458 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3459 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3460 6f3d1eb0 2018-03-12 stsp exit(1);
3461 b1ebc001 2019-08-13 stsp }
3462 b1ebc001 2019-08-13 stsp
3463 b1ebc001 2019-08-13 stsp static int
3464 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3465 b1ebc001 2019-08-13 stsp {
3466 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3467 b1ebc001 2019-08-13 stsp long long n;
3468 b1ebc001 2019-08-13 stsp const char *errstr;
3469 b1ebc001 2019-08-13 stsp
3470 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3471 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3472 b1ebc001 2019-08-13 stsp return 0;
3473 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3474 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3475 b1ebc001 2019-08-13 stsp return 0;
3476 b1ebc001 2019-08-13 stsp return n;
3477 d1fe46f9 2020-04-18 stsp }
3478 d1fe46f9 2020-04-18 stsp
3479 d1fe46f9 2020-04-18 stsp static const struct got_error *
3480 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3481 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3482 d1fe46f9 2020-04-18 stsp {
3483 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3484 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3485 d1fe46f9 2020-04-18 stsp
3486 d1fe46f9 2020-04-18 stsp *id = NULL;
3487 d1fe46f9 2020-04-18 stsp
3488 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3489 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3490 d1fe46f9 2020-04-18 stsp int obj_type;
3491 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3492 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3493 d1fe46f9 2020-04-18 stsp if (err)
3494 d1fe46f9 2020-04-18 stsp return err;
3495 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3496 d1fe46f9 2020-04-18 stsp if (err)
3497 d1fe46f9 2020-04-18 stsp return err;
3498 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3499 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3500 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3501 d1fe46f9 2020-04-18 stsp if (err)
3502 d1fe46f9 2020-04-18 stsp return err;
3503 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3504 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3505 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3506 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3507 d1fe46f9 2020-04-18 stsp }
3508 d1fe46f9 2020-04-18 stsp free(*id);
3509 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3510 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3511 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3512 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3513 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3514 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3515 d1fe46f9 2020-04-18 stsp if (err)
3516 d1fe46f9 2020-04-18 stsp return err;
3517 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3518 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3519 d1fe46f9 2020-04-18 stsp } else {
3520 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3521 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3522 d1fe46f9 2020-04-18 stsp }
3523 d1fe46f9 2020-04-18 stsp
3524 d1fe46f9 2020-04-18 stsp return err;
3525 6f3d1eb0 2018-03-12 stsp }
3526 6f3d1eb0 2018-03-12 stsp
3527 4ed7e80c 2018-05-20 stsp static const struct got_error *
3528 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3529 f42b1b34 2018-03-12 stsp {
3530 f42b1b34 2018-03-12 stsp const struct got_error *error;
3531 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3532 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3533 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3534 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3535 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3536 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3537 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3538 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3539 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3540 64a96a6d 2018-04-01 stsp const char *errstr;
3541 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3542 1b3893a2 2019-03-18 stsp
3543 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3544 5c860e29 2018-03-12 stsp
3545 6715a751 2018-03-16 stsp #ifndef PROFILE
3546 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3547 6098196c 2019-01-04 stsp NULL)
3548 ad242220 2018-09-08 stsp == -1)
3549 f42b1b34 2018-03-12 stsp err(1, "pledge");
3550 6715a751 2018-03-16 stsp #endif
3551 79109fed 2018-03-27 stsp
3552 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3553 b1ebc001 2019-08-13 stsp
3554 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3555 79109fed 2018-03-27 stsp switch (ch) {
3556 79109fed 2018-03-27 stsp case 'p':
3557 79109fed 2018-03-27 stsp show_patch = 1;
3558 d142fc45 2018-04-01 stsp break;
3559 0208f208 2020-05-05 stsp case 'P':
3560 0208f208 2020-05-05 stsp show_changed_paths = 1;
3561 0208f208 2020-05-05 stsp break;
3562 d142fc45 2018-04-01 stsp case 'c':
3563 d142fc45 2018-04-01 stsp start_commit = optarg;
3564 64a96a6d 2018-04-01 stsp break;
3565 c0cc5c62 2018-10-18 stsp case 'C':
3566 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3567 4a8520aa 2018-10-18 stsp &errstr);
3568 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3569 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3570 c0cc5c62 2018-10-18 stsp break;
3571 64a96a6d 2018-04-01 stsp case 'l':
3572 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3573 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3574 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3575 cc54c501 2019-07-15 stsp break;
3576 48c8c60d 2020-01-27 stsp case 'b':
3577 48c8c60d 2020-01-27 stsp log_branches = 1;
3578 a0603db2 2018-06-10 stsp break;
3579 04ca23f4 2018-07-16 stsp case 'r':
3580 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3581 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3582 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3583 9ba1d308 2019-10-21 stsp optarg);
3584 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3585 04ca23f4 2018-07-16 stsp break;
3586 dbec59df 2020-04-18 stsp case 'R':
3587 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3588 dbec59df 2020-04-18 stsp break;
3589 6841bf13 2019-11-29 kn case 's':
3590 6841bf13 2019-11-29 kn search_pattern = optarg;
3591 6841bf13 2019-11-29 kn break;
3592 d1fe46f9 2020-04-18 stsp case 'x':
3593 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3594 d1fe46f9 2020-04-18 stsp break;
3595 79109fed 2018-03-27 stsp default:
3596 2deda0b9 2019-03-07 stsp usage_log();
3597 79109fed 2018-03-27 stsp /* NOTREACHED */
3598 79109fed 2018-03-27 stsp }
3599 79109fed 2018-03-27 stsp }
3600 79109fed 2018-03-27 stsp
3601 79109fed 2018-03-27 stsp argc -= optind;
3602 79109fed 2018-03-27 stsp argv += optind;
3603 f42b1b34 2018-03-12 stsp
3604 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3605 dc1edbfa 2019-11-29 kn diff_context = 3;
3606 dc1edbfa 2019-11-29 kn else if (!show_patch)
3607 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3608 dc1edbfa 2019-11-29 kn
3609 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3610 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3611 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3612 04ca23f4 2018-07-16 stsp goto done;
3613 04ca23f4 2018-07-16 stsp }
3614 cffc0aa4 2019-02-05 stsp
3615 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3616 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3617 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3618 50f2fada 2020-04-24 stsp goto done;
3619 50f2fada 2020-04-24 stsp error = NULL;
3620 50f2fada 2020-04-24 stsp }
3621 cffc0aa4 2019-02-05 stsp
3622 e7301579 2019-03-18 stsp if (argc == 0) {
3623 cbd1af7a 2019-03-18 stsp path = strdup("");
3624 e7301579 2019-03-18 stsp if (path == NULL) {
3625 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3626 cbd1af7a 2019-03-18 stsp goto done;
3627 e7301579 2019-03-18 stsp }
3628 e7301579 2019-03-18 stsp } else if (argc == 1) {
3629 e7301579 2019-03-18 stsp if (worktree) {
3630 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3631 e7301579 2019-03-18 stsp argv[0]);
3632 e7301579 2019-03-18 stsp if (error)
3633 e7301579 2019-03-18 stsp goto done;
3634 e7301579 2019-03-18 stsp } else {
3635 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3636 e7301579 2019-03-18 stsp if (path == NULL) {
3637 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3638 e7301579 2019-03-18 stsp goto done;
3639 e7301579 2019-03-18 stsp }
3640 e7301579 2019-03-18 stsp }
3641 cbd1af7a 2019-03-18 stsp } else
3642 cbd1af7a 2019-03-18 stsp usage_log();
3643 cbd1af7a 2019-03-18 stsp
3644 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3645 5486daa2 2019-05-11 stsp repo_path = worktree ?
3646 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3647 5486daa2 2019-05-11 stsp }
3648 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3649 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3650 cffc0aa4 2019-02-05 stsp goto done;
3651 04ca23f4 2018-07-16 stsp }
3652 6098196c 2019-01-04 stsp
3653 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3654 d7d4f210 2018-03-12 stsp if (error != NULL)
3655 04ca23f4 2018-07-16 stsp goto done;
3656 f42b1b34 2018-03-12 stsp
3657 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3658 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3659 c02c541e 2019-03-29 stsp if (error)
3660 c02c541e 2019-03-29 stsp goto done;
3661 c02c541e 2019-03-29 stsp
3662 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3663 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3664 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3665 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3666 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3667 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
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_ref_resolve(&start_id, repo, head_ref);
3671 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3672 3235492e 2018-04-01 stsp if (error != NULL)
3673 d1fe46f9 2020-04-18 stsp goto done;
3674 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3675 d1fe46f9 2020-04-18 stsp start_id);
3676 d1fe46f9 2020-04-18 stsp if (error != NULL)
3677 d1fe46f9 2020-04-18 stsp goto done;
3678 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3679 3235492e 2018-04-01 stsp } else {
3680 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3681 d1fe46f9 2020-04-18 stsp if (error != NULL)
3682 d1fe46f9 2020-04-18 stsp goto done;
3683 3235492e 2018-04-01 stsp }
3684 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3685 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3686 d1fe46f9 2020-04-18 stsp if (error != NULL)
3687 d1fe46f9 2020-04-18 stsp goto done;
3688 d1fe46f9 2020-04-18 stsp }
3689 04ca23f4 2018-07-16 stsp
3690 deeabeae 2019-08-27 stsp if (worktree) {
3691 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3692 deeabeae 2019-08-27 stsp char *p;
3693 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3694 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3695 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3696 deeabeae 2019-08-27 stsp goto done;
3697 deeabeae 2019-08-27 stsp }
3698 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3699 deeabeae 2019-08-27 stsp free(p);
3700 deeabeae 2019-08-27 stsp } else
3701 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3702 04ca23f4 2018-07-16 stsp if (error != NULL)
3703 04ca23f4 2018-07-16 stsp goto done;
3704 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3705 04ca23f4 2018-07-16 stsp free(path);
3706 04ca23f4 2018-07-16 stsp path = in_repo_path;
3707 04ca23f4 2018-07-16 stsp }
3708 199a4027 2019-02-02 stsp
3709 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3710 199a4027 2019-02-02 stsp if (error)
3711 199a4027 2019-02-02 stsp goto done;
3712 04ca23f4 2018-07-16 stsp
3713 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3714 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3715 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3716 04ca23f4 2018-07-16 stsp done:
3717 04ca23f4 2018-07-16 stsp free(path);
3718 04ca23f4 2018-07-16 stsp free(repo_path);
3719 04ca23f4 2018-07-16 stsp free(cwd);
3720 cffc0aa4 2019-02-05 stsp if (worktree)
3721 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3722 ad242220 2018-09-08 stsp if (repo) {
3723 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3724 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3725 ad242220 2018-09-08 stsp if (error == NULL)
3726 ad242220 2018-09-08 stsp error = repo_error;
3727 ad242220 2018-09-08 stsp }
3728 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3729 8bf5b3c9 2018-03-17 stsp return error;
3730 5c860e29 2018-03-12 stsp }
3731 5c860e29 2018-03-12 stsp
3732 4ed7e80c 2018-05-20 stsp __dead static void
3733 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3734 3f8b7d6a 2018-04-01 stsp {
3735 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3736 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3737 3f8b7d6a 2018-04-01 stsp exit(1);
3738 b00d56cd 2018-04-01 stsp }
3739 b00d56cd 2018-04-01 stsp
3740 b72f483a 2019-02-05 stsp struct print_diff_arg {
3741 b72f483a 2019-02-05 stsp struct got_repository *repo;
3742 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3743 b72f483a 2019-02-05 stsp int diff_context;
3744 3fc0c068 2019-02-10 stsp const char *id_str;
3745 3fc0c068 2019-02-10 stsp int header_shown;
3746 98eaaa12 2019-08-03 stsp int diff_staged;
3747 63035f9f 2019-10-06 stsp int ignore_whitespace;
3748 b72f483a 2019-02-05 stsp };
3749 39449a05 2020-07-23 stsp
3750 39449a05 2020-07-23 stsp /*
3751 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3752 39449a05 2020-07-23 stsp * it as content to the diff engine.
3753 39449a05 2020-07-23 stsp */
3754 39449a05 2020-07-23 stsp static const struct got_error *
3755 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3756 39449a05 2020-07-23 stsp const char *abspath)
3757 39449a05 2020-07-23 stsp {
3758 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3759 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3760 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3761 39449a05 2020-07-23 stsp
3762 39449a05 2020-07-23 stsp *fd = -1;
3763 39449a05 2020-07-23 stsp
3764 39449a05 2020-07-23 stsp if (dirfd != -1) {
3765 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, 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("readlinkat", abspath);
3768 39449a05 2020-07-23 stsp } else {
3769 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3770 39449a05 2020-07-23 stsp if (target_len == -1)
3771 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3772 39449a05 2020-07-23 stsp }
3773 39449a05 2020-07-23 stsp
3774 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3775 39449a05 2020-07-23 stsp if (*fd == -1)
3776 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3777 39449a05 2020-07-23 stsp
3778 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3779 39449a05 2020-07-23 stsp if (outlen == -1) {
3780 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3781 39449a05 2020-07-23 stsp goto done;
3782 39449a05 2020-07-23 stsp }
3783 39449a05 2020-07-23 stsp
3784 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3785 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3786 39449a05 2020-07-23 stsp goto done;
3787 39449a05 2020-07-23 stsp }
3788 39449a05 2020-07-23 stsp done:
3789 39449a05 2020-07-23 stsp if (err) {
3790 39449a05 2020-07-23 stsp close(*fd);
3791 39449a05 2020-07-23 stsp *fd = -1;
3792 39449a05 2020-07-23 stsp }
3793 39449a05 2020-07-23 stsp return err;
3794 39449a05 2020-07-23 stsp }
3795 b72f483a 2019-02-05 stsp
3796 4ed7e80c 2018-05-20 stsp static const struct got_error *
3797 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3798 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3799 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3800 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3801 b72f483a 2019-02-05 stsp {
3802 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3803 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3804 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3805 12463d8b 2019-12-13 stsp int fd = -1;
3806 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3807 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3808 b72f483a 2019-02-05 stsp struct stat sb;
3809 b72f483a 2019-02-05 stsp
3810 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3811 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3812 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3813 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3814 98eaaa12 2019-08-03 stsp return NULL;
3815 98eaaa12 2019-08-03 stsp } else {
3816 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3817 98eaaa12 2019-08-03 stsp return NULL;
3818 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3819 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3820 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3821 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3822 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3823 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3824 98eaaa12 2019-08-03 stsp return NULL;
3825 98eaaa12 2019-08-03 stsp }
3826 3fc0c068 2019-02-10 stsp
3827 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3828 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3829 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3830 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3831 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3832 3fc0c068 2019-02-10 stsp }
3833 b72f483a 2019-02-05 stsp
3834 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3835 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3836 98eaaa12 2019-08-03 stsp switch (staged_status) {
3837 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3838 98eaaa12 2019-08-03 stsp label1 = path;
3839 98eaaa12 2019-08-03 stsp label2 = path;
3840 98eaaa12 2019-08-03 stsp break;
3841 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3842 98eaaa12 2019-08-03 stsp label2 = path;
3843 98eaaa12 2019-08-03 stsp break;
3844 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3845 98eaaa12 2019-08-03 stsp label1 = path;
3846 98eaaa12 2019-08-03 stsp break;
3847 98eaaa12 2019-08-03 stsp default:
3848 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3849 98eaaa12 2019-08-03 stsp }
3850 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3851 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3852 63035f9f 2019-10-06 stsp a->repo, stdout);
3853 98eaaa12 2019-08-03 stsp }
3854 98eaaa12 2019-08-03 stsp
3855 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3856 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3857 4ce46740 2019-08-08 stsp char *id_str;
3858 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3859 408b4ebc 2019-08-03 stsp 8192);
3860 4ce46740 2019-08-08 stsp if (err)
3861 4ce46740 2019-08-08 stsp goto done;
3862 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3863 4ce46740 2019-08-08 stsp if (err)
3864 4ce46740 2019-08-08 stsp goto done;
3865 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3866 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3867 4ce46740 2019-08-08 stsp free(id_str);
3868 4ce46740 2019-08-08 stsp goto done;
3869 4ce46740 2019-08-08 stsp }
3870 4ce46740 2019-08-08 stsp free(id_str);
3871 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3872 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3873 4ce46740 2019-08-08 stsp if (err)
3874 4ce46740 2019-08-08 stsp goto done;
3875 4ce46740 2019-08-08 stsp }
3876 d00136be 2019-03-26 stsp
3877 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3878 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3879 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3880 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3881 2ec1f75b 2019-03-26 stsp goto done;
3882 2ec1f75b 2019-03-26 stsp }
3883 b72f483a 2019-02-05 stsp
3884 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3885 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3886 12463d8b 2019-12-13 stsp if (fd == -1) {
3887 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3888 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
3889 39449a05 2020-07-23 stsp abspath);
3890 39449a05 2020-07-23 stsp goto done;
3891 39449a05 2020-07-23 stsp }
3892 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3893 39449a05 2020-07-23 stsp de_name, abspath);
3894 39449a05 2020-07-23 stsp if (err)
3895 39449a05 2020-07-23 stsp goto done;
3896 12463d8b 2019-12-13 stsp }
3897 12463d8b 2019-12-13 stsp } else {
3898 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3899 12463d8b 2019-12-13 stsp if (fd == -1) {
3900 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3901 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
3902 39449a05 2020-07-23 stsp abspath);
3903 39449a05 2020-07-23 stsp goto done;
3904 39449a05 2020-07-23 stsp }
3905 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3906 39449a05 2020-07-23 stsp de_name, abspath);
3907 39449a05 2020-07-23 stsp if (err)
3908 39449a05 2020-07-23 stsp goto done;
3909 12463d8b 2019-12-13 stsp }
3910 12463d8b 2019-12-13 stsp }
3911 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3912 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3913 2ec1f75b 2019-03-26 stsp goto done;
3914 2ec1f75b 2019-03-26 stsp }
3915 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3916 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3917 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3918 2ec1f75b 2019-03-26 stsp goto done;
3919 2ec1f75b 2019-03-26 stsp }
3920 12463d8b 2019-12-13 stsp fd = -1;
3921 2ec1f75b 2019-03-26 stsp } else
3922 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3923 b72f483a 2019-02-05 stsp
3924 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3925 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3926 b72f483a 2019-02-05 stsp done:
3927 b72f483a 2019-02-05 stsp if (blob1)
3928 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3929 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3930 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3931 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3932 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3933 b72f483a 2019-02-05 stsp free(abspath);
3934 927df6b7 2019-02-10 stsp return err;
3935 927df6b7 2019-02-10 stsp }
3936 927df6b7 2019-02-10 stsp
3937 927df6b7 2019-02-10 stsp static const struct got_error *
3938 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3939 b00d56cd 2018-04-01 stsp {
3940 b00d56cd 2018-04-01 stsp const struct got_error *error;
3941 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3942 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3943 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3944 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3945 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3946 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3947 15a94983 2018-12-23 stsp int type1, type2;
3948 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3949 c0cc5c62 2018-10-18 stsp const char *errstr;
3950 927df6b7 2019-02-10 stsp char *path = NULL;
3951 b00d56cd 2018-04-01 stsp
3952 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3953 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3954 25eccc22 2019-01-04 stsp NULL) == -1)
3955 b00d56cd 2018-04-01 stsp err(1, "pledge");
3956 b00d56cd 2018-04-01 stsp #endif
3957 b00d56cd 2018-04-01 stsp
3958 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3959 b00d56cd 2018-04-01 stsp switch (ch) {
3960 c0cc5c62 2018-10-18 stsp case 'C':
3961 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3962 dfcab68b 2019-11-29 kn &errstr);
3963 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3964 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3965 c0cc5c62 2018-10-18 stsp break;
3966 b72f483a 2019-02-05 stsp case 'r':
3967 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3968 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3969 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3970 9ba1d308 2019-10-21 stsp optarg);
3971 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3972 b72f483a 2019-02-05 stsp break;
3973 98eaaa12 2019-08-03 stsp case 's':
3974 98eaaa12 2019-08-03 stsp diff_staged = 1;
3975 63035f9f 2019-10-06 stsp break;
3976 63035f9f 2019-10-06 stsp case 'w':
3977 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3978 98eaaa12 2019-08-03 stsp break;
3979 b00d56cd 2018-04-01 stsp default:
3980 2deda0b9 2019-03-07 stsp usage_diff();
3981 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3982 b00d56cd 2018-04-01 stsp }
3983 b00d56cd 2018-04-01 stsp }
3984 b00d56cd 2018-04-01 stsp
3985 b00d56cd 2018-04-01 stsp argc -= optind;
3986 b00d56cd 2018-04-01 stsp argv += optind;
3987 b00d56cd 2018-04-01 stsp
3988 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3989 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3990 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3991 b72f483a 2019-02-05 stsp goto done;
3992 b72f483a 2019-02-05 stsp }
3993 927df6b7 2019-02-10 stsp if (argc <= 1) {
3994 b72f483a 2019-02-05 stsp if (repo_path)
3995 b72f483a 2019-02-05 stsp errx(1,
3996 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3997 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3998 fa51e947 2020-03-27 stsp if (error) {
3999 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4000 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4001 fa51e947 2020-03-27 stsp cwd);
4002 1f03b8da 2020-03-20 stsp goto done;
4003 fa51e947 2020-03-27 stsp }
4004 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4005 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4006 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4007 927df6b7 2019-02-10 stsp goto done;
4008 927df6b7 2019-02-10 stsp }
4009 927df6b7 2019-02-10 stsp if (argc == 1) {
4010 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4011 cbd1af7a 2019-03-18 stsp argv[0]);
4012 927df6b7 2019-02-10 stsp if (error)
4013 927df6b7 2019-02-10 stsp goto done;
4014 927df6b7 2019-02-10 stsp } else {
4015 927df6b7 2019-02-10 stsp path = strdup("");
4016 927df6b7 2019-02-10 stsp if (path == NULL) {
4017 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4018 927df6b7 2019-02-10 stsp goto done;
4019 927df6b7 2019-02-10 stsp }
4020 927df6b7 2019-02-10 stsp }
4021 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4022 98eaaa12 2019-08-03 stsp if (diff_staged)
4023 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4024 98eaaa12 2019-08-03 stsp "objects in repository");
4025 15a94983 2018-12-23 stsp id_str1 = argv[0];
4026 15a94983 2018-12-23 stsp id_str2 = argv[1];
4027 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4028 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4029 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4030 30db809c 2019-06-05 stsp goto done;
4031 1f03b8da 2020-03-20 stsp if (worktree) {
4032 1f03b8da 2020-03-20 stsp repo_path = strdup(
4033 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4034 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4035 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4036 1f03b8da 2020-03-20 stsp goto done;
4037 1f03b8da 2020-03-20 stsp }
4038 1f03b8da 2020-03-20 stsp } else {
4039 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4040 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4041 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4042 1f03b8da 2020-03-20 stsp goto done;
4043 1f03b8da 2020-03-20 stsp }
4044 30db809c 2019-06-05 stsp }
4045 30db809c 2019-06-05 stsp }
4046 b00d56cd 2018-04-01 stsp } else
4047 b00d56cd 2018-04-01 stsp usage_diff();
4048 b00d56cd 2018-04-01 stsp
4049 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4050 76089277 2018-04-01 stsp free(repo_path);
4051 b00d56cd 2018-04-01 stsp if (error != NULL)
4052 b00d56cd 2018-04-01 stsp goto done;
4053 b00d56cd 2018-04-01 stsp
4054 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4055 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4056 c02c541e 2019-03-29 stsp if (error)
4057 c02c541e 2019-03-29 stsp goto done;
4058 c02c541e 2019-03-29 stsp
4059 30db809c 2019-06-05 stsp if (argc <= 1) {
4060 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4061 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4062 b72f483a 2019-02-05 stsp char *id_str;
4063 72ea6654 2019-07-27 stsp
4064 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4065 72ea6654 2019-07-27 stsp
4066 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4067 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4068 b72f483a 2019-02-05 stsp if (error)
4069 b72f483a 2019-02-05 stsp goto done;
4070 b72f483a 2019-02-05 stsp arg.repo = repo;
4071 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4072 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4073 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4074 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4075 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4076 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4077 b72f483a 2019-02-05 stsp
4078 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4079 72ea6654 2019-07-27 stsp if (error)
4080 72ea6654 2019-07-27 stsp goto done;
4081 72ea6654 2019-07-27 stsp
4082 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4083 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4084 b72f483a 2019-02-05 stsp free(id_str);
4085 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4086 b72f483a 2019-02-05 stsp goto done;
4087 b72f483a 2019-02-05 stsp }
4088 b72f483a 2019-02-05 stsp
4089 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4090 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4091 0adb7196 2019-08-11 stsp if (error)
4092 0adb7196 2019-08-11 stsp goto done;
4093 b00d56cd 2018-04-01 stsp
4094 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4095 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4096 0adb7196 2019-08-11 stsp if (error)
4097 0adb7196 2019-08-11 stsp goto done;
4098 b00d56cd 2018-04-01 stsp
4099 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4100 15a94983 2018-12-23 stsp if (error)
4101 15a94983 2018-12-23 stsp goto done;
4102 15a94983 2018-12-23 stsp
4103 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4104 15a94983 2018-12-23 stsp if (error)
4105 15a94983 2018-12-23 stsp goto done;
4106 15a94983 2018-12-23 stsp
4107 15a94983 2018-12-23 stsp if (type1 != type2) {
4108 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4109 b00d56cd 2018-04-01 stsp goto done;
4110 b00d56cd 2018-04-01 stsp }
4111 b00d56cd 2018-04-01 stsp
4112 15a94983 2018-12-23 stsp switch (type1) {
4113 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4114 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4115 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4116 b00d56cd 2018-04-01 stsp break;
4117 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4118 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4119 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4120 b00d56cd 2018-04-01 stsp break;
4121 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4122 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4123 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4124 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4125 b00d56cd 2018-04-01 stsp break;
4126 b00d56cd 2018-04-01 stsp default:
4127 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4128 b00d56cd 2018-04-01 stsp }
4129 b00d56cd 2018-04-01 stsp done:
4130 5e70831e 2019-05-28 stsp free(label1);
4131 5e70831e 2019-05-28 stsp free(label2);
4132 15a94983 2018-12-23 stsp free(id1);
4133 15a94983 2018-12-23 stsp free(id2);
4134 927df6b7 2019-02-10 stsp free(path);
4135 b72f483a 2019-02-05 stsp if (worktree)
4136 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4137 ad242220 2018-09-08 stsp if (repo) {
4138 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4139 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4140 ad242220 2018-09-08 stsp if (error == NULL)
4141 ad242220 2018-09-08 stsp error = repo_error;
4142 ad242220 2018-09-08 stsp }
4143 b00d56cd 2018-04-01 stsp return error;
4144 404c43c4 2018-06-21 stsp }
4145 404c43c4 2018-06-21 stsp
4146 404c43c4 2018-06-21 stsp __dead static void
4147 404c43c4 2018-06-21 stsp usage_blame(void)
4148 404c43c4 2018-06-21 stsp {
4149 9270e621 2019-02-05 stsp fprintf(stderr,
4150 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4151 404c43c4 2018-06-21 stsp getprogname());
4152 404c43c4 2018-06-21 stsp exit(1);
4153 b00d56cd 2018-04-01 stsp }
4154 b00d56cd 2018-04-01 stsp
4155 28315671 2019-08-14 stsp struct blame_line {
4156 28315671 2019-08-14 stsp int annotated;
4157 28315671 2019-08-14 stsp char *id_str;
4158 82f6abb8 2019-08-14 stsp char *committer;
4159 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4160 28315671 2019-08-14 stsp };
4161 28315671 2019-08-14 stsp
4162 28315671 2019-08-14 stsp struct blame_cb_args {
4163 28315671 2019-08-14 stsp struct blame_line *lines;
4164 28315671 2019-08-14 stsp int nlines;
4165 7ef28ff8 2019-08-14 stsp int nlines_prec;
4166 28315671 2019-08-14 stsp int lineno_cur;
4167 28315671 2019-08-14 stsp off_t *line_offsets;
4168 28315671 2019-08-14 stsp FILE *f;
4169 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4170 28315671 2019-08-14 stsp };
4171 28315671 2019-08-14 stsp
4172 404c43c4 2018-06-21 stsp static const struct got_error *
4173 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4174 28315671 2019-08-14 stsp {
4175 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4176 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4177 28315671 2019-08-14 stsp struct blame_line *bline;
4178 82f6abb8 2019-08-14 stsp char *line = NULL;
4179 28315671 2019-08-14 stsp size_t linesize = 0;
4180 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4181 28315671 2019-08-14 stsp off_t offset;
4182 bcb49d15 2019-08-14 stsp struct tm tm;
4183 bcb49d15 2019-08-14 stsp time_t committer_time;
4184 28315671 2019-08-14 stsp
4185 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4186 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4187 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4188 28315671 2019-08-14 stsp
4189 28315671 2019-08-14 stsp if (sigint_received)
4190 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4191 28315671 2019-08-14 stsp
4192 2839f8b9 2019-08-18 stsp if (lineno == -1)
4193 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4194 2839f8b9 2019-08-18 stsp
4195 28315671 2019-08-14 stsp /* Annotate this line. */
4196 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4197 28315671 2019-08-14 stsp if (bline->annotated)
4198 28315671 2019-08-14 stsp return NULL;
4199 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4200 28315671 2019-08-14 stsp if (err)
4201 28315671 2019-08-14 stsp return err;
4202 82f6abb8 2019-08-14 stsp
4203 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4204 82f6abb8 2019-08-14 stsp if (err)
4205 82f6abb8 2019-08-14 stsp goto done;
4206 82f6abb8 2019-08-14 stsp
4207 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4208 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4209 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4210 bcb49d15 2019-08-14 stsp goto done;
4211 bcb49d15 2019-08-14 stsp }
4212 bcb49d15 2019-08-14 stsp
4213 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4214 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4215 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4216 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4217 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4218 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4219 82f6abb8 2019-08-14 stsp goto done;
4220 82f6abb8 2019-08-14 stsp }
4221 28315671 2019-08-14 stsp bline->annotated = 1;
4222 28315671 2019-08-14 stsp
4223 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4224 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4225 28315671 2019-08-14 stsp if (!bline->annotated)
4226 82f6abb8 2019-08-14 stsp goto done;
4227 28315671 2019-08-14 stsp
4228 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4229 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4230 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4231 82f6abb8 2019-08-14 stsp goto done;
4232 82f6abb8 2019-08-14 stsp }
4233 28315671 2019-08-14 stsp
4234 28315671 2019-08-14 stsp while (bline->annotated) {
4235 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4236 82f6abb8 2019-08-14 stsp size_t len;
4237 82f6abb8 2019-08-14 stsp
4238 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4239 28315671 2019-08-14 stsp if (ferror(a->f))
4240 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4241 28315671 2019-08-14 stsp break;
4242 28315671 2019-08-14 stsp }
4243 82f6abb8 2019-08-14 stsp
4244 82f6abb8 2019-08-14 stsp committer = bline->committer;
4245 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4246 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4247 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4248 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4249 82f6abb8 2019-08-14 stsp if (at)
4250 82f6abb8 2019-08-14 stsp *at = '\0';
4251 82f6abb8 2019-08-14 stsp len = strlen(committer);
4252 82f6abb8 2019-08-14 stsp if (len >= 9)
4253 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4254 28315671 2019-08-14 stsp
4255 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4256 28315671 2019-08-14 stsp if (nl)
4257 28315671 2019-08-14 stsp *nl = '\0';
4258 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4259 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4260 28315671 2019-08-14 stsp
4261 28315671 2019-08-14 stsp a->lineno_cur++;
4262 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4263 28315671 2019-08-14 stsp }
4264 82f6abb8 2019-08-14 stsp done:
4265 82f6abb8 2019-08-14 stsp if (commit)
4266 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4267 28315671 2019-08-14 stsp free(line);
4268 28315671 2019-08-14 stsp return err;
4269 28315671 2019-08-14 stsp }
4270 28315671 2019-08-14 stsp
4271 28315671 2019-08-14 stsp static const struct got_error *
4272 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4273 404c43c4 2018-06-21 stsp {
4274 404c43c4 2018-06-21 stsp const struct got_error *error;
4275 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4276 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4277 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4278 0587e10c 2020-07-23 stsp char *link_target = NULL;
4279 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4280 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4281 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4282 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4283 28315671 2019-08-14 stsp struct blame_cb_args bca;
4284 28315671 2019-08-14 stsp int ch, obj_type, i;
4285 b02560ec 2019-08-19 stsp size_t filesize;
4286 90f3c347 2019-08-19 stsp
4287 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4288 404c43c4 2018-06-21 stsp
4289 404c43c4 2018-06-21 stsp #ifndef PROFILE
4290 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4291 36e2fb66 2019-01-04 stsp NULL) == -1)
4292 404c43c4 2018-06-21 stsp err(1, "pledge");
4293 404c43c4 2018-06-21 stsp #endif
4294 404c43c4 2018-06-21 stsp
4295 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4296 404c43c4 2018-06-21 stsp switch (ch) {
4297 404c43c4 2018-06-21 stsp case 'c':
4298 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4299 404c43c4 2018-06-21 stsp break;
4300 66bea077 2018-08-02 stsp case 'r':
4301 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4302 66bea077 2018-08-02 stsp if (repo_path == NULL)
4303 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4304 9ba1d308 2019-10-21 stsp optarg);
4305 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4306 66bea077 2018-08-02 stsp break;
4307 404c43c4 2018-06-21 stsp default:
4308 2deda0b9 2019-03-07 stsp usage_blame();
4309 404c43c4 2018-06-21 stsp /* NOTREACHED */
4310 404c43c4 2018-06-21 stsp }
4311 404c43c4 2018-06-21 stsp }
4312 404c43c4 2018-06-21 stsp
4313 404c43c4 2018-06-21 stsp argc -= optind;
4314 404c43c4 2018-06-21 stsp argv += optind;
4315 404c43c4 2018-06-21 stsp
4316 a39318fd 2018-08-02 stsp if (argc == 1)
4317 404c43c4 2018-06-21 stsp path = argv[0];
4318 a39318fd 2018-08-02 stsp else
4319 404c43c4 2018-06-21 stsp usage_blame();
4320 404c43c4 2018-06-21 stsp
4321 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4322 66bea077 2018-08-02 stsp if (cwd == NULL) {
4323 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4324 66bea077 2018-08-02 stsp goto done;
4325 66bea077 2018-08-02 stsp }
4326 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4327 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4328 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4329 66bea077 2018-08-02 stsp goto done;
4330 0c06baac 2019-02-05 stsp else
4331 0c06baac 2019-02-05 stsp error = NULL;
4332 0c06baac 2019-02-05 stsp if (worktree) {
4333 0c06baac 2019-02-05 stsp repo_path =
4334 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4335 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4336 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4337 1f03b8da 2020-03-20 stsp if (error)
4338 1f03b8da 2020-03-20 stsp goto done;
4339 1f03b8da 2020-03-20 stsp }
4340 0c06baac 2019-02-05 stsp } else {
4341 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4342 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4343 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4344 0c06baac 2019-02-05 stsp goto done;
4345 0c06baac 2019-02-05 stsp }
4346 66bea077 2018-08-02 stsp }
4347 66bea077 2018-08-02 stsp }
4348 36e2fb66 2019-01-04 stsp
4349 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4350 c02c541e 2019-03-29 stsp if (error != NULL)
4351 36e2fb66 2019-01-04 stsp goto done;
4352 66bea077 2018-08-02 stsp
4353 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4354 c02c541e 2019-03-29 stsp if (error)
4355 404c43c4 2018-06-21 stsp goto done;
4356 404c43c4 2018-06-21 stsp
4357 0c06baac 2019-02-05 stsp if (worktree) {
4358 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4359 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4360 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4361 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4362 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4363 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4364 6efaaa2d 2019-02-05 stsp path) == -1) {
4365 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4366 0c06baac 2019-02-05 stsp goto done;
4367 0c06baac 2019-02-05 stsp }
4368 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4369 0c06baac 2019-02-05 stsp free(p);
4370 0c06baac 2019-02-05 stsp } else {
4371 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4372 0c06baac 2019-02-05 stsp }
4373 0c06baac 2019-02-05 stsp if (error)
4374 66bea077 2018-08-02 stsp goto done;
4375 66bea077 2018-08-02 stsp
4376 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4377 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4378 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4379 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4380 404c43c4 2018-06-21 stsp if (error != NULL)
4381 66bea077 2018-08-02 stsp goto done;
4382 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4383 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4384 404c43c4 2018-06-21 stsp if (error != NULL)
4385 66bea077 2018-08-02 stsp goto done;
4386 404c43c4 2018-06-21 stsp } else {
4387 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4388 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4389 30837e32 2019-07-25 stsp if (error)
4390 66bea077 2018-08-02 stsp goto done;
4391 404c43c4 2018-06-21 stsp }
4392 404c43c4 2018-06-21 stsp
4393 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4394 0587e10c 2020-07-23 stsp commit_id, repo);
4395 0587e10c 2020-07-23 stsp if (error)
4396 0587e10c 2020-07-23 stsp goto done;
4397 0587e10c 2020-07-23 stsp
4398 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4399 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4400 28315671 2019-08-14 stsp if (error)
4401 28315671 2019-08-14 stsp goto done;
4402 28315671 2019-08-14 stsp
4403 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4404 28315671 2019-08-14 stsp if (error)
4405 28315671 2019-08-14 stsp goto done;
4406 28315671 2019-08-14 stsp
4407 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4408 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4409 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4410 28315671 2019-08-14 stsp goto done;
4411 28315671 2019-08-14 stsp }
4412 28315671 2019-08-14 stsp
4413 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4414 28315671 2019-08-14 stsp if (error)
4415 28315671 2019-08-14 stsp goto done;
4416 28315671 2019-08-14 stsp bca.f = got_opentemp();
4417 28315671 2019-08-14 stsp if (bca.f == NULL) {
4418 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4419 28315671 2019-08-14 stsp goto done;
4420 28315671 2019-08-14 stsp }
4421 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4422 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4423 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4424 28315671 2019-08-14 stsp goto done;
4425 28315671 2019-08-14 stsp
4426 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4427 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4428 b02560ec 2019-08-19 stsp bca.nlines--;
4429 b02560ec 2019-08-19 stsp
4430 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4431 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4432 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4433 28315671 2019-08-14 stsp goto done;
4434 28315671 2019-08-14 stsp }
4435 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4436 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4437 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4438 7ef28ff8 2019-08-14 stsp while (i > 0) {
4439 7ef28ff8 2019-08-14 stsp i /= 10;
4440 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4441 7ef28ff8 2019-08-14 stsp }
4442 82f6abb8 2019-08-14 stsp bca.repo = repo;
4443 7ef28ff8 2019-08-14 stsp
4444 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4445 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4446 404c43c4 2018-06-21 stsp done:
4447 66bea077 2018-08-02 stsp free(in_repo_path);
4448 0587e10c 2020-07-23 stsp free(link_target);
4449 66bea077 2018-08-02 stsp free(repo_path);
4450 66bea077 2018-08-02 stsp free(cwd);
4451 404c43c4 2018-06-21 stsp free(commit_id);
4452 28315671 2019-08-14 stsp free(obj_id);
4453 28315671 2019-08-14 stsp if (blob)
4454 28315671 2019-08-14 stsp got_object_blob_close(blob);
4455 0c06baac 2019-02-05 stsp if (worktree)
4456 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4457 ad242220 2018-09-08 stsp if (repo) {
4458 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4459 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4460 ad242220 2018-09-08 stsp if (error == NULL)
4461 ad242220 2018-09-08 stsp error = repo_error;
4462 ad242220 2018-09-08 stsp }
4463 3affba96 2019-09-22 stsp if (bca.lines) {
4464 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4465 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4466 3affba96 2019-09-22 stsp free(bline->id_str);
4467 3affba96 2019-09-22 stsp free(bline->committer);
4468 3affba96 2019-09-22 stsp }
4469 3affba96 2019-09-22 stsp free(bca.lines);
4470 28315671 2019-08-14 stsp }
4471 28315671 2019-08-14 stsp free(bca.line_offsets);
4472 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4473 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4474 404c43c4 2018-06-21 stsp return error;
4475 5de5890b 2018-10-18 stsp }
4476 5de5890b 2018-10-18 stsp
4477 5de5890b 2018-10-18 stsp __dead static void
4478 5de5890b 2018-10-18 stsp usage_tree(void)
4479 5de5890b 2018-10-18 stsp {
4480 c1669e2e 2019-01-09 stsp fprintf(stderr,
4481 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4482 5de5890b 2018-10-18 stsp getprogname());
4483 5de5890b 2018-10-18 stsp exit(1);
4484 5de5890b 2018-10-18 stsp }
4485 5de5890b 2018-10-18 stsp
4486 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4487 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4488 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4489 c1669e2e 2019-01-09 stsp {
4490 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4491 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4492 848d6979 2019-08-12 stsp const char *modestr = "";
4493 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4494 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4495 5de5890b 2018-10-18 stsp
4496 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4497 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4498 c1669e2e 2019-01-09 stsp path++;
4499 c1669e2e 2019-01-09 stsp
4500 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4501 63c5ca5d 2019-08-24 stsp modestr = "$";
4502 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4503 0d6c6ee3 2020-05-20 stsp int i;
4504 0d6c6ee3 2020-05-20 stsp
4505 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4506 0d6c6ee3 2020-05-20 stsp if (err)
4507 0d6c6ee3 2020-05-20 stsp return err;
4508 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4509 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4510 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4511 0d6c6ee3 2020-05-20 stsp }
4512 0d6c6ee3 2020-05-20 stsp
4513 848d6979 2019-08-12 stsp modestr = "@";
4514 0d6c6ee3 2020-05-20 stsp }
4515 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4516 848d6979 2019-08-12 stsp modestr = "/";
4517 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4518 848d6979 2019-08-12 stsp modestr = "*";
4519 848d6979 2019-08-12 stsp
4520 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4521 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4522 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4523 0d6c6ee3 2020-05-20 stsp
4524 0d6c6ee3 2020-05-20 stsp free(link_target);
4525 0d6c6ee3 2020-05-20 stsp return NULL;
4526 c1669e2e 2019-01-09 stsp }
4527 c1669e2e 2019-01-09 stsp
4528 5de5890b 2018-10-18 stsp static const struct got_error *
4529 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4530 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4531 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4532 5de5890b 2018-10-18 stsp {
4533 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4534 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4535 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4536 56e0773d 2019-11-28 stsp int nentries, i;
4537 5de5890b 2018-10-18 stsp
4538 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4539 5de5890b 2018-10-18 stsp if (err)
4540 5de5890b 2018-10-18 stsp goto done;
4541 5de5890b 2018-10-18 stsp
4542 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4543 5de5890b 2018-10-18 stsp if (err)
4544 5de5890b 2018-10-18 stsp goto done;
4545 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4546 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4547 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4548 5de5890b 2018-10-18 stsp char *id = NULL;
4549 84453469 2018-11-11 stsp
4550 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4551 84453469 2018-11-11 stsp break;
4552 84453469 2018-11-11 stsp
4553 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4554 5de5890b 2018-10-18 stsp if (show_ids) {
4555 5de5890b 2018-10-18 stsp char *id_str;
4556 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4557 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4558 5de5890b 2018-10-18 stsp if (err)
4559 5de5890b 2018-10-18 stsp goto done;
4560 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4561 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4562 5de5890b 2018-10-18 stsp free(id_str);
4563 5de5890b 2018-10-18 stsp goto done;
4564 5de5890b 2018-10-18 stsp }
4565 5de5890b 2018-10-18 stsp free(id_str);
4566 5de5890b 2018-10-18 stsp }
4567 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4568 5de5890b 2018-10-18 stsp free(id);
4569 0d6c6ee3 2020-05-20 stsp if (err)
4570 0d6c6ee3 2020-05-20 stsp goto done;
4571 c1669e2e 2019-01-09 stsp
4572 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4573 c1669e2e 2019-01-09 stsp char *child_path;
4574 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4575 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4576 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4577 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4578 c1669e2e 2019-01-09 stsp goto done;
4579 c1669e2e 2019-01-09 stsp }
4580 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4581 c1669e2e 2019-01-09 stsp root_path, repo);
4582 c1669e2e 2019-01-09 stsp free(child_path);
4583 c1669e2e 2019-01-09 stsp if (err)
4584 c1669e2e 2019-01-09 stsp goto done;
4585 c1669e2e 2019-01-09 stsp }
4586 5de5890b 2018-10-18 stsp }
4587 5de5890b 2018-10-18 stsp done:
4588 5de5890b 2018-10-18 stsp if (tree)
4589 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4590 5de5890b 2018-10-18 stsp free(tree_id);
4591 5de5890b 2018-10-18 stsp return err;
4592 404c43c4 2018-06-21 stsp }
4593 404c43c4 2018-06-21 stsp
4594 5de5890b 2018-10-18 stsp static const struct got_error *
4595 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4596 5de5890b 2018-10-18 stsp {
4597 5de5890b 2018-10-18 stsp const struct got_error *error;
4598 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4599 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4600 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4601 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4602 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4603 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4604 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4605 5de5890b 2018-10-18 stsp int ch;
4606 5de5890b 2018-10-18 stsp
4607 5de5890b 2018-10-18 stsp #ifndef PROFILE
4608 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4609 0f8d269b 2019-01-04 stsp NULL) == -1)
4610 5de5890b 2018-10-18 stsp err(1, "pledge");
4611 5de5890b 2018-10-18 stsp #endif
4612 5de5890b 2018-10-18 stsp
4613 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4614 5de5890b 2018-10-18 stsp switch (ch) {
4615 5de5890b 2018-10-18 stsp case 'c':
4616 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4617 5de5890b 2018-10-18 stsp break;
4618 5de5890b 2018-10-18 stsp case 'r':
4619 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4620 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4621 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4622 9ba1d308 2019-10-21 stsp optarg);
4623 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4624 5de5890b 2018-10-18 stsp break;
4625 5de5890b 2018-10-18 stsp case 'i':
4626 5de5890b 2018-10-18 stsp show_ids = 1;
4627 5de5890b 2018-10-18 stsp break;
4628 c1669e2e 2019-01-09 stsp case 'R':
4629 c1669e2e 2019-01-09 stsp recurse = 1;
4630 c1669e2e 2019-01-09 stsp break;
4631 5de5890b 2018-10-18 stsp default:
4632 2deda0b9 2019-03-07 stsp usage_tree();
4633 5de5890b 2018-10-18 stsp /* NOTREACHED */
4634 5de5890b 2018-10-18 stsp }
4635 5de5890b 2018-10-18 stsp }
4636 5de5890b 2018-10-18 stsp
4637 5de5890b 2018-10-18 stsp argc -= optind;
4638 5de5890b 2018-10-18 stsp argv += optind;
4639 5de5890b 2018-10-18 stsp
4640 5de5890b 2018-10-18 stsp if (argc == 1)
4641 5de5890b 2018-10-18 stsp path = argv[0];
4642 5de5890b 2018-10-18 stsp else if (argc > 1)
4643 5de5890b 2018-10-18 stsp usage_tree();
4644 5de5890b 2018-10-18 stsp else
4645 7a2c19d6 2019-02-05 stsp path = NULL;
4646 7a2c19d6 2019-02-05 stsp
4647 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4648 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4649 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4650 9bf7a39b 2019-02-05 stsp goto done;
4651 9bf7a39b 2019-02-05 stsp }
4652 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4653 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4654 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4655 7a2c19d6 2019-02-05 stsp goto done;
4656 7a2c19d6 2019-02-05 stsp else
4657 7a2c19d6 2019-02-05 stsp error = NULL;
4658 7a2c19d6 2019-02-05 stsp if (worktree) {
4659 7a2c19d6 2019-02-05 stsp repo_path =
4660 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4661 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4662 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4663 7a2c19d6 2019-02-05 stsp if (error)
4664 7a2c19d6 2019-02-05 stsp goto done;
4665 7a2c19d6 2019-02-05 stsp } else {
4666 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4667 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4668 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4669 7a2c19d6 2019-02-05 stsp goto done;
4670 7a2c19d6 2019-02-05 stsp }
4671 5de5890b 2018-10-18 stsp }
4672 5de5890b 2018-10-18 stsp }
4673 5de5890b 2018-10-18 stsp
4674 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4675 5de5890b 2018-10-18 stsp if (error != NULL)
4676 c02c541e 2019-03-29 stsp goto done;
4677 c02c541e 2019-03-29 stsp
4678 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4679 c02c541e 2019-03-29 stsp if (error)
4680 5de5890b 2018-10-18 stsp goto done;
4681 5de5890b 2018-10-18 stsp
4682 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4683 9bf7a39b 2019-02-05 stsp if (worktree) {
4684 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4685 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4686 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4687 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4688 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4689 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4690 9bf7a39b 2019-02-05 stsp goto done;
4691 9bf7a39b 2019-02-05 stsp }
4692 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4693 9bf7a39b 2019-02-05 stsp free(p);
4694 9bf7a39b 2019-02-05 stsp if (error)
4695 9bf7a39b 2019-02-05 stsp goto done;
4696 9bf7a39b 2019-02-05 stsp } else
4697 9bf7a39b 2019-02-05 stsp path = "/";
4698 9bf7a39b 2019-02-05 stsp }
4699 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4700 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4701 9bf7a39b 2019-02-05 stsp if (error != NULL)
4702 9bf7a39b 2019-02-05 stsp goto done;
4703 9bf7a39b 2019-02-05 stsp }
4704 5de5890b 2018-10-18 stsp
4705 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4706 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4707 4e0a20a4 2020-03-23 tracey if (worktree)
4708 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4709 4e0a20a4 2020-03-23 tracey else
4710 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4711 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4712 5de5890b 2018-10-18 stsp if (error != NULL)
4713 5de5890b 2018-10-18 stsp goto done;
4714 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4715 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4716 5de5890b 2018-10-18 stsp if (error != NULL)
4717 5de5890b 2018-10-18 stsp goto done;
4718 5de5890b 2018-10-18 stsp } else {
4719 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4720 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4721 30837e32 2019-07-25 stsp if (error)
4722 5de5890b 2018-10-18 stsp goto done;
4723 5de5890b 2018-10-18 stsp }
4724 5de5890b 2018-10-18 stsp
4725 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4726 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4727 5de5890b 2018-10-18 stsp done:
4728 5de5890b 2018-10-18 stsp free(in_repo_path);
4729 5de5890b 2018-10-18 stsp free(repo_path);
4730 5de5890b 2018-10-18 stsp free(cwd);
4731 5de5890b 2018-10-18 stsp free(commit_id);
4732 7a2c19d6 2019-02-05 stsp if (worktree)
4733 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4734 5de5890b 2018-10-18 stsp if (repo) {
4735 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4736 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4737 5de5890b 2018-10-18 stsp if (error == NULL)
4738 5de5890b 2018-10-18 stsp error = repo_error;
4739 5de5890b 2018-10-18 stsp }
4740 5de5890b 2018-10-18 stsp return error;
4741 5de5890b 2018-10-18 stsp }
4742 5de5890b 2018-10-18 stsp
4743 6bad629b 2019-02-04 stsp __dead static void
4744 6bad629b 2019-02-04 stsp usage_status(void)
4745 6bad629b 2019-02-04 stsp {
4746 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4747 081470ac 2020-08-13 stsp getprogname());
4748 6bad629b 2019-02-04 stsp exit(1);
4749 6bad629b 2019-02-04 stsp }
4750 5c860e29 2018-03-12 stsp
4751 b72f483a 2019-02-05 stsp static const struct got_error *
4752 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4753 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4754 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4755 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4756 6bad629b 2019-02-04 stsp {
4757 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4758 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4759 081470ac 2020-08-13 stsp if (arg) {
4760 081470ac 2020-08-13 stsp char *status_codes = arg;
4761 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4762 081470ac 2020-08-13 stsp int i;
4763 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4764 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4765 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4766 081470ac 2020-08-13 stsp break;
4767 081470ac 2020-08-13 stsp }
4768 081470ac 2020-08-13 stsp if (i == ncodes)
4769 081470ac 2020-08-13 stsp return NULL;
4770 081470ac 2020-08-13 stsp }
4771 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4772 b72f483a 2019-02-05 stsp return NULL;
4773 6bad629b 2019-02-04 stsp }
4774 5c860e29 2018-03-12 stsp
4775 6bad629b 2019-02-04 stsp static const struct got_error *
4776 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4777 6bad629b 2019-02-04 stsp {
4778 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4779 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4780 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4781 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4782 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4783 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4784 081470ac 2020-08-13 stsp int ch, i;
4785 5c860e29 2018-03-12 stsp
4786 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4787 72ea6654 2019-07-27 stsp
4788 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4789 6bad629b 2019-02-04 stsp switch (ch) {
4790 081470ac 2020-08-13 stsp case 's':
4791 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4792 081470ac 2020-08-13 stsp switch (optarg[i]) {
4793 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4794 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4795 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4796 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4797 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4798 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4799 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4800 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4801 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4802 081470ac 2020-08-13 stsp break;
4803 081470ac 2020-08-13 stsp default:
4804 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4805 081470ac 2020-08-13 stsp optarg[i]);
4806 081470ac 2020-08-13 stsp }
4807 081470ac 2020-08-13 stsp }
4808 081470ac 2020-08-13 stsp status_codes = optarg;
4809 081470ac 2020-08-13 stsp break;
4810 5c860e29 2018-03-12 stsp default:
4811 2deda0b9 2019-03-07 stsp usage_status();
4812 6bad629b 2019-02-04 stsp /* NOTREACHED */
4813 5c860e29 2018-03-12 stsp }
4814 5c860e29 2018-03-12 stsp }
4815 5c860e29 2018-03-12 stsp
4816 6bad629b 2019-02-04 stsp argc -= optind;
4817 6bad629b 2019-02-04 stsp argv += optind;
4818 5c860e29 2018-03-12 stsp
4819 6bad629b 2019-02-04 stsp #ifndef PROFILE
4820 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4821 6bad629b 2019-02-04 stsp NULL) == -1)
4822 6bad629b 2019-02-04 stsp err(1, "pledge");
4823 f42b1b34 2018-03-12 stsp #endif
4824 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4825 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4826 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4827 927df6b7 2019-02-10 stsp goto done;
4828 927df6b7 2019-02-10 stsp }
4829 927df6b7 2019-02-10 stsp
4830 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4831 fa51e947 2020-03-27 stsp if (error) {
4832 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4833 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4834 a5edda0a 2019-07-27 stsp goto done;
4835 fa51e947 2020-03-27 stsp }
4836 6bad629b 2019-02-04 stsp
4837 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4838 c9956ddf 2019-09-08 stsp NULL);
4839 6bad629b 2019-02-04 stsp if (error != NULL)
4840 6bad629b 2019-02-04 stsp goto done;
4841 6bad629b 2019-02-04 stsp
4842 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4843 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4844 087fb88c 2019-08-04 stsp if (error)
4845 087fb88c 2019-08-04 stsp goto done;
4846 087fb88c 2019-08-04 stsp
4847 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4848 6bad629b 2019-02-04 stsp if (error)
4849 6bad629b 2019-02-04 stsp goto done;
4850 6bad629b 2019-02-04 stsp
4851 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4852 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4853 6bad629b 2019-02-04 stsp done:
4854 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4855 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4856 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4857 927df6b7 2019-02-10 stsp free(cwd);
4858 d0eebce4 2019-03-11 stsp return error;
4859 d0eebce4 2019-03-11 stsp }
4860 d0eebce4 2019-03-11 stsp
4861 d0eebce4 2019-03-11 stsp __dead static void
4862 d0eebce4 2019-03-11 stsp usage_ref(void)
4863 d0eebce4 2019-03-11 stsp {
4864 d0eebce4 2019-03-11 stsp fprintf(stderr,
4865 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4866 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4867 d0eebce4 2019-03-11 stsp getprogname());
4868 d0eebce4 2019-03-11 stsp exit(1);
4869 d0eebce4 2019-03-11 stsp }
4870 d0eebce4 2019-03-11 stsp
4871 d0eebce4 2019-03-11 stsp static const struct got_error *
4872 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4873 d0eebce4 2019-03-11 stsp {
4874 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4875 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4876 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4877 d0eebce4 2019-03-11 stsp
4878 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4879 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4880 d0eebce4 2019-03-11 stsp if (err)
4881 d0eebce4 2019-03-11 stsp return err;
4882 d0eebce4 2019-03-11 stsp
4883 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4884 d0eebce4 2019-03-11 stsp char *refstr;
4885 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4886 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4887 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4888 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4889 d0eebce4 2019-03-11 stsp free(refstr);
4890 d0eebce4 2019-03-11 stsp }
4891 d0eebce4 2019-03-11 stsp
4892 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4893 d0eebce4 2019-03-11 stsp return NULL;
4894 d0eebce4 2019-03-11 stsp }
4895 d0eebce4 2019-03-11 stsp
4896 d0eebce4 2019-03-11 stsp static const struct got_error *
4897 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4898 d0eebce4 2019-03-11 stsp {
4899 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4900 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4901 d0eebce4 2019-03-11 stsp
4902 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4903 d0eebce4 2019-03-11 stsp if (err)
4904 d0eebce4 2019-03-11 stsp return err;
4905 d0eebce4 2019-03-11 stsp
4906 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4907 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4908 d0eebce4 2019-03-11 stsp return err;
4909 d0eebce4 2019-03-11 stsp }
4910 d0eebce4 2019-03-11 stsp
4911 d0eebce4 2019-03-11 stsp static const struct got_error *
4912 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4913 d0eebce4 2019-03-11 stsp {
4914 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4915 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4916 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4917 d1644381 2019-07-14 stsp
4918 d1644381 2019-07-14 stsp /*
4919 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4920 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4921 d1644381 2019-07-14 stsp * an unintended typo.
4922 d1644381 2019-07-14 stsp */
4923 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4924 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4925 d0eebce4 2019-03-11 stsp
4926 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4927 dd88155e 2019-06-29 stsp repo);
4928 d83d9d5c 2019-05-13 stsp if (err) {
4929 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4930 d0eebce4 2019-03-11 stsp
4931 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4932 d83d9d5c 2019-05-13 stsp return err;
4933 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4934 d83d9d5c 2019-05-13 stsp if (err)
4935 d83d9d5c 2019-05-13 stsp return err;
4936 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4937 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4938 d83d9d5c 2019-05-13 stsp if (err)
4939 d83d9d5c 2019-05-13 stsp return err;
4940 d83d9d5c 2019-05-13 stsp }
4941 d83d9d5c 2019-05-13 stsp
4942 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4943 d0eebce4 2019-03-11 stsp if (err)
4944 d0eebce4 2019-03-11 stsp goto done;
4945 d0eebce4 2019-03-11 stsp
4946 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4947 d0eebce4 2019-03-11 stsp done:
4948 d0eebce4 2019-03-11 stsp if (ref)
4949 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4950 d0eebce4 2019-03-11 stsp free(id);
4951 d1c1ae5f 2019-08-12 stsp return err;
4952 d1c1ae5f 2019-08-12 stsp }
4953 d1c1ae5f 2019-08-12 stsp
4954 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4955 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4956 d1c1ae5f 2019-08-12 stsp {
4957 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4958 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4959 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4960 d1c1ae5f 2019-08-12 stsp
4961 d1c1ae5f 2019-08-12 stsp /*
4962 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4963 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4964 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4965 d1c1ae5f 2019-08-12 stsp */
4966 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4967 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4968 d1c1ae5f 2019-08-12 stsp
4969 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4970 d1c1ae5f 2019-08-12 stsp if (err)
4971 d1c1ae5f 2019-08-12 stsp return err;
4972 d1c1ae5f 2019-08-12 stsp
4973 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4974 d1c1ae5f 2019-08-12 stsp if (err)
4975 d1c1ae5f 2019-08-12 stsp goto done;
4976 d1c1ae5f 2019-08-12 stsp
4977 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4978 d1c1ae5f 2019-08-12 stsp done:
4979 d1c1ae5f 2019-08-12 stsp if (target_ref)
4980 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4981 d1c1ae5f 2019-08-12 stsp if (ref)
4982 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4983 d0eebce4 2019-03-11 stsp return err;
4984 d0eebce4 2019-03-11 stsp }
4985 d0eebce4 2019-03-11 stsp
4986 d0eebce4 2019-03-11 stsp static const struct got_error *
4987 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4988 d0eebce4 2019-03-11 stsp {
4989 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4990 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4991 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4992 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4993 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4994 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4995 b2070a3f 2020-03-22 stsp char *refname = NULL;
4996 d0eebce4 2019-03-11 stsp
4997 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4998 d0eebce4 2019-03-11 stsp switch (ch) {
4999 e31abbf2 2020-03-22 stsp case 'c':
5000 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5001 e31abbf2 2020-03-22 stsp break;
5002 d0eebce4 2019-03-11 stsp case 'd':
5003 e31abbf2 2020-03-22 stsp do_delete = 1;
5004 d0eebce4 2019-03-11 stsp break;
5005 d0eebce4 2019-03-11 stsp case 'r':
5006 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5007 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5008 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5009 9ba1d308 2019-10-21 stsp optarg);
5010 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5011 d0eebce4 2019-03-11 stsp break;
5012 d0eebce4 2019-03-11 stsp case 'l':
5013 d0eebce4 2019-03-11 stsp do_list = 1;
5014 d1c1ae5f 2019-08-12 stsp break;
5015 d1c1ae5f 2019-08-12 stsp case 's':
5016 e31abbf2 2020-03-22 stsp symref_target = optarg;
5017 d0eebce4 2019-03-11 stsp break;
5018 d0eebce4 2019-03-11 stsp default:
5019 d0eebce4 2019-03-11 stsp usage_ref();
5020 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5021 d0eebce4 2019-03-11 stsp }
5022 d0eebce4 2019-03-11 stsp }
5023 d0eebce4 2019-03-11 stsp
5024 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5025 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5026 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5027 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5028 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5029 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5030 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5031 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5032 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5033 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5034 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5035 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5036 d0eebce4 2019-03-11 stsp
5037 d0eebce4 2019-03-11 stsp argc -= optind;
5038 d0eebce4 2019-03-11 stsp argv += optind;
5039 d0eebce4 2019-03-11 stsp
5040 e31abbf2 2020-03-22 stsp if (do_list) {
5041 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5042 d0eebce4 2019-03-11 stsp usage_ref();
5043 b2070a3f 2020-03-22 stsp if (argc == 1) {
5044 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5045 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5046 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5047 b2070a3f 2020-03-22 stsp goto done;
5048 b2070a3f 2020-03-22 stsp }
5049 b2070a3f 2020-03-22 stsp }
5050 e31abbf2 2020-03-22 stsp } else {
5051 e31abbf2 2020-03-22 stsp if (argc != 1)
5052 e31abbf2 2020-03-22 stsp usage_ref();
5053 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5054 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5055 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5056 b2070a3f 2020-03-22 stsp goto done;
5057 b2070a3f 2020-03-22 stsp }
5058 e31abbf2 2020-03-22 stsp }
5059 b2070a3f 2020-03-22 stsp
5060 b2070a3f 2020-03-22 stsp if (refname)
5061 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5062 d0eebce4 2019-03-11 stsp
5063 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5064 e0b57350 2019-03-12 stsp if (do_list) {
5065 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5066 e0b57350 2019-03-12 stsp NULL) == -1)
5067 e0b57350 2019-03-12 stsp err(1, "pledge");
5068 e0b57350 2019-03-12 stsp } else {
5069 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5070 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5071 e0b57350 2019-03-12 stsp err(1, "pledge");
5072 e0b57350 2019-03-12 stsp }
5073 d0eebce4 2019-03-11 stsp #endif
5074 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5075 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5076 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5077 d0eebce4 2019-03-11 stsp goto done;
5078 d0eebce4 2019-03-11 stsp }
5079 d0eebce4 2019-03-11 stsp
5080 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5081 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5082 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5083 d0eebce4 2019-03-11 stsp goto done;
5084 d0eebce4 2019-03-11 stsp else
5085 d0eebce4 2019-03-11 stsp error = NULL;
5086 d0eebce4 2019-03-11 stsp if (worktree) {
5087 d0eebce4 2019-03-11 stsp repo_path =
5088 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5089 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5090 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5091 d0eebce4 2019-03-11 stsp if (error)
5092 d0eebce4 2019-03-11 stsp goto done;
5093 d0eebce4 2019-03-11 stsp } else {
5094 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5095 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5096 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5097 d0eebce4 2019-03-11 stsp goto done;
5098 d0eebce4 2019-03-11 stsp }
5099 d0eebce4 2019-03-11 stsp }
5100 d0eebce4 2019-03-11 stsp }
5101 d0eebce4 2019-03-11 stsp
5102 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5103 d0eebce4 2019-03-11 stsp if (error != NULL)
5104 d0eebce4 2019-03-11 stsp goto done;
5105 d0eebce4 2019-03-11 stsp
5106 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5107 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5108 c02c541e 2019-03-29 stsp if (error)
5109 c02c541e 2019-03-29 stsp goto done;
5110 c02c541e 2019-03-29 stsp
5111 d0eebce4 2019-03-11 stsp if (do_list)
5112 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5113 e31abbf2 2020-03-22 stsp else if (do_delete)
5114 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5115 e31abbf2 2020-03-22 stsp else if (symref_target)
5116 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5117 e31abbf2 2020-03-22 stsp else {
5118 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5119 e31abbf2 2020-03-22 stsp usage_ref();
5120 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5121 e31abbf2 2020-03-22 stsp }
5122 4e759de4 2019-06-26 stsp done:
5123 b2070a3f 2020-03-22 stsp free(refname);
5124 4e759de4 2019-06-26 stsp if (repo)
5125 4e759de4 2019-06-26 stsp got_repo_close(repo);
5126 4e759de4 2019-06-26 stsp if (worktree)
5127 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5128 4e759de4 2019-06-26 stsp free(cwd);
5129 4e759de4 2019-06-26 stsp free(repo_path);
5130 4e759de4 2019-06-26 stsp return error;
5131 4e759de4 2019-06-26 stsp }
5132 4e759de4 2019-06-26 stsp
5133 4e759de4 2019-06-26 stsp __dead static void
5134 4e759de4 2019-06-26 stsp usage_branch(void)
5135 4e759de4 2019-06-26 stsp {
5136 4e759de4 2019-06-26 stsp fprintf(stderr,
5137 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5138 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5139 4e759de4 2019-06-26 stsp exit(1);
5140 4e759de4 2019-06-26 stsp }
5141 4e759de4 2019-06-26 stsp
5142 4e759de4 2019-06-26 stsp static const struct got_error *
5143 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5144 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5145 4e99b47e 2019-10-04 stsp {
5146 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5147 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5148 4e99b47e 2019-10-04 stsp char *refstr;
5149 4e99b47e 2019-10-04 stsp
5150 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5151 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5152 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5153 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5154 4e99b47e 2019-10-04 stsp
5155 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5156 4e99b47e 2019-10-04 stsp if (err)
5157 4e99b47e 2019-10-04 stsp return err;
5158 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5159 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5160 4e99b47e 2019-10-04 stsp marker = "* ";
5161 4e99b47e 2019-10-04 stsp else
5162 4e99b47e 2019-10-04 stsp marker = "~ ";
5163 4e99b47e 2019-10-04 stsp free(id);
5164 4e99b47e 2019-10-04 stsp }
5165 4e99b47e 2019-10-04 stsp
5166 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5167 4e99b47e 2019-10-04 stsp refname += 11;
5168 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5169 4e99b47e 2019-10-04 stsp refname += 18;
5170 4e99b47e 2019-10-04 stsp
5171 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5172 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5173 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5174 4e99b47e 2019-10-04 stsp
5175 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5176 4e99b47e 2019-10-04 stsp free(refstr);
5177 4e99b47e 2019-10-04 stsp return NULL;
5178 4e99b47e 2019-10-04 stsp }
5179 4e99b47e 2019-10-04 stsp
5180 4e99b47e 2019-10-04 stsp static const struct got_error *
5181 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5182 ad89fa31 2019-10-04 stsp {
5183 ad89fa31 2019-10-04 stsp const char *refname;
5184 ad89fa31 2019-10-04 stsp
5185 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5186 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5187 ad89fa31 2019-10-04 stsp
5188 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5189 ad89fa31 2019-10-04 stsp
5190 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5191 ad89fa31 2019-10-04 stsp refname += 11;
5192 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5193 ad89fa31 2019-10-04 stsp refname += 18;
5194 ad89fa31 2019-10-04 stsp
5195 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5196 ad89fa31 2019-10-04 stsp
5197 ad89fa31 2019-10-04 stsp return NULL;
5198 ad89fa31 2019-10-04 stsp }
5199 ad89fa31 2019-10-04 stsp
5200 ad89fa31 2019-10-04 stsp static const struct got_error *
5201 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5202 4e759de4 2019-06-26 stsp {
5203 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5204 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5205 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5206 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5207 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5208 4e759de4 2019-06-26 stsp
5209 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5210 ba882ee3 2019-07-11 stsp
5211 4e99b47e 2019-10-04 stsp if (worktree) {
5212 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5213 4e99b47e 2019-10-04 stsp worktree);
5214 4e99b47e 2019-10-04 stsp if (err)
5215 4e99b47e 2019-10-04 stsp return err;
5216 4e99b47e 2019-10-04 stsp
5217 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5218 4e99b47e 2019-10-04 stsp worktree);
5219 4e99b47e 2019-10-04 stsp if (err)
5220 4e99b47e 2019-10-04 stsp return err;
5221 4e99b47e 2019-10-04 stsp
5222 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5223 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5224 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5225 4e99b47e 2019-10-04 stsp if (err)
5226 4e99b47e 2019-10-04 stsp return err;
5227 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5228 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5229 4e99b47e 2019-10-04 stsp }
5230 4e99b47e 2019-10-04 stsp }
5231 4e99b47e 2019-10-04 stsp
5232 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5233 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5234 4e759de4 2019-06-26 stsp if (err)
5235 4e759de4 2019-06-26 stsp return err;
5236 4e759de4 2019-06-26 stsp
5237 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5238 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5239 4e759de4 2019-06-26 stsp
5240 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5241 4e759de4 2019-06-26 stsp return NULL;
5242 4e759de4 2019-06-26 stsp }
5243 4e759de4 2019-06-26 stsp
5244 4e759de4 2019-06-26 stsp static const struct got_error *
5245 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5246 45cd4e47 2019-08-25 stsp const char *branch_name)
5247 4e759de4 2019-06-26 stsp {
5248 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5249 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5250 4e759de4 2019-06-26 stsp char *refname;
5251 4e759de4 2019-06-26 stsp
5252 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5253 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5254 4e759de4 2019-06-26 stsp
5255 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5256 4e759de4 2019-06-26 stsp if (err)
5257 4e759de4 2019-06-26 stsp goto done;
5258 4e759de4 2019-06-26 stsp
5259 45cd4e47 2019-08-25 stsp if (worktree &&
5260 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5261 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5262 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5263 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5264 45cd4e47 2019-08-25 stsp goto done;
5265 45cd4e47 2019-08-25 stsp }
5266 45cd4e47 2019-08-25 stsp
5267 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5268 4e759de4 2019-06-26 stsp done:
5269 45cd4e47 2019-08-25 stsp if (ref)
5270 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5271 4e759de4 2019-06-26 stsp free(refname);
5272 4e759de4 2019-06-26 stsp return err;
5273 4e759de4 2019-06-26 stsp }
5274 4e759de4 2019-06-26 stsp
5275 4e759de4 2019-06-26 stsp static const struct got_error *
5276 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5277 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5278 4e759de4 2019-06-26 stsp {
5279 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5280 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5281 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5282 d3f84d51 2019-07-11 stsp
5283 d3f84d51 2019-07-11 stsp /*
5284 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5285 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5286 d3f84d51 2019-07-11 stsp * an unintended typo.
5287 d3f84d51 2019-07-11 stsp */
5288 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5289 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5290 4e759de4 2019-06-26 stsp
5291 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5292 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5293 4e759de4 2019-06-26 stsp goto done;
5294 4e759de4 2019-06-26 stsp }
5295 4e759de4 2019-06-26 stsp
5296 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5297 4e759de4 2019-06-26 stsp if (err == NULL) {
5298 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5299 4e759de4 2019-06-26 stsp goto done;
5300 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5301 4e759de4 2019-06-26 stsp goto done;
5302 4e759de4 2019-06-26 stsp
5303 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5304 4e759de4 2019-06-26 stsp if (err)
5305 4e759de4 2019-06-26 stsp goto done;
5306 4e759de4 2019-06-26 stsp
5307 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5308 d0eebce4 2019-03-11 stsp done:
5309 4e759de4 2019-06-26 stsp if (ref)
5310 4e759de4 2019-06-26 stsp got_ref_close(ref);
5311 4e759de4 2019-06-26 stsp free(base_refname);
5312 4e759de4 2019-06-26 stsp free(refname);
5313 4e759de4 2019-06-26 stsp return err;
5314 4e759de4 2019-06-26 stsp }
5315 4e759de4 2019-06-26 stsp
5316 4e759de4 2019-06-26 stsp static const struct got_error *
5317 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5318 4e759de4 2019-06-26 stsp {
5319 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5320 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5321 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5322 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5323 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5324 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5325 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5326 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5327 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5328 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5329 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5330 4e759de4 2019-06-26 stsp
5331 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5332 da76fce2 2020-02-24 stsp
5333 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5334 4e759de4 2019-06-26 stsp switch (ch) {
5335 a74f7e83 2019-11-10 stsp case 'c':
5336 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5337 a74f7e83 2019-11-10 stsp break;
5338 4e759de4 2019-06-26 stsp case 'd':
5339 4e759de4 2019-06-26 stsp delref = optarg;
5340 4e759de4 2019-06-26 stsp break;
5341 4e759de4 2019-06-26 stsp case 'r':
5342 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5343 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5344 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5345 9ba1d308 2019-10-21 stsp optarg);
5346 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5347 4e759de4 2019-06-26 stsp break;
5348 4e759de4 2019-06-26 stsp case 'l':
5349 4e759de4 2019-06-26 stsp do_list = 1;
5350 da76fce2 2020-02-24 stsp break;
5351 da76fce2 2020-02-24 stsp case 'n':
5352 da76fce2 2020-02-24 stsp do_update = 0;
5353 4e759de4 2019-06-26 stsp break;
5354 4e759de4 2019-06-26 stsp default:
5355 4e759de4 2019-06-26 stsp usage_branch();
5356 4e759de4 2019-06-26 stsp /* NOTREACHED */
5357 4e759de4 2019-06-26 stsp }
5358 4e759de4 2019-06-26 stsp }
5359 4e759de4 2019-06-26 stsp
5360 4e759de4 2019-06-26 stsp if (do_list && delref)
5361 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5362 4e759de4 2019-06-26 stsp
5363 4e759de4 2019-06-26 stsp argc -= optind;
5364 4e759de4 2019-06-26 stsp argv += optind;
5365 ad89fa31 2019-10-04 stsp
5366 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5367 ad89fa31 2019-10-04 stsp do_show = 1;
5368 4e759de4 2019-06-26 stsp
5369 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5370 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5371 a74f7e83 2019-11-10 stsp
5372 4e759de4 2019-06-26 stsp if (do_list || delref) {
5373 4e759de4 2019-06-26 stsp if (argc > 0)
5374 4e759de4 2019-06-26 stsp usage_branch();
5375 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5376 4e759de4 2019-06-26 stsp usage_branch();
5377 4e759de4 2019-06-26 stsp
5378 4e759de4 2019-06-26 stsp #ifndef PROFILE
5379 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5380 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5381 4e759de4 2019-06-26 stsp NULL) == -1)
5382 4e759de4 2019-06-26 stsp err(1, "pledge");
5383 4e759de4 2019-06-26 stsp } else {
5384 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5385 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5386 4e759de4 2019-06-26 stsp err(1, "pledge");
5387 4e759de4 2019-06-26 stsp }
5388 4e759de4 2019-06-26 stsp #endif
5389 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5390 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5391 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5392 4e759de4 2019-06-26 stsp goto done;
5393 4e759de4 2019-06-26 stsp }
5394 4e759de4 2019-06-26 stsp
5395 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5396 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5397 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5398 4e759de4 2019-06-26 stsp goto done;
5399 4e759de4 2019-06-26 stsp else
5400 4e759de4 2019-06-26 stsp error = NULL;
5401 4e759de4 2019-06-26 stsp if (worktree) {
5402 4e759de4 2019-06-26 stsp repo_path =
5403 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5404 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5405 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5406 4e759de4 2019-06-26 stsp if (error)
5407 4e759de4 2019-06-26 stsp goto done;
5408 4e759de4 2019-06-26 stsp } else {
5409 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5410 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5411 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5412 4e759de4 2019-06-26 stsp goto done;
5413 4e759de4 2019-06-26 stsp }
5414 4e759de4 2019-06-26 stsp }
5415 4e759de4 2019-06-26 stsp }
5416 4e759de4 2019-06-26 stsp
5417 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5418 4e759de4 2019-06-26 stsp if (error != NULL)
5419 4e759de4 2019-06-26 stsp goto done;
5420 4e759de4 2019-06-26 stsp
5421 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5422 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5423 4e759de4 2019-06-26 stsp if (error)
5424 4e759de4 2019-06-26 stsp goto done;
5425 4e759de4 2019-06-26 stsp
5426 ad89fa31 2019-10-04 stsp if (do_show)
5427 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5428 ad89fa31 2019-10-04 stsp else if (do_list)
5429 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5430 4e759de4 2019-06-26 stsp else if (delref)
5431 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5432 4e759de4 2019-06-26 stsp else {
5433 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5434 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5435 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5436 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5437 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5438 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5439 a74f7e83 2019-11-10 stsp if (error)
5440 a74f7e83 2019-11-10 stsp goto done;
5441 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5442 da76fce2 2020-02-24 stsp if (error)
5443 da76fce2 2020-02-24 stsp goto done;
5444 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5445 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5446 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5447 da76fce2 2020-02-24 stsp
5448 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5449 da76fce2 2020-02-24 stsp if (error)
5450 da76fce2 2020-02-24 stsp goto done;
5451 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5452 da76fce2 2020-02-24 stsp worktree);
5453 da76fce2 2020-02-24 stsp if (error)
5454 da76fce2 2020-02-24 stsp goto done;
5455 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5456 da76fce2 2020-02-24 stsp == -1) {
5457 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5458 da76fce2 2020-02-24 stsp goto done;
5459 da76fce2 2020-02-24 stsp }
5460 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5461 da76fce2 2020-02-24 stsp free(branch_refname);
5462 da76fce2 2020-02-24 stsp if (error)
5463 da76fce2 2020-02-24 stsp goto done;
5464 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5465 da76fce2 2020-02-24 stsp repo);
5466 da76fce2 2020-02-24 stsp if (error)
5467 da76fce2 2020-02-24 stsp goto done;
5468 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5469 da76fce2 2020-02-24 stsp commit_id);
5470 da76fce2 2020-02-24 stsp if (error)
5471 da76fce2 2020-02-24 stsp goto done;
5472 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5473 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5474 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5475 9627c110 2020-04-18 stsp NULL);
5476 da76fce2 2020-02-24 stsp if (error)
5477 da76fce2 2020-02-24 stsp goto done;
5478 9627c110 2020-04-18 stsp if (upa.did_something)
5479 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5480 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5481 da76fce2 2020-02-24 stsp }
5482 4e759de4 2019-06-26 stsp }
5483 4e759de4 2019-06-26 stsp done:
5484 da76fce2 2020-02-24 stsp if (ref)
5485 da76fce2 2020-02-24 stsp got_ref_close(ref);
5486 d0eebce4 2019-03-11 stsp if (repo)
5487 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5488 d0eebce4 2019-03-11 stsp if (worktree)
5489 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5490 d0eebce4 2019-03-11 stsp free(cwd);
5491 d0eebce4 2019-03-11 stsp free(repo_path);
5492 da76fce2 2020-02-24 stsp free(commit_id);
5493 da76fce2 2020-02-24 stsp free(commit_id_str);
5494 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5495 da76fce2 2020-02-24 stsp free((char *)pe->path);
5496 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5497 d00136be 2019-03-26 stsp return error;
5498 d00136be 2019-03-26 stsp }
5499 d00136be 2019-03-26 stsp
5500 8e7bd50a 2019-08-22 stsp
5501 d00136be 2019-03-26 stsp __dead static void
5502 8e7bd50a 2019-08-22 stsp usage_tag(void)
5503 8e7bd50a 2019-08-22 stsp {
5504 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5505 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5506 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5507 8e7bd50a 2019-08-22 stsp exit(1);
5508 b8bad2ba 2019-08-23 stsp }
5509 b8bad2ba 2019-08-23 stsp
5510 b8bad2ba 2019-08-23 stsp #if 0
5511 b8bad2ba 2019-08-23 stsp static const struct got_error *
5512 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5513 b8bad2ba 2019-08-23 stsp {
5514 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5515 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5516 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5517 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5518 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5519 b8bad2ba 2019-08-23 stsp
5520 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5521 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5522 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5523 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5524 b8bad2ba 2019-08-23 stsp if (err)
5525 b8bad2ba 2019-08-23 stsp return err;
5526 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5527 b8bad2ba 2019-08-23 stsp continue;
5528 b8bad2ba 2019-08-23 stsp } else {
5529 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5530 b8bad2ba 2019-08-23 stsp if (err)
5531 b8bad2ba 2019-08-23 stsp break;
5532 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5533 b8bad2ba 2019-08-23 stsp free(re_id);
5534 b8bad2ba 2019-08-23 stsp if (err)
5535 b8bad2ba 2019-08-23 stsp break;
5536 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5537 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5538 b8bad2ba 2019-08-23 stsp }
5539 b8bad2ba 2019-08-23 stsp
5540 b8bad2ba 2019-08-23 stsp while (se) {
5541 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5542 b8bad2ba 2019-08-23 stsp if (err)
5543 b8bad2ba 2019-08-23 stsp break;
5544 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5545 b8bad2ba 2019-08-23 stsp free(se_id);
5546 b8bad2ba 2019-08-23 stsp if (err)
5547 b8bad2ba 2019-08-23 stsp break;
5548 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5549 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5550 b8bad2ba 2019-08-23 stsp
5551 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5552 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5553 b8bad2ba 2019-08-23 stsp if (err)
5554 b8bad2ba 2019-08-23 stsp return err;
5555 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5556 b8bad2ba 2019-08-23 stsp break;
5557 b8bad2ba 2019-08-23 stsp }
5558 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5559 b8bad2ba 2019-08-23 stsp continue;
5560 b8bad2ba 2019-08-23 stsp }
5561 b8bad2ba 2019-08-23 stsp }
5562 b8bad2ba 2019-08-23 stsp done:
5563 b8bad2ba 2019-08-23 stsp return err;
5564 8e7bd50a 2019-08-22 stsp }
5565 b8bad2ba 2019-08-23 stsp #endif
5566 b8bad2ba 2019-08-23 stsp
5567 b8bad2ba 2019-08-23 stsp static const struct got_error *
5568 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5569 8e7bd50a 2019-08-22 stsp {
5570 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5571 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5572 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5573 8e7bd50a 2019-08-22 stsp
5574 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5575 8e7bd50a 2019-08-22 stsp
5576 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5577 8e7bd50a 2019-08-22 stsp if (err)
5578 8e7bd50a 2019-08-22 stsp return err;
5579 8e7bd50a 2019-08-22 stsp
5580 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5581 8e7bd50a 2019-08-22 stsp const char *refname;
5582 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5583 8e7bd50a 2019-08-22 stsp char datebuf[26];
5584 d4efa91b 2020-01-14 stsp const char *tagger;
5585 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5586 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5587 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5588 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5589 8e7bd50a 2019-08-22 stsp
5590 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5591 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5592 8e7bd50a 2019-08-22 stsp continue;
5593 8e7bd50a 2019-08-22 stsp refname += 10;
5594 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5595 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5596 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5597 8e7bd50a 2019-08-22 stsp break;
5598 8e7bd50a 2019-08-22 stsp }
5599 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5600 8e7bd50a 2019-08-22 stsp free(refstr);
5601 8e7bd50a 2019-08-22 stsp
5602 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5603 8e7bd50a 2019-08-22 stsp if (err)
5604 8e7bd50a 2019-08-22 stsp break;
5605 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5606 d4efa91b 2020-01-14 stsp if (err) {
5607 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5608 d4efa91b 2020-01-14 stsp free(id);
5609 d4efa91b 2020-01-14 stsp break;
5610 d4efa91b 2020-01-14 stsp }
5611 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5612 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5613 d4efa91b 2020-01-14 stsp if (err) {
5614 d4efa91b 2020-01-14 stsp free(id);
5615 d4efa91b 2020-01-14 stsp break;
5616 d4efa91b 2020-01-14 stsp }
5617 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5618 d4efa91b 2020-01-14 stsp tagger_time =
5619 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5620 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5621 d4efa91b 2020-01-14 stsp free(id);
5622 d4efa91b 2020-01-14 stsp if (err)
5623 d4efa91b 2020-01-14 stsp break;
5624 d4efa91b 2020-01-14 stsp } else {
5625 d4efa91b 2020-01-14 stsp free(id);
5626 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5627 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5628 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5629 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5630 d4efa91b 2020-01-14 stsp if (err)
5631 d4efa91b 2020-01-14 stsp break;
5632 d4efa91b 2020-01-14 stsp }
5633 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5634 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5635 2417344c 2019-08-23 stsp if (datestr)
5636 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5637 d4efa91b 2020-01-14 stsp if (commit)
5638 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5639 d4efa91b 2020-01-14 stsp else {
5640 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5641 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5642 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5643 d4efa91b 2020-01-14 stsp id_str);
5644 d4efa91b 2020-01-14 stsp break;
5645 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5646 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5647 d4efa91b 2020-01-14 stsp id_str);
5648 d4efa91b 2020-01-14 stsp break;
5649 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5650 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5651 d4efa91b 2020-01-14 stsp id_str);
5652 d4efa91b 2020-01-14 stsp break;
5653 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5654 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5655 d4efa91b 2020-01-14 stsp id_str);
5656 d4efa91b 2020-01-14 stsp break;
5657 d4efa91b 2020-01-14 stsp default:
5658 d4efa91b 2020-01-14 stsp break;
5659 d4efa91b 2020-01-14 stsp }
5660 8e7bd50a 2019-08-22 stsp }
5661 8e7bd50a 2019-08-22 stsp free(id_str);
5662 d4efa91b 2020-01-14 stsp if (commit) {
5663 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5664 d4efa91b 2020-01-14 stsp if (err)
5665 d4efa91b 2020-01-14 stsp break;
5666 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5667 d4efa91b 2020-01-14 stsp } else {
5668 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5669 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5670 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5671 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5672 d4efa91b 2020-01-14 stsp break;
5673 d4efa91b 2020-01-14 stsp }
5674 8e7bd50a 2019-08-22 stsp }
5675 8e7bd50a 2019-08-22 stsp
5676 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5677 8e7bd50a 2019-08-22 stsp do {
5678 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5679 8e7bd50a 2019-08-22 stsp if (line)
5680 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5681 8e7bd50a 2019-08-22 stsp } while (line);
5682 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5683 8e7bd50a 2019-08-22 stsp }
5684 8e7bd50a 2019-08-22 stsp
5685 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5686 8e7bd50a 2019-08-22 stsp return NULL;
5687 8e7bd50a 2019-08-22 stsp }
5688 8e7bd50a 2019-08-22 stsp
5689 8e7bd50a 2019-08-22 stsp static const struct got_error *
5690 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5691 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5692 8e7bd50a 2019-08-22 stsp {
5693 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5694 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5695 f372d5cd 2019-10-21 stsp char *editor = NULL;
5696 1601cb9f 2020-09-11 naddy int initial_content_len;
5697 8e7bd50a 2019-08-22 stsp int fd = -1;
5698 8e7bd50a 2019-08-22 stsp
5699 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5700 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5701 8e7bd50a 2019-08-22 stsp goto done;
5702 8e7bd50a 2019-08-22 stsp }
5703 8e7bd50a 2019-08-22 stsp
5704 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5705 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5706 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5707 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5708 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5709 8e7bd50a 2019-08-22 stsp goto done;
5710 8e7bd50a 2019-08-22 stsp }
5711 8e7bd50a 2019-08-22 stsp
5712 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5713 8e7bd50a 2019-08-22 stsp if (err)
5714 8e7bd50a 2019-08-22 stsp goto done;
5715 8e7bd50a 2019-08-22 stsp
5716 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5717 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5718 97972933 2020-09-11 stsp goto done;
5719 97972933 2020-09-11 stsp }
5720 8e7bd50a 2019-08-22 stsp
5721 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5722 8e7bd50a 2019-08-22 stsp if (err)
5723 8e7bd50a 2019-08-22 stsp goto done;
5724 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5725 8e7bd50a 2019-08-22 stsp done:
5726 8e7bd50a 2019-08-22 stsp free(initial_content);
5727 8e7bd50a 2019-08-22 stsp free(template);
5728 8e7bd50a 2019-08-22 stsp free(editor);
5729 8e7bd50a 2019-08-22 stsp
5730 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5731 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5732 97972933 2020-09-11 stsp
5733 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5734 59f86c76 2020-09-11 stsp if (err == NULL)
5735 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5736 59f86c76 2020-09-11 stsp if (err) {
5737 59f86c76 2020-09-11 stsp free(*tagmsg);
5738 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5739 8e7bd50a 2019-08-22 stsp }
5740 8e7bd50a 2019-08-22 stsp return err;
5741 8e7bd50a 2019-08-22 stsp }
5742 8e7bd50a 2019-08-22 stsp
5743 8e7bd50a 2019-08-22 stsp static const struct got_error *
5744 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5745 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5746 8e7bd50a 2019-08-22 stsp {
5747 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5748 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5749 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5750 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5751 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5752 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5753 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5754 8e7bd50a 2019-08-22 stsp
5755 8e7bd50a 2019-08-22 stsp /*
5756 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5757 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5758 8e7bd50a 2019-08-22 stsp * an unintended typo.
5759 8e7bd50a 2019-08-22 stsp */
5760 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5761 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5762 8e7bd50a 2019-08-22 stsp
5763 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5764 8e7bd50a 2019-08-22 stsp if (err)
5765 8e7bd50a 2019-08-22 stsp return err;
5766 8e7bd50a 2019-08-22 stsp
5767 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5768 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5769 8e7bd50a 2019-08-22 stsp if (err)
5770 8e7bd50a 2019-08-22 stsp goto done;
5771 8e7bd50a 2019-08-22 stsp
5772 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5773 8e7bd50a 2019-08-22 stsp if (err)
5774 8e7bd50a 2019-08-22 stsp goto done;
5775 8e7bd50a 2019-08-22 stsp
5776 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5777 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5778 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5779 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5780 8e7bd50a 2019-08-22 stsp goto done;
5781 8e7bd50a 2019-08-22 stsp }
5782 8e7bd50a 2019-08-22 stsp tag_name += 10;
5783 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5784 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5785 8e7bd50a 2019-08-22 stsp goto done;
5786 8e7bd50a 2019-08-22 stsp }
5787 8e7bd50a 2019-08-22 stsp
5788 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5789 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5790 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5791 8e7bd50a 2019-08-22 stsp goto done;
5792 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5793 8e7bd50a 2019-08-22 stsp goto done;
5794 8e7bd50a 2019-08-22 stsp
5795 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5796 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5797 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5798 f372d5cd 2019-10-21 stsp if (err) {
5799 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5800 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5801 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5802 8e7bd50a 2019-08-22 stsp goto done;
5803 f372d5cd 2019-10-21 stsp }
5804 8e7bd50a 2019-08-22 stsp }
5805 8e7bd50a 2019-08-22 stsp
5806 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5807 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5808 f372d5cd 2019-10-21 stsp if (err) {
5809 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5810 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5811 8e7bd50a 2019-08-22 stsp goto done;
5812 f372d5cd 2019-10-21 stsp }
5813 8e7bd50a 2019-08-22 stsp
5814 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5815 f372d5cd 2019-10-21 stsp if (err) {
5816 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5817 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5818 8e7bd50a 2019-08-22 stsp goto done;
5819 f372d5cd 2019-10-21 stsp }
5820 8e7bd50a 2019-08-22 stsp
5821 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5822 f372d5cd 2019-10-21 stsp if (err) {
5823 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5824 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5825 f372d5cd 2019-10-21 stsp goto done;
5826 f372d5cd 2019-10-21 stsp }
5827 8e7bd50a 2019-08-22 stsp
5828 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5829 f372d5cd 2019-10-21 stsp if (err) {
5830 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5831 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5832 f372d5cd 2019-10-21 stsp goto done;
5833 8e7bd50a 2019-08-22 stsp }
5834 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5835 8e7bd50a 2019-08-22 stsp done:
5836 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5837 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5838 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5839 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5840 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5841 f372d5cd 2019-10-21 stsp free(tag_id_str);
5842 8e7bd50a 2019-08-22 stsp if (ref)
5843 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5844 8e7bd50a 2019-08-22 stsp free(commit_id);
5845 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5846 8e7bd50a 2019-08-22 stsp free(refname);
5847 8e7bd50a 2019-08-22 stsp free(tagmsg);
5848 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5849 aba9c984 2019-09-08 stsp free(tagger);
5850 8e7bd50a 2019-08-22 stsp return err;
5851 8e7bd50a 2019-08-22 stsp }
5852 8e7bd50a 2019-08-22 stsp
5853 8e7bd50a 2019-08-22 stsp static const struct got_error *
5854 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5855 8e7bd50a 2019-08-22 stsp {
5856 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5857 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5858 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5859 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5860 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5861 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5862 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5863 8e7bd50a 2019-08-22 stsp
5864 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5865 8e7bd50a 2019-08-22 stsp switch (ch) {
5866 80106605 2020-02-24 stsp case 'c':
5867 80106605 2020-02-24 stsp commit_id_arg = optarg;
5868 80106605 2020-02-24 stsp break;
5869 8e7bd50a 2019-08-22 stsp case 'm':
5870 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5871 8e7bd50a 2019-08-22 stsp break;
5872 8e7bd50a 2019-08-22 stsp case 'r':
5873 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5874 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5875 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5876 9ba1d308 2019-10-21 stsp optarg);
5877 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5878 8e7bd50a 2019-08-22 stsp break;
5879 8e7bd50a 2019-08-22 stsp case 'l':
5880 8e7bd50a 2019-08-22 stsp do_list = 1;
5881 8e7bd50a 2019-08-22 stsp break;
5882 8e7bd50a 2019-08-22 stsp default:
5883 8e7bd50a 2019-08-22 stsp usage_tag();
5884 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5885 8e7bd50a 2019-08-22 stsp }
5886 8e7bd50a 2019-08-22 stsp }
5887 8e7bd50a 2019-08-22 stsp
5888 8e7bd50a 2019-08-22 stsp argc -= optind;
5889 8e7bd50a 2019-08-22 stsp argv += optind;
5890 8e7bd50a 2019-08-22 stsp
5891 8e7bd50a 2019-08-22 stsp if (do_list) {
5892 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5893 775ce909 2020-03-22 stsp errx(1,
5894 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5895 8e7bd50a 2019-08-22 stsp if (tagmsg)
5896 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5897 8e7bd50a 2019-08-22 stsp if (argc > 0)
5898 8e7bd50a 2019-08-22 stsp usage_tag();
5899 80106605 2020-02-24 stsp } else if (argc != 1)
5900 8e7bd50a 2019-08-22 stsp usage_tag();
5901 80106605 2020-02-24 stsp
5902 a2887370 2019-08-23 stsp tag_name = argv[0];
5903 8e7bd50a 2019-08-22 stsp
5904 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5905 8e7bd50a 2019-08-22 stsp if (do_list) {
5906 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5907 8e7bd50a 2019-08-22 stsp NULL) == -1)
5908 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5909 8e7bd50a 2019-08-22 stsp } else {
5910 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5911 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5912 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5913 8e7bd50a 2019-08-22 stsp }
5914 8e7bd50a 2019-08-22 stsp #endif
5915 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5916 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5917 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5918 8e7bd50a 2019-08-22 stsp goto done;
5919 8e7bd50a 2019-08-22 stsp }
5920 8e7bd50a 2019-08-22 stsp
5921 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5922 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5923 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5924 8e7bd50a 2019-08-22 stsp goto done;
5925 8e7bd50a 2019-08-22 stsp else
5926 8e7bd50a 2019-08-22 stsp error = NULL;
5927 8e7bd50a 2019-08-22 stsp if (worktree) {
5928 8e7bd50a 2019-08-22 stsp repo_path =
5929 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5930 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5931 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5932 8e7bd50a 2019-08-22 stsp if (error)
5933 8e7bd50a 2019-08-22 stsp goto done;
5934 8e7bd50a 2019-08-22 stsp } else {
5935 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5936 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5937 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5938 8e7bd50a 2019-08-22 stsp goto done;
5939 8e7bd50a 2019-08-22 stsp }
5940 8e7bd50a 2019-08-22 stsp }
5941 8e7bd50a 2019-08-22 stsp }
5942 8e7bd50a 2019-08-22 stsp
5943 8e7bd50a 2019-08-22 stsp if (do_list) {
5944 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5945 c9956ddf 2019-09-08 stsp if (error != NULL)
5946 c9956ddf 2019-09-08 stsp goto done;
5947 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5948 8e7bd50a 2019-08-22 stsp if (error)
5949 8e7bd50a 2019-08-22 stsp goto done;
5950 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5951 8e7bd50a 2019-08-22 stsp } else {
5952 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5953 c9956ddf 2019-09-08 stsp if (error)
5954 c9956ddf 2019-09-08 stsp goto done;
5955 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5956 c9956ddf 2019-09-08 stsp if (error != NULL)
5957 c9956ddf 2019-09-08 stsp goto done;
5958 c9956ddf 2019-09-08 stsp
5959 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5960 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5961 8e7bd50a 2019-08-22 stsp if (error)
5962 8e7bd50a 2019-08-22 stsp goto done;
5963 8e7bd50a 2019-08-22 stsp }
5964 8e7bd50a 2019-08-22 stsp
5965 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5966 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5967 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5968 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5969 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5970 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5971 8e7bd50a 2019-08-22 stsp if (error)
5972 8e7bd50a 2019-08-22 stsp goto done;
5973 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5974 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5975 8e7bd50a 2019-08-22 stsp if (error)
5976 8e7bd50a 2019-08-22 stsp goto done;
5977 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5978 8e7bd50a 2019-08-22 stsp free(commit_id);
5979 8e7bd50a 2019-08-22 stsp if (error)
5980 8e7bd50a 2019-08-22 stsp goto done;
5981 8e7bd50a 2019-08-22 stsp }
5982 8e7bd50a 2019-08-22 stsp
5983 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
5984 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5985 8e7bd50a 2019-08-22 stsp }
5986 8e7bd50a 2019-08-22 stsp done:
5987 8e7bd50a 2019-08-22 stsp if (repo)
5988 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5989 8e7bd50a 2019-08-22 stsp if (worktree)
5990 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5991 8e7bd50a 2019-08-22 stsp free(cwd);
5992 8e7bd50a 2019-08-22 stsp free(repo_path);
5993 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5994 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5995 8e7bd50a 2019-08-22 stsp return error;
5996 8e7bd50a 2019-08-22 stsp }
5997 8e7bd50a 2019-08-22 stsp
5998 8e7bd50a 2019-08-22 stsp __dead static void
5999 d00136be 2019-03-26 stsp usage_add(void)
6000 d00136be 2019-03-26 stsp {
6001 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6002 022fae89 2019-12-06 tracey getprogname());
6003 d00136be 2019-03-26 stsp exit(1);
6004 d00136be 2019-03-26 stsp }
6005 d00136be 2019-03-26 stsp
6006 d00136be 2019-03-26 stsp static const struct got_error *
6007 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6008 4e68cba3 2019-11-23 stsp {
6009 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6010 4e68cba3 2019-11-23 stsp path++;
6011 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6012 4e68cba3 2019-11-23 stsp return NULL;
6013 4e68cba3 2019-11-23 stsp }
6014 4e68cba3 2019-11-23 stsp
6015 4e68cba3 2019-11-23 stsp static const struct got_error *
6016 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6017 d00136be 2019-03-26 stsp {
6018 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6019 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6020 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6021 1dd54920 2019-05-11 stsp char *cwd = NULL;
6022 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6023 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6024 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6025 1dd54920 2019-05-11 stsp
6026 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6027 d00136be 2019-03-26 stsp
6028 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6029 d00136be 2019-03-26 stsp switch (ch) {
6030 022fae89 2019-12-06 tracey case 'I':
6031 022fae89 2019-12-06 tracey no_ignores = 1;
6032 022fae89 2019-12-06 tracey break;
6033 4e68cba3 2019-11-23 stsp case 'R':
6034 4e68cba3 2019-11-23 stsp can_recurse = 1;
6035 4e68cba3 2019-11-23 stsp break;
6036 d00136be 2019-03-26 stsp default:
6037 d00136be 2019-03-26 stsp usage_add();
6038 d00136be 2019-03-26 stsp /* NOTREACHED */
6039 d00136be 2019-03-26 stsp }
6040 d00136be 2019-03-26 stsp }
6041 d00136be 2019-03-26 stsp
6042 d00136be 2019-03-26 stsp argc -= optind;
6043 d00136be 2019-03-26 stsp argv += optind;
6044 d00136be 2019-03-26 stsp
6045 43012d58 2019-07-14 stsp #ifndef PROFILE
6046 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6047 43012d58 2019-07-14 stsp NULL) == -1)
6048 43012d58 2019-07-14 stsp err(1, "pledge");
6049 43012d58 2019-07-14 stsp #endif
6050 723c305c 2019-05-11 jcs if (argc < 1)
6051 d00136be 2019-03-26 stsp usage_add();
6052 d00136be 2019-03-26 stsp
6053 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6054 d00136be 2019-03-26 stsp if (cwd == NULL) {
6055 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6056 d00136be 2019-03-26 stsp goto done;
6057 d00136be 2019-03-26 stsp }
6058 723c305c 2019-05-11 jcs
6059 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6060 fa51e947 2020-03-27 stsp if (error) {
6061 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6062 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6063 d00136be 2019-03-26 stsp goto done;
6064 fa51e947 2020-03-27 stsp }
6065 d00136be 2019-03-26 stsp
6066 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6067 c9956ddf 2019-09-08 stsp NULL);
6068 031a5338 2019-03-26 stsp if (error != NULL)
6069 031a5338 2019-03-26 stsp goto done;
6070 031a5338 2019-03-26 stsp
6071 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6072 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6073 d00136be 2019-03-26 stsp if (error)
6074 d00136be 2019-03-26 stsp goto done;
6075 d00136be 2019-03-26 stsp
6076 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6077 6d022e97 2019-08-04 stsp if (error)
6078 022fae89 2019-12-06 tracey goto done;
6079 022fae89 2019-12-06 tracey
6080 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6081 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6082 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6083 6d022e97 2019-08-04 stsp goto done;
6084 723c305c 2019-05-11 jcs
6085 022fae89 2019-12-06 tracey }
6086 022fae89 2019-12-06 tracey
6087 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6088 4e68cba3 2019-11-23 stsp char *ondisk_path;
6089 4e68cba3 2019-11-23 stsp struct stat sb;
6090 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6091 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6092 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6093 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6094 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6095 4e68cba3 2019-11-23 stsp goto done;
6096 4e68cba3 2019-11-23 stsp }
6097 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6098 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6099 4e68cba3 2019-11-23 stsp free(ondisk_path);
6100 4e68cba3 2019-11-23 stsp continue;
6101 4e68cba3 2019-11-23 stsp }
6102 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6103 4e68cba3 2019-11-23 stsp ondisk_path);
6104 4e68cba3 2019-11-23 stsp free(ondisk_path);
6105 4e68cba3 2019-11-23 stsp goto done;
6106 4e68cba3 2019-11-23 stsp }
6107 4e68cba3 2019-11-23 stsp free(ondisk_path);
6108 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6109 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6110 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6111 4e68cba3 2019-11-23 stsp goto done;
6112 4e68cba3 2019-11-23 stsp }
6113 4e68cba3 2019-11-23 stsp }
6114 4e68cba3 2019-11-23 stsp }
6115 022fae89 2019-12-06 tracey
6116 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6117 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6118 d00136be 2019-03-26 stsp done:
6119 031a5338 2019-03-26 stsp if (repo)
6120 031a5338 2019-03-26 stsp got_repo_close(repo);
6121 d00136be 2019-03-26 stsp if (worktree)
6122 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6123 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6124 1dd54920 2019-05-11 stsp free((char *)pe->path);
6125 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6126 2ec1f75b 2019-03-26 stsp free(cwd);
6127 2ec1f75b 2019-03-26 stsp return error;
6128 2ec1f75b 2019-03-26 stsp }
6129 2ec1f75b 2019-03-26 stsp
6130 2ec1f75b 2019-03-26 stsp __dead static void
6131 648e4ef7 2019-07-09 stsp usage_remove(void)
6132 2ec1f75b 2019-03-26 stsp {
6133 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6134 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6135 2ec1f75b 2019-03-26 stsp exit(1);
6136 2a06fe5f 2019-08-24 stsp }
6137 2a06fe5f 2019-08-24 stsp
6138 2a06fe5f 2019-08-24 stsp static const struct got_error *
6139 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6140 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6141 2a06fe5f 2019-08-24 stsp {
6142 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6143 f2a9dc41 2019-12-13 tracey path++;
6144 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6145 2a06fe5f 2019-08-24 stsp return NULL;
6146 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6147 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6148 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6149 2a06fe5f 2019-08-24 stsp return NULL;
6150 2ec1f75b 2019-03-26 stsp }
6151 2ec1f75b 2019-03-26 stsp
6152 2ec1f75b 2019-03-26 stsp static const struct got_error *
6153 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6154 2ec1f75b 2019-03-26 stsp {
6155 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6156 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6157 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6158 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6159 17ed4618 2019-06-02 stsp char *cwd = NULL;
6160 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6161 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6162 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6163 2ec1f75b 2019-03-26 stsp
6164 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6165 17ed4618 2019-06-02 stsp
6166 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6167 2ec1f75b 2019-03-26 stsp switch (ch) {
6168 2ec1f75b 2019-03-26 stsp case 'f':
6169 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6170 2ec1f75b 2019-03-26 stsp break;
6171 70e3e7f5 2019-12-13 tracey case 'k':
6172 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6173 70e3e7f5 2019-12-13 tracey break;
6174 f2a9dc41 2019-12-13 tracey case 'R':
6175 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6176 f2a9dc41 2019-12-13 tracey break;
6177 766841c2 2020-08-13 stsp case 's':
6178 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6179 766841c2 2020-08-13 stsp switch (optarg[i]) {
6180 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6181 766841c2 2020-08-13 stsp delete_local_mods = 1;
6182 766841c2 2020-08-13 stsp break;
6183 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6184 766841c2 2020-08-13 stsp break;
6185 766841c2 2020-08-13 stsp default:
6186 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6187 766841c2 2020-08-13 stsp optarg[i]);
6188 766841c2 2020-08-13 stsp }
6189 766841c2 2020-08-13 stsp }
6190 766841c2 2020-08-13 stsp status_codes = optarg;
6191 766841c2 2020-08-13 stsp break;
6192 2ec1f75b 2019-03-26 stsp default:
6193 f2a9dc41 2019-12-13 tracey usage_remove();
6194 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6195 2ec1f75b 2019-03-26 stsp }
6196 2ec1f75b 2019-03-26 stsp }
6197 2ec1f75b 2019-03-26 stsp
6198 2ec1f75b 2019-03-26 stsp argc -= optind;
6199 2ec1f75b 2019-03-26 stsp argv += optind;
6200 2ec1f75b 2019-03-26 stsp
6201 43012d58 2019-07-14 stsp #ifndef PROFILE
6202 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6203 43012d58 2019-07-14 stsp NULL) == -1)
6204 43012d58 2019-07-14 stsp err(1, "pledge");
6205 43012d58 2019-07-14 stsp #endif
6206 17ed4618 2019-06-02 stsp if (argc < 1)
6207 648e4ef7 2019-07-09 stsp usage_remove();
6208 2ec1f75b 2019-03-26 stsp
6209 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6210 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6211 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6212 2ec1f75b 2019-03-26 stsp goto done;
6213 2ec1f75b 2019-03-26 stsp }
6214 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6215 fa51e947 2020-03-27 stsp if (error) {
6216 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6217 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6218 2ec1f75b 2019-03-26 stsp goto done;
6219 fa51e947 2020-03-27 stsp }
6220 2ec1f75b 2019-03-26 stsp
6221 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6222 c9956ddf 2019-09-08 stsp NULL);
6223 2af4a041 2019-05-11 jcs if (error)
6224 2ec1f75b 2019-03-26 stsp goto done;
6225 2ec1f75b 2019-03-26 stsp
6226 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6227 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6228 2ec1f75b 2019-03-26 stsp if (error)
6229 2ec1f75b 2019-03-26 stsp goto done;
6230 2ec1f75b 2019-03-26 stsp
6231 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6232 6d022e97 2019-08-04 stsp if (error)
6233 6d022e97 2019-08-04 stsp goto done;
6234 17ed4618 2019-06-02 stsp
6235 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6236 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6237 f2a9dc41 2019-12-13 tracey struct stat sb;
6238 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6239 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6240 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6241 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6242 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6243 f2a9dc41 2019-12-13 tracey goto done;
6244 f2a9dc41 2019-12-13 tracey }
6245 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6246 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6247 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6248 f2a9dc41 2019-12-13 tracey continue;
6249 f2a9dc41 2019-12-13 tracey }
6250 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6251 f2a9dc41 2019-12-13 tracey ondisk_path);
6252 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6253 f2a9dc41 2019-12-13 tracey goto done;
6254 f2a9dc41 2019-12-13 tracey }
6255 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6256 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6257 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6258 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6259 f2a9dc41 2019-12-13 tracey goto done;
6260 f2a9dc41 2019-12-13 tracey }
6261 f2a9dc41 2019-12-13 tracey }
6262 f2a9dc41 2019-12-13 tracey }
6263 f2a9dc41 2019-12-13 tracey
6264 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6265 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6266 766841c2 2020-08-13 stsp repo, keep_on_disk);
6267 a129376b 2019-03-28 stsp done:
6268 a129376b 2019-03-28 stsp if (repo)
6269 a129376b 2019-03-28 stsp got_repo_close(repo);
6270 a129376b 2019-03-28 stsp if (worktree)
6271 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6272 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6273 17ed4618 2019-06-02 stsp free((char *)pe->path);
6274 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6275 a129376b 2019-03-28 stsp free(cwd);
6276 a129376b 2019-03-28 stsp return error;
6277 a129376b 2019-03-28 stsp }
6278 a129376b 2019-03-28 stsp
6279 a129376b 2019-03-28 stsp __dead static void
6280 a129376b 2019-03-28 stsp usage_revert(void)
6281 a129376b 2019-03-28 stsp {
6282 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6283 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6284 a129376b 2019-03-28 stsp exit(1);
6285 a129376b 2019-03-28 stsp }
6286 a129376b 2019-03-28 stsp
6287 1ee397ad 2019-07-12 stsp static const struct got_error *
6288 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6289 a129376b 2019-03-28 stsp {
6290 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6291 fb9704af 2020-01-27 stsp return NULL;
6292 fb9704af 2020-01-27 stsp
6293 a129376b 2019-03-28 stsp while (path[0] == '/')
6294 a129376b 2019-03-28 stsp path++;
6295 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6296 33aa809d 2019-08-08 stsp return NULL;
6297 33aa809d 2019-08-08 stsp }
6298 33aa809d 2019-08-08 stsp
6299 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6300 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6301 33aa809d 2019-08-08 stsp const char *action;
6302 33aa809d 2019-08-08 stsp };
6303 33aa809d 2019-08-08 stsp
6304 33aa809d 2019-08-08 stsp static const struct got_error *
6305 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6306 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6307 33aa809d 2019-08-08 stsp {
6308 33aa809d 2019-08-08 stsp char *line = NULL;
6309 33aa809d 2019-08-08 stsp size_t linesize = 0;
6310 33aa809d 2019-08-08 stsp ssize_t linelen;
6311 33aa809d 2019-08-08 stsp
6312 33aa809d 2019-08-08 stsp switch (status) {
6313 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6314 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6315 33aa809d 2019-08-08 stsp break;
6316 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6317 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6318 33aa809d 2019-08-08 stsp break;
6319 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6320 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6321 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6322 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6323 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6324 33aa809d 2019-08-08 stsp printf("%s", line);
6325 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6326 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6327 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6328 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6329 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6330 33aa809d 2019-08-08 stsp break;
6331 33aa809d 2019-08-08 stsp default:
6332 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6333 33aa809d 2019-08-08 stsp }
6334 33aa809d 2019-08-08 stsp
6335 33aa809d 2019-08-08 stsp return NULL;
6336 33aa809d 2019-08-08 stsp }
6337 33aa809d 2019-08-08 stsp
6338 33aa809d 2019-08-08 stsp static const struct got_error *
6339 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6340 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6341 33aa809d 2019-08-08 stsp {
6342 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6343 33aa809d 2019-08-08 stsp char *line = NULL;
6344 33aa809d 2019-08-08 stsp size_t linesize = 0;
6345 33aa809d 2019-08-08 stsp ssize_t linelen;
6346 33aa809d 2019-08-08 stsp int resp = ' ';
6347 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6348 33aa809d 2019-08-08 stsp
6349 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6350 33aa809d 2019-08-08 stsp
6351 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6352 33aa809d 2019-08-08 stsp char *nl;
6353 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6354 33aa809d 2019-08-08 stsp a->action);
6355 33aa809d 2019-08-08 stsp if (err)
6356 33aa809d 2019-08-08 stsp return err;
6357 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6358 33aa809d 2019-08-08 stsp if (linelen == -1) {
6359 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6360 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6361 33aa809d 2019-08-08 stsp return NULL;
6362 33aa809d 2019-08-08 stsp }
6363 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6364 33aa809d 2019-08-08 stsp if (nl)
6365 33aa809d 2019-08-08 stsp *nl = '\0';
6366 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6367 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6368 33aa809d 2019-08-08 stsp printf("y\n");
6369 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6370 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6371 33aa809d 2019-08-08 stsp printf("n\n");
6372 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6373 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6374 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6375 33aa809d 2019-08-08 stsp printf("q\n");
6376 33aa809d 2019-08-08 stsp } else
6377 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6378 33aa809d 2019-08-08 stsp free(line);
6379 33aa809d 2019-08-08 stsp return NULL;
6380 33aa809d 2019-08-08 stsp }
6381 33aa809d 2019-08-08 stsp
6382 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6383 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6384 33aa809d 2019-08-08 stsp a->action);
6385 33aa809d 2019-08-08 stsp if (err)
6386 33aa809d 2019-08-08 stsp return err;
6387 33aa809d 2019-08-08 stsp resp = getchar();
6388 33aa809d 2019-08-08 stsp if (resp == '\n')
6389 33aa809d 2019-08-08 stsp resp = getchar();
6390 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6391 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6392 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6393 33aa809d 2019-08-08 stsp resp = ' ';
6394 33aa809d 2019-08-08 stsp }
6395 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6396 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6397 33aa809d 2019-08-08 stsp resp = ' ';
6398 33aa809d 2019-08-08 stsp }
6399 33aa809d 2019-08-08 stsp }
6400 33aa809d 2019-08-08 stsp
6401 33aa809d 2019-08-08 stsp if (resp == 'y')
6402 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6403 33aa809d 2019-08-08 stsp else if (resp == 'n')
6404 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6405 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6406 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6407 33aa809d 2019-08-08 stsp
6408 1ee397ad 2019-07-12 stsp return NULL;
6409 a129376b 2019-03-28 stsp }
6410 a129376b 2019-03-28 stsp
6411 33aa809d 2019-08-08 stsp
6412 a129376b 2019-03-28 stsp static const struct got_error *
6413 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6414 a129376b 2019-03-28 stsp {
6415 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6416 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6417 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6418 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6419 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6420 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6421 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6422 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6423 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6424 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6425 a129376b 2019-03-28 stsp
6426 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6427 e20a8b6f 2019-06-04 stsp
6428 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6429 a129376b 2019-03-28 stsp switch (ch) {
6430 33aa809d 2019-08-08 stsp case 'p':
6431 33aa809d 2019-08-08 stsp pflag = 1;
6432 33aa809d 2019-08-08 stsp break;
6433 33aa809d 2019-08-08 stsp case 'F':
6434 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6435 33aa809d 2019-08-08 stsp break;
6436 0f6d7415 2019-08-08 stsp case 'R':
6437 0f6d7415 2019-08-08 stsp can_recurse = 1;
6438 0f6d7415 2019-08-08 stsp break;
6439 a129376b 2019-03-28 stsp default:
6440 a129376b 2019-03-28 stsp usage_revert();
6441 a129376b 2019-03-28 stsp /* NOTREACHED */
6442 a129376b 2019-03-28 stsp }
6443 a129376b 2019-03-28 stsp }
6444 a129376b 2019-03-28 stsp
6445 a129376b 2019-03-28 stsp argc -= optind;
6446 a129376b 2019-03-28 stsp argv += optind;
6447 a129376b 2019-03-28 stsp
6448 43012d58 2019-07-14 stsp #ifndef PROFILE
6449 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6450 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6451 43012d58 2019-07-14 stsp err(1, "pledge");
6452 43012d58 2019-07-14 stsp #endif
6453 e20a8b6f 2019-06-04 stsp if (argc < 1)
6454 a129376b 2019-03-28 stsp usage_revert();
6455 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6456 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6457 a129376b 2019-03-28 stsp
6458 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6459 a129376b 2019-03-28 stsp if (cwd == NULL) {
6460 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6461 a129376b 2019-03-28 stsp goto done;
6462 a129376b 2019-03-28 stsp }
6463 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6464 fa51e947 2020-03-27 stsp if (error) {
6465 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6466 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6467 2ec1f75b 2019-03-26 stsp goto done;
6468 fa51e947 2020-03-27 stsp }
6469 a129376b 2019-03-28 stsp
6470 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6471 c9956ddf 2019-09-08 stsp NULL);
6472 a129376b 2019-03-28 stsp if (error != NULL)
6473 a129376b 2019-03-28 stsp goto done;
6474 a129376b 2019-03-28 stsp
6475 33aa809d 2019-08-08 stsp if (patch_script_path) {
6476 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6477 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6478 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6479 33aa809d 2019-08-08 stsp patch_script_path);
6480 33aa809d 2019-08-08 stsp goto done;
6481 33aa809d 2019-08-08 stsp }
6482 33aa809d 2019-08-08 stsp }
6483 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6484 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6485 a129376b 2019-03-28 stsp if (error)
6486 a129376b 2019-03-28 stsp goto done;
6487 a129376b 2019-03-28 stsp
6488 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6489 6d022e97 2019-08-04 stsp if (error)
6490 6d022e97 2019-08-04 stsp goto done;
6491 00db391e 2019-08-03 stsp
6492 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6493 0f6d7415 2019-08-08 stsp char *ondisk_path;
6494 0f6d7415 2019-08-08 stsp struct stat sb;
6495 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6496 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6497 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6498 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6499 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6500 0f6d7415 2019-08-08 stsp goto done;
6501 0f6d7415 2019-08-08 stsp }
6502 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6503 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6504 0f6d7415 2019-08-08 stsp free(ondisk_path);
6505 0f6d7415 2019-08-08 stsp continue;
6506 0f6d7415 2019-08-08 stsp }
6507 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6508 0f6d7415 2019-08-08 stsp ondisk_path);
6509 0f6d7415 2019-08-08 stsp free(ondisk_path);
6510 0f6d7415 2019-08-08 stsp goto done;
6511 0f6d7415 2019-08-08 stsp }
6512 0f6d7415 2019-08-08 stsp free(ondisk_path);
6513 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6514 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6515 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6516 0f6d7415 2019-08-08 stsp goto done;
6517 0f6d7415 2019-08-08 stsp }
6518 0f6d7415 2019-08-08 stsp }
6519 0f6d7415 2019-08-08 stsp }
6520 0f6d7415 2019-08-08 stsp
6521 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6522 33aa809d 2019-08-08 stsp cpa.action = "revert";
6523 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6524 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6525 2ec1f75b 2019-03-26 stsp done:
6526 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6527 33aa809d 2019-08-08 stsp error == NULL)
6528 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6529 2ec1f75b 2019-03-26 stsp if (repo)
6530 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6531 2ec1f75b 2019-03-26 stsp if (worktree)
6532 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6533 2ec1f75b 2019-03-26 stsp free(path);
6534 d00136be 2019-03-26 stsp free(cwd);
6535 6bad629b 2019-02-04 stsp return error;
6536 c4296144 2019-05-09 stsp }
6537 c4296144 2019-05-09 stsp
6538 c4296144 2019-05-09 stsp __dead static void
6539 c4296144 2019-05-09 stsp usage_commit(void)
6540 c4296144 2019-05-09 stsp {
6541 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6542 5c1e53bc 2019-07-28 stsp getprogname());
6543 c4296144 2019-05-09 stsp exit(1);
6544 33ad4cbe 2019-05-12 jcs }
6545 33ad4cbe 2019-05-12 jcs
6546 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6547 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6548 e2ba3d07 2019-05-13 stsp const char *editor;
6549 e0870e44 2019-05-13 stsp const char *worktree_path;
6550 76d98825 2019-06-03 stsp const char *branch_name;
6551 314a6357 2019-05-13 stsp const char *repo_path;
6552 e0870e44 2019-05-13 stsp char *logmsg_path;
6553 e2ba3d07 2019-05-13 stsp
6554 e2ba3d07 2019-05-13 stsp };
6555 e0870e44 2019-05-13 stsp
6556 33ad4cbe 2019-05-12 jcs static const struct got_error *
6557 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6558 33ad4cbe 2019-05-12 jcs void *arg)
6559 33ad4cbe 2019-05-12 jcs {
6560 76d98825 2019-06-03 stsp char *initial_content = NULL;
6561 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6562 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6563 e0870e44 2019-05-13 stsp char *template = NULL;
6564 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6565 1601cb9f 2020-09-11 naddy int initial_content_len;
6566 97972933 2020-09-11 stsp int fd = -1;
6567 33ad4cbe 2019-05-12 jcs size_t len;
6568 33ad4cbe 2019-05-12 jcs
6569 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6570 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6571 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6572 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6573 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6574 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6575 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6576 33ad4cbe 2019-05-12 jcs return NULL;
6577 33ad4cbe 2019-05-12 jcs }
6578 33ad4cbe 2019-05-12 jcs
6579 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6580 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6581 76d98825 2019-06-03 stsp
6582 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6583 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6584 1601cb9f 2020-09-11 naddy a->branch_name);
6585 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6586 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6587 e0870e44 2019-05-13 stsp
6588 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6589 793c30b5 2019-05-13 stsp if (err)
6590 e0870e44 2019-05-13 stsp goto done;
6591 33ad4cbe 2019-05-12 jcs
6592 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6593 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6594 97972933 2020-09-11 stsp goto done;
6595 97972933 2020-09-11 stsp }
6596 33ad4cbe 2019-05-12 jcs
6597 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6598 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6599 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6600 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6601 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6602 33ad4cbe 2019-05-12 jcs }
6603 33ad4cbe 2019-05-12 jcs
6604 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6605 33ad4cbe 2019-05-12 jcs done:
6606 76d98825 2019-06-03 stsp free(initial_content);
6607 e0870e44 2019-05-13 stsp free(template);
6608 314a6357 2019-05-13 stsp
6609 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6610 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6611 97972933 2020-09-11 stsp
6612 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6613 59f86c76 2020-09-11 stsp if (err == NULL)
6614 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6615 59f86c76 2020-09-11 stsp if (err) {
6616 59f86c76 2020-09-11 stsp free(*logmsg);
6617 59f86c76 2020-09-11 stsp *logmsg = NULL;
6618 3ce1b845 2019-07-15 stsp }
6619 33ad4cbe 2019-05-12 jcs return err;
6620 6bad629b 2019-02-04 stsp }
6621 c4296144 2019-05-09 stsp
6622 c4296144 2019-05-09 stsp static const struct got_error *
6623 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6624 c4296144 2019-05-09 stsp {
6625 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6626 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6627 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6628 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6629 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6630 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6631 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6632 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6633 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6634 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6635 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6636 5c1e53bc 2019-07-28 stsp
6637 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6638 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6639 c4296144 2019-05-09 stsp
6640 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6641 c4296144 2019-05-09 stsp switch (ch) {
6642 c4296144 2019-05-09 stsp case 'm':
6643 c4296144 2019-05-09 stsp logmsg = optarg;
6644 c4296144 2019-05-09 stsp break;
6645 35213c7c 2020-07-23 stsp case 'S':
6646 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6647 35213c7c 2020-07-23 stsp break;
6648 c4296144 2019-05-09 stsp default:
6649 c4296144 2019-05-09 stsp usage_commit();
6650 c4296144 2019-05-09 stsp /* NOTREACHED */
6651 c4296144 2019-05-09 stsp }
6652 c4296144 2019-05-09 stsp }
6653 c4296144 2019-05-09 stsp
6654 c4296144 2019-05-09 stsp argc -= optind;
6655 c4296144 2019-05-09 stsp argv += optind;
6656 c4296144 2019-05-09 stsp
6657 43012d58 2019-07-14 stsp #ifndef PROFILE
6658 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6659 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6660 43012d58 2019-07-14 stsp err(1, "pledge");
6661 43012d58 2019-07-14 stsp #endif
6662 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6663 c4296144 2019-05-09 stsp if (cwd == NULL) {
6664 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6665 c4296144 2019-05-09 stsp goto done;
6666 c4296144 2019-05-09 stsp }
6667 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6668 fa51e947 2020-03-27 stsp if (error) {
6669 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6670 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6671 7d5807f4 2019-07-11 stsp goto done;
6672 fa51e947 2020-03-27 stsp }
6673 7d5807f4 2019-07-11 stsp
6674 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6675 c4296144 2019-05-09 stsp if (error)
6676 a698f62e 2019-07-25 stsp goto done;
6677 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6678 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6679 c4296144 2019-05-09 stsp goto done;
6680 a698f62e 2019-07-25 stsp }
6681 5c1e53bc 2019-07-28 stsp
6682 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6683 916f288c 2019-07-30 stsp worktree);
6684 5c1e53bc 2019-07-28 stsp if (error)
6685 5c1e53bc 2019-07-28 stsp goto done;
6686 c4296144 2019-05-09 stsp
6687 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6688 c9956ddf 2019-09-08 stsp if (error)
6689 c9956ddf 2019-09-08 stsp goto done;
6690 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6691 c9956ddf 2019-09-08 stsp gitconfig_path);
6692 c4296144 2019-05-09 stsp if (error != NULL)
6693 c4296144 2019-05-09 stsp goto done;
6694 c4296144 2019-05-09 stsp
6695 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6696 aba9c984 2019-09-08 stsp if (error)
6697 aba9c984 2019-09-08 stsp return error;
6698 aba9c984 2019-09-08 stsp
6699 314a6357 2019-05-13 stsp /*
6700 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6701 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6702 314a6357 2019-05-13 stsp */
6703 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6704 314a6357 2019-05-13 stsp error = get_editor(&editor);
6705 314a6357 2019-05-13 stsp else
6706 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6707 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6708 c4296144 2019-05-09 stsp if (error)
6709 c4296144 2019-05-09 stsp goto done;
6710 c4296144 2019-05-09 stsp
6711 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6712 087fb88c 2019-08-04 stsp if (error)
6713 087fb88c 2019-08-04 stsp goto done;
6714 087fb88c 2019-08-04 stsp
6715 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6716 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6717 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6718 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6719 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6720 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6721 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6722 916f288c 2019-07-30 stsp goto done;
6723 916f288c 2019-07-30 stsp }
6724 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6725 916f288c 2019-07-30 stsp }
6726 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6727 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6728 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6729 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6730 e0870e44 2019-05-13 stsp if (error) {
6731 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6732 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6733 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6734 c4296144 2019-05-09 stsp goto done;
6735 e0870e44 2019-05-13 stsp }
6736 c4296144 2019-05-09 stsp
6737 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6738 c4296144 2019-05-09 stsp if (error)
6739 c4296144 2019-05-09 stsp goto done;
6740 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6741 c4296144 2019-05-09 stsp done:
6742 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6743 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6744 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6745 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6746 7266f21f 2019-10-21 stsp error == NULL)
6747 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6748 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6749 c4296144 2019-05-09 stsp if (repo)
6750 c4296144 2019-05-09 stsp got_repo_close(repo);
6751 c4296144 2019-05-09 stsp if (worktree)
6752 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6753 c4296144 2019-05-09 stsp free(cwd);
6754 c4296144 2019-05-09 stsp free(id_str);
6755 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6756 e2ba3d07 2019-05-13 stsp free(editor);
6757 aba9c984 2019-09-08 stsp free(author);
6758 234035bc 2019-06-01 stsp return error;
6759 234035bc 2019-06-01 stsp }
6760 234035bc 2019-06-01 stsp
6761 234035bc 2019-06-01 stsp __dead static void
6762 234035bc 2019-06-01 stsp usage_cherrypick(void)
6763 234035bc 2019-06-01 stsp {
6764 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6765 234035bc 2019-06-01 stsp exit(1);
6766 234035bc 2019-06-01 stsp }
6767 234035bc 2019-06-01 stsp
6768 234035bc 2019-06-01 stsp static const struct got_error *
6769 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6770 234035bc 2019-06-01 stsp {
6771 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6772 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6773 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6774 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6775 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6776 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6777 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6778 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6779 9627c110 2020-04-18 stsp int ch;
6780 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6781 234035bc 2019-06-01 stsp
6782 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6783 234035bc 2019-06-01 stsp switch (ch) {
6784 234035bc 2019-06-01 stsp default:
6785 234035bc 2019-06-01 stsp usage_cherrypick();
6786 234035bc 2019-06-01 stsp /* NOTREACHED */
6787 234035bc 2019-06-01 stsp }
6788 234035bc 2019-06-01 stsp }
6789 234035bc 2019-06-01 stsp
6790 234035bc 2019-06-01 stsp argc -= optind;
6791 234035bc 2019-06-01 stsp argv += optind;
6792 234035bc 2019-06-01 stsp
6793 43012d58 2019-07-14 stsp #ifndef PROFILE
6794 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6795 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6796 43012d58 2019-07-14 stsp err(1, "pledge");
6797 43012d58 2019-07-14 stsp #endif
6798 234035bc 2019-06-01 stsp if (argc != 1)
6799 234035bc 2019-06-01 stsp usage_cherrypick();
6800 234035bc 2019-06-01 stsp
6801 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6802 234035bc 2019-06-01 stsp if (cwd == NULL) {
6803 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6804 234035bc 2019-06-01 stsp goto done;
6805 234035bc 2019-06-01 stsp }
6806 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6807 fa51e947 2020-03-27 stsp if (error) {
6808 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6809 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6810 fa51e947 2020-03-27 stsp cwd);
6811 234035bc 2019-06-01 stsp goto done;
6812 fa51e947 2020-03-27 stsp }
6813 234035bc 2019-06-01 stsp
6814 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6815 c9956ddf 2019-09-08 stsp NULL);
6816 234035bc 2019-06-01 stsp if (error != NULL)
6817 234035bc 2019-06-01 stsp goto done;
6818 234035bc 2019-06-01 stsp
6819 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6820 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6821 234035bc 2019-06-01 stsp if (error)
6822 234035bc 2019-06-01 stsp goto done;
6823 234035bc 2019-06-01 stsp
6824 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6825 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6826 234035bc 2019-06-01 stsp if (error != NULL) {
6827 234035bc 2019-06-01 stsp struct got_reference *ref;
6828 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6829 234035bc 2019-06-01 stsp goto done;
6830 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6831 234035bc 2019-06-01 stsp if (error != NULL)
6832 234035bc 2019-06-01 stsp goto done;
6833 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6834 234035bc 2019-06-01 stsp got_ref_close(ref);
6835 234035bc 2019-06-01 stsp if (error != NULL)
6836 234035bc 2019-06-01 stsp goto done;
6837 234035bc 2019-06-01 stsp }
6838 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6839 234035bc 2019-06-01 stsp if (error)
6840 234035bc 2019-06-01 stsp goto done;
6841 234035bc 2019-06-01 stsp
6842 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6843 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6844 234035bc 2019-06-01 stsp if (error != NULL)
6845 234035bc 2019-06-01 stsp goto done;
6846 234035bc 2019-06-01 stsp
6847 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6848 234035bc 2019-06-01 stsp if (error) {
6849 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6850 234035bc 2019-06-01 stsp goto done;
6851 234035bc 2019-06-01 stsp error = NULL;
6852 234035bc 2019-06-01 stsp } else {
6853 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6854 234035bc 2019-06-01 stsp goto done;
6855 234035bc 2019-06-01 stsp }
6856 234035bc 2019-06-01 stsp
6857 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6858 234035bc 2019-06-01 stsp if (error)
6859 234035bc 2019-06-01 stsp goto done;
6860 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6861 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6862 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6863 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6864 03415a1a 2019-06-02 stsp NULL);
6865 234035bc 2019-06-01 stsp if (error != NULL)
6866 234035bc 2019-06-01 stsp goto done;
6867 234035bc 2019-06-01 stsp
6868 9627c110 2020-04-18 stsp if (upa.did_something)
6869 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6870 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6871 234035bc 2019-06-01 stsp done:
6872 234035bc 2019-06-01 stsp if (commit)
6873 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6874 234035bc 2019-06-01 stsp free(commit_id_str);
6875 234035bc 2019-06-01 stsp if (head_ref)
6876 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6877 234035bc 2019-06-01 stsp if (worktree)
6878 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6879 234035bc 2019-06-01 stsp if (repo)
6880 234035bc 2019-06-01 stsp got_repo_close(repo);
6881 c4296144 2019-05-09 stsp return error;
6882 c4296144 2019-05-09 stsp }
6883 5ef14e63 2019-06-02 stsp
6884 5ef14e63 2019-06-02 stsp __dead static void
6885 5ef14e63 2019-06-02 stsp usage_backout(void)
6886 5ef14e63 2019-06-02 stsp {
6887 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6888 5ef14e63 2019-06-02 stsp exit(1);
6889 5ef14e63 2019-06-02 stsp }
6890 5ef14e63 2019-06-02 stsp
6891 5ef14e63 2019-06-02 stsp static const struct got_error *
6892 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6893 5ef14e63 2019-06-02 stsp {
6894 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6895 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6896 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6897 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6898 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6899 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6900 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6901 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6902 9627c110 2020-04-18 stsp int ch;
6903 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6904 5ef14e63 2019-06-02 stsp
6905 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6906 5ef14e63 2019-06-02 stsp switch (ch) {
6907 5ef14e63 2019-06-02 stsp default:
6908 5ef14e63 2019-06-02 stsp usage_backout();
6909 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6910 5ef14e63 2019-06-02 stsp }
6911 5ef14e63 2019-06-02 stsp }
6912 5ef14e63 2019-06-02 stsp
6913 5ef14e63 2019-06-02 stsp argc -= optind;
6914 5ef14e63 2019-06-02 stsp argv += optind;
6915 5ef14e63 2019-06-02 stsp
6916 43012d58 2019-07-14 stsp #ifndef PROFILE
6917 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6918 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6919 43012d58 2019-07-14 stsp err(1, "pledge");
6920 43012d58 2019-07-14 stsp #endif
6921 5ef14e63 2019-06-02 stsp if (argc != 1)
6922 5ef14e63 2019-06-02 stsp usage_backout();
6923 5ef14e63 2019-06-02 stsp
6924 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6925 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6926 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6927 5ef14e63 2019-06-02 stsp goto done;
6928 5ef14e63 2019-06-02 stsp }
6929 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6930 fa51e947 2020-03-27 stsp if (error) {
6931 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6932 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6933 5ef14e63 2019-06-02 stsp goto done;
6934 fa51e947 2020-03-27 stsp }
6935 5ef14e63 2019-06-02 stsp
6936 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6937 c9956ddf 2019-09-08 stsp NULL);
6938 5ef14e63 2019-06-02 stsp if (error != NULL)
6939 5ef14e63 2019-06-02 stsp goto done;
6940 5ef14e63 2019-06-02 stsp
6941 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6942 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6943 5ef14e63 2019-06-02 stsp if (error)
6944 5ef14e63 2019-06-02 stsp goto done;
6945 5ef14e63 2019-06-02 stsp
6946 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6947 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6948 5ef14e63 2019-06-02 stsp if (error != NULL) {
6949 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6950 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6951 5ef14e63 2019-06-02 stsp goto done;
6952 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6953 5ef14e63 2019-06-02 stsp if (error != NULL)
6954 5ef14e63 2019-06-02 stsp goto done;
6955 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6956 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6957 5ef14e63 2019-06-02 stsp if (error != NULL)
6958 5ef14e63 2019-06-02 stsp goto done;
6959 5ef14e63 2019-06-02 stsp }
6960 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6961 5ef14e63 2019-06-02 stsp if (error)
6962 5ef14e63 2019-06-02 stsp goto done;
6963 5ef14e63 2019-06-02 stsp
6964 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6965 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6966 5ef14e63 2019-06-02 stsp if (error != NULL)
6967 5ef14e63 2019-06-02 stsp goto done;
6968 5ef14e63 2019-06-02 stsp
6969 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6970 5ef14e63 2019-06-02 stsp if (error)
6971 5ef14e63 2019-06-02 stsp goto done;
6972 5ef14e63 2019-06-02 stsp
6973 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6974 5ef14e63 2019-06-02 stsp if (error)
6975 5ef14e63 2019-06-02 stsp goto done;
6976 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6977 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6978 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6979 5ef14e63 2019-06-02 stsp goto done;
6980 5ef14e63 2019-06-02 stsp }
6981 5ef14e63 2019-06-02 stsp
6982 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6983 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6984 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
6985 5ef14e63 2019-06-02 stsp if (error != NULL)
6986 5ef14e63 2019-06-02 stsp goto done;
6987 5ef14e63 2019-06-02 stsp
6988 9627c110 2020-04-18 stsp if (upa.did_something)
6989 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6990 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6991 5ef14e63 2019-06-02 stsp done:
6992 5ef14e63 2019-06-02 stsp if (commit)
6993 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6994 5ef14e63 2019-06-02 stsp free(commit_id_str);
6995 5ef14e63 2019-06-02 stsp if (head_ref)
6996 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6997 818c7501 2019-07-11 stsp if (worktree)
6998 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6999 818c7501 2019-07-11 stsp if (repo)
7000 818c7501 2019-07-11 stsp got_repo_close(repo);
7001 818c7501 2019-07-11 stsp return error;
7002 818c7501 2019-07-11 stsp }
7003 818c7501 2019-07-11 stsp
7004 818c7501 2019-07-11 stsp __dead static void
7005 818c7501 2019-07-11 stsp usage_rebase(void)
7006 818c7501 2019-07-11 stsp {
7007 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7008 af54c8f8 2019-07-11 stsp getprogname());
7009 818c7501 2019-07-11 stsp exit(1);
7010 0ebf8283 2019-07-24 stsp }
7011 0ebf8283 2019-07-24 stsp
7012 0ebf8283 2019-07-24 stsp void
7013 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7014 0ebf8283 2019-07-24 stsp {
7015 0ebf8283 2019-07-24 stsp char *nl;
7016 0ebf8283 2019-07-24 stsp size_t len;
7017 0ebf8283 2019-07-24 stsp
7018 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7019 0ebf8283 2019-07-24 stsp if (len > limit)
7020 0ebf8283 2019-07-24 stsp len = limit;
7021 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7022 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7023 0ebf8283 2019-07-24 stsp if (nl)
7024 0ebf8283 2019-07-24 stsp *nl = '\0';
7025 0ebf8283 2019-07-24 stsp }
7026 0ebf8283 2019-07-24 stsp
7027 0ebf8283 2019-07-24 stsp static const struct got_error *
7028 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7029 0ebf8283 2019-07-24 stsp {
7030 5943eee2 2019-08-13 stsp const struct got_error *err;
7031 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7032 5943eee2 2019-08-13 stsp const char *s;
7033 0ebf8283 2019-07-24 stsp
7034 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7035 5943eee2 2019-08-13 stsp if (err)
7036 5943eee2 2019-08-13 stsp return err;
7037 0ebf8283 2019-07-24 stsp
7038 5943eee2 2019-08-13 stsp s = logmsg0;
7039 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7040 5943eee2 2019-08-13 stsp s++;
7041 5943eee2 2019-08-13 stsp
7042 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7043 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7044 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7045 5943eee2 2019-08-13 stsp goto done;
7046 5943eee2 2019-08-13 stsp }
7047 0ebf8283 2019-07-24 stsp
7048 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7049 5943eee2 2019-08-13 stsp done:
7050 5943eee2 2019-08-13 stsp free(logmsg0);
7051 a0ea4fc0 2020-02-28 stsp return err;
7052 a0ea4fc0 2020-02-28 stsp }
7053 a0ea4fc0 2020-02-28 stsp
7054 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7055 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7056 a0ea4fc0 2020-02-28 stsp {
7057 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7058 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7059 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7060 a0ea4fc0 2020-02-28 stsp
7061 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7062 a0ea4fc0 2020-02-28 stsp if (err)
7063 a0ea4fc0 2020-02-28 stsp return err;
7064 a0ea4fc0 2020-02-28 stsp
7065 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7066 a0ea4fc0 2020-02-28 stsp if (err)
7067 a0ea4fc0 2020-02-28 stsp goto done;
7068 a0ea4fc0 2020-02-28 stsp
7069 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7070 a0ea4fc0 2020-02-28 stsp
7071 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7072 a0ea4fc0 2020-02-28 stsp if (err)
7073 a0ea4fc0 2020-02-28 stsp goto done;
7074 a0ea4fc0 2020-02-28 stsp
7075 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7076 a0ea4fc0 2020-02-28 stsp done:
7077 a0ea4fc0 2020-02-28 stsp free(id_str);
7078 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7079 a0ea4fc0 2020-02-28 stsp free(logmsg);
7080 5943eee2 2019-08-13 stsp return err;
7081 818c7501 2019-07-11 stsp }
7082 818c7501 2019-07-11 stsp
7083 818c7501 2019-07-11 stsp static const struct got_error *
7084 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7085 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7086 818c7501 2019-07-11 stsp {
7087 818c7501 2019-07-11 stsp const struct got_error *err;
7088 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7089 818c7501 2019-07-11 stsp
7090 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7091 818c7501 2019-07-11 stsp if (err)
7092 818c7501 2019-07-11 stsp goto done;
7093 818c7501 2019-07-11 stsp
7094 ff0d2220 2019-07-11 stsp if (new_id) {
7095 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7096 ff0d2220 2019-07-11 stsp if (err)
7097 ff0d2220 2019-07-11 stsp goto done;
7098 ff0d2220 2019-07-11 stsp }
7099 818c7501 2019-07-11 stsp
7100 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7101 ff0d2220 2019-07-11 stsp if (new_id_str)
7102 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7103 0ebf8283 2019-07-24 stsp
7104 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7105 0ebf8283 2019-07-24 stsp if (err)
7106 0ebf8283 2019-07-24 stsp goto done;
7107 0ebf8283 2019-07-24 stsp
7108 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7109 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7110 818c7501 2019-07-11 stsp done:
7111 818c7501 2019-07-11 stsp free(old_id_str);
7112 818c7501 2019-07-11 stsp free(new_id_str);
7113 272a1371 2020-02-28 stsp free(logmsg);
7114 818c7501 2019-07-11 stsp return err;
7115 818c7501 2019-07-11 stsp }
7116 818c7501 2019-07-11 stsp
7117 1ee397ad 2019-07-12 stsp static const struct got_error *
7118 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7119 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7120 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7121 818c7501 2019-07-11 stsp {
7122 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7123 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7124 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7125 818c7501 2019-07-11 stsp }
7126 818c7501 2019-07-11 stsp
7127 818c7501 2019-07-11 stsp static const struct got_error *
7128 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7129 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7130 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7131 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7132 ff0d2220 2019-07-11 stsp {
7133 ff0d2220 2019-07-11 stsp const struct got_error *error;
7134 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7135 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7136 ff0d2220 2019-07-11 stsp
7137 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7138 ff0d2220 2019-07-11 stsp if (error)
7139 ff0d2220 2019-07-11 stsp return error;
7140 ff0d2220 2019-07-11 stsp
7141 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7142 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7143 ff0d2220 2019-07-11 stsp if (error) {
7144 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7145 ff0d2220 2019-07-11 stsp goto done;
7146 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7147 ff0d2220 2019-07-11 stsp } else {
7148 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7149 ff0d2220 2019-07-11 stsp free(new_commit_id);
7150 ff0d2220 2019-07-11 stsp }
7151 ff0d2220 2019-07-11 stsp done:
7152 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7153 ff0d2220 2019-07-11 stsp return error;
7154 64c6d990 2019-07-11 stsp }
7155 64c6d990 2019-07-11 stsp
7156 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7157 64c6d990 2019-07-11 stsp const char *path_prefix;
7158 64c6d990 2019-07-11 stsp size_t len;
7159 8ca9bd68 2019-07-25 stsp int errcode;
7160 64c6d990 2019-07-11 stsp };
7161 64c6d990 2019-07-11 stsp
7162 64c6d990 2019-07-11 stsp static const struct got_error *
7163 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7164 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7165 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7166 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7167 64c6d990 2019-07-11 stsp {
7168 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7169 64c6d990 2019-07-11 stsp
7170 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7171 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7172 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7173 64c6d990 2019-07-11 stsp
7174 64c6d990 2019-07-11 stsp return NULL;
7175 64c6d990 2019-07-11 stsp }
7176 64c6d990 2019-07-11 stsp
7177 64c6d990 2019-07-11 stsp static const struct got_error *
7178 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7179 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7180 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7181 64c6d990 2019-07-11 stsp {
7182 64c6d990 2019-07-11 stsp const struct got_error *err;
7183 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7184 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7185 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7186 64c6d990 2019-07-11 stsp
7187 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7188 64c6d990 2019-07-11 stsp return NULL;
7189 64c6d990 2019-07-11 stsp
7190 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7191 64c6d990 2019-07-11 stsp if (err)
7192 64c6d990 2019-07-11 stsp goto done;
7193 64c6d990 2019-07-11 stsp
7194 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7195 64c6d990 2019-07-11 stsp if (err)
7196 64c6d990 2019-07-11 stsp goto done;
7197 64c6d990 2019-07-11 stsp
7198 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7199 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7200 64c6d990 2019-07-11 stsp if (err)
7201 64c6d990 2019-07-11 stsp goto done;
7202 64c6d990 2019-07-11 stsp
7203 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7204 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7205 64c6d990 2019-07-11 stsp if (err)
7206 64c6d990 2019-07-11 stsp goto done;
7207 64c6d990 2019-07-11 stsp
7208 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7209 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7210 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7211 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7212 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7213 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7214 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7215 64c6d990 2019-07-11 stsp done:
7216 64c6d990 2019-07-11 stsp if (tree1)
7217 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7218 64c6d990 2019-07-11 stsp if (tree2)
7219 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7220 64c6d990 2019-07-11 stsp if (commit)
7221 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7222 64c6d990 2019-07-11 stsp if (parent_commit)
7223 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7224 64c6d990 2019-07-11 stsp return err;
7225 ff0d2220 2019-07-11 stsp }
7226 ff0d2220 2019-07-11 stsp
7227 ff0d2220 2019-07-11 stsp static const struct got_error *
7228 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7229 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7230 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7231 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7232 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7233 af61c510 2019-07-19 stsp {
7234 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7235 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7236 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7237 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7238 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7239 af61c510 2019-07-19 stsp
7240 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7241 af61c510 2019-07-19 stsp if (err)
7242 af61c510 2019-07-19 stsp return err;
7243 af61c510 2019-07-19 stsp
7244 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7245 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7246 af61c510 2019-07-19 stsp if (err)
7247 af61c510 2019-07-19 stsp goto done;
7248 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7249 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7250 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7251 af61c510 2019-07-19 stsp if (err) {
7252 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7253 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7254 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7255 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7256 af61c510 2019-07-19 stsp "been reached?!?");
7257 ee780d5c 2020-01-04 stsp }
7258 ee780d5c 2020-01-04 stsp goto done;
7259 af61c510 2019-07-19 stsp } else {
7260 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7261 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7262 af61c510 2019-07-19 stsp if (err)
7263 af61c510 2019-07-19 stsp goto done;
7264 af61c510 2019-07-19 stsp
7265 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7266 af61c510 2019-07-19 stsp if (err)
7267 af61c510 2019-07-19 stsp goto done;
7268 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7269 af61c510 2019-07-19 stsp commit_id = parent_id;
7270 af61c510 2019-07-19 stsp }
7271 af61c510 2019-07-19 stsp }
7272 af61c510 2019-07-19 stsp done:
7273 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7274 af61c510 2019-07-19 stsp return err;
7275 af61c510 2019-07-19 stsp }
7276 af61c510 2019-07-19 stsp
7277 af61c510 2019-07-19 stsp static const struct got_error *
7278 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7279 818c7501 2019-07-11 stsp {
7280 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7281 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7282 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7283 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7284 818c7501 2019-07-11 stsp char *cwd = NULL;
7285 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7286 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7287 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7288 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7289 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7290 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7291 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7292 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7293 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7294 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7295 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7296 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7297 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7298 818c7501 2019-07-11 stsp
7299 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7300 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7301 818c7501 2019-07-11 stsp
7302 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7303 818c7501 2019-07-11 stsp switch (ch) {
7304 818c7501 2019-07-11 stsp case 'a':
7305 818c7501 2019-07-11 stsp abort_rebase = 1;
7306 818c7501 2019-07-11 stsp break;
7307 818c7501 2019-07-11 stsp case 'c':
7308 818c7501 2019-07-11 stsp continue_rebase = 1;
7309 818c7501 2019-07-11 stsp break;
7310 818c7501 2019-07-11 stsp default:
7311 818c7501 2019-07-11 stsp usage_rebase();
7312 818c7501 2019-07-11 stsp /* NOTREACHED */
7313 818c7501 2019-07-11 stsp }
7314 818c7501 2019-07-11 stsp }
7315 818c7501 2019-07-11 stsp
7316 818c7501 2019-07-11 stsp argc -= optind;
7317 818c7501 2019-07-11 stsp argv += optind;
7318 818c7501 2019-07-11 stsp
7319 43012d58 2019-07-14 stsp #ifndef PROFILE
7320 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7321 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7322 43012d58 2019-07-14 stsp err(1, "pledge");
7323 43012d58 2019-07-14 stsp #endif
7324 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7325 818c7501 2019-07-11 stsp usage_rebase();
7326 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7327 818c7501 2019-07-11 stsp if (argc != 0)
7328 818c7501 2019-07-11 stsp usage_rebase();
7329 818c7501 2019-07-11 stsp } else if (argc != 1)
7330 818c7501 2019-07-11 stsp usage_rebase();
7331 818c7501 2019-07-11 stsp
7332 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7333 818c7501 2019-07-11 stsp if (cwd == NULL) {
7334 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7335 818c7501 2019-07-11 stsp goto done;
7336 818c7501 2019-07-11 stsp }
7337 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7338 fa51e947 2020-03-27 stsp if (error) {
7339 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7340 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7341 818c7501 2019-07-11 stsp goto done;
7342 fa51e947 2020-03-27 stsp }
7343 818c7501 2019-07-11 stsp
7344 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7345 c9956ddf 2019-09-08 stsp NULL);
7346 818c7501 2019-07-11 stsp if (error != NULL)
7347 818c7501 2019-07-11 stsp goto done;
7348 818c7501 2019-07-11 stsp
7349 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7350 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7351 7ef62c4e 2020-02-24 stsp if (error)
7352 7ef62c4e 2020-02-24 stsp goto done;
7353 7ef62c4e 2020-02-24 stsp
7354 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7355 7ef62c4e 2020-02-24 stsp worktree);
7356 818c7501 2019-07-11 stsp if (error)
7357 7ef62c4e 2020-02-24 stsp goto done;
7358 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7359 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7360 818c7501 2019-07-11 stsp goto done;
7361 7ef62c4e 2020-02-24 stsp }
7362 818c7501 2019-07-11 stsp
7363 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7364 818c7501 2019-07-11 stsp if (error)
7365 818c7501 2019-07-11 stsp goto done;
7366 818c7501 2019-07-11 stsp
7367 f6794adc 2019-07-23 stsp if (abort_rebase) {
7368 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7369 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7370 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7371 f6794adc 2019-07-23 stsp goto done;
7372 f6794adc 2019-07-23 stsp }
7373 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7374 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7375 3e3a69f1 2019-07-25 stsp worktree, repo);
7376 818c7501 2019-07-11 stsp if (error)
7377 818c7501 2019-07-11 stsp goto done;
7378 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7379 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7380 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7381 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7382 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7383 818c7501 2019-07-11 stsp if (error)
7384 818c7501 2019-07-11 stsp goto done;
7385 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7386 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7387 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7388 818c7501 2019-07-11 stsp }
7389 818c7501 2019-07-11 stsp
7390 818c7501 2019-07-11 stsp if (continue_rebase) {
7391 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7392 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7393 f6794adc 2019-07-23 stsp goto done;
7394 f6794adc 2019-07-23 stsp }
7395 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7396 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7397 3e3a69f1 2019-07-25 stsp worktree, repo);
7398 818c7501 2019-07-11 stsp if (error)
7399 818c7501 2019-07-11 stsp goto done;
7400 818c7501 2019-07-11 stsp
7401 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7402 01757395 2019-07-12 stsp resume_commit_id, repo);
7403 818c7501 2019-07-11 stsp if (error)
7404 818c7501 2019-07-11 stsp goto done;
7405 818c7501 2019-07-11 stsp
7406 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7407 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7408 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7409 818c7501 2019-07-11 stsp goto done;
7410 818c7501 2019-07-11 stsp }
7411 818c7501 2019-07-11 stsp } else {
7412 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7413 818c7501 2019-07-11 stsp if (error != NULL)
7414 818c7501 2019-07-11 stsp goto done;
7415 ff0d2220 2019-07-11 stsp }
7416 818c7501 2019-07-11 stsp
7417 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7418 ff0d2220 2019-07-11 stsp if (error)
7419 ff0d2220 2019-07-11 stsp goto done;
7420 ff0d2220 2019-07-11 stsp
7421 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7422 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7423 a51a74b3 2019-07-27 stsp
7424 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7425 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7426 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7427 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7428 818c7501 2019-07-11 stsp if (error)
7429 818c7501 2019-07-11 stsp goto done;
7430 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7431 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7432 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7433 818c7501 2019-07-11 stsp "with work tree's branch");
7434 818c7501 2019-07-11 stsp goto done;
7435 818c7501 2019-07-11 stsp }
7436 818c7501 2019-07-11 stsp
7437 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7438 a51a74b3 2019-07-27 stsp if (error) {
7439 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7440 a51a74b3 2019-07-27 stsp goto done;
7441 a51a74b3 2019-07-27 stsp error = NULL;
7442 a51a74b3 2019-07-27 stsp } else {
7443 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7444 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7445 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7446 a51a74b3 2019-07-27 stsp goto done;
7447 a51a74b3 2019-07-27 stsp }
7448 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7449 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7450 818c7501 2019-07-11 stsp if (error)
7451 818c7501 2019-07-11 stsp goto done;
7452 818c7501 2019-07-11 stsp }
7453 818c7501 2019-07-11 stsp
7454 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7455 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7456 818c7501 2019-07-11 stsp if (error)
7457 818c7501 2019-07-11 stsp goto done;
7458 818c7501 2019-07-11 stsp
7459 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7460 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7461 fc66b545 2019-08-12 stsp if (pid == NULL) {
7462 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7463 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7464 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7465 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7466 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7467 fc66b545 2019-08-12 stsp if (error)
7468 fc66b545 2019-08-12 stsp goto done;
7469 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7470 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7471 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7472 9627c110 2020-04-18 stsp
7473 fc66b545 2019-08-12 stsp }
7474 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7475 fc66b545 2019-08-12 stsp goto done;
7476 fc66b545 2019-08-12 stsp }
7477 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7478 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7479 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7480 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7481 818c7501 2019-07-11 stsp commit = NULL;
7482 818c7501 2019-07-11 stsp if (error)
7483 818c7501 2019-07-11 stsp goto done;
7484 64c6d990 2019-07-11 stsp
7485 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7486 38b0338b 2019-11-29 stsp if (continue_rebase) {
7487 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7488 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7489 38b0338b 2019-11-29 stsp goto done;
7490 38b0338b 2019-11-29 stsp } else {
7491 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7492 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7493 38b0338b 2019-11-29 stsp char *id_str;
7494 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7495 38b0338b 2019-11-29 stsp new_base_branch);
7496 38b0338b 2019-11-29 stsp if (error)
7497 38b0338b 2019-11-29 stsp goto done;
7498 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7499 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7500 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7501 38b0338b 2019-11-29 stsp free(id_str);
7502 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7503 38b0338b 2019-11-29 stsp new_head_commit_id);
7504 38b0338b 2019-11-29 stsp if (error)
7505 38b0338b 2019-11-29 stsp goto done;
7506 38b0338b 2019-11-29 stsp }
7507 818c7501 2019-07-11 stsp }
7508 818c7501 2019-07-11 stsp
7509 818c7501 2019-07-11 stsp pid = NULL;
7510 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7511 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7512 9627c110 2020-04-18 stsp
7513 818c7501 2019-07-11 stsp commit_id = qid->id;
7514 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7515 818c7501 2019-07-11 stsp pid = qid;
7516 818c7501 2019-07-11 stsp
7517 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7518 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7519 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7520 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7521 818c7501 2019-07-11 stsp if (error)
7522 818c7501 2019-07-11 stsp goto done;
7523 9627c110 2020-04-18 stsp
7524 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7525 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7526 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7527 818c7501 2019-07-11 stsp
7528 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7529 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7530 a0ea4fc0 2020-02-28 stsp if (error)
7531 a0ea4fc0 2020-02-28 stsp goto done;
7532 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7533 818c7501 2019-07-11 stsp break;
7534 01757395 2019-07-12 stsp }
7535 818c7501 2019-07-11 stsp
7536 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7537 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7538 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7539 818c7501 2019-07-11 stsp if (error)
7540 818c7501 2019-07-11 stsp goto done;
7541 818c7501 2019-07-11 stsp }
7542 818c7501 2019-07-11 stsp
7543 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7544 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7545 818c7501 2019-07-11 stsp if (error)
7546 818c7501 2019-07-11 stsp goto done;
7547 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7548 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7549 818c7501 2019-07-11 stsp } else
7550 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7551 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7552 818c7501 2019-07-11 stsp done:
7553 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7554 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7555 818c7501 2019-07-11 stsp free(resume_commit_id);
7556 818c7501 2019-07-11 stsp free(yca_id);
7557 818c7501 2019-07-11 stsp if (commit)
7558 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7559 818c7501 2019-07-11 stsp if (branch)
7560 818c7501 2019-07-11 stsp got_ref_close(branch);
7561 818c7501 2019-07-11 stsp if (new_base_branch)
7562 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7563 818c7501 2019-07-11 stsp if (tmp_branch)
7564 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7565 5ef14e63 2019-06-02 stsp if (worktree)
7566 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7567 5ef14e63 2019-06-02 stsp if (repo)
7568 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7569 5ef14e63 2019-06-02 stsp return error;
7570 0ebf8283 2019-07-24 stsp }
7571 0ebf8283 2019-07-24 stsp
7572 0ebf8283 2019-07-24 stsp __dead static void
7573 0ebf8283 2019-07-24 stsp usage_histedit(void)
7574 0ebf8283 2019-07-24 stsp {
7575 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7576 0ebf8283 2019-07-24 stsp getprogname());
7577 0ebf8283 2019-07-24 stsp exit(1);
7578 0ebf8283 2019-07-24 stsp }
7579 0ebf8283 2019-07-24 stsp
7580 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7581 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7582 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7583 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7584 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7585 0ebf8283 2019-07-24 stsp
7586 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7587 0ebf8283 2019-07-24 stsp unsigned char code;
7588 0ebf8283 2019-07-24 stsp const char *name;
7589 0ebf8283 2019-07-24 stsp const char *desc;
7590 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7591 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7592 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7593 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7594 82997472 2020-01-29 stsp "be used" },
7595 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7596 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7597 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7598 0ebf8283 2019-07-24 stsp };
7599 0ebf8283 2019-07-24 stsp
7600 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7601 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7602 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7603 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7604 0ebf8283 2019-07-24 stsp char *logmsg;
7605 0ebf8283 2019-07-24 stsp };
7606 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7607 0ebf8283 2019-07-24 stsp
7608 0ebf8283 2019-07-24 stsp static const struct got_error *
7609 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7610 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7611 0ebf8283 2019-07-24 stsp {
7612 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7613 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7614 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7615 8138f3e1 2019-08-11 stsp int n;
7616 0ebf8283 2019-07-24 stsp
7617 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7618 0ebf8283 2019-07-24 stsp if (err)
7619 0ebf8283 2019-07-24 stsp goto done;
7620 0ebf8283 2019-07-24 stsp
7621 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7622 0ebf8283 2019-07-24 stsp if (err)
7623 0ebf8283 2019-07-24 stsp goto done;
7624 0ebf8283 2019-07-24 stsp
7625 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7626 0ebf8283 2019-07-24 stsp if (err)
7627 0ebf8283 2019-07-24 stsp goto done;
7628 0ebf8283 2019-07-24 stsp
7629 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7630 0ebf8283 2019-07-24 stsp if (n < 0)
7631 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7632 0ebf8283 2019-07-24 stsp done:
7633 0ebf8283 2019-07-24 stsp if (commit)
7634 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7635 0ebf8283 2019-07-24 stsp free(id_str);
7636 0ebf8283 2019-07-24 stsp free(logmsg);
7637 0ebf8283 2019-07-24 stsp return err;
7638 0ebf8283 2019-07-24 stsp }
7639 0ebf8283 2019-07-24 stsp
7640 0ebf8283 2019-07-24 stsp static const struct got_error *
7641 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7642 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7643 0ebf8283 2019-07-24 stsp {
7644 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7645 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7646 0ebf8283 2019-07-24 stsp
7647 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7648 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7649 0ebf8283 2019-07-24 stsp
7650 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7651 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7652 0ebf8283 2019-07-24 stsp f, repo);
7653 0ebf8283 2019-07-24 stsp if (err)
7654 0ebf8283 2019-07-24 stsp break;
7655 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7656 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7657 083957f4 2020-02-24 stsp if (n < 0) {
7658 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7659 083957f4 2020-02-24 stsp break;
7660 083957f4 2020-02-24 stsp }
7661 083957f4 2020-02-24 stsp }
7662 0ebf8283 2019-07-24 stsp }
7663 0ebf8283 2019-07-24 stsp
7664 0ebf8283 2019-07-24 stsp return err;
7665 0ebf8283 2019-07-24 stsp }
7666 0ebf8283 2019-07-24 stsp
7667 0ebf8283 2019-07-24 stsp static const struct got_error *
7668 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7669 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7670 0ebf8283 2019-07-24 stsp {
7671 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7672 0ebf8283 2019-07-24 stsp int n, i;
7673 514f2ffe 2020-01-29 stsp char *id_str;
7674 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7675 514f2ffe 2020-01-29 stsp
7676 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7677 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7678 514f2ffe 2020-01-29 stsp if (err)
7679 514f2ffe 2020-01-29 stsp return err;
7680 514f2ffe 2020-01-29 stsp
7681 514f2ffe 2020-01-29 stsp n = fprintf(f,
7682 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7683 514f2ffe 2020-01-29 stsp "# commit %s\n"
7684 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7685 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7686 514f2ffe 2020-01-29 stsp if (n < 0) {
7687 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7688 514f2ffe 2020-01-29 stsp goto done;
7689 514f2ffe 2020-01-29 stsp }
7690 0ebf8283 2019-07-24 stsp
7691 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7692 514f2ffe 2020-01-29 stsp if (n < 0) {
7693 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7694 514f2ffe 2020-01-29 stsp goto done;
7695 514f2ffe 2020-01-29 stsp }
7696 0ebf8283 2019-07-24 stsp
7697 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7698 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7699 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7700 0ebf8283 2019-07-24 stsp cmd->desc);
7701 0ebf8283 2019-07-24 stsp if (n < 0) {
7702 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7703 0ebf8283 2019-07-24 stsp break;
7704 0ebf8283 2019-07-24 stsp }
7705 0ebf8283 2019-07-24 stsp }
7706 514f2ffe 2020-01-29 stsp done:
7707 514f2ffe 2020-01-29 stsp free(id_str);
7708 0ebf8283 2019-07-24 stsp return err;
7709 0ebf8283 2019-07-24 stsp }
7710 0ebf8283 2019-07-24 stsp
7711 0ebf8283 2019-07-24 stsp static const struct got_error *
7712 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7713 0ebf8283 2019-07-24 stsp {
7714 0ebf8283 2019-07-24 stsp static char msg[42];
7715 0ebf8283 2019-07-24 stsp int ret;
7716 0ebf8283 2019-07-24 stsp
7717 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7718 0ebf8283 2019-07-24 stsp lineno);
7719 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7720 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7721 0ebf8283 2019-07-24 stsp
7722 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7723 0ebf8283 2019-07-24 stsp }
7724 0ebf8283 2019-07-24 stsp
7725 0ebf8283 2019-07-24 stsp static const struct got_error *
7726 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7727 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7728 0ebf8283 2019-07-24 stsp {
7729 0ebf8283 2019-07-24 stsp const struct got_error *err;
7730 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7731 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7732 0ebf8283 2019-07-24 stsp
7733 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7734 0ebf8283 2019-07-24 stsp if (err)
7735 0ebf8283 2019-07-24 stsp return err;
7736 0ebf8283 2019-07-24 stsp
7737 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7738 0ebf8283 2019-07-24 stsp if (err)
7739 0ebf8283 2019-07-24 stsp goto done;
7740 0ebf8283 2019-07-24 stsp
7741 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7742 5943eee2 2019-08-13 stsp if (err)
7743 5943eee2 2019-08-13 stsp goto done;
7744 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7745 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7746 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7747 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7748 0ebf8283 2019-07-24 stsp }
7749 0ebf8283 2019-07-24 stsp done:
7750 0ebf8283 2019-07-24 stsp if (folded_commit)
7751 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7752 0ebf8283 2019-07-24 stsp free(id_str);
7753 5943eee2 2019-08-13 stsp free(folded_logmsg);
7754 0ebf8283 2019-07-24 stsp return err;
7755 0ebf8283 2019-07-24 stsp }
7756 0ebf8283 2019-07-24 stsp
7757 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7758 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7759 0ebf8283 2019-07-24 stsp {
7760 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7761 0ebf8283 2019-07-24 stsp
7762 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7763 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7764 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7765 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7766 3f9de99f 2019-07-24 stsp folded = prev;
7767 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7768 0ebf8283 2019-07-24 stsp }
7769 0ebf8283 2019-07-24 stsp
7770 0ebf8283 2019-07-24 stsp return folded;
7771 0ebf8283 2019-07-24 stsp }
7772 0ebf8283 2019-07-24 stsp
7773 0ebf8283 2019-07-24 stsp static const struct got_error *
7774 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7775 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7776 0ebf8283 2019-07-24 stsp {
7777 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7778 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7779 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7780 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7781 1601cb9f 2020-09-11 naddy int logmsg_len;
7782 0ebf8283 2019-07-24 stsp int fd;
7783 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7784 0ebf8283 2019-07-24 stsp
7785 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7786 0ebf8283 2019-07-24 stsp if (err)
7787 0ebf8283 2019-07-24 stsp return err;
7788 0ebf8283 2019-07-24 stsp
7789 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7790 0ebf8283 2019-07-24 stsp if (folded) {
7791 0ebf8283 2019-07-24 stsp while (folded != hle) {
7792 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7793 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7794 3f9de99f 2019-07-24 stsp continue;
7795 3f9de99f 2019-07-24 stsp }
7796 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7797 0ebf8283 2019-07-24 stsp logmsg, repo);
7798 0ebf8283 2019-07-24 stsp if (err)
7799 0ebf8283 2019-07-24 stsp goto done;
7800 0ebf8283 2019-07-24 stsp free(logmsg);
7801 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7802 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7803 0ebf8283 2019-07-24 stsp }
7804 0ebf8283 2019-07-24 stsp }
7805 0ebf8283 2019-07-24 stsp
7806 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7807 0ebf8283 2019-07-24 stsp if (err)
7808 0ebf8283 2019-07-24 stsp goto done;
7809 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7810 5943eee2 2019-08-13 stsp if (err)
7811 5943eee2 2019-08-13 stsp goto done;
7812 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7813 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7814 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7815 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7816 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7817 0ebf8283 2019-07-24 stsp goto done;
7818 0ebf8283 2019-07-24 stsp }
7819 0ebf8283 2019-07-24 stsp free(logmsg);
7820 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7821 0ebf8283 2019-07-24 stsp
7822 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7823 0ebf8283 2019-07-24 stsp if (err)
7824 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp
7826 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7827 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7828 0ebf8283 2019-07-24 stsp if (err)
7829 0ebf8283 2019-07-24 stsp goto done;
7830 0ebf8283 2019-07-24 stsp
7831 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7832 0ebf8283 2019-07-24 stsp close(fd);
7833 0ebf8283 2019-07-24 stsp
7834 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7835 0ebf8283 2019-07-24 stsp if (err)
7836 0ebf8283 2019-07-24 stsp goto done;
7837 0ebf8283 2019-07-24 stsp
7838 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7839 0ebf8283 2019-07-24 stsp if (err) {
7840 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7841 0ebf8283 2019-07-24 stsp goto done;
7842 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7843 0ebf8283 2019-07-24 stsp }
7844 0ebf8283 2019-07-24 stsp done:
7845 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7846 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7847 0ebf8283 2019-07-24 stsp free(logmsg_path);
7848 0ebf8283 2019-07-24 stsp free(logmsg);
7849 5943eee2 2019-08-13 stsp free(orig_logmsg);
7850 0ebf8283 2019-07-24 stsp free(editor);
7851 0ebf8283 2019-07-24 stsp if (commit)
7852 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7853 0ebf8283 2019-07-24 stsp return err;
7854 5ef14e63 2019-06-02 stsp }
7855 0ebf8283 2019-07-24 stsp
7856 0ebf8283 2019-07-24 stsp static const struct got_error *
7857 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7858 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7859 0ebf8283 2019-07-24 stsp {
7860 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7861 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7862 0ebf8283 2019-07-24 stsp size_t size;
7863 0ebf8283 2019-07-24 stsp ssize_t len;
7864 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7865 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7866 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7867 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7868 0ebf8283 2019-07-24 stsp
7869 0ebf8283 2019-07-24 stsp for (;;) {
7870 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7871 0ebf8283 2019-07-24 stsp if (len == -1) {
7872 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7873 0ebf8283 2019-07-24 stsp if (feof(f))
7874 0ebf8283 2019-07-24 stsp break;
7875 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7876 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7877 0ebf8283 2019-07-24 stsp break;
7878 0ebf8283 2019-07-24 stsp }
7879 0ebf8283 2019-07-24 stsp lineno++;
7880 0ebf8283 2019-07-24 stsp p = line;
7881 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7882 0ebf8283 2019-07-24 stsp p++;
7883 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7884 0ebf8283 2019-07-24 stsp free(line);
7885 0ebf8283 2019-07-24 stsp line = NULL;
7886 0ebf8283 2019-07-24 stsp continue;
7887 0ebf8283 2019-07-24 stsp }
7888 0ebf8283 2019-07-24 stsp cmd = NULL;
7889 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7890 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7891 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7892 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7893 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7894 0ebf8283 2019-07-24 stsp break;
7895 0ebf8283 2019-07-24 stsp }
7896 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7897 0ebf8283 2019-07-24 stsp p++;
7898 0ebf8283 2019-07-24 stsp break;
7899 0ebf8283 2019-07-24 stsp }
7900 0ebf8283 2019-07-24 stsp }
7901 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7902 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7903 0ebf8283 2019-07-24 stsp break;
7904 0ebf8283 2019-07-24 stsp }
7905 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7906 0ebf8283 2019-07-24 stsp p++;
7907 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7908 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7909 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7910 0ebf8283 2019-07-24 stsp break;
7911 0ebf8283 2019-07-24 stsp }
7912 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7913 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7914 0ebf8283 2019-07-24 stsp if (err)
7915 0ebf8283 2019-07-24 stsp break;
7916 0ebf8283 2019-07-24 stsp } else {
7917 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7918 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7919 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7920 0ebf8283 2019-07-24 stsp break;
7921 0ebf8283 2019-07-24 stsp }
7922 0ebf8283 2019-07-24 stsp }
7923 0ebf8283 2019-07-24 stsp free(line);
7924 0ebf8283 2019-07-24 stsp line = NULL;
7925 0ebf8283 2019-07-24 stsp continue;
7926 0ebf8283 2019-07-24 stsp } else {
7927 0ebf8283 2019-07-24 stsp end = p;
7928 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7929 0ebf8283 2019-07-24 stsp end++;
7930 0ebf8283 2019-07-24 stsp *end = '\0';
7931 0ebf8283 2019-07-24 stsp
7932 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7933 0ebf8283 2019-07-24 stsp if (err) {
7934 0ebf8283 2019-07-24 stsp /* override error code */
7935 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7936 0ebf8283 2019-07-24 stsp break;
7937 0ebf8283 2019-07-24 stsp }
7938 0ebf8283 2019-07-24 stsp }
7939 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7940 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7941 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7942 0ebf8283 2019-07-24 stsp break;
7943 0ebf8283 2019-07-24 stsp }
7944 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7945 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7946 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7947 0ebf8283 2019-07-24 stsp commit_id = NULL;
7948 0ebf8283 2019-07-24 stsp free(line);
7949 0ebf8283 2019-07-24 stsp line = NULL;
7950 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7951 0ebf8283 2019-07-24 stsp }
7952 0ebf8283 2019-07-24 stsp
7953 0ebf8283 2019-07-24 stsp free(line);
7954 0ebf8283 2019-07-24 stsp free(commit_id);
7955 0ebf8283 2019-07-24 stsp return err;
7956 0ebf8283 2019-07-24 stsp }
7957 0ebf8283 2019-07-24 stsp
7958 0ebf8283 2019-07-24 stsp static const struct got_error *
7959 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7960 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7961 bfce7f83 2019-07-27 stsp {
7962 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7963 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7964 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7965 5b87815e 2020-03-05 stsp static char msg[92];
7966 bfce7f83 2019-07-27 stsp char *id_str;
7967 bfce7f83 2019-07-27 stsp
7968 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7969 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7970 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7971 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7972 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7973 5b87815e 2020-03-05 stsp
7974 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7975 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7976 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7977 5b87815e 2020-03-05 stsp if (hle == hle2)
7978 5b87815e 2020-03-05 stsp continue;
7979 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7980 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7981 5b87815e 2020-03-05 stsp continue;
7982 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7983 5b87815e 2020-03-05 stsp if (err)
7984 5b87815e 2020-03-05 stsp return err;
7985 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7986 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7987 5b87815e 2020-03-05 stsp free(id_str);
7988 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7989 5b87815e 2020-03-05 stsp }
7990 5b87815e 2020-03-05 stsp }
7991 bfce7f83 2019-07-27 stsp
7992 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7993 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7994 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7995 bfce7f83 2019-07-27 stsp break;
7996 bfce7f83 2019-07-27 stsp }
7997 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7998 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7999 bfce7f83 2019-07-27 stsp if (err)
8000 bfce7f83 2019-07-27 stsp return err;
8001 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8002 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8003 bfce7f83 2019-07-27 stsp free(id_str);
8004 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8005 bfce7f83 2019-07-27 stsp }
8006 bfce7f83 2019-07-27 stsp }
8007 bfce7f83 2019-07-27 stsp
8008 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8009 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8010 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8011 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8012 bfce7f83 2019-07-27 stsp
8013 bfce7f83 2019-07-27 stsp return NULL;
8014 bfce7f83 2019-07-27 stsp }
8015 bfce7f83 2019-07-27 stsp
8016 bfce7f83 2019-07-27 stsp static const struct got_error *
8017 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8018 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8019 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8020 0ebf8283 2019-07-24 stsp {
8021 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8022 0ebf8283 2019-07-24 stsp char *editor;
8023 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8024 0ebf8283 2019-07-24 stsp
8025 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8026 0ebf8283 2019-07-24 stsp if (err)
8027 0ebf8283 2019-07-24 stsp return err;
8028 0ebf8283 2019-07-24 stsp
8029 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8030 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8031 0ebf8283 2019-07-24 stsp goto done;
8032 0ebf8283 2019-07-24 stsp }
8033 0ebf8283 2019-07-24 stsp
8034 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8035 0ebf8283 2019-07-24 stsp if (f == NULL) {
8036 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8037 0ebf8283 2019-07-24 stsp goto done;
8038 0ebf8283 2019-07-24 stsp }
8039 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8040 bfce7f83 2019-07-27 stsp if (err)
8041 bfce7f83 2019-07-27 stsp goto done;
8042 bfce7f83 2019-07-27 stsp
8043 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8044 0ebf8283 2019-07-24 stsp done:
8045 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8046 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8047 0ebf8283 2019-07-24 stsp free(editor);
8048 0ebf8283 2019-07-24 stsp return err;
8049 0ebf8283 2019-07-24 stsp }
8050 0ebf8283 2019-07-24 stsp
8051 0ebf8283 2019-07-24 stsp static const struct got_error *
8052 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8053 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8054 514f2ffe 2020-01-29 stsp struct got_repository *);
8055 0ebf8283 2019-07-24 stsp
8056 0ebf8283 2019-07-24 stsp static const struct got_error *
8057 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8058 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8059 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8060 0ebf8283 2019-07-24 stsp {
8061 0ebf8283 2019-07-24 stsp const struct got_error *err;
8062 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8063 0ebf8283 2019-07-24 stsp char *path = NULL;
8064 0ebf8283 2019-07-24 stsp
8065 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8066 0ebf8283 2019-07-24 stsp if (err)
8067 0ebf8283 2019-07-24 stsp return err;
8068 0ebf8283 2019-07-24 stsp
8069 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8070 0ebf8283 2019-07-24 stsp if (err)
8071 0ebf8283 2019-07-24 stsp goto done;
8072 0ebf8283 2019-07-24 stsp
8073 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8074 0ebf8283 2019-07-24 stsp if (err)
8075 0ebf8283 2019-07-24 stsp goto done;
8076 0ebf8283 2019-07-24 stsp
8077 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8078 083957f4 2020-02-24 stsp rewind(f);
8079 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8080 083957f4 2020-02-24 stsp } else {
8081 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8082 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8083 0ebf8283 2019-07-24 stsp goto done;
8084 083957f4 2020-02-24 stsp }
8085 083957f4 2020-02-24 stsp f = NULL;
8086 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8087 083957f4 2020-02-24 stsp if (err) {
8088 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8089 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8090 083957f4 2020-02-24 stsp goto done;
8091 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8092 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8093 083957f4 2020-02-24 stsp }
8094 0ebf8283 2019-07-24 stsp }
8095 0ebf8283 2019-07-24 stsp done:
8096 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8097 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8098 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8099 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8100 0ebf8283 2019-07-24 stsp free(path);
8101 0ebf8283 2019-07-24 stsp return err;
8102 0ebf8283 2019-07-24 stsp }
8103 0ebf8283 2019-07-24 stsp
8104 0ebf8283 2019-07-24 stsp static const struct got_error *
8105 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8106 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8107 0ebf8283 2019-07-24 stsp {
8108 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8109 0ebf8283 2019-07-24 stsp char *path = NULL;
8110 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8111 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8112 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8113 0ebf8283 2019-07-24 stsp
8114 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8115 0ebf8283 2019-07-24 stsp if (err)
8116 0ebf8283 2019-07-24 stsp return err;
8117 0ebf8283 2019-07-24 stsp
8118 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8119 0ebf8283 2019-07-24 stsp if (f == NULL) {
8120 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8121 0ebf8283 2019-07-24 stsp goto done;
8122 0ebf8283 2019-07-24 stsp }
8123 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8124 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8125 0ebf8283 2019-07-24 stsp repo);
8126 0ebf8283 2019-07-24 stsp if (err)
8127 0ebf8283 2019-07-24 stsp break;
8128 0ebf8283 2019-07-24 stsp
8129 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8130 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8131 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8132 0ebf8283 2019-07-24 stsp if (n < 0) {
8133 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8134 0ebf8283 2019-07-24 stsp break;
8135 0ebf8283 2019-07-24 stsp }
8136 0ebf8283 2019-07-24 stsp }
8137 0ebf8283 2019-07-24 stsp }
8138 0ebf8283 2019-07-24 stsp done:
8139 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8140 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8141 0ebf8283 2019-07-24 stsp free(path);
8142 0ebf8283 2019-07-24 stsp if (commit)
8143 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8144 0ebf8283 2019-07-24 stsp return err;
8145 0ebf8283 2019-07-24 stsp }
8146 0ebf8283 2019-07-24 stsp
8147 bfce7f83 2019-07-27 stsp void
8148 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8149 bfce7f83 2019-07-27 stsp {
8150 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8151 bfce7f83 2019-07-27 stsp
8152 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8153 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8154 bfce7f83 2019-07-27 stsp free(hle);
8155 bfce7f83 2019-07-27 stsp }
8156 bfce7f83 2019-07-27 stsp }
8157 bfce7f83 2019-07-27 stsp
8158 0ebf8283 2019-07-24 stsp static const struct got_error *
8159 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8160 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8161 0ebf8283 2019-07-24 stsp {
8162 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8163 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8164 0ebf8283 2019-07-24 stsp
8165 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8166 0ebf8283 2019-07-24 stsp if (f == NULL) {
8167 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8168 0ebf8283 2019-07-24 stsp goto done;
8169 0ebf8283 2019-07-24 stsp }
8170 0ebf8283 2019-07-24 stsp
8171 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8172 0ebf8283 2019-07-24 stsp done:
8173 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8174 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8175 0ebf8283 2019-07-24 stsp return err;
8176 0ebf8283 2019-07-24 stsp }
8177 0ebf8283 2019-07-24 stsp
8178 0ebf8283 2019-07-24 stsp static const struct got_error *
8179 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8180 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8181 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8182 0ebf8283 2019-07-24 stsp {
8183 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8184 0ebf8283 2019-07-24 stsp int resp = ' ';
8185 0ebf8283 2019-07-24 stsp
8186 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8187 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8188 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8189 0ebf8283 2019-07-24 stsp resp = getchar();
8190 a61a4414 2019-08-07 stsp if (resp == '\n')
8191 a61a4414 2019-08-07 stsp resp = getchar();
8192 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8193 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8194 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8195 bfce7f83 2019-07-27 stsp repo);
8196 426ebf2e 2019-07-25 stsp if (err) {
8197 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8198 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8199 426ebf2e 2019-07-25 stsp break;
8200 bfce7f83 2019-07-27 stsp prev_err = err;
8201 426ebf2e 2019-07-25 stsp resp = ' ';
8202 426ebf2e 2019-07-25 stsp continue;
8203 426ebf2e 2019-07-25 stsp }
8204 bfce7f83 2019-07-27 stsp break;
8205 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8206 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8207 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8208 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8209 426ebf2e 2019-07-25 stsp if (err) {
8210 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8211 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8212 426ebf2e 2019-07-25 stsp break;
8213 bfce7f83 2019-07-27 stsp prev_err = err;
8214 426ebf2e 2019-07-25 stsp resp = ' ';
8215 426ebf2e 2019-07-25 stsp continue;
8216 426ebf2e 2019-07-25 stsp }
8217 bfce7f83 2019-07-27 stsp break;
8218 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8219 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8220 0ebf8283 2019-07-24 stsp break;
8221 426ebf2e 2019-07-25 stsp } else
8222 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8223 0ebf8283 2019-07-24 stsp }
8224 0ebf8283 2019-07-24 stsp
8225 0ebf8283 2019-07-24 stsp return err;
8226 0ebf8283 2019-07-24 stsp }
8227 0ebf8283 2019-07-24 stsp
8228 0ebf8283 2019-07-24 stsp static const struct got_error *
8229 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8230 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8231 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8232 0ebf8283 2019-07-24 stsp {
8233 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8234 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8235 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8236 3e3a69f1 2019-07-25 stsp branch, repo);
8237 0ebf8283 2019-07-24 stsp }
8238 0ebf8283 2019-07-24 stsp
8239 0ebf8283 2019-07-24 stsp static const struct got_error *
8240 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8241 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8242 0ebf8283 2019-07-24 stsp {
8243 0ebf8283 2019-07-24 stsp const struct got_error *err;
8244 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8245 0ebf8283 2019-07-24 stsp
8246 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8247 0ebf8283 2019-07-24 stsp if (err)
8248 0ebf8283 2019-07-24 stsp goto done;
8249 0ebf8283 2019-07-24 stsp
8250 0ebf8283 2019-07-24 stsp if (new_id) {
8251 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8252 0ebf8283 2019-07-24 stsp if (err)
8253 0ebf8283 2019-07-24 stsp goto done;
8254 0ebf8283 2019-07-24 stsp }
8255 0ebf8283 2019-07-24 stsp
8256 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8257 0ebf8283 2019-07-24 stsp if (new_id_str)
8258 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8259 0ebf8283 2019-07-24 stsp
8260 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8261 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8262 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8263 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8264 0ebf8283 2019-07-24 stsp goto done;
8265 0ebf8283 2019-07-24 stsp }
8266 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8267 0ebf8283 2019-07-24 stsp } else {
8268 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8269 0ebf8283 2019-07-24 stsp if (err)
8270 0ebf8283 2019-07-24 stsp goto done;
8271 0ebf8283 2019-07-24 stsp }
8272 0ebf8283 2019-07-24 stsp
8273 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8274 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8275 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8276 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8277 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8278 0ebf8283 2019-07-24 stsp break;
8279 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8280 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8281 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8282 0ebf8283 2019-07-24 stsp logmsg);
8283 0ebf8283 2019-07-24 stsp break;
8284 0ebf8283 2019-07-24 stsp default:
8285 0ebf8283 2019-07-24 stsp break;
8286 0ebf8283 2019-07-24 stsp }
8287 0ebf8283 2019-07-24 stsp done:
8288 0ebf8283 2019-07-24 stsp free(old_id_str);
8289 0ebf8283 2019-07-24 stsp free(new_id_str);
8290 0ebf8283 2019-07-24 stsp return err;
8291 0ebf8283 2019-07-24 stsp }
8292 0ebf8283 2019-07-24 stsp
8293 0ebf8283 2019-07-24 stsp static const struct got_error *
8294 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8295 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8296 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8297 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8298 0ebf8283 2019-07-24 stsp {
8299 0ebf8283 2019-07-24 stsp const struct got_error *err;
8300 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8301 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8302 0ebf8283 2019-07-24 stsp
8303 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8304 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8305 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8306 0ebf8283 2019-07-24 stsp if (err)
8307 0ebf8283 2019-07-24 stsp return err;
8308 0ebf8283 2019-07-24 stsp }
8309 0ebf8283 2019-07-24 stsp
8310 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8311 0ebf8283 2019-07-24 stsp if (err)
8312 0ebf8283 2019-07-24 stsp return err;
8313 0ebf8283 2019-07-24 stsp
8314 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8315 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8316 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8317 0ebf8283 2019-07-24 stsp if (err) {
8318 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8319 0ebf8283 2019-07-24 stsp goto done;
8320 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8321 0ebf8283 2019-07-24 stsp } else {
8322 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8323 0ebf8283 2019-07-24 stsp free(new_commit_id);
8324 0ebf8283 2019-07-24 stsp }
8325 0ebf8283 2019-07-24 stsp done:
8326 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8327 0ebf8283 2019-07-24 stsp return err;
8328 0ebf8283 2019-07-24 stsp }
8329 0ebf8283 2019-07-24 stsp
8330 0ebf8283 2019-07-24 stsp static const struct got_error *
8331 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8332 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8333 0ebf8283 2019-07-24 stsp {
8334 0ebf8283 2019-07-24 stsp const struct got_error *error;
8335 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8336 0ebf8283 2019-07-24 stsp
8337 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8338 0ebf8283 2019-07-24 stsp repo);
8339 0ebf8283 2019-07-24 stsp if (error)
8340 0ebf8283 2019-07-24 stsp return error;
8341 0ebf8283 2019-07-24 stsp
8342 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8343 0ebf8283 2019-07-24 stsp if (error)
8344 0ebf8283 2019-07-24 stsp return error;
8345 0ebf8283 2019-07-24 stsp
8346 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8347 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8348 0ebf8283 2019-07-24 stsp return error;
8349 ab20a43a 2020-01-29 stsp }
8350 ab20a43a 2020-01-29 stsp
8351 ab20a43a 2020-01-29 stsp static const struct got_error *
8352 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8353 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8354 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8355 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8356 ab20a43a 2020-01-29 stsp {
8357 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8358 ab20a43a 2020-01-29 stsp
8359 ab20a43a 2020-01-29 stsp switch (status) {
8360 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8361 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8362 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8363 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8364 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8365 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8366 ab20a43a 2020-01-29 stsp default:
8367 ab20a43a 2020-01-29 stsp break;
8368 ab20a43a 2020-01-29 stsp }
8369 ab20a43a 2020-01-29 stsp
8370 ab20a43a 2020-01-29 stsp switch (staged_status) {
8371 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8372 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8373 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8374 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8375 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8376 ab20a43a 2020-01-29 stsp default:
8377 ab20a43a 2020-01-29 stsp break;
8378 ab20a43a 2020-01-29 stsp }
8379 ab20a43a 2020-01-29 stsp
8380 ab20a43a 2020-01-29 stsp return NULL;
8381 0ebf8283 2019-07-24 stsp }
8382 0ebf8283 2019-07-24 stsp
8383 0ebf8283 2019-07-24 stsp static const struct got_error *
8384 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8385 0ebf8283 2019-07-24 stsp {
8386 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8387 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8388 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8389 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8390 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8391 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8392 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8393 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8394 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8395 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8396 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8397 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8398 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8399 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8400 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8401 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8402 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8403 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8404 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8405 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8406 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8407 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8408 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8409 0ebf8283 2019-07-24 stsp
8410 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8411 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8412 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8413 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8414 0ebf8283 2019-07-24 stsp
8415 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8416 0ebf8283 2019-07-24 stsp switch (ch) {
8417 0ebf8283 2019-07-24 stsp case 'a':
8418 0ebf8283 2019-07-24 stsp abort_edit = 1;
8419 0ebf8283 2019-07-24 stsp break;
8420 0ebf8283 2019-07-24 stsp case 'c':
8421 0ebf8283 2019-07-24 stsp continue_edit = 1;
8422 0ebf8283 2019-07-24 stsp break;
8423 0ebf8283 2019-07-24 stsp case 'F':
8424 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8425 0ebf8283 2019-07-24 stsp break;
8426 083957f4 2020-02-24 stsp case 'm':
8427 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8428 083957f4 2020-02-24 stsp break;
8429 0ebf8283 2019-07-24 stsp default:
8430 0ebf8283 2019-07-24 stsp usage_histedit();
8431 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8432 0ebf8283 2019-07-24 stsp }
8433 0ebf8283 2019-07-24 stsp }
8434 0ebf8283 2019-07-24 stsp
8435 0ebf8283 2019-07-24 stsp argc -= optind;
8436 0ebf8283 2019-07-24 stsp argv += optind;
8437 0ebf8283 2019-07-24 stsp
8438 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8439 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8440 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8441 0ebf8283 2019-07-24 stsp err(1, "pledge");
8442 0ebf8283 2019-07-24 stsp #endif
8443 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8444 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8445 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8446 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8447 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8448 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8449 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8450 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8451 0ebf8283 2019-07-24 stsp if (argc != 0)
8452 0ebf8283 2019-07-24 stsp usage_histedit();
8453 0ebf8283 2019-07-24 stsp
8454 0ebf8283 2019-07-24 stsp /*
8455 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8456 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8457 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8458 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8459 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8460 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8461 0ebf8283 2019-07-24 stsp */
8462 0ebf8283 2019-07-24 stsp
8463 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8464 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8465 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8466 0ebf8283 2019-07-24 stsp goto done;
8467 0ebf8283 2019-07-24 stsp }
8468 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8469 fa51e947 2020-03-27 stsp if (error) {
8470 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8471 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8472 0ebf8283 2019-07-24 stsp goto done;
8473 fa51e947 2020-03-27 stsp }
8474 0ebf8283 2019-07-24 stsp
8475 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8476 c9956ddf 2019-09-08 stsp NULL);
8477 0ebf8283 2019-07-24 stsp if (error != NULL)
8478 0ebf8283 2019-07-24 stsp goto done;
8479 0ebf8283 2019-07-24 stsp
8480 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8481 0ebf8283 2019-07-24 stsp if (error)
8482 0ebf8283 2019-07-24 stsp goto done;
8483 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8484 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8485 0ebf8283 2019-07-24 stsp goto done;
8486 0ebf8283 2019-07-24 stsp }
8487 0ebf8283 2019-07-24 stsp
8488 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8489 0ebf8283 2019-07-24 stsp if (error)
8490 0ebf8283 2019-07-24 stsp goto done;
8491 0ebf8283 2019-07-24 stsp
8492 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8493 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8494 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8495 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8496 083957f4 2020-02-24 stsp "before the -m option can be used");
8497 083957f4 2020-02-24 stsp goto done;
8498 083957f4 2020-02-24 stsp }
8499 083957f4 2020-02-24 stsp
8500 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8501 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8502 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8503 3e3a69f1 2019-07-25 stsp worktree, repo);
8504 0ebf8283 2019-07-24 stsp if (error)
8505 0ebf8283 2019-07-24 stsp goto done;
8506 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8507 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8508 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8509 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8510 0ebf8283 2019-07-24 stsp if (error)
8511 0ebf8283 2019-07-24 stsp goto done;
8512 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8513 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8514 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8515 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8516 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8517 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8518 0ebf8283 2019-07-24 stsp goto done;
8519 0ebf8283 2019-07-24 stsp }
8520 0ebf8283 2019-07-24 stsp
8521 0ebf8283 2019-07-24 stsp if (continue_edit) {
8522 0ebf8283 2019-07-24 stsp char *path;
8523 0ebf8283 2019-07-24 stsp
8524 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8525 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8526 0ebf8283 2019-07-24 stsp goto done;
8527 0ebf8283 2019-07-24 stsp }
8528 0ebf8283 2019-07-24 stsp
8529 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8530 0ebf8283 2019-07-24 stsp if (error)
8531 0ebf8283 2019-07-24 stsp goto done;
8532 0ebf8283 2019-07-24 stsp
8533 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8534 0ebf8283 2019-07-24 stsp free(path);
8535 0ebf8283 2019-07-24 stsp if (error)
8536 0ebf8283 2019-07-24 stsp goto done;
8537 0ebf8283 2019-07-24 stsp
8538 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8539 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8540 3e3a69f1 2019-07-25 stsp worktree, repo);
8541 0ebf8283 2019-07-24 stsp if (error)
8542 0ebf8283 2019-07-24 stsp goto done;
8543 0ebf8283 2019-07-24 stsp
8544 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8545 0ebf8283 2019-07-24 stsp if (error)
8546 0ebf8283 2019-07-24 stsp goto done;
8547 0ebf8283 2019-07-24 stsp
8548 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8549 0ebf8283 2019-07-24 stsp head_commit_id);
8550 0ebf8283 2019-07-24 stsp if (error)
8551 0ebf8283 2019-07-24 stsp goto done;
8552 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8553 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8554 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8555 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8556 3aac7cf7 2019-07-25 stsp goto done;
8557 3aac7cf7 2019-07-25 stsp }
8558 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8559 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8560 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8561 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8562 0ebf8283 2019-07-24 stsp commit = NULL;
8563 0ebf8283 2019-07-24 stsp if (error)
8564 0ebf8283 2019-07-24 stsp goto done;
8565 0ebf8283 2019-07-24 stsp } else {
8566 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8567 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8568 0ebf8283 2019-07-24 stsp goto done;
8569 0ebf8283 2019-07-24 stsp }
8570 0ebf8283 2019-07-24 stsp
8571 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8572 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8573 0ebf8283 2019-07-24 stsp if (error != NULL)
8574 0ebf8283 2019-07-24 stsp goto done;
8575 0ebf8283 2019-07-24 stsp
8576 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8577 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8578 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8579 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8580 c7d20a3f 2019-07-30 stsp goto done;
8581 c7d20a3f 2019-07-30 stsp }
8582 c7d20a3f 2019-07-30 stsp
8583 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8584 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8585 bfce7f83 2019-07-27 stsp branch = NULL;
8586 0ebf8283 2019-07-24 stsp if (error)
8587 0ebf8283 2019-07-24 stsp goto done;
8588 0ebf8283 2019-07-24 stsp
8589 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8590 0ebf8283 2019-07-24 stsp head_commit_id);
8591 0ebf8283 2019-07-24 stsp if (error)
8592 0ebf8283 2019-07-24 stsp goto done;
8593 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8594 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8595 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8596 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8597 3aac7cf7 2019-07-25 stsp goto done;
8598 3aac7cf7 2019-07-25 stsp }
8599 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8600 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8601 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8602 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8603 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8604 0ebf8283 2019-07-24 stsp commit = NULL;
8605 0ebf8283 2019-07-24 stsp if (error)
8606 0ebf8283 2019-07-24 stsp goto done;
8607 0ebf8283 2019-07-24 stsp
8608 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8609 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8610 c5996fff 2020-01-29 stsp goto done;
8611 c5996fff 2020-01-29 stsp }
8612 c5996fff 2020-01-29 stsp
8613 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8614 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8615 bfce7f83 2019-07-27 stsp if (error)
8616 bfce7f83 2019-07-27 stsp goto done;
8617 bfce7f83 2019-07-27 stsp
8618 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8619 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8620 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8621 6ba2bea2 2019-07-27 stsp if (error) {
8622 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8623 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8624 9627c110 2020-04-18 stsp update_progress, &upa);
8625 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8626 0ebf8283 2019-07-24 stsp goto done;
8627 6ba2bea2 2019-07-27 stsp }
8628 0ebf8283 2019-07-24 stsp } else {
8629 514f2ffe 2020-01-29 stsp const char *branch_name;
8630 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8631 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8632 514f2ffe 2020-01-29 stsp branch_name += 11;
8633 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8634 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8635 6ba2bea2 2019-07-27 stsp if (error) {
8636 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8637 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8638 9627c110 2020-04-18 stsp update_progress, &upa);
8639 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8640 0ebf8283 2019-07-24 stsp goto done;
8641 6ba2bea2 2019-07-27 stsp }
8642 0ebf8283 2019-07-24 stsp
8643 0ebf8283 2019-07-24 stsp }
8644 0ebf8283 2019-07-24 stsp
8645 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8646 0ebf8283 2019-07-24 stsp repo);
8647 6ba2bea2 2019-07-27 stsp if (error) {
8648 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8649 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8650 9627c110 2020-04-18 stsp update_progress, &upa);
8651 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8652 0ebf8283 2019-07-24 stsp goto done;
8653 6ba2bea2 2019-07-27 stsp }
8654 0ebf8283 2019-07-24 stsp
8655 0ebf8283 2019-07-24 stsp }
8656 0ebf8283 2019-07-24 stsp
8657 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8658 4ec14e60 2019-08-28 hiltjo if (error)
8659 0ebf8283 2019-07-24 stsp goto done;
8660 0ebf8283 2019-07-24 stsp
8661 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8662 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8663 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8664 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8665 0ebf8283 2019-07-24 stsp continue;
8666 0ebf8283 2019-07-24 stsp
8667 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8668 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8669 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8670 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8671 0ebf8283 2019-07-24 stsp repo);
8672 ab20a43a 2020-01-29 stsp if (error)
8673 ab20a43a 2020-01-29 stsp goto done;
8674 0ebf8283 2019-07-24 stsp } else {
8675 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8676 ab20a43a 2020-01-29 stsp int have_changes = 0;
8677 ab20a43a 2020-01-29 stsp
8678 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8679 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8680 ab20a43a 2020-01-29 stsp if (error)
8681 ab20a43a 2020-01-29 stsp goto done;
8682 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8683 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8684 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8685 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8686 ab20a43a 2020-01-29 stsp if (error) {
8687 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8688 ab20a43a 2020-01-29 stsp goto done;
8689 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8690 ab20a43a 2020-01-29 stsp goto done;
8691 ab20a43a 2020-01-29 stsp }
8692 ab20a43a 2020-01-29 stsp if (have_changes) {
8693 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8694 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8695 ab20a43a 2020-01-29 stsp if (error)
8696 ab20a43a 2020-01-29 stsp goto done;
8697 ab20a43a 2020-01-29 stsp } else {
8698 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8699 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8700 ab20a43a 2020-01-29 stsp if (error)
8701 ab20a43a 2020-01-29 stsp goto done;
8702 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8703 ab20a43a 2020-01-29 stsp hle, NULL);
8704 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8705 ab20a43a 2020-01-29 stsp commit = NULL;
8706 ab20a43a 2020-01-29 stsp if (error)
8707 ab20a43a 2020-01-29 stsp goto done;
8708 ab20a43a 2020-01-29 stsp }
8709 0ebf8283 2019-07-24 stsp }
8710 0ebf8283 2019-07-24 stsp continue;
8711 0ebf8283 2019-07-24 stsp }
8712 0ebf8283 2019-07-24 stsp
8713 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8714 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8715 0ebf8283 2019-07-24 stsp if (error)
8716 0ebf8283 2019-07-24 stsp goto done;
8717 0ebf8283 2019-07-24 stsp continue;
8718 0ebf8283 2019-07-24 stsp }
8719 0ebf8283 2019-07-24 stsp
8720 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8721 0ebf8283 2019-07-24 stsp hle->commit_id);
8722 0ebf8283 2019-07-24 stsp if (error)
8723 0ebf8283 2019-07-24 stsp goto done;
8724 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8725 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8726 0ebf8283 2019-07-24 stsp
8727 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8728 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8729 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8730 0ebf8283 2019-07-24 stsp if (error)
8731 0ebf8283 2019-07-24 stsp goto done;
8732 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8733 0ebf8283 2019-07-24 stsp commit = NULL;
8734 0ebf8283 2019-07-24 stsp
8735 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8736 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8737 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8738 9627c110 2020-04-18 stsp
8739 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8740 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8741 a0ea4fc0 2020-02-28 stsp repo);
8742 a0ea4fc0 2020-02-28 stsp if (error)
8743 a0ea4fc0 2020-02-28 stsp goto done;
8744 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8745 0ebf8283 2019-07-24 stsp break;
8746 0ebf8283 2019-07-24 stsp }
8747 0ebf8283 2019-07-24 stsp
8748 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8749 0ebf8283 2019-07-24 stsp char *id_str;
8750 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8751 0ebf8283 2019-07-24 stsp if (error)
8752 0ebf8283 2019-07-24 stsp goto done;
8753 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8754 0ebf8283 2019-07-24 stsp id_str);
8755 0ebf8283 2019-07-24 stsp free(id_str);
8756 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8757 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8758 3e3a69f1 2019-07-25 stsp fileindex);
8759 0ebf8283 2019-07-24 stsp goto done;
8760 0ebf8283 2019-07-24 stsp }
8761 0ebf8283 2019-07-24 stsp
8762 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8763 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8764 0ebf8283 2019-07-24 stsp if (error)
8765 0ebf8283 2019-07-24 stsp goto done;
8766 0ebf8283 2019-07-24 stsp continue;
8767 0ebf8283 2019-07-24 stsp }
8768 0ebf8283 2019-07-24 stsp
8769 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8770 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8771 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8772 0ebf8283 2019-07-24 stsp if (error)
8773 0ebf8283 2019-07-24 stsp goto done;
8774 0ebf8283 2019-07-24 stsp }
8775 0ebf8283 2019-07-24 stsp
8776 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8777 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8778 0ebf8283 2019-07-24 stsp if (error)
8779 0ebf8283 2019-07-24 stsp goto done;
8780 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8781 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8782 0ebf8283 2019-07-24 stsp } else
8783 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8784 3e3a69f1 2019-07-25 stsp branch, repo);
8785 0ebf8283 2019-07-24 stsp done:
8786 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8787 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8788 0ebf8283 2019-07-24 stsp free(head_commit_id);
8789 0ebf8283 2019-07-24 stsp free(base_commit_id);
8790 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8791 0ebf8283 2019-07-24 stsp if (commit)
8792 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8793 0ebf8283 2019-07-24 stsp if (branch)
8794 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8795 0ebf8283 2019-07-24 stsp if (tmp_branch)
8796 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8797 0ebf8283 2019-07-24 stsp if (worktree)
8798 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8799 2822a352 2019-10-15 stsp if (repo)
8800 2822a352 2019-10-15 stsp got_repo_close(repo);
8801 2822a352 2019-10-15 stsp return error;
8802 2822a352 2019-10-15 stsp }
8803 2822a352 2019-10-15 stsp
8804 2822a352 2019-10-15 stsp __dead static void
8805 2822a352 2019-10-15 stsp usage_integrate(void)
8806 2822a352 2019-10-15 stsp {
8807 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8808 2822a352 2019-10-15 stsp exit(1);
8809 2822a352 2019-10-15 stsp }
8810 2822a352 2019-10-15 stsp
8811 2822a352 2019-10-15 stsp static const struct got_error *
8812 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8813 2822a352 2019-10-15 stsp {
8814 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8815 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8816 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8817 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8818 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8819 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8820 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8821 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8822 9627c110 2020-04-18 stsp int ch;
8823 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8824 2822a352 2019-10-15 stsp
8825 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8826 2822a352 2019-10-15 stsp switch (ch) {
8827 2822a352 2019-10-15 stsp default:
8828 2822a352 2019-10-15 stsp usage_integrate();
8829 2822a352 2019-10-15 stsp /* NOTREACHED */
8830 2822a352 2019-10-15 stsp }
8831 2822a352 2019-10-15 stsp }
8832 2822a352 2019-10-15 stsp
8833 2822a352 2019-10-15 stsp argc -= optind;
8834 2822a352 2019-10-15 stsp argv += optind;
8835 2822a352 2019-10-15 stsp
8836 2822a352 2019-10-15 stsp if (argc != 1)
8837 2822a352 2019-10-15 stsp usage_integrate();
8838 2822a352 2019-10-15 stsp branch_arg = argv[0];
8839 2822a352 2019-10-15 stsp
8840 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8841 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8842 2822a352 2019-10-15 stsp err(1, "pledge");
8843 2822a352 2019-10-15 stsp
8844 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8845 2822a352 2019-10-15 stsp if (cwd == NULL) {
8846 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8847 2822a352 2019-10-15 stsp goto done;
8848 2822a352 2019-10-15 stsp }
8849 2822a352 2019-10-15 stsp
8850 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8851 fa51e947 2020-03-27 stsp if (error) {
8852 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8853 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8854 fa51e947 2020-03-27 stsp cwd);
8855 2822a352 2019-10-15 stsp goto done;
8856 fa51e947 2020-03-27 stsp }
8857 2822a352 2019-10-15 stsp
8858 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8859 2822a352 2019-10-15 stsp if (error)
8860 2822a352 2019-10-15 stsp goto done;
8861 2822a352 2019-10-15 stsp
8862 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8863 2822a352 2019-10-15 stsp NULL);
8864 2822a352 2019-10-15 stsp if (error != NULL)
8865 2822a352 2019-10-15 stsp goto done;
8866 2822a352 2019-10-15 stsp
8867 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8868 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8869 2822a352 2019-10-15 stsp if (error)
8870 2822a352 2019-10-15 stsp goto done;
8871 2822a352 2019-10-15 stsp
8872 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8873 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8874 2822a352 2019-10-15 stsp goto done;
8875 2822a352 2019-10-15 stsp }
8876 2822a352 2019-10-15 stsp
8877 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8878 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8879 2822a352 2019-10-15 stsp if (error)
8880 2822a352 2019-10-15 stsp goto done;
8881 2822a352 2019-10-15 stsp
8882 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8883 2822a352 2019-10-15 stsp if (refname == NULL) {
8884 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8885 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8886 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8887 2822a352 2019-10-15 stsp goto done;
8888 2822a352 2019-10-15 stsp }
8889 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8890 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8891 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8892 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8893 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8894 2822a352 2019-10-15 stsp goto done;
8895 2822a352 2019-10-15 stsp }
8896 2822a352 2019-10-15 stsp
8897 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8898 2822a352 2019-10-15 stsp if (error)
8899 2822a352 2019-10-15 stsp goto done;
8900 2822a352 2019-10-15 stsp
8901 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8902 2822a352 2019-10-15 stsp if (error)
8903 2822a352 2019-10-15 stsp goto done;
8904 2822a352 2019-10-15 stsp
8905 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8906 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8907 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8908 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8909 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8910 2822a352 2019-10-15 stsp goto done;
8911 2822a352 2019-10-15 stsp }
8912 2822a352 2019-10-15 stsp
8913 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8914 2822a352 2019-10-15 stsp if (error) {
8915 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8916 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8917 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8918 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8919 2822a352 2019-10-15 stsp goto done;
8920 2822a352 2019-10-15 stsp }
8921 2822a352 2019-10-15 stsp
8922 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8923 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8924 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
8925 2822a352 2019-10-15 stsp check_cancelled, NULL);
8926 2822a352 2019-10-15 stsp if (error)
8927 2822a352 2019-10-15 stsp goto done;
8928 2822a352 2019-10-15 stsp
8929 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8930 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8931 2822a352 2019-10-15 stsp done:
8932 715dc77e 2019-08-03 stsp if (repo)
8933 715dc77e 2019-08-03 stsp got_repo_close(repo);
8934 2822a352 2019-10-15 stsp if (worktree)
8935 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8936 2822a352 2019-10-15 stsp free(cwd);
8937 2822a352 2019-10-15 stsp free(base_commit_id);
8938 2822a352 2019-10-15 stsp free(commit_id);
8939 2822a352 2019-10-15 stsp free(refname);
8940 2822a352 2019-10-15 stsp free(base_refname);
8941 715dc77e 2019-08-03 stsp return error;
8942 715dc77e 2019-08-03 stsp }
8943 715dc77e 2019-08-03 stsp
8944 715dc77e 2019-08-03 stsp __dead static void
8945 715dc77e 2019-08-03 stsp usage_stage(void)
8946 715dc77e 2019-08-03 stsp {
8947 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8948 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
8949 715dc77e 2019-08-03 stsp getprogname());
8950 715dc77e 2019-08-03 stsp exit(1);
8951 715dc77e 2019-08-03 stsp }
8952 715dc77e 2019-08-03 stsp
8953 715dc77e 2019-08-03 stsp static const struct got_error *
8954 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8955 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8956 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8957 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8958 408b4ebc 2019-08-03 stsp {
8959 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8960 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8961 408b4ebc 2019-08-03 stsp
8962 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8963 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8964 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8965 408b4ebc 2019-08-03 stsp return NULL;
8966 408b4ebc 2019-08-03 stsp
8967 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8968 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8969 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8970 408b4ebc 2019-08-03 stsp else
8971 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8972 408b4ebc 2019-08-03 stsp if (err)
8973 408b4ebc 2019-08-03 stsp return err;
8974 408b4ebc 2019-08-03 stsp
8975 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8976 408b4ebc 2019-08-03 stsp free(id_str);
8977 dc424a06 2019-08-07 stsp return NULL;
8978 dc424a06 2019-08-07 stsp }
8979 2e1f37b0 2019-08-08 stsp
8980 dc424a06 2019-08-07 stsp static const struct got_error *
8981 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8982 715dc77e 2019-08-03 stsp {
8983 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8984 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8985 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8986 715dc77e 2019-08-03 stsp char *cwd = NULL;
8987 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8988 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8989 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8990 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8991 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8992 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8993 715dc77e 2019-08-03 stsp
8994 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8995 715dc77e 2019-08-03 stsp
8996 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8997 715dc77e 2019-08-03 stsp switch (ch) {
8998 408b4ebc 2019-08-03 stsp case 'l':
8999 408b4ebc 2019-08-03 stsp list_stage = 1;
9000 408b4ebc 2019-08-03 stsp break;
9001 dc424a06 2019-08-07 stsp case 'p':
9002 dc424a06 2019-08-07 stsp pflag = 1;
9003 dc424a06 2019-08-07 stsp break;
9004 dc424a06 2019-08-07 stsp case 'F':
9005 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9006 dc424a06 2019-08-07 stsp break;
9007 35213c7c 2020-07-23 stsp case 'S':
9008 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9009 35213c7c 2020-07-23 stsp break;
9010 715dc77e 2019-08-03 stsp default:
9011 715dc77e 2019-08-03 stsp usage_stage();
9012 715dc77e 2019-08-03 stsp /* NOTREACHED */
9013 715dc77e 2019-08-03 stsp }
9014 715dc77e 2019-08-03 stsp }
9015 715dc77e 2019-08-03 stsp
9016 715dc77e 2019-08-03 stsp argc -= optind;
9017 715dc77e 2019-08-03 stsp argv += optind;
9018 715dc77e 2019-08-03 stsp
9019 715dc77e 2019-08-03 stsp #ifndef PROFILE
9020 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9021 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9022 715dc77e 2019-08-03 stsp err(1, "pledge");
9023 715dc77e 2019-08-03 stsp #endif
9024 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9025 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9026 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9027 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9028 715dc77e 2019-08-03 stsp
9029 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9030 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9031 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9032 715dc77e 2019-08-03 stsp goto done;
9033 715dc77e 2019-08-03 stsp }
9034 715dc77e 2019-08-03 stsp
9035 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9036 fa51e947 2020-03-27 stsp if (error) {
9037 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9038 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9039 715dc77e 2019-08-03 stsp goto done;
9040 fa51e947 2020-03-27 stsp }
9041 715dc77e 2019-08-03 stsp
9042 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9043 c9956ddf 2019-09-08 stsp NULL);
9044 715dc77e 2019-08-03 stsp if (error != NULL)
9045 715dc77e 2019-08-03 stsp goto done;
9046 715dc77e 2019-08-03 stsp
9047 dc424a06 2019-08-07 stsp if (patch_script_path) {
9048 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9049 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9050 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9051 dc424a06 2019-08-07 stsp patch_script_path);
9052 dc424a06 2019-08-07 stsp goto done;
9053 dc424a06 2019-08-07 stsp }
9054 dc424a06 2019-08-07 stsp }
9055 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9056 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9057 715dc77e 2019-08-03 stsp if (error)
9058 715dc77e 2019-08-03 stsp goto done;
9059 715dc77e 2019-08-03 stsp
9060 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9061 6d022e97 2019-08-04 stsp if (error)
9062 6d022e97 2019-08-04 stsp goto done;
9063 715dc77e 2019-08-03 stsp
9064 6d022e97 2019-08-04 stsp if (list_stage)
9065 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9066 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9067 2e1f37b0 2019-08-08 stsp else {
9068 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9069 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9070 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9071 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9072 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9073 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9074 2e1f37b0 2019-08-08 stsp }
9075 ad493afc 2019-08-03 stsp done:
9076 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9077 2e1f37b0 2019-08-08 stsp error == NULL)
9078 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9079 ad493afc 2019-08-03 stsp if (repo)
9080 ad493afc 2019-08-03 stsp got_repo_close(repo);
9081 ad493afc 2019-08-03 stsp if (worktree)
9082 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9083 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9084 ad493afc 2019-08-03 stsp free((char *)pe->path);
9085 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9086 ad493afc 2019-08-03 stsp free(cwd);
9087 ad493afc 2019-08-03 stsp return error;
9088 ad493afc 2019-08-03 stsp }
9089 ad493afc 2019-08-03 stsp
9090 ad493afc 2019-08-03 stsp __dead static void
9091 ad493afc 2019-08-03 stsp usage_unstage(void)
9092 ad493afc 2019-08-03 stsp {
9093 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9094 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9095 ad493afc 2019-08-03 stsp getprogname());
9096 ad493afc 2019-08-03 stsp exit(1);
9097 ad493afc 2019-08-03 stsp }
9098 ad493afc 2019-08-03 stsp
9099 ad493afc 2019-08-03 stsp
9100 ad493afc 2019-08-03 stsp static const struct got_error *
9101 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9102 ad493afc 2019-08-03 stsp {
9103 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9104 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9105 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9106 ad493afc 2019-08-03 stsp char *cwd = NULL;
9107 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9108 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9109 9627c110 2020-04-18 stsp int ch, pflag = 0;
9110 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9111 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9112 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9113 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9114 ad493afc 2019-08-03 stsp
9115 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9116 ad493afc 2019-08-03 stsp
9117 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9118 ad493afc 2019-08-03 stsp switch (ch) {
9119 2e1f37b0 2019-08-08 stsp case 'p':
9120 2e1f37b0 2019-08-08 stsp pflag = 1;
9121 2e1f37b0 2019-08-08 stsp break;
9122 2e1f37b0 2019-08-08 stsp case 'F':
9123 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9124 2e1f37b0 2019-08-08 stsp break;
9125 ad493afc 2019-08-03 stsp default:
9126 ad493afc 2019-08-03 stsp usage_unstage();
9127 ad493afc 2019-08-03 stsp /* NOTREACHED */
9128 ad493afc 2019-08-03 stsp }
9129 ad493afc 2019-08-03 stsp }
9130 ad493afc 2019-08-03 stsp
9131 ad493afc 2019-08-03 stsp argc -= optind;
9132 ad493afc 2019-08-03 stsp argv += optind;
9133 ad493afc 2019-08-03 stsp
9134 ad493afc 2019-08-03 stsp #ifndef PROFILE
9135 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9136 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9137 ad493afc 2019-08-03 stsp err(1, "pledge");
9138 ad493afc 2019-08-03 stsp #endif
9139 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9140 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9141 2e1f37b0 2019-08-08 stsp
9142 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9143 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9144 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9145 ad493afc 2019-08-03 stsp goto done;
9146 ad493afc 2019-08-03 stsp }
9147 ad493afc 2019-08-03 stsp
9148 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9149 fa51e947 2020-03-27 stsp if (error) {
9150 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9151 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9152 ad493afc 2019-08-03 stsp goto done;
9153 fa51e947 2020-03-27 stsp }
9154 ad493afc 2019-08-03 stsp
9155 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9156 c9956ddf 2019-09-08 stsp NULL);
9157 ad493afc 2019-08-03 stsp if (error != NULL)
9158 ad493afc 2019-08-03 stsp goto done;
9159 ad493afc 2019-08-03 stsp
9160 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9161 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9162 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9163 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9164 2e1f37b0 2019-08-08 stsp patch_script_path);
9165 2e1f37b0 2019-08-08 stsp goto done;
9166 2e1f37b0 2019-08-08 stsp }
9167 2e1f37b0 2019-08-08 stsp }
9168 2e1f37b0 2019-08-08 stsp
9169 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9170 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9171 ad493afc 2019-08-03 stsp if (error)
9172 ad493afc 2019-08-03 stsp goto done;
9173 ad493afc 2019-08-03 stsp
9174 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9175 ad493afc 2019-08-03 stsp if (error)
9176 ad493afc 2019-08-03 stsp goto done;
9177 ad493afc 2019-08-03 stsp
9178 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9179 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9180 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9181 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9182 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9183 9627c110 2020-04-18 stsp if (!error)
9184 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9185 715dc77e 2019-08-03 stsp done:
9186 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9187 2e1f37b0 2019-08-08 stsp error == NULL)
9188 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9189 0ebf8283 2019-07-24 stsp if (repo)
9190 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9191 715dc77e 2019-08-03 stsp if (worktree)
9192 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9193 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9194 715dc77e 2019-08-03 stsp free((char *)pe->path);
9195 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9196 715dc77e 2019-08-03 stsp free(cwd);
9197 01073a5d 2019-08-22 stsp return error;
9198 01073a5d 2019-08-22 stsp }
9199 01073a5d 2019-08-22 stsp
9200 01073a5d 2019-08-22 stsp __dead static void
9201 01073a5d 2019-08-22 stsp usage_cat(void)
9202 01073a5d 2019-08-22 stsp {
9203 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9204 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9205 01073a5d 2019-08-22 stsp exit(1);
9206 01073a5d 2019-08-22 stsp }
9207 01073a5d 2019-08-22 stsp
9208 01073a5d 2019-08-22 stsp static const struct got_error *
9209 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9210 01073a5d 2019-08-22 stsp {
9211 01073a5d 2019-08-22 stsp const struct got_error *err;
9212 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9213 01073a5d 2019-08-22 stsp
9214 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9215 01073a5d 2019-08-22 stsp if (err)
9216 01073a5d 2019-08-22 stsp return err;
9217 01073a5d 2019-08-22 stsp
9218 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9219 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9220 01073a5d 2019-08-22 stsp return err;
9221 01073a5d 2019-08-22 stsp }
9222 01073a5d 2019-08-22 stsp
9223 01073a5d 2019-08-22 stsp static const struct got_error *
9224 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9225 01073a5d 2019-08-22 stsp {
9226 01073a5d 2019-08-22 stsp const struct got_error *err;
9227 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9228 56e0773d 2019-11-28 stsp int nentries, i;
9229 01073a5d 2019-08-22 stsp
9230 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9231 01073a5d 2019-08-22 stsp if (err)
9232 01073a5d 2019-08-22 stsp return err;
9233 01073a5d 2019-08-22 stsp
9234 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9235 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9236 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9237 01073a5d 2019-08-22 stsp char *id_str;
9238 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9239 01073a5d 2019-08-22 stsp break;
9240 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9241 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9242 01073a5d 2019-08-22 stsp if (err)
9243 01073a5d 2019-08-22 stsp break;
9244 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9245 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9246 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9247 01073a5d 2019-08-22 stsp free(id_str);
9248 01073a5d 2019-08-22 stsp }
9249 01073a5d 2019-08-22 stsp
9250 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9251 01073a5d 2019-08-22 stsp return err;
9252 01073a5d 2019-08-22 stsp }
9253 01073a5d 2019-08-22 stsp
9254 01073a5d 2019-08-22 stsp static const struct got_error *
9255 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9256 01073a5d 2019-08-22 stsp {
9257 01073a5d 2019-08-22 stsp const struct got_error *err;
9258 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9259 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9260 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9261 01073a5d 2019-08-22 stsp char *id_str = NULL;
9262 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9263 01073a5d 2019-08-22 stsp
9264 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9265 01073a5d 2019-08-22 stsp if (err)
9266 01073a5d 2019-08-22 stsp return err;
9267 01073a5d 2019-08-22 stsp
9268 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9269 01073a5d 2019-08-22 stsp if (err)
9270 01073a5d 2019-08-22 stsp goto done;
9271 01073a5d 2019-08-22 stsp
9272 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9273 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9274 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9275 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9276 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9277 01073a5d 2019-08-22 stsp char *pid_str;
9278 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9279 01073a5d 2019-08-22 stsp if (err)
9280 01073a5d 2019-08-22 stsp goto done;
9281 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9282 01073a5d 2019-08-22 stsp free(pid_str);
9283 01073a5d 2019-08-22 stsp }
9284 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9285 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9286 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9287 01073a5d 2019-08-22 stsp
9288 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9289 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9290 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9291 01073a5d 2019-08-22 stsp
9292 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9293 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9294 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9295 01073a5d 2019-08-22 stsp done:
9296 01073a5d 2019-08-22 stsp free(id_str);
9297 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9298 01073a5d 2019-08-22 stsp return err;
9299 01073a5d 2019-08-22 stsp }
9300 01073a5d 2019-08-22 stsp
9301 01073a5d 2019-08-22 stsp static const struct got_error *
9302 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9303 01073a5d 2019-08-22 stsp {
9304 01073a5d 2019-08-22 stsp const struct got_error *err;
9305 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9306 01073a5d 2019-08-22 stsp char *id_str = NULL;
9307 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9308 01073a5d 2019-08-22 stsp
9309 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9310 01073a5d 2019-08-22 stsp if (err)
9311 01073a5d 2019-08-22 stsp return err;
9312 01073a5d 2019-08-22 stsp
9313 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9314 01073a5d 2019-08-22 stsp if (err)
9315 01073a5d 2019-08-22 stsp goto done;
9316 01073a5d 2019-08-22 stsp
9317 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9318 e15d5241 2019-08-22 stsp
9319 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9320 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9321 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9322 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9323 01073a5d 2019-08-22 stsp break;
9324 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9325 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9326 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9327 01073a5d 2019-08-22 stsp break;
9328 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9329 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9330 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9331 01073a5d 2019-08-22 stsp break;
9332 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9333 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9334 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9335 01073a5d 2019-08-22 stsp break;
9336 01073a5d 2019-08-22 stsp default:
9337 01073a5d 2019-08-22 stsp break;
9338 01073a5d 2019-08-22 stsp }
9339 01073a5d 2019-08-22 stsp
9340 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9341 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9342 e15d5241 2019-08-22 stsp
9343 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9344 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9345 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9346 01073a5d 2019-08-22 stsp
9347 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9348 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9349 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9350 01073a5d 2019-08-22 stsp done:
9351 01073a5d 2019-08-22 stsp free(id_str);
9352 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9353 01073a5d 2019-08-22 stsp return err;
9354 01073a5d 2019-08-22 stsp }
9355 01073a5d 2019-08-22 stsp
9356 01073a5d 2019-08-22 stsp static const struct got_error *
9357 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9358 01073a5d 2019-08-22 stsp {
9359 01073a5d 2019-08-22 stsp const struct got_error *error;
9360 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9361 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9362 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9363 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9364 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9365 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9366 01073a5d 2019-08-22 stsp
9367 01073a5d 2019-08-22 stsp #ifndef PROFILE
9368 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9369 01073a5d 2019-08-22 stsp NULL) == -1)
9370 01073a5d 2019-08-22 stsp err(1, "pledge");
9371 01073a5d 2019-08-22 stsp #endif
9372 01073a5d 2019-08-22 stsp
9373 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9374 01073a5d 2019-08-22 stsp switch (ch) {
9375 896e9b6f 2019-08-26 stsp case 'c':
9376 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9377 896e9b6f 2019-08-26 stsp break;
9378 01073a5d 2019-08-22 stsp case 'r':
9379 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9380 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9381 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9382 9ba1d308 2019-10-21 stsp optarg);
9383 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9384 01073a5d 2019-08-22 stsp break;
9385 896e9b6f 2019-08-26 stsp case 'P':
9386 896e9b6f 2019-08-26 stsp force_path = 1;
9387 896e9b6f 2019-08-26 stsp break;
9388 01073a5d 2019-08-22 stsp default:
9389 01073a5d 2019-08-22 stsp usage_cat();
9390 01073a5d 2019-08-22 stsp /* NOTREACHED */
9391 01073a5d 2019-08-22 stsp }
9392 01073a5d 2019-08-22 stsp }
9393 01073a5d 2019-08-22 stsp
9394 01073a5d 2019-08-22 stsp argc -= optind;
9395 01073a5d 2019-08-22 stsp argv += optind;
9396 01073a5d 2019-08-22 stsp
9397 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9398 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9399 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9400 01073a5d 2019-08-22 stsp goto done;
9401 01073a5d 2019-08-22 stsp }
9402 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9403 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9404 01073a5d 2019-08-22 stsp goto done;
9405 01073a5d 2019-08-22 stsp if (worktree) {
9406 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9407 01073a5d 2019-08-22 stsp repo_path = strdup(
9408 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9409 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9410 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9411 01073a5d 2019-08-22 stsp goto done;
9412 01073a5d 2019-08-22 stsp }
9413 01073a5d 2019-08-22 stsp }
9414 01073a5d 2019-08-22 stsp }
9415 01073a5d 2019-08-22 stsp
9416 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9417 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9418 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9419 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9420 01073a5d 2019-08-22 stsp }
9421 01073a5d 2019-08-22 stsp
9422 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9423 01073a5d 2019-08-22 stsp free(repo_path);
9424 01073a5d 2019-08-22 stsp if (error != NULL)
9425 01073a5d 2019-08-22 stsp goto done;
9426 01073a5d 2019-08-22 stsp
9427 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9428 896e9b6f 2019-08-26 stsp if (error)
9429 896e9b6f 2019-08-26 stsp goto done;
9430 896e9b6f 2019-08-26 stsp
9431 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9432 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9433 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9434 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9435 01073a5d 2019-08-22 stsp if (error)
9436 01073a5d 2019-08-22 stsp goto done;
9437 01073a5d 2019-08-22 stsp
9438 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9439 896e9b6f 2019-08-26 stsp if (force_path) {
9440 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9441 896e9b6f 2019-08-26 stsp argv[i]);
9442 896e9b6f 2019-08-26 stsp if (error)
9443 896e9b6f 2019-08-26 stsp break;
9444 896e9b6f 2019-08-26 stsp } else {
9445 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9446 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9447 896e9b6f 2019-08-26 stsp if (error) {
9448 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9449 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9450 896e9b6f 2019-08-26 stsp break;
9451 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9452 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9453 896e9b6f 2019-08-26 stsp if (error)
9454 896e9b6f 2019-08-26 stsp break;
9455 896e9b6f 2019-08-26 stsp }
9456 896e9b6f 2019-08-26 stsp }
9457 01073a5d 2019-08-22 stsp
9458 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9459 01073a5d 2019-08-22 stsp if (error)
9460 01073a5d 2019-08-22 stsp break;
9461 01073a5d 2019-08-22 stsp
9462 01073a5d 2019-08-22 stsp switch (obj_type) {
9463 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9464 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9465 01073a5d 2019-08-22 stsp break;
9466 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9467 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9468 01073a5d 2019-08-22 stsp break;
9469 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9470 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9471 01073a5d 2019-08-22 stsp break;
9472 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9473 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9474 01073a5d 2019-08-22 stsp break;
9475 01073a5d 2019-08-22 stsp default:
9476 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9477 01073a5d 2019-08-22 stsp break;
9478 01073a5d 2019-08-22 stsp }
9479 01073a5d 2019-08-22 stsp if (error)
9480 01073a5d 2019-08-22 stsp break;
9481 01073a5d 2019-08-22 stsp free(label);
9482 01073a5d 2019-08-22 stsp label = NULL;
9483 01073a5d 2019-08-22 stsp free(id);
9484 01073a5d 2019-08-22 stsp id = NULL;
9485 01073a5d 2019-08-22 stsp }
9486 01073a5d 2019-08-22 stsp done:
9487 01073a5d 2019-08-22 stsp free(label);
9488 01073a5d 2019-08-22 stsp free(id);
9489 896e9b6f 2019-08-26 stsp free(commit_id);
9490 01073a5d 2019-08-22 stsp if (worktree)
9491 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9492 01073a5d 2019-08-22 stsp if (repo) {
9493 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9494 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9495 01073a5d 2019-08-22 stsp if (error == NULL)
9496 01073a5d 2019-08-22 stsp error = repo_error;
9497 b2118c49 2020-07-28 stsp }
9498 b2118c49 2020-07-28 stsp return error;
9499 b2118c49 2020-07-28 stsp }
9500 b2118c49 2020-07-28 stsp
9501 b2118c49 2020-07-28 stsp __dead static void
9502 b2118c49 2020-07-28 stsp usage_info(void)
9503 b2118c49 2020-07-28 stsp {
9504 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9505 b2118c49 2020-07-28 stsp getprogname());
9506 b2118c49 2020-07-28 stsp exit(1);
9507 b2118c49 2020-07-28 stsp }
9508 b2118c49 2020-07-28 stsp
9509 b2118c49 2020-07-28 stsp static const struct got_error *
9510 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9511 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9512 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9513 b2118c49 2020-07-28 stsp {
9514 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9515 b2118c49 2020-07-28 stsp char *id_str = NULL;
9516 b2118c49 2020-07-28 stsp char datebuf[128];
9517 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9518 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9519 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9520 b2118c49 2020-07-28 stsp
9521 b2118c49 2020-07-28 stsp /*
9522 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9523 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9524 b2118c49 2020-07-28 stsp */
9525 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9526 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9527 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9528 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9529 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9530 b2118c49 2020-07-28 stsp }
9531 b2118c49 2020-07-28 stsp
9532 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9533 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9534 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9535 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9536 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9537 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9538 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9539 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9540 b2118c49 2020-07-28 stsp else
9541 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9542 b2118c49 2020-07-28 stsp
9543 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9544 b2118c49 2020-07-28 stsp if (tm == NULL)
9545 b2118c49 2020-07-28 stsp return NULL;
9546 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9547 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9548 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9549 b2118c49 2020-07-28 stsp
9550 b2118c49 2020-07-28 stsp if (blob_id) {
9551 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9552 b2118c49 2020-07-28 stsp if (err)
9553 b2118c49 2020-07-28 stsp return err;
9554 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9555 b2118c49 2020-07-28 stsp free(id_str);
9556 b2118c49 2020-07-28 stsp }
9557 b2118c49 2020-07-28 stsp
9558 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9559 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9560 b2118c49 2020-07-28 stsp if (err)
9561 b2118c49 2020-07-28 stsp return err;
9562 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9563 b2118c49 2020-07-28 stsp free(id_str);
9564 b2118c49 2020-07-28 stsp }
9565 b2118c49 2020-07-28 stsp
9566 b2118c49 2020-07-28 stsp if (commit_id) {
9567 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9568 b2118c49 2020-07-28 stsp if (err)
9569 b2118c49 2020-07-28 stsp return err;
9570 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9571 b2118c49 2020-07-28 stsp free(id_str);
9572 b2118c49 2020-07-28 stsp }
9573 b2118c49 2020-07-28 stsp
9574 b2118c49 2020-07-28 stsp return NULL;
9575 b2118c49 2020-07-28 stsp }
9576 b2118c49 2020-07-28 stsp
9577 b2118c49 2020-07-28 stsp static const struct got_error *
9578 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9579 b2118c49 2020-07-28 stsp {
9580 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9581 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9582 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9583 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9584 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9585 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9586 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9587 b2118c49 2020-07-28 stsp
9588 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9589 b2118c49 2020-07-28 stsp
9590 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9591 b2118c49 2020-07-28 stsp switch (ch) {
9592 b2118c49 2020-07-28 stsp default:
9593 b2118c49 2020-07-28 stsp usage_info();
9594 b2118c49 2020-07-28 stsp /* NOTREACHED */
9595 b2118c49 2020-07-28 stsp }
9596 01073a5d 2019-08-22 stsp }
9597 b2118c49 2020-07-28 stsp
9598 b2118c49 2020-07-28 stsp argc -= optind;
9599 b2118c49 2020-07-28 stsp argv += optind;
9600 b2118c49 2020-07-28 stsp
9601 b2118c49 2020-07-28 stsp #ifndef PROFILE
9602 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9603 b2118c49 2020-07-28 stsp NULL) == -1)
9604 b2118c49 2020-07-28 stsp err(1, "pledge");
9605 b2118c49 2020-07-28 stsp #endif
9606 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9607 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9608 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9609 b2118c49 2020-07-28 stsp goto done;
9610 b2118c49 2020-07-28 stsp }
9611 b2118c49 2020-07-28 stsp
9612 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9613 b2118c49 2020-07-28 stsp if (error) {
9614 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9615 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9616 b2118c49 2020-07-28 stsp goto done;
9617 b2118c49 2020-07-28 stsp }
9618 b2118c49 2020-07-28 stsp
9619 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9620 b2118c49 2020-07-28 stsp if (error)
9621 b2118c49 2020-07-28 stsp goto done;
9622 b2118c49 2020-07-28 stsp
9623 b2118c49 2020-07-28 stsp if (argc >= 1) {
9624 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9625 b2118c49 2020-07-28 stsp worktree);
9626 b2118c49 2020-07-28 stsp if (error)
9627 b2118c49 2020-07-28 stsp goto done;
9628 b2118c49 2020-07-28 stsp show_files = 1;
9629 b2118c49 2020-07-28 stsp }
9630 b2118c49 2020-07-28 stsp
9631 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9632 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9633 b2118c49 2020-07-28 stsp if (error)
9634 b2118c49 2020-07-28 stsp goto done;
9635 b2118c49 2020-07-28 stsp
9636 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9637 b2118c49 2020-07-28 stsp if (error)
9638 b2118c49 2020-07-28 stsp goto done;
9639 b2118c49 2020-07-28 stsp
9640 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9641 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9642 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9643 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9644 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9645 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9646 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9647 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9648 b2118c49 2020-07-28 stsp
9649 b2118c49 2020-07-28 stsp if (show_files) {
9650 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9651 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9652 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9653 b2118c49 2020-07-28 stsp continue;
9654 b2118c49 2020-07-28 stsp /*
9655 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9656 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9657 b2118c49 2020-07-28 stsp */
9658 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9659 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9660 b2118c49 2020-07-28 stsp }
9661 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9662 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9663 b2118c49 2020-07-28 stsp if (error)
9664 b2118c49 2020-07-28 stsp goto done;
9665 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9666 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9667 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9668 b2118c49 2020-07-28 stsp break;
9669 b2118c49 2020-07-28 stsp }
9670 b2118c49 2020-07-28 stsp }
9671 b2118c49 2020-07-28 stsp }
9672 b2118c49 2020-07-28 stsp done:
9673 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9674 b2118c49 2020-07-28 stsp free((char *)pe->path);
9675 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9676 b2118c49 2020-07-28 stsp free(cwd);
9677 b2118c49 2020-07-28 stsp free(id_str);
9678 b2118c49 2020-07-28 stsp free(uuidstr);
9679 0ebf8283 2019-07-24 stsp return error;
9680 0ebf8283 2019-07-24 stsp }