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 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 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 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
175 ce5b7c56 2019-07-09 stsp {
176 6059809a 2020-12-17 stsp size_t i;
177 ce5b7c56 2019-07-09 stsp
178 6879ba42 2020-10-01 naddy fprintf(fp, "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 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
182 ce5b7c56 2019-07-09 stsp }
183 6879ba42 2020-10-01 naddy fputc('\n', fp);
184 ce5b7c56 2019-07-09 stsp }
185 5c860e29 2018-03-12 stsp
186 ff69268e 2020-12-13 stsp __dead static void
187 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
188 ff69268e 2020-12-13 stsp {
189 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
190 ff69268e 2020-12-13 stsp }
191 ff69268e 2020-12-13 stsp
192 5c860e29 2018-03-12 stsp int
193 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
194 5c860e29 2018-03-12 stsp {
195 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
196 6059809a 2020-12-17 stsp size_t i;
197 5c860e29 2018-03-12 stsp int ch;
198 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
199 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
200 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
201 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
202 83cd27f8 2020-01-13 stsp };
203 5c860e29 2018-03-12 stsp
204 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
205 5c860e29 2018-03-12 stsp
206 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
207 5c860e29 2018-03-12 stsp switch (ch) {
208 1b6b95a8 2018-03-12 stsp case 'h':
209 1b6b95a8 2018-03-12 stsp hflag = 1;
210 53ccebc2 2019-07-30 stsp break;
211 53ccebc2 2019-07-30 stsp case 'V':
212 53ccebc2 2019-07-30 stsp Vflag = 1;
213 1b6b95a8 2018-03-12 stsp break;
214 5c860e29 2018-03-12 stsp default:
215 6879ba42 2020-10-01 naddy usage(hflag, 1);
216 5c860e29 2018-03-12 stsp /* NOTREACHED */
217 5c860e29 2018-03-12 stsp }
218 5c860e29 2018-03-12 stsp }
219 5c860e29 2018-03-12 stsp
220 5c860e29 2018-03-12 stsp argc -= optind;
221 5c860e29 2018-03-12 stsp argv += optind;
222 9814e6a3 2020-09-27 naddy optind = 1;
223 9814e6a3 2020-09-27 naddy optreset = 1;
224 53ccebc2 2019-07-30 stsp
225 53ccebc2 2019-07-30 stsp if (Vflag) {
226 53ccebc2 2019-07-30 stsp got_version_print_str();
227 6879ba42 2020-10-01 naddy return 0;
228 53ccebc2 2019-07-30 stsp }
229 5c860e29 2018-03-12 stsp
230 5c860e29 2018-03-12 stsp if (argc <= 0)
231 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
232 5c860e29 2018-03-12 stsp
233 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
234 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
235 99437157 2018-11-11 stsp
236 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
237 d7d4f210 2018-03-12 stsp const struct got_error *error;
238 d7d4f210 2018-03-12 stsp
239 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
240 5c860e29 2018-03-12 stsp
241 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
243 5c860e29 2018-03-12 stsp continue;
244 5c860e29 2018-03-12 stsp
245 1b6b95a8 2018-03-12 stsp if (hflag)
246 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
247 1b6b95a8 2018-03-12 stsp
248 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
249 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
250 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
251 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
252 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
253 70015d7a 2019-11-08 stsp !(sigint_received &&
254 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
256 d7d4f210 2018-03-12 stsp return 1;
257 d7d4f210 2018-03-12 stsp }
258 d7d4f210 2018-03-12 stsp
259 d7d4f210 2018-03-12 stsp return 0;
260 5c860e29 2018-03-12 stsp }
261 5c860e29 2018-03-12 stsp
262 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 6879ba42 2020-10-01 naddy list_commands(stderr);
264 5c860e29 2018-03-12 stsp return 1;
265 5c860e29 2018-03-12 stsp }
266 5c860e29 2018-03-12 stsp
267 4ed7e80c 2018-05-20 stsp __dead static void
268 6879ba42 2020-10-01 naddy usage(int hflag, int status)
269 5c860e29 2018-03-12 stsp {
270 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
271 6879ba42 2020-10-01 naddy
272 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
273 53ccebc2 2019-07-30 stsp getprogname());
274 ce5b7c56 2019-07-09 stsp if (hflag)
275 6879ba42 2020-10-01 naddy list_commands(fp);
276 6879ba42 2020-10-01 naddy exit(status);
277 5c860e29 2018-03-12 stsp }
278 5c860e29 2018-03-12 stsp
279 0266afb7 2019-01-04 stsp static const struct got_error *
280 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
281 e2ba3d07 2019-05-13 stsp {
282 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
283 e2ba3d07 2019-05-13 stsp const char *editor;
284 8920fa04 2019-08-18 stsp
285 8920fa04 2019-08-18 stsp *abspath = NULL;
286 e2ba3d07 2019-05-13 stsp
287 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
288 e2ba3d07 2019-05-13 stsp if (editor == NULL)
289 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
290 e2ba3d07 2019-05-13 stsp
291 0ee7065d 2019-05-13 stsp if (editor) {
292 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
293 0ee7065d 2019-05-13 stsp if (err)
294 0ee7065d 2019-05-13 stsp return err;
295 0ee7065d 2019-05-13 stsp }
296 e2ba3d07 2019-05-13 stsp
297 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
298 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
299 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
300 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
301 0ee7065d 2019-05-13 stsp }
302 0ee7065d 2019-05-13 stsp
303 e2ba3d07 2019-05-13 stsp return NULL;
304 e2ba3d07 2019-05-13 stsp }
305 e2ba3d07 2019-05-13 stsp
306 e2ba3d07 2019-05-13 stsp static const struct got_error *
307 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
308 c530dc23 2019-07-23 stsp const char *worktree_path)
309 0266afb7 2019-01-04 stsp {
310 163ce85a 2019-05-13 stsp const struct got_error *err;
311 0266afb7 2019-01-04 stsp
312 37c06ea4 2019-07-15 stsp #ifdef PROFILE
313 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
314 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
315 37c06ea4 2019-07-15 stsp #endif
316 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
318 0266afb7 2019-01-04 stsp
319 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
321 0266afb7 2019-01-04 stsp
322 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
324 0266afb7 2019-01-04 stsp
325 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
326 163ce85a 2019-05-13 stsp if (err != NULL)
327 163ce85a 2019-05-13 stsp return err;
328 0266afb7 2019-01-04 stsp
329 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
330 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
331 0266afb7 2019-01-04 stsp
332 0266afb7 2019-01-04 stsp return NULL;
333 2c7829a4 2019-06-17 stsp }
334 2c7829a4 2019-06-17 stsp
335 2c7829a4 2019-06-17 stsp __dead static void
336 2c7829a4 2019-06-17 stsp usage_init(void)
337 2c7829a4 2019-06-17 stsp {
338 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
339 2c7829a4 2019-06-17 stsp exit(1);
340 2c7829a4 2019-06-17 stsp }
341 2c7829a4 2019-06-17 stsp
342 2c7829a4 2019-06-17 stsp static const struct got_error *
343 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
344 2c7829a4 2019-06-17 stsp {
345 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
346 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
347 2c7829a4 2019-06-17 stsp int ch;
348 2c7829a4 2019-06-17 stsp
349 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
350 2c7829a4 2019-06-17 stsp switch (ch) {
351 2c7829a4 2019-06-17 stsp default:
352 2c7829a4 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp /* NOTREACHED */
354 2c7829a4 2019-06-17 stsp }
355 2c7829a4 2019-06-17 stsp }
356 2c7829a4 2019-06-17 stsp
357 2c7829a4 2019-06-17 stsp argc -= optind;
358 2c7829a4 2019-06-17 stsp argv += optind;
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp #ifndef PROFILE
361 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
362 2c7829a4 2019-06-17 stsp err(1, "pledge");
363 2c7829a4 2019-06-17 stsp #endif
364 2c7829a4 2019-06-17 stsp if (argc != 1)
365 bc20e173 2019-06-17 stsp usage_init();
366 2c7829a4 2019-06-17 stsp
367 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
368 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
369 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
370 2c7829a4 2019-06-17 stsp
371 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
372 2c7829a4 2019-06-17 stsp
373 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
374 2c7829a4 2019-06-17 stsp if (error &&
375 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
376 2c7829a4 2019-06-17 stsp goto done;
377 2c7829a4 2019-06-17 stsp
378 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
379 2c7829a4 2019-06-17 stsp if (error)
380 2c7829a4 2019-06-17 stsp goto done;
381 2c7829a4 2019-06-17 stsp
382 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
383 3ce1b845 2019-07-15 stsp done:
384 3ce1b845 2019-07-15 stsp free(repo_path);
385 3ce1b845 2019-07-15 stsp return error;
386 3ce1b845 2019-07-15 stsp }
387 3ce1b845 2019-07-15 stsp
388 3ce1b845 2019-07-15 stsp __dead static void
389 3ce1b845 2019-07-15 stsp usage_import(void)
390 3ce1b845 2019-07-15 stsp {
391 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
393 3ce1b845 2019-07-15 stsp exit(1);
394 3ce1b845 2019-07-15 stsp }
395 3ce1b845 2019-07-15 stsp
396 3ce1b845 2019-07-15 stsp int
397 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
398 3ce1b845 2019-07-15 stsp {
399 3ce1b845 2019-07-15 stsp pid_t pid;
400 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
401 3ce1b845 2019-07-15 stsp int st = -1;
402 3ce1b845 2019-07-15 stsp
403 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
404 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
405 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
408 3ce1b845 2019-07-15 stsp case -1:
409 3ce1b845 2019-07-15 stsp goto doneediting;
410 3ce1b845 2019-07-15 stsp case 0:
411 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
412 3ce1b845 2019-07-15 stsp _exit(127);
413 3ce1b845 2019-07-15 stsp }
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
416 3ce1b845 2019-07-15 stsp if (errno != EINTR)
417 3ce1b845 2019-07-15 stsp break;
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp doneediting:
420 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
421 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
422 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
423 3ce1b845 2019-07-15 stsp
424 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
425 3ce1b845 2019-07-15 stsp errno = EINTR;
426 3ce1b845 2019-07-15 stsp return -1;
427 3ce1b845 2019-07-15 stsp }
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
430 3ce1b845 2019-07-15 stsp }
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp static const struct got_error *
433 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
435 28cf319f 2021-01-28 stsp int require_modification)
436 3ce1b845 2019-07-15 stsp {
437 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
438 bfa12d5e 2020-09-26 stsp char *line = NULL;
439 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
440 bfa12d5e 2020-09-26 stsp ssize_t linelen;
441 3ce1b845 2019-07-15 stsp struct stat st, st2;
442 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
443 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
444 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
445 3ce1b845 2019-07-15 stsp
446 3ce1b845 2019-07-15 stsp *logmsg = NULL;
447 3ce1b845 2019-07-15 stsp
448 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
449 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
452 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
453 3ce1b845 2019-07-15 stsp
454 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
455 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
456 3ce1b845 2019-07-15 stsp
457 28cf319f 2021-01-28 stsp if (require_modification &&
458 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
459 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
461 3ce1b845 2019-07-15 stsp
462 bfa12d5e 2020-09-26 stsp /*
463 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
464 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
465 bfa12d5e 2020-09-26 stsp * has in fact been edited.
466 bfa12d5e 2020-09-26 stsp */
467 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
468 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
469 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
470 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
471 bfa12d5e 2020-09-26 stsp
472 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
473 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
474 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
475 bfa12d5e 2020-09-26 stsp goto done;
476 bfa12d5e 2020-09-26 stsp }
477 bfa12d5e 2020-09-26 stsp s = buf;
478 bfa12d5e 2020-09-26 stsp len = 0;
479 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
480 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
481 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
482 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
483 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
484 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
485 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
486 bfa12d5e 2020-09-26 stsp goto done;
487 bfa12d5e 2020-09-26 stsp }
488 bfa12d5e 2020-09-26 stsp }
489 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
490 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
491 bfa12d5e 2020-09-26 stsp len--;
492 bfa12d5e 2020-09-26 stsp }
493 bfa12d5e 2020-09-26 stsp
494 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
495 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
496 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
497 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
498 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
499 3ce1b845 2019-07-15 stsp
500 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
501 3ce1b845 2019-07-15 stsp if (fp == NULL) {
502 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
503 3ce1b845 2019-07-15 stsp goto done;
504 3ce1b845 2019-07-15 stsp }
505 bfa12d5e 2020-09-26 stsp
506 bfa12d5e 2020-09-26 stsp len = 0;
507 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
508 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
509 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
510 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
511 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
512 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
513 bfa12d5e 2020-09-26 stsp goto done;
514 bfa12d5e 2020-09-26 stsp }
515 3ce1b845 2019-07-15 stsp }
516 bfa12d5e 2020-09-26 stsp free(line);
517 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
518 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
519 bfa12d5e 2020-09-26 stsp goto done;
520 bfa12d5e 2020-09-26 stsp }
521 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
522 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
523 3ce1b845 2019-07-15 stsp len--;
524 3ce1b845 2019-07-15 stsp }
525 3ce1b845 2019-07-15 stsp
526 bfa12d5e 2020-09-26 stsp if (len == 0) {
527 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
528 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
529 bfa12d5e 2020-09-26 stsp goto done;
530 bfa12d5e 2020-09-26 stsp }
531 28cf319f 2021-01-28 stsp if (require_modification &&
532 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
533 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
534 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
535 3ce1b845 2019-07-15 stsp done:
536 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
537 bfa12d5e 2020-09-26 stsp free(buf);
538 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
539 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
540 3ce1b845 2019-07-15 stsp if (err) {
541 3ce1b845 2019-07-15 stsp free(*logmsg);
542 3ce1b845 2019-07-15 stsp *logmsg = NULL;
543 3ce1b845 2019-07-15 stsp }
544 3ce1b845 2019-07-15 stsp return err;
545 3ce1b845 2019-07-15 stsp }
546 3ce1b845 2019-07-15 stsp
547 3ce1b845 2019-07-15 stsp static const struct got_error *
548 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
549 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
550 3ce1b845 2019-07-15 stsp {
551 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
552 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
553 1601cb9f 2020-09-11 naddy int initial_content_len;
554 97972933 2020-09-11 stsp int fd = -1;
555 3ce1b845 2019-07-15 stsp
556 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
557 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
558 1601cb9f 2020-09-11 naddy branch_name);
559 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
560 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
561 3ce1b845 2019-07-15 stsp
562 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
563 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
564 3ce1b845 2019-07-15 stsp if (err)
565 3ce1b845 2019-07-15 stsp goto done;
566 3ce1b845 2019-07-15 stsp
567 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
568 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
569 97972933 2020-09-11 stsp goto done;
570 97972933 2020-09-11 stsp }
571 3ce1b845 2019-07-15 stsp
572 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
573 0d5bb276 2020-12-15 stsp initial_content_len, 1);
574 3ce1b845 2019-07-15 stsp done:
575 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
576 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
577 3ce1b845 2019-07-15 stsp free(initial_content);
578 59f86c76 2020-09-11 stsp if (err) {
579 59f86c76 2020-09-11 stsp free(*logmsg_path);
580 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
581 59f86c76 2020-09-11 stsp }
582 3ce1b845 2019-07-15 stsp return err;
583 3ce1b845 2019-07-15 stsp }
584 3ce1b845 2019-07-15 stsp
585 3ce1b845 2019-07-15 stsp static const struct got_error *
586 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
587 3ce1b845 2019-07-15 stsp {
588 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
589 3ce1b845 2019-07-15 stsp return NULL;
590 3ce1b845 2019-07-15 stsp }
591 3ce1b845 2019-07-15 stsp
592 3ce1b845 2019-07-15 stsp static const struct got_error *
593 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
594 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
595 84792843 2019-08-09 stsp {
596 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
597 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
598 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
599 84792843 2019-08-09 stsp
600 84792843 2019-08-09 stsp *author = NULL;
601 aba9c984 2019-09-08 stsp
602 50b0790e 2020-09-11 stsp if (worktree)
603 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
604 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
605 50b0790e 2020-09-11 stsp
606 50b0790e 2020-09-11 stsp /*
607 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
608 50b0790e 2020-09-11 stsp * significant to least significant:
609 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
610 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
611 50b0790e 2020-09-11 stsp * 3) repository's git config file
612 50b0790e 2020-09-11 stsp * 4) environment variables
613 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
614 50b0790e 2020-09-11 stsp */
615 50b0790e 2020-09-11 stsp
616 50b0790e 2020-09-11 stsp if (worktree_conf)
617 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
618 50b0790e 2020-09-11 stsp if (got_author == NULL)
619 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
620 84792843 2019-08-09 stsp if (got_author == NULL) {
621 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
622 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
623 c9956ddf 2019-09-08 stsp if (name && email) {
624 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
625 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
626 c9956ddf 2019-09-08 stsp return NULL;
627 c9956ddf 2019-09-08 stsp }
628 257add31 2020-09-09 stsp
629 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
630 257add31 2020-09-09 stsp if (got_author == NULL) {
631 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
632 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
633 257add31 2020-09-09 stsp repo);
634 257add31 2020-09-09 stsp if (name && email) {
635 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
636 257add31 2020-09-09 stsp == -1)
637 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
638 257add31 2020-09-09 stsp return NULL;
639 257add31 2020-09-09 stsp }
640 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
641 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
642 257add31 2020-09-09 stsp }
643 84792843 2019-08-09 stsp }
644 84792843 2019-08-09 stsp
645 aba9c984 2019-09-08 stsp *author = strdup(got_author);
646 aba9c984 2019-09-08 stsp if (*author == NULL)
647 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
648 84792843 2019-08-09 stsp
649 84792843 2019-08-09 stsp /*
650 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
651 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
652 84792843 2019-08-09 stsp */
653 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
654 84792843 2019-08-09 stsp got_author++;
655 aba9c984 2019-09-08 stsp if (*got_author != '<') {
656 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
657 aba9c984 2019-09-08 stsp goto done;
658 aba9c984 2019-09-08 stsp }
659 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
660 84792843 2019-08-09 stsp got_author++;
661 aba9c984 2019-09-08 stsp if (*got_author != '@') {
662 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
663 aba9c984 2019-09-08 stsp goto done;
664 aba9c984 2019-09-08 stsp }
665 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
666 84792843 2019-08-09 stsp got_author++;
667 84792843 2019-08-09 stsp if (*got_author != '>')
668 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
669 aba9c984 2019-09-08 stsp done:
670 aba9c984 2019-09-08 stsp if (err) {
671 aba9c984 2019-09-08 stsp free(*author);
672 aba9c984 2019-09-08 stsp *author = NULL;
673 aba9c984 2019-09-08 stsp }
674 aba9c984 2019-09-08 stsp return err;
675 c9956ddf 2019-09-08 stsp }
676 c9956ddf 2019-09-08 stsp
677 c9956ddf 2019-09-08 stsp static const struct got_error *
678 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
679 c9956ddf 2019-09-08 stsp {
680 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
681 c9956ddf 2019-09-08 stsp
682 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
683 c9956ddf 2019-09-08 stsp if (homedir) {
684 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
685 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
686 c9956ddf 2019-09-08 stsp
687 c9956ddf 2019-09-08 stsp }
688 c9956ddf 2019-09-08 stsp return NULL;
689 84792843 2019-08-09 stsp }
690 84792843 2019-08-09 stsp
691 84792843 2019-08-09 stsp static const struct got_error *
692 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
693 3ce1b845 2019-07-15 stsp {
694 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
695 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
696 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
697 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
698 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
699 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
700 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
701 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
702 3ce1b845 2019-07-15 stsp int ch;
703 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
704 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
705 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
706 3ce1b845 2019-07-15 stsp
707 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
708 3ce1b845 2019-07-15 stsp
709 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
710 3ce1b845 2019-07-15 stsp switch (ch) {
711 3ce1b845 2019-07-15 stsp case 'b':
712 3ce1b845 2019-07-15 stsp branch_name = optarg;
713 3ce1b845 2019-07-15 stsp break;
714 3ce1b845 2019-07-15 stsp case 'm':
715 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
716 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
717 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
718 3ce1b845 2019-07-15 stsp goto done;
719 3ce1b845 2019-07-15 stsp }
720 3ce1b845 2019-07-15 stsp break;
721 3ce1b845 2019-07-15 stsp case 'r':
722 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
723 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
724 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
725 9ba1d308 2019-10-21 stsp optarg);
726 3ce1b845 2019-07-15 stsp goto done;
727 3ce1b845 2019-07-15 stsp }
728 3ce1b845 2019-07-15 stsp break;
729 3ce1b845 2019-07-15 stsp case 'I':
730 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
731 3ce1b845 2019-07-15 stsp break;
732 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
733 3ce1b845 2019-07-15 stsp NULL);
734 3ce1b845 2019-07-15 stsp if (error)
735 3ce1b845 2019-07-15 stsp goto done;
736 3ce1b845 2019-07-15 stsp break;
737 3ce1b845 2019-07-15 stsp default:
738 b2b65d18 2019-08-22 stsp usage_import();
739 3ce1b845 2019-07-15 stsp /* NOTREACHED */
740 3ce1b845 2019-07-15 stsp }
741 3ce1b845 2019-07-15 stsp }
742 3ce1b845 2019-07-15 stsp
743 3ce1b845 2019-07-15 stsp argc -= optind;
744 3ce1b845 2019-07-15 stsp argv += optind;
745 3ce1b845 2019-07-15 stsp
746 3ce1b845 2019-07-15 stsp #ifndef PROFILE
747 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
748 aba9c984 2019-09-08 stsp "unveil",
749 3ce1b845 2019-07-15 stsp NULL) == -1)
750 3ce1b845 2019-07-15 stsp err(1, "pledge");
751 3ce1b845 2019-07-15 stsp #endif
752 3ce1b845 2019-07-15 stsp if (argc != 1)
753 3ce1b845 2019-07-15 stsp usage_import();
754 2c7829a4 2019-06-17 stsp
755 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
756 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
757 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
758 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
759 3ce1b845 2019-07-15 stsp }
760 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
761 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
762 c9956ddf 2019-09-08 stsp if (error)
763 c9956ddf 2019-09-08 stsp goto done;
764 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
765 3ce1b845 2019-07-15 stsp if (error)
766 3ce1b845 2019-07-15 stsp goto done;
767 aba9c984 2019-09-08 stsp
768 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
769 aba9c984 2019-09-08 stsp if (error)
770 aba9c984 2019-09-08 stsp return error;
771 e560b7e0 2019-11-28 stsp
772 e560b7e0 2019-11-28 stsp /*
773 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
774 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
775 e560b7e0 2019-11-28 stsp * an unintended typo.
776 e560b7e0 2019-11-28 stsp */
777 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
778 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
779 3ce1b845 2019-07-15 stsp
780 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
781 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
782 3ce1b845 2019-07-15 stsp goto done;
783 3ce1b845 2019-07-15 stsp }
784 3ce1b845 2019-07-15 stsp
785 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
786 3ce1b845 2019-07-15 stsp if (error) {
787 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
788 3ce1b845 2019-07-15 stsp goto done;
789 3ce1b845 2019-07-15 stsp } else {
790 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
791 3ce1b845 2019-07-15 stsp "import target branch already exists");
792 3ce1b845 2019-07-15 stsp goto done;
793 3ce1b845 2019-07-15 stsp }
794 3ce1b845 2019-07-15 stsp
795 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
796 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
797 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
798 3ce1b845 2019-07-15 stsp goto done;
799 3ce1b845 2019-07-15 stsp }
800 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
801 3ce1b845 2019-07-15 stsp
802 3ce1b845 2019-07-15 stsp /*
803 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
804 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
805 3ce1b845 2019-07-15 stsp */
806 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
807 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
808 3ce1b845 2019-07-15 stsp if (error)
809 3ce1b845 2019-07-15 stsp goto done;
810 8e158b01 2019-09-22 stsp free(logmsg);
811 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
812 ef293bdd 2019-10-21 stsp path_dir, refname);
813 ef293bdd 2019-10-21 stsp if (error) {
814 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
815 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
816 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
817 3ce1b845 2019-07-15 stsp goto done;
818 ef293bdd 2019-10-21 stsp }
819 3ce1b845 2019-07-15 stsp }
820 3ce1b845 2019-07-15 stsp
821 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
822 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
823 ef293bdd 2019-10-21 stsp if (logmsg_path)
824 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
825 3ce1b845 2019-07-15 stsp goto done;
826 ef293bdd 2019-10-21 stsp }
827 3ce1b845 2019-07-15 stsp
828 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
829 ef293bdd 2019-10-21 stsp if (error) {
830 ef293bdd 2019-10-21 stsp if (logmsg_path)
831 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
832 ef293bdd 2019-10-21 stsp goto done;
833 ef293bdd 2019-10-21 stsp }
834 ef293bdd 2019-10-21 stsp
835 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
836 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
837 ef293bdd 2019-10-21 stsp if (error) {
838 ef293bdd 2019-10-21 stsp if (logmsg_path)
839 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
840 3ce1b845 2019-07-15 stsp goto done;
841 ef293bdd 2019-10-21 stsp }
842 3ce1b845 2019-07-15 stsp
843 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
844 ef293bdd 2019-10-21 stsp if (error) {
845 ef293bdd 2019-10-21 stsp if (logmsg_path)
846 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
847 3ce1b845 2019-07-15 stsp goto done;
848 ef293bdd 2019-10-21 stsp }
849 3ce1b845 2019-07-15 stsp
850 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
851 ef293bdd 2019-10-21 stsp if (error) {
852 ef293bdd 2019-10-21 stsp if (logmsg_path)
853 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
854 3ce1b845 2019-07-15 stsp goto done;
855 ef293bdd 2019-10-21 stsp }
856 3ce1b845 2019-07-15 stsp
857 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
858 ef293bdd 2019-10-21 stsp if (error) {
859 ef293bdd 2019-10-21 stsp if (logmsg_path)
860 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
861 3ce1b845 2019-07-15 stsp goto done;
862 ef293bdd 2019-10-21 stsp }
863 3ce1b845 2019-07-15 stsp
864 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
865 3ce1b845 2019-07-15 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
867 ef293bdd 2019-10-21 stsp if (logmsg_path)
868 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
869 3ce1b845 2019-07-15 stsp goto done;
870 ef293bdd 2019-10-21 stsp }
871 3ce1b845 2019-07-15 stsp
872 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
873 3ce1b845 2019-07-15 stsp branch_ref);
874 ef293bdd 2019-10-21 stsp if (error) {
875 ef293bdd 2019-10-21 stsp if (logmsg_path)
876 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
877 3ce1b845 2019-07-15 stsp goto done;
878 ef293bdd 2019-10-21 stsp }
879 3ce1b845 2019-07-15 stsp
880 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
881 ef293bdd 2019-10-21 stsp if (error) {
882 ef293bdd 2019-10-21 stsp if (logmsg_path)
883 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
884 3ce1b845 2019-07-15 stsp goto done;
885 ef293bdd 2019-10-21 stsp }
886 3ce1b845 2019-07-15 stsp }
887 3ce1b845 2019-07-15 stsp
888 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
889 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
890 2c7829a4 2019-06-17 stsp done:
891 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
892 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
893 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
894 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
895 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
896 8e158b01 2019-09-22 stsp free(logmsg);
897 ef293bdd 2019-10-21 stsp free(logmsg_path);
898 2c7829a4 2019-06-17 stsp free(repo_path);
899 3ce1b845 2019-07-15 stsp free(editor);
900 3ce1b845 2019-07-15 stsp free(refname);
901 3ce1b845 2019-07-15 stsp free(new_commit_id);
902 3ce1b845 2019-07-15 stsp free(id_str);
903 aba9c984 2019-09-08 stsp free(author);
904 c9956ddf 2019-09-08 stsp free(gitconfig_path);
905 3ce1b845 2019-07-15 stsp if (branch_ref)
906 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
907 3ce1b845 2019-07-15 stsp if (head_ref)
908 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
909 2c7829a4 2019-06-17 stsp return error;
910 93658fb9 2020-03-18 stsp }
911 93658fb9 2020-03-18 stsp
912 93658fb9 2020-03-18 stsp __dead static void
913 93658fb9 2020-03-18 stsp usage_clone(void)
914 93658fb9 2020-03-18 stsp {
915 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
916 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
917 93658fb9 2020-03-18 stsp exit(1);
918 93658fb9 2020-03-18 stsp }
919 892ac3b6 2020-03-18 stsp
920 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
921 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
922 892ac3b6 2020-03-18 stsp int last_p_indexed;
923 892ac3b6 2020-03-18 stsp int last_p_resolved;
924 68999b92 2020-03-18 stsp int verbosity;
925 04d9a9ec 2020-09-24 stsp
926 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
927 04d9a9ec 2020-09-24 stsp
928 04d9a9ec 2020-09-24 stsp int create_configs;
929 04d9a9ec 2020-09-24 stsp int configs_created;
930 04d9a9ec 2020-09-24 stsp struct {
931 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
932 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
933 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
934 04d9a9ec 2020-09-24 stsp const char *proto;
935 04d9a9ec 2020-09-24 stsp const char *host;
936 04d9a9ec 2020-09-24 stsp const char *port;
937 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
938 04d9a9ec 2020-09-24 stsp const char *git_url;
939 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
940 04d9a9ec 2020-09-24 stsp int mirror_references;
941 04d9a9ec 2020-09-24 stsp } config_info;
942 892ac3b6 2020-03-18 stsp };
943 93658fb9 2020-03-18 stsp
944 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
945 93658fb9 2020-03-18 stsp static const struct got_error *
946 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
947 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
948 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
949 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
950 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
951 04d9a9ec 2020-09-24 stsp
952 04d9a9ec 2020-09-24 stsp static const struct got_error *
953 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
954 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
955 531c3985 2020-03-18 stsp {
956 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
957 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
958 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
959 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
960 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
961 04d9a9ec 2020-09-24 stsp
962 04d9a9ec 2020-09-24 stsp /*
963 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
964 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
965 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
966 04d9a9ec 2020-09-24 stsp * we have all required information.
967 04d9a9ec 2020-09-24 stsp */
968 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
969 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
970 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
971 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
972 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
973 62d463ca 2020-10-20 naddy a->config_info.git_url,
974 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
975 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
976 62d463ca 2020-10-20 naddy a->config_info.symrefs,
977 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
978 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
979 04d9a9ec 2020-09-24 stsp if (err)
980 04d9a9ec 2020-09-24 stsp return err;
981 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
982 04d9a9ec 2020-09-24 stsp }
983 b2409d58 2020-03-18 stsp
984 68999b92 2020-03-18 stsp if (a->verbosity < 0)
985 68999b92 2020-03-18 stsp return NULL;
986 68999b92 2020-03-18 stsp
987 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
988 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
989 892ac3b6 2020-03-18 stsp fflush(stdout);
990 12d1281e 2020-03-19 stsp return NULL;
991 b2409d58 2020-03-18 stsp }
992 b2409d58 2020-03-18 stsp
993 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
994 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
995 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
996 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
997 892ac3b6 2020-03-18 stsp print_size = 1;
998 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
999 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1000 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1001 892ac3b6 2020-03-18 stsp }
1002 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1003 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1004 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1005 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1006 892ac3b6 2020-03-18 stsp print_indexed = 1;
1007 892ac3b6 2020-03-18 stsp print_size = 1;
1008 892ac3b6 2020-03-18 stsp }
1009 61cc1a7a 2020-03-18 stsp }
1010 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1011 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1012 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1013 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1014 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1015 892ac3b6 2020-03-18 stsp print_resolved = 1;
1016 892ac3b6 2020-03-18 stsp print_indexed = 1;
1017 892ac3b6 2020-03-18 stsp print_size = 1;
1018 892ac3b6 2020-03-18 stsp }
1019 61cc1a7a 2020-03-18 stsp }
1020 3168e5da 2020-09-10 stsp
1021 d2cdc636 2020-03-18 stsp }
1022 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1023 892ac3b6 2020-03-18 stsp printf("\r");
1024 892ac3b6 2020-03-18 stsp if (print_size)
1025 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1026 d715f13e 2020-03-19 stsp if (print_indexed)
1027 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1028 d715f13e 2020-03-19 stsp if (print_resolved)
1029 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1030 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1031 892ac3b6 2020-03-18 stsp fflush(stdout);
1032 892ac3b6 2020-03-18 stsp
1033 531c3985 2020-03-18 stsp return NULL;
1034 531c3985 2020-03-18 stsp }
1035 531c3985 2020-03-18 stsp
1036 531c3985 2020-03-18 stsp static const struct got_error *
1037 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1038 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1039 4ba14133 2020-03-20 stsp {
1040 4ba14133 2020-03-20 stsp const struct got_error *err;
1041 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1042 4ba14133 2020-03-20 stsp
1043 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1044 4ba14133 2020-03-20 stsp if (err)
1045 4ba14133 2020-03-20 stsp return err;
1046 4ba14133 2020-03-20 stsp
1047 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1048 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1049 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1050 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1051 6338a6a1 2020-03-21 stsp }
1052 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1053 4ba14133 2020-03-20 stsp return err;
1054 4ba14133 2020-03-20 stsp }
1055 4ba14133 2020-03-20 stsp
1056 4ba14133 2020-03-20 stsp static const struct got_error *
1057 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1058 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1059 41b0de12 2020-03-21 stsp {
1060 41b0de12 2020-03-21 stsp const struct got_error *err;
1061 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1062 41b0de12 2020-03-21 stsp
1063 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1064 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1065 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1066 41b0de12 2020-03-21 stsp
1067 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1068 41b0de12 2020-03-21 stsp }
1069 41b0de12 2020-03-21 stsp
1070 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1071 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1072 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1073 41b0de12 2020-03-21 stsp char *id_str;
1074 41b0de12 2020-03-21 stsp
1075 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1076 41b0de12 2020-03-21 stsp if (err)
1077 41b0de12 2020-03-21 stsp return err;
1078 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1079 41b0de12 2020-03-21 stsp free(id_str);
1080 41b0de12 2020-03-21 stsp }
1081 41b0de12 2020-03-21 stsp
1082 41b0de12 2020-03-21 stsp return NULL;
1083 6338a6a1 2020-03-21 stsp }
1084 6338a6a1 2020-03-21 stsp
1085 6338a6a1 2020-03-21 stsp static const struct got_error *
1086 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1087 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1088 6338a6a1 2020-03-21 stsp {
1089 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1090 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1091 6338a6a1 2020-03-21 stsp char *id_str;
1092 6338a6a1 2020-03-21 stsp
1093 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1094 6338a6a1 2020-03-21 stsp if (err)
1095 6338a6a1 2020-03-21 stsp return err;
1096 6338a6a1 2020-03-21 stsp
1097 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1098 6338a6a1 2020-03-21 stsp if (err)
1099 6338a6a1 2020-03-21 stsp goto done;
1100 6338a6a1 2020-03-21 stsp
1101 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1102 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1103 6338a6a1 2020-03-21 stsp
1104 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1105 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1106 6338a6a1 2020-03-21 stsp done:
1107 6338a6a1 2020-03-21 stsp free(id_str);
1108 6338a6a1 2020-03-21 stsp return err;
1109 0e4002ca 2020-03-21 stsp }
1110 0e4002ca 2020-03-21 stsp
1111 0e4002ca 2020-03-21 stsp static int
1112 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1113 0e4002ca 2020-03-21 stsp {
1114 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1115 0e4002ca 2020-03-21 stsp return 0;
1116 0e4002ca 2020-03-21 stsp refname += 5;
1117 0e4002ca 2020-03-21 stsp
1118 0e4002ca 2020-03-21 stsp /*
1119 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1120 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1121 0e4002ca 2020-03-21 stsp */
1122 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1123 0e4002ca 2020-03-21 stsp return 0;
1124 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1125 0e4002ca 2020-03-21 stsp return 0;
1126 0e4002ca 2020-03-21 stsp
1127 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1128 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1129 0e4002ca 2020-03-21 stsp
1130 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1131 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1132 0e4002ca 2020-03-21 stsp return 1;
1133 0e4002ca 2020-03-21 stsp
1134 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1135 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1136 41b0de12 2020-03-21 stsp }
1137 41b0de12 2020-03-21 stsp
1138 0e4002ca 2020-03-21 stsp static int
1139 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1140 0e4002ca 2020-03-21 stsp {
1141 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1142 0e4002ca 2020-03-21 stsp
1143 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1144 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1145 0e4002ca 2020-03-21 stsp return 1;
1146 0e4002ca 2020-03-21 stsp }
1147 0e4002ca 2020-03-21 stsp
1148 0e4002ca 2020-03-21 stsp return 0;
1149 0e4002ca 2020-03-21 stsp }
1150 0e4002ca 2020-03-21 stsp
1151 41b0de12 2020-03-21 stsp static const struct got_error *
1152 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1153 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1154 0e4002ca 2020-03-21 stsp {
1155 0e4002ca 2020-03-21 stsp const struct got_error *err;
1156 0e4002ca 2020-03-21 stsp char *remote_refname;
1157 0e4002ca 2020-03-21 stsp
1158 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1159 0e4002ca 2020-03-21 stsp refname += 5;
1160 0e4002ca 2020-03-21 stsp
1161 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1162 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1163 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1164 0e4002ca 2020-03-21 stsp
1165 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1166 0e4002ca 2020-03-21 stsp free(remote_refname);
1167 7c0b7f42 2020-09-24 stsp return err;
1168 7c0b7f42 2020-09-24 stsp }
1169 7c0b7f42 2020-09-24 stsp
1170 7c0b7f42 2020-09-24 stsp static const struct got_error *
1171 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1172 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1173 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1174 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1175 99495ddb 2021-01-10 stsp struct got_repository *repo)
1176 7c0b7f42 2020-09-24 stsp {
1177 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1178 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1179 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1180 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1181 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1182 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1183 7c0b7f42 2020-09-24 stsp ssize_t n;
1184 7c0b7f42 2020-09-24 stsp
1185 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1186 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1187 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1188 132af4a5 2021-01-05 stsp char *s;
1189 132af4a5 2021-01-05 stsp branchname = pe->path;
1190 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1191 132af4a5 2021-01-05 stsp branchname += 11;
1192 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1193 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1194 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1195 132af4a5 2021-01-05 stsp goto done;
1196 132af4a5 2021-01-05 stsp }
1197 132af4a5 2021-01-05 stsp free(branches);
1198 132af4a5 2021-01-05 stsp branches = s;
1199 132af4a5 2021-01-05 stsp }
1200 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1201 15d3c221 2021-01-05 stsp branchname = default_branch;
1202 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1203 15d3c221 2021-01-05 stsp branchname += 11;
1204 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1205 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1206 132af4a5 2021-01-05 stsp goto done;
1207 99495ddb 2021-01-10 stsp }
1208 99495ddb 2021-01-10 stsp }
1209 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1210 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1211 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1212 99495ddb 2021-01-10 stsp char *s;
1213 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1214 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1215 99495ddb 2021-01-10 stsp branchname += 5;
1216 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1217 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1218 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1219 99495ddb 2021-01-10 stsp goto done;
1220 99495ddb 2021-01-10 stsp }
1221 99495ddb 2021-01-10 stsp free(refs);
1222 99495ddb 2021-01-10 stsp refs = s;
1223 132af4a5 2021-01-05 stsp }
1224 15d3c221 2021-01-05 stsp }
1225 15d3c221 2021-01-05 stsp
1226 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1227 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1228 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1229 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1230 7c0b7f42 2020-09-24 stsp goto done;
1231 7c0b7f42 2020-09-24 stsp }
1232 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1233 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1234 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1235 7c0b7f42 2020-09-24 stsp goto done;
1236 7c0b7f42 2020-09-24 stsp }
1237 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1238 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1239 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1240 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1241 7c0b7f42 2020-09-24 stsp "%s%s%s"
1242 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1243 15d3c221 2021-01-05 stsp "%s%s%s"
1244 99495ddb 2021-01-10 stsp "%s%s%s"
1245 7c0b7f42 2020-09-24 stsp "%s"
1246 0c8b29c5 2021-01-05 stsp "%s"
1247 7c0b7f42 2020-09-24 stsp "}\n",
1248 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1249 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1250 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1251 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1252 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1253 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1254 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1255 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1256 7c0b7f42 2020-09-24 stsp goto done;
1257 7c0b7f42 2020-09-24 stsp }
1258 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1259 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1260 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1261 7c0b7f42 2020-09-24 stsp goto done;
1262 7c0b7f42 2020-09-24 stsp }
1263 7c0b7f42 2020-09-24 stsp
1264 7c0b7f42 2020-09-24 stsp done:
1265 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1266 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1267 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1268 132af4a5 2021-01-05 stsp free(branches);
1269 7c0b7f42 2020-09-24 stsp return err;
1270 7c0b7f42 2020-09-24 stsp }
1271 7c0b7f42 2020-09-24 stsp
1272 7c0b7f42 2020-09-24 stsp static const struct got_error *
1273 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1274 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1275 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1276 99495ddb 2021-01-10 stsp struct got_repository *repo)
1277 7c0b7f42 2020-09-24 stsp {
1278 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1279 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1280 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1281 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1282 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1283 56d0a753 2021-01-20 stsp const char *branchname;
1284 7c0b7f42 2020-09-24 stsp ssize_t n;
1285 7c0b7f42 2020-09-24 stsp
1286 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1287 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1288 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1289 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1290 7c0b7f42 2020-09-24 stsp goto done;
1291 7c0b7f42 2020-09-24 stsp }
1292 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1293 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1294 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1295 7c0b7f42 2020-09-24 stsp goto done;
1296 7c0b7f42 2020-09-24 stsp }
1297 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1298 56d0a753 2021-01-20 stsp if (mirror_references) {
1299 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1300 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1301 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1302 56d0a753 2021-01-20 stsp goto done;
1303 56d0a753 2021-01-20 stsp }
1304 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1305 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1306 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1307 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1308 7c0b7f42 2020-09-24 stsp goto done;
1309 132af4a5 2021-01-05 stsp }
1310 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1311 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1312 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1313 132af4a5 2021-01-05 stsp char *s;
1314 132af4a5 2021-01-05 stsp branchname = pe->path;
1315 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1316 132af4a5 2021-01-05 stsp branchname += 11;
1317 56d0a753 2021-01-20 stsp if (mirror_references) {
1318 56d0a753 2021-01-20 stsp if (asprintf(&s,
1319 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1320 56d0a753 2021-01-20 stsp branches ? branches : "",
1321 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1322 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1323 56d0a753 2021-01-20 stsp goto done;
1324 56d0a753 2021-01-20 stsp }
1325 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1326 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1327 132af4a5 2021-01-05 stsp branches ? branches : "",
1328 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1329 132af4a5 2021-01-05 stsp branchname) == -1) {
1330 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1331 132af4a5 2021-01-05 stsp goto done;
1332 132af4a5 2021-01-05 stsp }
1333 132af4a5 2021-01-05 stsp free(branches);
1334 132af4a5 2021-01-05 stsp branches = s;
1335 7c0b7f42 2020-09-24 stsp }
1336 7c0b7f42 2020-09-24 stsp } else {
1337 7c0b7f42 2020-09-24 stsp /*
1338 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1339 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1340 7c0b7f42 2020-09-24 stsp */
1341 04d9a9ec 2020-09-24 stsp if (default_branch) {
1342 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1343 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1344 7c0b7f42 2020-09-24 stsp branchname += 11;
1345 7c0b7f42 2020-09-24 stsp } else
1346 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1347 56d0a753 2021-01-20 stsp if (mirror_references) {
1348 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1349 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1350 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1351 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1352 56d0a753 2021-01-20 stsp goto done;
1353 56d0a753 2021-01-20 stsp }
1354 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1355 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1356 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1357 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1358 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1359 7c0b7f42 2020-09-24 stsp goto done;
1360 99495ddb 2021-01-10 stsp }
1361 99495ddb 2021-01-10 stsp }
1362 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1363 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1364 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1365 99495ddb 2021-01-10 stsp char *s;
1366 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1367 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1368 99495ddb 2021-01-10 stsp refname += 5;
1369 56d0a753 2021-01-20 stsp if (mirror_references) {
1370 56d0a753 2021-01-20 stsp if (asprintf(&s,
1371 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1372 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1373 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1374 56d0a753 2021-01-20 stsp goto done;
1375 56d0a753 2021-01-20 stsp }
1376 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1377 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1378 99495ddb 2021-01-10 stsp refs ? refs : "",
1379 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1380 99495ddb 2021-01-10 stsp refname) == -1) {
1381 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1382 99495ddb 2021-01-10 stsp goto done;
1383 99495ddb 2021-01-10 stsp }
1384 99495ddb 2021-01-10 stsp free(refs);
1385 99495ddb 2021-01-10 stsp refs = s;
1386 7c0b7f42 2020-09-24 stsp }
1387 132af4a5 2021-01-05 stsp }
1388 99495ddb 2021-01-10 stsp
1389 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1390 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1391 132af4a5 2021-01-05 stsp "\turl = %s\n"
1392 132af4a5 2021-01-05 stsp "%s"
1393 99495ddb 2021-01-10 stsp "%s"
1394 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1395 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1396 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1397 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1398 132af4a5 2021-01-05 stsp goto done;
1399 7c0b7f42 2020-09-24 stsp }
1400 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1401 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1402 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1403 7c0b7f42 2020-09-24 stsp goto done;
1404 7c0b7f42 2020-09-24 stsp }
1405 7c0b7f42 2020-09-24 stsp done:
1406 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1407 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1408 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1409 132af4a5 2021-01-05 stsp free(branches);
1410 0e4002ca 2020-03-21 stsp return err;
1411 0e4002ca 2020-03-21 stsp }
1412 0e4002ca 2020-03-21 stsp
1413 0e4002ca 2020-03-21 stsp static const struct got_error *
1414 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1415 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1416 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1417 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1418 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1419 04d9a9ec 2020-09-24 stsp {
1420 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1421 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1422 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1423 04d9a9ec 2020-09-24 stsp
1424 04d9a9ec 2020-09-24 stsp /*
1425 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1426 04d9a9ec 2020-09-24 stsp * one of those.
1427 04d9a9ec 2020-09-24 stsp */
1428 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1429 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1430 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1431 04d9a9ec 2020-09-24 stsp } else {
1432 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1433 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1434 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1435 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1436 04d9a9ec 2020-09-24 stsp
1437 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1438 04d9a9ec 2020-09-24 stsp continue;
1439 04d9a9ec 2020-09-24 stsp
1440 04d9a9ec 2020-09-24 stsp default_branch = target;
1441 04d9a9ec 2020-09-24 stsp break;
1442 04d9a9ec 2020-09-24 stsp }
1443 04d9a9ec 2020-09-24 stsp }
1444 04d9a9ec 2020-09-24 stsp
1445 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1446 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1447 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1448 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1449 04d9a9ec 2020-09-24 stsp if (err)
1450 04d9a9ec 2020-09-24 stsp return err;
1451 04d9a9ec 2020-09-24 stsp
1452 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1453 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1454 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1455 04d9a9ec 2020-09-24 stsp }
1456 04d9a9ec 2020-09-24 stsp
1457 04d9a9ec 2020-09-24 stsp static const struct got_error *
1458 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1459 93658fb9 2020-03-18 stsp {
1460 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1461 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1462 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1463 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1464 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1465 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1466 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1467 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1468 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1469 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1470 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1471 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1472 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1473 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1474 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1475 93658fb9 2020-03-18 stsp
1476 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1477 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1478 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1479 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1480 d9b4d0c0 2020-03-18 stsp
1481 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1482 93658fb9 2020-03-18 stsp switch (ch) {
1483 659e7fbd 2020-03-20 stsp case 'a':
1484 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1485 4ba14133 2020-03-20 stsp break;
1486 4ba14133 2020-03-20 stsp case 'b':
1487 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1488 4ba14133 2020-03-20 stsp optarg, NULL);
1489 4ba14133 2020-03-20 stsp if (error)
1490 4ba14133 2020-03-20 stsp return error;
1491 659e7fbd 2020-03-20 stsp break;
1492 41b0de12 2020-03-21 stsp case 'l':
1493 41b0de12 2020-03-21 stsp list_refs_only = 1;
1494 41b0de12 2020-03-21 stsp break;
1495 469dd726 2020-03-20 stsp case 'm':
1496 469dd726 2020-03-20 stsp mirror_references = 1;
1497 469dd726 2020-03-20 stsp break;
1498 68999b92 2020-03-18 stsp case 'v':
1499 68999b92 2020-03-18 stsp if (verbosity < 0)
1500 68999b92 2020-03-18 stsp verbosity = 0;
1501 68999b92 2020-03-18 stsp else if (verbosity < 3)
1502 68999b92 2020-03-18 stsp verbosity++;
1503 68999b92 2020-03-18 stsp break;
1504 68999b92 2020-03-18 stsp case 'q':
1505 68999b92 2020-03-18 stsp verbosity = -1;
1506 68999b92 2020-03-18 stsp break;
1507 0e4002ca 2020-03-21 stsp case 'R':
1508 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1509 0e4002ca 2020-03-21 stsp optarg, NULL);
1510 0e4002ca 2020-03-21 stsp if (error)
1511 0e4002ca 2020-03-21 stsp return error;
1512 0e4002ca 2020-03-21 stsp break;
1513 93658fb9 2020-03-18 stsp default:
1514 93658fb9 2020-03-18 stsp usage_clone();
1515 93658fb9 2020-03-18 stsp break;
1516 93658fb9 2020-03-18 stsp }
1517 93658fb9 2020-03-18 stsp }
1518 93658fb9 2020-03-18 stsp argc -= optind;
1519 93658fb9 2020-03-18 stsp argv += optind;
1520 39c64a6a 2020-03-18 stsp
1521 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1522 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1523 41b0de12 2020-03-21 stsp if (list_refs_only) {
1524 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1525 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1526 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1527 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1528 41b0de12 2020-03-21 stsp if (mirror_references)
1529 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1530 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1531 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1532 41b0de12 2020-03-21 stsp }
1533 4ba14133 2020-03-20 stsp
1534 93658fb9 2020-03-18 stsp uri = argv[0];
1535 9df6f38b 2020-03-18 stsp
1536 9df6f38b 2020-03-18 stsp if (argc == 1)
1537 93658fb9 2020-03-18 stsp dirname = NULL;
1538 9df6f38b 2020-03-18 stsp else if (argc == 2)
1539 93658fb9 2020-03-18 stsp dirname = argv[1];
1540 93658fb9 2020-03-18 stsp else
1541 93658fb9 2020-03-18 stsp usage_clone();
1542 09838ffc 2020-03-18 stsp
1543 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1544 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1545 39c64a6a 2020-03-18 stsp if (error)
1546 09f63084 2020-03-20 stsp goto done;
1547 09f63084 2020-03-20 stsp
1548 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1549 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1550 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1551 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1552 09838ffc 2020-03-18 stsp goto done;
1553 09f63084 2020-03-20 stsp }
1554 09838ffc 2020-03-18 stsp
1555 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1556 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1557 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1558 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1559 39c64a6a 2020-03-18 stsp err(1, "pledge");
1560 b46f3e71 2020-03-18 stsp #endif
1561 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1562 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1563 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1564 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1566 39c64a6a 2020-03-18 stsp err(1, "pledge");
1567 b46f3e71 2020-03-18 stsp #endif
1568 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1569 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1570 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1571 39c64a6a 2020-03-18 stsp goto done;
1572 39c64a6a 2020-03-18 stsp } else {
1573 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1574 39c64a6a 2020-03-18 stsp goto done;
1575 39c64a6a 2020-03-18 stsp }
1576 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1577 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1578 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1579 bb64b798 2020-03-18 stsp goto done;
1580 bb64b798 2020-03-18 stsp }
1581 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1582 bb64b798 2020-03-18 stsp } else
1583 bb64b798 2020-03-18 stsp repo_path = dirname;
1584 bb64b798 2020-03-18 stsp
1585 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1586 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1587 2751fe64 2020-09-24 stsp if (error &&
1588 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1589 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1590 41b0de12 2020-03-21 stsp goto done;
1591 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1592 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1593 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1594 41b0de12 2020-03-21 stsp goto done;
1595 2751fe64 2020-09-24 stsp }
1596 41b0de12 2020-03-21 stsp }
1597 bb64b798 2020-03-18 stsp
1598 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1599 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1600 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1601 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1602 ee448f5f 2020-03-18 stsp goto done;
1603 ee448f5f 2020-03-18 stsp }
1604 ee448f5f 2020-03-18 stsp }
1605 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1606 ee448f5f 2020-03-18 stsp if (error)
1607 ee448f5f 2020-03-18 stsp goto done;
1608 f79e6490 2020-04-19 stsp
1609 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1610 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1611 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1612 ee448f5f 2020-03-18 stsp
1613 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1614 9c52365f 2020-03-21 stsp server_path, verbosity);
1615 39c64a6a 2020-03-18 stsp if (error)
1616 bb64b798 2020-03-18 stsp goto done;
1617 bb64b798 2020-03-18 stsp
1618 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1619 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1620 2751fe64 2020-09-24 stsp if (error)
1621 2751fe64 2020-09-24 stsp goto done;
1622 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1623 2751fe64 2020-09-24 stsp if (error)
1624 2751fe64 2020-09-24 stsp goto done;
1625 2751fe64 2020-09-24 stsp }
1626 2751fe64 2020-09-24 stsp
1627 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1628 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1629 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1630 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1631 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1632 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1633 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1634 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1635 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1636 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1637 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1644 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1645 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1646 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1647 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1648 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1649 39c64a6a 2020-03-18 stsp if (error)
1650 d9b4d0c0 2020-03-18 stsp goto done;
1651 d9b4d0c0 2020-03-18 stsp
1652 41b0de12 2020-03-21 stsp if (list_refs_only) {
1653 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1654 41b0de12 2020-03-21 stsp goto done;
1655 41b0de12 2020-03-21 stsp }
1656 41b0de12 2020-03-21 stsp
1657 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1658 39c64a6a 2020-03-18 stsp if (error)
1659 d9b4d0c0 2020-03-18 stsp goto done;
1660 68999b92 2020-03-18 stsp if (verbosity >= 0)
1661 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1662 d9b4d0c0 2020-03-18 stsp free(id_str);
1663 d9b4d0c0 2020-03-18 stsp
1664 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1665 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1666 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1667 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1668 7ebc0570 2020-03-18 stsp char *remote_refname;
1669 0e4002ca 2020-03-21 stsp
1670 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1671 0e4002ca 2020-03-21 stsp !mirror_references) {
1672 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1673 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1674 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1675 0e4002ca 2020-03-21 stsp if (error)
1676 0e4002ca 2020-03-21 stsp goto done;
1677 0e4002ca 2020-03-21 stsp continue;
1678 0e4002ca 2020-03-21 stsp }
1679 668a20f6 2020-03-18 stsp
1680 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1681 39c64a6a 2020-03-18 stsp if (error)
1682 d9b4d0c0 2020-03-18 stsp goto done;
1683 d9b4d0c0 2020-03-18 stsp
1684 469dd726 2020-03-20 stsp if (mirror_references)
1685 469dd726 2020-03-20 stsp continue;
1686 469dd726 2020-03-20 stsp
1687 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1688 7ebc0570 2020-03-18 stsp continue;
1689 7ebc0570 2020-03-18 stsp
1690 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1691 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1692 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1693 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1694 7ebc0570 2020-03-18 stsp goto done;
1695 7ebc0570 2020-03-18 stsp }
1696 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1697 f298ae0f 2020-03-25 stsp free(remote_refname);
1698 39c64a6a 2020-03-18 stsp if (error)
1699 d9b4d0c0 2020-03-18 stsp goto done;
1700 d9b4d0c0 2020-03-18 stsp }
1701 d9b4d0c0 2020-03-18 stsp
1702 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1703 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1704 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1705 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1706 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1707 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1708 d9b4d0c0 2020-03-18 stsp
1709 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1710 d9b4d0c0 2020-03-18 stsp continue;
1711 d9b4d0c0 2020-03-18 stsp
1712 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1713 39c64a6a 2020-03-18 stsp if (error) {
1714 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1715 55330abe 2020-03-20 stsp error = NULL;
1716 d9b4d0c0 2020-03-18 stsp continue;
1717 55330abe 2020-03-20 stsp }
1718 d9b4d0c0 2020-03-18 stsp goto done;
1719 d9b4d0c0 2020-03-18 stsp }
1720 d9b4d0c0 2020-03-18 stsp
1721 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1722 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1723 f298ae0f 2020-03-25 stsp if (error)
1724 f298ae0f 2020-03-25 stsp goto done;
1725 f298ae0f 2020-03-25 stsp
1726 f298ae0f 2020-03-25 stsp if (mirror_references)
1727 f298ae0f 2020-03-25 stsp continue;
1728 f298ae0f 2020-03-25 stsp
1729 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1730 f298ae0f 2020-03-25 stsp continue;
1731 f298ae0f 2020-03-25 stsp
1732 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1733 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1734 f298ae0f 2020-03-25 stsp refname) == -1) {
1735 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1736 f298ae0f 2020-03-25 stsp goto done;
1737 f298ae0f 2020-03-25 stsp }
1738 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1739 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1740 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1741 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1742 f298ae0f 2020-03-25 stsp free(remote_refname);
1743 f298ae0f 2020-03-25 stsp goto done;
1744 f298ae0f 2020-03-25 stsp }
1745 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1746 f298ae0f 2020-03-25 stsp if (error) {
1747 f298ae0f 2020-03-25 stsp free(remote_refname);
1748 f298ae0f 2020-03-25 stsp free(remote_target);
1749 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1750 f298ae0f 2020-03-25 stsp error = NULL;
1751 f298ae0f 2020-03-25 stsp continue;
1752 f298ae0f 2020-03-25 stsp }
1753 f298ae0f 2020-03-25 stsp goto done;
1754 f298ae0f 2020-03-25 stsp }
1755 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1756 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1757 f298ae0f 2020-03-25 stsp free(remote_refname);
1758 f298ae0f 2020-03-25 stsp free(remote_target);
1759 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1760 39c64a6a 2020-03-18 stsp if (error)
1761 d9b4d0c0 2020-03-18 stsp goto done;
1762 4ba14133 2020-03-20 stsp }
1763 4ba14133 2020-03-20 stsp if (pe == NULL) {
1764 4ba14133 2020-03-20 stsp /*
1765 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1766 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1767 4ba14133 2020-03-20 stsp * which could be fetched instead.
1768 4ba14133 2020-03-20 stsp */
1769 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1770 4ba14133 2020-03-20 stsp const char *target = pe->path;
1771 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1772 d9b4d0c0 2020-03-18 stsp
1773 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1774 4ba14133 2020-03-20 stsp if (error) {
1775 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1776 4ba14133 2020-03-20 stsp error = NULL;
1777 4ba14133 2020-03-20 stsp continue;
1778 4ba14133 2020-03-20 stsp }
1779 4ba14133 2020-03-20 stsp goto done;
1780 4ba14133 2020-03-20 stsp }
1781 4ba14133 2020-03-20 stsp
1782 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1783 04d9a9ec 2020-09-24 stsp verbosity, repo);
1784 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1785 4ba14133 2020-03-20 stsp if (error)
1786 4ba14133 2020-03-20 stsp goto done;
1787 4ba14133 2020-03-20 stsp break;
1788 4ba14133 2020-03-20 stsp }
1789 659e7fbd 2020-03-20 stsp }
1790 659e7fbd 2020-03-20 stsp
1791 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1792 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1793 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1794 09838ffc 2020-03-18 stsp done:
1795 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1796 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1797 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1798 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1799 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1800 9c52365f 2020-03-21 stsp }
1801 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1802 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1803 1d0f4054 2021-06-17 stsp if (repo) {
1804 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1805 1d0f4054 2021-06-17 stsp if (error == NULL)
1806 1d0f4054 2021-06-17 stsp error = close_err;
1807 1d0f4054 2021-06-17 stsp }
1808 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1809 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1810 d9b4d0c0 2020-03-18 stsp free(pe->data);
1811 d9b4d0c0 2020-03-18 stsp }
1812 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1813 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1814 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1815 d9b4d0c0 2020-03-18 stsp free(pe->data);
1816 d9b4d0c0 2020-03-18 stsp }
1817 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1818 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1819 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1820 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1821 09838ffc 2020-03-18 stsp free(proto);
1822 09838ffc 2020-03-18 stsp free(host);
1823 09838ffc 2020-03-18 stsp free(port);
1824 09838ffc 2020-03-18 stsp free(server_path);
1825 09838ffc 2020-03-18 stsp free(repo_name);
1826 bb64b798 2020-03-18 stsp free(default_destdir);
1827 b46f3e71 2020-03-18 stsp free(git_url);
1828 39c64a6a 2020-03-18 stsp return error;
1829 7848a0e1 2020-03-19 stsp }
1830 7848a0e1 2020-03-19 stsp
1831 7848a0e1 2020-03-19 stsp static const struct got_error *
1832 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1833 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1834 7848a0e1 2020-03-19 stsp {
1835 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1836 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1837 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1838 7848a0e1 2020-03-19 stsp
1839 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1840 7848a0e1 2020-03-19 stsp if (err)
1841 7848a0e1 2020-03-19 stsp goto done;
1842 7848a0e1 2020-03-19 stsp
1843 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1844 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1845 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1846 88609724 2020-03-21 stsp if (err)
1847 88609724 2020-03-21 stsp goto done;
1848 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1849 88609724 2020-03-21 stsp goto done;
1850 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1851 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1852 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1853 db6d8ad8 2020-03-21 stsp }
1854 db6d8ad8 2020-03-21 stsp goto done;
1855 db6d8ad8 2020-03-21 stsp }
1856 db6d8ad8 2020-03-21 stsp
1857 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1858 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1859 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1860 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1861 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1862 688f11b3 2020-03-21 stsp }
1863 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1864 7848a0e1 2020-03-19 stsp if (err)
1865 7848a0e1 2020-03-19 stsp goto done;
1866 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1867 e8a967e0 2020-03-21 stsp if (err)
1868 e8a967e0 2020-03-21 stsp goto done;
1869 7848a0e1 2020-03-19 stsp } else {
1870 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1871 7848a0e1 2020-03-19 stsp if (err)
1872 7848a0e1 2020-03-19 stsp goto done;
1873 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1874 6338a6a1 2020-03-21 stsp goto done;
1875 6338a6a1 2020-03-21 stsp
1876 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1877 6338a6a1 2020-03-21 stsp if (err)
1878 6338a6a1 2020-03-21 stsp goto done;
1879 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1880 6338a6a1 2020-03-21 stsp if (err)
1881 6338a6a1 2020-03-21 stsp goto done;
1882 7848a0e1 2020-03-19 stsp }
1883 6338a6a1 2020-03-21 stsp
1884 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1885 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1886 6338a6a1 2020-03-21 stsp new_id_str);
1887 7848a0e1 2020-03-19 stsp done:
1888 7848a0e1 2020-03-19 stsp free(old_id);
1889 7848a0e1 2020-03-19 stsp free(new_id_str);
1890 7848a0e1 2020-03-19 stsp return err;
1891 2ab43947 2020-03-18 stsp }
1892 f1bcca34 2020-03-25 stsp
1893 f1bcca34 2020-03-25 stsp static const struct got_error *
1894 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1895 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1896 f1bcca34 2020-03-25 stsp {
1897 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1898 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1899 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1900 f1bcca34 2020-03-25 stsp
1901 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1902 bcf34b0e 2020-03-26 stsp if (err) {
1903 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1904 bcf34b0e 2020-03-26 stsp return err;
1905 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1906 bcf34b0e 2020-03-26 stsp if (err)
1907 bcf34b0e 2020-03-26 stsp goto done;
1908 2ab43947 2020-03-18 stsp
1909 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1910 bcf34b0e 2020-03-26 stsp if (err)
1911 bcf34b0e 2020-03-26 stsp goto done;
1912 f1bcca34 2020-03-25 stsp
1913 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1914 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1915 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1916 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1917 bcf34b0e 2020-03-26 stsp } else {
1918 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1919 f1bcca34 2020-03-25 stsp
1920 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1921 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1922 bcf34b0e 2020-03-26 stsp goto done;
1923 bcf34b0e 2020-03-26 stsp
1924 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1925 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1926 bcf34b0e 2020-03-26 stsp if (err)
1927 bcf34b0e 2020-03-26 stsp goto done;
1928 bcf34b0e 2020-03-26 stsp
1929 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1930 bcf34b0e 2020-03-26 stsp if (err)
1931 bcf34b0e 2020-03-26 stsp goto done;
1932 bcf34b0e 2020-03-26 stsp
1933 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1934 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1935 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1936 bcf34b0e 2020-03-26 stsp
1937 bcf34b0e 2020-03-26 stsp }
1938 f1bcca34 2020-03-25 stsp done:
1939 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1940 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1941 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1942 bcf34b0e 2020-03-26 stsp err = unlock_err;
1943 bcf34b0e 2020-03-26 stsp }
1944 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1945 f1bcca34 2020-03-25 stsp return err;
1946 f1bcca34 2020-03-25 stsp }
1947 f1bcca34 2020-03-25 stsp
1948 2ab43947 2020-03-18 stsp __dead static void
1949 7848a0e1 2020-03-19 stsp usage_fetch(void)
1950 7848a0e1 2020-03-19 stsp {
1951 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1952 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1953 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1954 13f12b09 2020-03-21 stsp getprogname());
1955 7848a0e1 2020-03-19 stsp exit(1);
1956 7848a0e1 2020-03-19 stsp }
1957 7848a0e1 2020-03-19 stsp
1958 7848a0e1 2020-03-19 stsp static const struct got_error *
1959 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1960 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1961 f21ec2f0 2020-03-21 stsp {
1962 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1963 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1964 3789fd73 2020-03-26 stsp char *id_str = NULL;
1965 3789fd73 2020-03-26 stsp
1966 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1967 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1968 3789fd73 2020-03-26 stsp if (err)
1969 3789fd73 2020-03-26 stsp return err;
1970 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1971 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1972 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1973 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1974 3789fd73 2020-03-26 stsp }
1975 3789fd73 2020-03-26 stsp } else {
1976 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1977 3789fd73 2020-03-26 stsp if (err)
1978 3789fd73 2020-03-26 stsp return err;
1979 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1980 3789fd73 2020-03-26 stsp if (err)
1981 3789fd73 2020-03-26 stsp goto done;
1982 3168e5da 2020-09-10 stsp
1983 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1984 3789fd73 2020-03-26 stsp if (err)
1985 3789fd73 2020-03-26 stsp goto done;
1986 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1987 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1988 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1989 3789fd73 2020-03-26 stsp }
1990 3789fd73 2020-03-26 stsp }
1991 3789fd73 2020-03-26 stsp done:
1992 3789fd73 2020-03-26 stsp free(id);
1993 3789fd73 2020-03-26 stsp free(id_str);
1994 3789fd73 2020-03-26 stsp return NULL;
1995 3789fd73 2020-03-26 stsp }
1996 3789fd73 2020-03-26 stsp
1997 3789fd73 2020-03-26 stsp static const struct got_error *
1998 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1999 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2000 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2001 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2002 3789fd73 2020-03-26 stsp {
2003 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2004 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2005 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2006 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2007 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2008 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2009 f21ec2f0 2020-03-21 stsp
2010 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2011 f21ec2f0 2020-03-21 stsp
2012 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2013 3789fd73 2020-03-26 stsp == -1)
2014 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2015 3789fd73 2020-03-26 stsp
2016 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2017 f21ec2f0 2020-03-21 stsp if (err)
2018 3789fd73 2020-03-26 stsp goto done;
2019 f21ec2f0 2020-03-21 stsp
2020 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2021 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2022 f21ec2f0 2020-03-21 stsp
2023 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2024 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2025 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2026 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2027 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2028 3789fd73 2020-03-26 stsp continue;
2029 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2030 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2031 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2032 3789fd73 2020-03-26 stsp goto done;
2033 3789fd73 2020-03-26 stsp }
2034 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2035 3789fd73 2020-03-26 stsp continue;
2036 3789fd73 2020-03-26 stsp }
2037 f21ec2f0 2020-03-21 stsp
2038 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2039 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2040 f21ec2f0 2020-03-21 stsp break;
2041 f21ec2f0 2020-03-21 stsp }
2042 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2043 f21ec2f0 2020-03-21 stsp continue;
2044 f21ec2f0 2020-03-21 stsp
2045 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2046 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2047 3789fd73 2020-03-26 stsp break;
2048 3789fd73 2020-03-26 stsp }
2049 3789fd73 2020-03-26 stsp if (pe != NULL)
2050 3789fd73 2020-03-26 stsp continue;
2051 f21ec2f0 2020-03-21 stsp
2052 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2053 f21ec2f0 2020-03-21 stsp if (err)
2054 f21ec2f0 2020-03-21 stsp break;
2055 3789fd73 2020-03-26 stsp
2056 3789fd73 2020-03-26 stsp if (local_refname) {
2057 3789fd73 2020-03-26 stsp struct got_reference *ref;
2058 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2059 3789fd73 2020-03-26 stsp if (err) {
2060 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2061 3789fd73 2020-03-26 stsp break;
2062 3789fd73 2020-03-26 stsp free(local_refname);
2063 3789fd73 2020-03-26 stsp local_refname = NULL;
2064 3789fd73 2020-03-26 stsp continue;
2065 3789fd73 2020-03-26 stsp }
2066 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2067 3789fd73 2020-03-26 stsp if (err)
2068 3789fd73 2020-03-26 stsp break;
2069 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2070 3789fd73 2020-03-26 stsp got_ref_close(ref);
2071 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2072 3789fd73 2020-03-26 stsp err = unlock_err;
2073 3789fd73 2020-03-26 stsp break;
2074 3789fd73 2020-03-26 stsp }
2075 3789fd73 2020-03-26 stsp
2076 3789fd73 2020-03-26 stsp free(local_refname);
2077 3789fd73 2020-03-26 stsp local_refname = NULL;
2078 6338a6a1 2020-03-21 stsp }
2079 f21ec2f0 2020-03-21 stsp }
2080 3789fd73 2020-03-26 stsp done:
2081 3789fd73 2020-03-26 stsp free(remote_namespace);
2082 3789fd73 2020-03-26 stsp free(local_refname);
2083 0e4002ca 2020-03-21 stsp return err;
2084 0e4002ca 2020-03-21 stsp }
2085 0e4002ca 2020-03-21 stsp
2086 0e4002ca 2020-03-21 stsp static const struct got_error *
2087 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2088 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2089 0e4002ca 2020-03-21 stsp {
2090 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2091 0e4002ca 2020-03-21 stsp char *remote_refname;
2092 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2093 0e4002ca 2020-03-21 stsp
2094 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2095 0e4002ca 2020-03-21 stsp refname += 5;
2096 0e4002ca 2020-03-21 stsp
2097 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2098 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2099 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2100 f21ec2f0 2020-03-21 stsp
2101 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2102 0e4002ca 2020-03-21 stsp if (err) {
2103 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2104 0e4002ca 2020-03-21 stsp goto done;
2105 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2106 0e4002ca 2020-03-21 stsp } else {
2107 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2108 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2109 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2110 9f142382 2020-03-21 stsp err = unlock_err;
2111 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2112 0e4002ca 2020-03-21 stsp }
2113 0e4002ca 2020-03-21 stsp done:
2114 0e4002ca 2020-03-21 stsp free(remote_refname);
2115 f21ec2f0 2020-03-21 stsp return err;
2116 f21ec2f0 2020-03-21 stsp }
2117 f21ec2f0 2020-03-21 stsp
2118 f21ec2f0 2020-03-21 stsp static const struct got_error *
2119 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2120 7848a0e1 2020-03-19 stsp {
2121 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2122 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2123 7848a0e1 2020-03-19 stsp const char *remote_name;
2124 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2125 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2126 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2127 7848a0e1 2020-03-19 stsp int nremotes;
2128 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2129 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2130 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2131 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2132 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2133 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2134 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2135 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2136 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2137 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2138 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2139 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2140 7848a0e1 2020-03-19 stsp
2141 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2142 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2143 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2144 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2145 7848a0e1 2020-03-19 stsp
2146 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2147 7848a0e1 2020-03-19 stsp switch (ch) {
2148 659e7fbd 2020-03-20 stsp case 'a':
2149 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2150 4ba14133 2020-03-20 stsp break;
2151 4ba14133 2020-03-20 stsp case 'b':
2152 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2153 4ba14133 2020-03-20 stsp optarg, NULL);
2154 4ba14133 2020-03-20 stsp if (error)
2155 4ba14133 2020-03-20 stsp return error;
2156 41b0de12 2020-03-21 stsp break;
2157 f21ec2f0 2020-03-21 stsp case 'd':
2158 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2159 f21ec2f0 2020-03-21 stsp break;
2160 41b0de12 2020-03-21 stsp case 'l':
2161 41b0de12 2020-03-21 stsp list_refs_only = 1;
2162 659e7fbd 2020-03-20 stsp break;
2163 7848a0e1 2020-03-19 stsp case 'r':
2164 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2165 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2166 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2167 7848a0e1 2020-03-19 stsp optarg);
2168 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2169 7848a0e1 2020-03-19 stsp break;
2170 db6d8ad8 2020-03-21 stsp case 't':
2171 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2172 db6d8ad8 2020-03-21 stsp break;
2173 7848a0e1 2020-03-19 stsp case 'v':
2174 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2175 7848a0e1 2020-03-19 stsp verbosity = 0;
2176 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2177 7848a0e1 2020-03-19 stsp verbosity++;
2178 7848a0e1 2020-03-19 stsp break;
2179 7848a0e1 2020-03-19 stsp case 'q':
2180 7848a0e1 2020-03-19 stsp verbosity = -1;
2181 7848a0e1 2020-03-19 stsp break;
2182 0e4002ca 2020-03-21 stsp case 'R':
2183 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2184 0e4002ca 2020-03-21 stsp optarg, NULL);
2185 0e4002ca 2020-03-21 stsp if (error)
2186 0e4002ca 2020-03-21 stsp return error;
2187 0e4002ca 2020-03-21 stsp break;
2188 7848a0e1 2020-03-19 stsp default:
2189 7848a0e1 2020-03-19 stsp usage_fetch();
2190 7848a0e1 2020-03-19 stsp break;
2191 7848a0e1 2020-03-19 stsp }
2192 7848a0e1 2020-03-19 stsp }
2193 7848a0e1 2020-03-19 stsp argc -= optind;
2194 7848a0e1 2020-03-19 stsp argv += optind;
2195 7848a0e1 2020-03-19 stsp
2196 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2197 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2198 41b0de12 2020-03-21 stsp if (list_refs_only) {
2199 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2200 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2201 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2202 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2203 f21ec2f0 2020-03-21 stsp if (delete_refs)
2204 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2205 41b0de12 2020-03-21 stsp }
2206 4ba14133 2020-03-20 stsp
2207 7848a0e1 2020-03-19 stsp if (argc == 0)
2208 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2209 7848a0e1 2020-03-19 stsp else if (argc == 1)
2210 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2211 7848a0e1 2020-03-19 stsp else
2212 7848a0e1 2020-03-19 stsp usage_fetch();
2213 7848a0e1 2020-03-19 stsp
2214 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2215 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2216 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2217 7848a0e1 2020-03-19 stsp goto done;
2218 7848a0e1 2020-03-19 stsp }
2219 7848a0e1 2020-03-19 stsp
2220 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2221 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2222 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2223 7848a0e1 2020-03-19 stsp goto done;
2224 7848a0e1 2020-03-19 stsp else
2225 7848a0e1 2020-03-19 stsp error = NULL;
2226 7848a0e1 2020-03-19 stsp if (worktree) {
2227 7848a0e1 2020-03-19 stsp repo_path =
2228 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2229 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2230 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2231 7848a0e1 2020-03-19 stsp if (error)
2232 7848a0e1 2020-03-19 stsp goto done;
2233 7848a0e1 2020-03-19 stsp } else {
2234 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2235 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2236 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2237 7848a0e1 2020-03-19 stsp goto done;
2238 7848a0e1 2020-03-19 stsp }
2239 7848a0e1 2020-03-19 stsp }
2240 7848a0e1 2020-03-19 stsp }
2241 7848a0e1 2020-03-19 stsp
2242 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2243 7848a0e1 2020-03-19 stsp if (error)
2244 7848a0e1 2020-03-19 stsp goto done;
2245 7848a0e1 2020-03-19 stsp
2246 50b0790e 2020-09-11 stsp if (worktree) {
2247 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2248 50b0790e 2020-09-11 stsp if (worktree_conf) {
2249 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2250 50b0790e 2020-09-11 stsp worktree_conf);
2251 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2252 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2253 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2254 50b0790e 2020-09-11 stsp break;
2255 54eb00d5 2020-10-20 stsp }
2256 50b0790e 2020-09-11 stsp }
2257 50b0790e 2020-09-11 stsp }
2258 7848a0e1 2020-03-19 stsp }
2259 50b0790e 2020-09-11 stsp if (remote == NULL) {
2260 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2261 50b0790e 2020-09-11 stsp if (repo_conf) {
2262 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2263 50b0790e 2020-09-11 stsp repo_conf);
2264 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2265 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2266 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2267 50b0790e 2020-09-11 stsp break;
2268 54eb00d5 2020-10-20 stsp }
2269 50b0790e 2020-09-11 stsp }
2270 50b0790e 2020-09-11 stsp }
2271 50b0790e 2020-09-11 stsp }
2272 50b0790e 2020-09-11 stsp if (remote == NULL) {
2273 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2274 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2275 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2276 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2277 257add31 2020-09-09 stsp break;
2278 54eb00d5 2020-10-20 stsp }
2279 257add31 2020-09-09 stsp }
2280 7848a0e1 2020-03-19 stsp }
2281 50b0790e 2020-09-11 stsp if (remote == NULL) {
2282 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2283 50b0790e 2020-09-11 stsp goto done;
2284 b8adfa55 2020-09-25 stsp }
2285 b8adfa55 2020-09-25 stsp
2286 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2287 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2288 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2289 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2290 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2291 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2292 99495ddb 2021-01-10 stsp }
2293 99495ddb 2021-01-10 stsp }
2294 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2295 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++) {
2296 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2297 99495ddb 2021-01-10 stsp remote->refs[i], NULL);
2298 b8adfa55 2020-09-25 stsp }
2299 50b0790e 2020-09-11 stsp }
2300 7848a0e1 2020-03-19 stsp
2301 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2302 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2303 7848a0e1 2020-03-19 stsp if (error)
2304 7848a0e1 2020-03-19 stsp goto done;
2305 7848a0e1 2020-03-19 stsp
2306 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2307 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2308 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2309 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2310 7848a0e1 2020-03-19 stsp err(1, "pledge");
2311 7848a0e1 2020-03-19 stsp #endif
2312 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2313 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2314 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2315 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2316 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2317 7848a0e1 2020-03-19 stsp err(1, "pledge");
2318 7848a0e1 2020-03-19 stsp #endif
2319 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2320 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2321 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2322 7848a0e1 2020-03-19 stsp goto done;
2323 7848a0e1 2020-03-19 stsp } else {
2324 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2325 7848a0e1 2020-03-19 stsp goto done;
2326 7848a0e1 2020-03-19 stsp }
2327 7848a0e1 2020-03-19 stsp
2328 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2329 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2330 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2331 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2332 7848a0e1 2020-03-19 stsp goto done;
2333 7848a0e1 2020-03-19 stsp }
2334 7848a0e1 2020-03-19 stsp }
2335 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2336 7848a0e1 2020-03-19 stsp if (error)
2337 7848a0e1 2020-03-19 stsp goto done;
2338 f79e6490 2020-04-19 stsp
2339 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2340 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2341 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2342 7848a0e1 2020-03-19 stsp
2343 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2344 9c52365f 2020-03-21 stsp server_path, verbosity);
2345 7848a0e1 2020-03-19 stsp if (error)
2346 7848a0e1 2020-03-19 stsp goto done;
2347 7848a0e1 2020-03-19 stsp
2348 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2349 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2350 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2351 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2352 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2353 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2354 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2355 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2356 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2357 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2358 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2359 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2360 7848a0e1 2020-03-19 stsp if (error)
2361 7848a0e1 2020-03-19 stsp goto done;
2362 7848a0e1 2020-03-19 stsp
2363 41b0de12 2020-03-21 stsp if (list_refs_only) {
2364 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2365 41b0de12 2020-03-21 stsp goto done;
2366 41b0de12 2020-03-21 stsp }
2367 41b0de12 2020-03-21 stsp
2368 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2369 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2370 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2371 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2372 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2373 984065c8 2020-03-19 stsp if (error)
2374 984065c8 2020-03-19 stsp goto done;
2375 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2376 984065c8 2020-03-19 stsp free(id_str);
2377 984065c8 2020-03-19 stsp id_str = NULL;
2378 984065c8 2020-03-19 stsp }
2379 7848a0e1 2020-03-19 stsp
2380 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2381 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2382 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2383 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2384 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2385 7848a0e1 2020-03-19 stsp char *remote_refname;
2386 7848a0e1 2020-03-19 stsp
2387 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2388 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2389 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2390 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2391 0e4002ca 2020-03-21 stsp if (error)
2392 0e4002ca 2020-03-21 stsp goto done;
2393 0e4002ca 2020-03-21 stsp continue;
2394 0e4002ca 2020-03-21 stsp }
2395 0e4002ca 2020-03-21 stsp
2396 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2397 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2398 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2399 7848a0e1 2020-03-19 stsp if (error) {
2400 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2401 7848a0e1 2020-03-19 stsp goto done;
2402 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2403 6338a6a1 2020-03-21 stsp repo);
2404 7848a0e1 2020-03-19 stsp if (error)
2405 7848a0e1 2020-03-19 stsp goto done;
2406 7848a0e1 2020-03-19 stsp } else {
2407 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2408 db6d8ad8 2020-03-21 stsp verbosity, repo);
2409 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2410 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2411 9f142382 2020-03-21 stsp error = unlock_err;
2412 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2413 7848a0e1 2020-03-19 stsp if (error)
2414 7848a0e1 2020-03-19 stsp goto done;
2415 7848a0e1 2020-03-19 stsp }
2416 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2417 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2418 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2419 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2420 7848a0e1 2020-03-19 stsp goto done;
2421 7848a0e1 2020-03-19 stsp }
2422 7848a0e1 2020-03-19 stsp
2423 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2424 7848a0e1 2020-03-19 stsp if (error) {
2425 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2426 7848a0e1 2020-03-19 stsp goto done;
2427 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2428 688f11b3 2020-03-21 stsp verbosity, repo);
2429 7848a0e1 2020-03-19 stsp if (error)
2430 7848a0e1 2020-03-19 stsp goto done;
2431 7848a0e1 2020-03-19 stsp } else {
2432 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2433 db6d8ad8 2020-03-21 stsp verbosity, repo);
2434 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2435 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2436 9f142382 2020-03-21 stsp error = unlock_err;
2437 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2438 7848a0e1 2020-03-19 stsp if (error)
2439 7848a0e1 2020-03-19 stsp goto done;
2440 7848a0e1 2020-03-19 stsp }
2441 2ec30c80 2020-03-20 stsp
2442 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2443 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2444 2ec30c80 2020-03-20 stsp if (error) {
2445 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2446 2ec30c80 2020-03-20 stsp goto done;
2447 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2448 6338a6a1 2020-03-21 stsp repo);
2449 2ec30c80 2020-03-20 stsp if (error)
2450 2ec30c80 2020-03-20 stsp goto done;
2451 9f142382 2020-03-21 stsp } else {
2452 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2453 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2454 9f142382 2020-03-21 stsp error = unlock_err;
2455 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2456 9f142382 2020-03-21 stsp }
2457 7848a0e1 2020-03-19 stsp }
2458 7848a0e1 2020-03-19 stsp }
2459 f1bcca34 2020-03-25 stsp if (delete_refs) {
2460 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2461 3789fd73 2020-03-26 stsp verbosity, repo);
2462 f1bcca34 2020-03-25 stsp if (error)
2463 f1bcca34 2020-03-25 stsp goto done;
2464 f1bcca34 2020-03-25 stsp }
2465 f1bcca34 2020-03-25 stsp
2466 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2467 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2468 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2469 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2470 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2471 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2472 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2473 f1bcca34 2020-03-25 stsp
2474 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2475 f1bcca34 2020-03-25 stsp continue;
2476 f1bcca34 2020-03-25 stsp
2477 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2478 f1bcca34 2020-03-25 stsp continue;
2479 f1bcca34 2020-03-25 stsp
2480 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2481 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2482 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2483 f1bcca34 2020-03-25 stsp goto done;
2484 f1bcca34 2020-03-25 stsp }
2485 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2486 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2487 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2488 f1bcca34 2020-03-25 stsp free(remote_refname);
2489 f1bcca34 2020-03-25 stsp goto done;
2490 f1bcca34 2020-03-25 stsp }
2491 f1bcca34 2020-03-25 stsp
2492 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2493 f1bcca34 2020-03-25 stsp 0);
2494 f1bcca34 2020-03-25 stsp if (error) {
2495 f1bcca34 2020-03-25 stsp free(remote_refname);
2496 f1bcca34 2020-03-25 stsp free(remote_target);
2497 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2498 f1bcca34 2020-03-25 stsp error = NULL;
2499 f1bcca34 2020-03-25 stsp continue;
2500 f1bcca34 2020-03-25 stsp }
2501 f1bcca34 2020-03-25 stsp goto done;
2502 f1bcca34 2020-03-25 stsp }
2503 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2504 f1bcca34 2020-03-25 stsp verbosity, repo);
2505 f1bcca34 2020-03-25 stsp free(remote_refname);
2506 f1bcca34 2020-03-25 stsp free(remote_target);
2507 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2508 f1bcca34 2020-03-25 stsp if (error)
2509 f1bcca34 2020-03-25 stsp goto done;
2510 f1bcca34 2020-03-25 stsp }
2511 f1bcca34 2020-03-25 stsp }
2512 7848a0e1 2020-03-19 stsp done:
2513 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2514 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2515 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2516 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2517 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2518 9c52365f 2020-03-21 stsp }
2519 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2520 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2521 1d0f4054 2021-06-17 stsp if (repo) {
2522 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2523 1d0f4054 2021-06-17 stsp if (error == NULL)
2524 1d0f4054 2021-06-17 stsp error = close_err;
2525 1d0f4054 2021-06-17 stsp }
2526 7848a0e1 2020-03-19 stsp if (worktree)
2527 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2528 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2529 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2530 7848a0e1 2020-03-19 stsp free(pe->data);
2531 7848a0e1 2020-03-19 stsp }
2532 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2533 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2534 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2535 7848a0e1 2020-03-19 stsp free(pe->data);
2536 7848a0e1 2020-03-19 stsp }
2537 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2538 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2539 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2540 7848a0e1 2020-03-19 stsp free(id_str);
2541 7848a0e1 2020-03-19 stsp free(cwd);
2542 7848a0e1 2020-03-19 stsp free(repo_path);
2543 7848a0e1 2020-03-19 stsp free(pack_hash);
2544 7848a0e1 2020-03-19 stsp free(proto);
2545 7848a0e1 2020-03-19 stsp free(host);
2546 7848a0e1 2020-03-19 stsp free(port);
2547 7848a0e1 2020-03-19 stsp free(server_path);
2548 7848a0e1 2020-03-19 stsp free(repo_name);
2549 7848a0e1 2020-03-19 stsp return error;
2550 7848a0e1 2020-03-19 stsp }
2551 7848a0e1 2020-03-19 stsp
2552 7848a0e1 2020-03-19 stsp
2553 7848a0e1 2020-03-19 stsp __dead static void
2554 2ab43947 2020-03-18 stsp usage_checkout(void)
2555 2ab43947 2020-03-18 stsp {
2556 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2557 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2558 2ab43947 2020-03-18 stsp exit(1);
2559 2ab43947 2020-03-18 stsp }
2560 2ab43947 2020-03-18 stsp
2561 2ab43947 2020-03-18 stsp static void
2562 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2563 2ab43947 2020-03-18 stsp {
2564 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2565 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2566 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2567 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2568 2ab43947 2020-03-18 stsp getprogname());
2569 2ab43947 2020-03-18 stsp }
2570 2ab43947 2020-03-18 stsp
2571 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2572 2ab43947 2020-03-18 stsp const char *worktree_path;
2573 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2574 2ab43947 2020-03-18 stsp };
2575 2ab43947 2020-03-18 stsp
2576 2ab43947 2020-03-18 stsp static const struct got_error *
2577 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2578 2ab43947 2020-03-18 stsp {
2579 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2580 2ab43947 2020-03-18 stsp
2581 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2582 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2583 2ab43947 2020-03-18 stsp return NULL;
2584 2ab43947 2020-03-18 stsp
2585 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2586 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2587 2ab43947 2020-03-18 stsp return NULL;
2588 2ab43947 2020-03-18 stsp }
2589 2ab43947 2020-03-18 stsp
2590 2ab43947 2020-03-18 stsp while (path[0] == '/')
2591 2ab43947 2020-03-18 stsp path++;
2592 2ab43947 2020-03-18 stsp
2593 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2594 2ab43947 2020-03-18 stsp return NULL;
2595 2ab43947 2020-03-18 stsp }
2596 2ab43947 2020-03-18 stsp
2597 2ab43947 2020-03-18 stsp static const struct got_error *
2598 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2599 2ab43947 2020-03-18 stsp {
2600 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2601 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2602 2ab43947 2020-03-18 stsp return NULL;
2603 2ab43947 2020-03-18 stsp }
2604 2ab43947 2020-03-18 stsp
2605 2ab43947 2020-03-18 stsp static const struct got_error *
2606 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2607 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2608 2ab43947 2020-03-18 stsp struct got_repository *repo)
2609 2ab43947 2020-03-18 stsp {
2610 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2611 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2612 2ab43947 2020-03-18 stsp
2613 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2614 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2615 2ab43947 2020-03-18 stsp if (err)
2616 2ab43947 2020-03-18 stsp return err;
2617 2ab43947 2020-03-18 stsp
2618 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2619 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2620 2ab43947 2020-03-18 stsp
2621 2ab43947 2020-03-18 stsp /*
2622 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2623 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2624 2ab43947 2020-03-18 stsp *
2625 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2626 2ab43947 2020-03-18 stsp *
2627 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2628 2ab43947 2020-03-18 stsp * \ /
2629 2ab43947 2020-03-18 stsp * C E
2630 2ab43947 2020-03-18 stsp * \ /
2631 2ab43947 2020-03-18 stsp * B (yca)
2632 2ab43947 2020-03-18 stsp * |
2633 2ab43947 2020-03-18 stsp * A
2634 2ab43947 2020-03-18 stsp *
2635 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2636 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2637 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2638 2ab43947 2020-03-18 stsp */
2639 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2640 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2641 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2642 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2643 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2644 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2645 2ab43947 2020-03-18 stsp
2646 2ab43947 2020-03-18 stsp free(yca_id);
2647 2ab43947 2020-03-18 stsp return NULL;
2648 2ab43947 2020-03-18 stsp }
2649 2ab43947 2020-03-18 stsp
2650 2ab43947 2020-03-18 stsp static const struct got_error *
2651 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2652 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2653 2ab43947 2020-03-18 stsp struct got_repository *repo)
2654 2ab43947 2020-03-18 stsp {
2655 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2656 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2657 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2658 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2659 2ab43947 2020-03-18 stsp
2660 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2661 2ab43947 2020-03-18 stsp if (err)
2662 2ab43947 2020-03-18 stsp goto done;
2663 2ab43947 2020-03-18 stsp
2664 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2665 2ab43947 2020-03-18 stsp is_same_branch = 1;
2666 2ab43947 2020-03-18 stsp goto done;
2667 2ab43947 2020-03-18 stsp }
2668 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2669 2ab43947 2020-03-18 stsp is_same_branch = 1;
2670 2ab43947 2020-03-18 stsp goto done;
2671 2ab43947 2020-03-18 stsp }
2672 2ab43947 2020-03-18 stsp
2673 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2674 2ab43947 2020-03-18 stsp if (err)
2675 2ab43947 2020-03-18 stsp goto done;
2676 2ab43947 2020-03-18 stsp
2677 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2678 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2679 2ab43947 2020-03-18 stsp if (err)
2680 2ab43947 2020-03-18 stsp goto done;
2681 2ab43947 2020-03-18 stsp
2682 2ab43947 2020-03-18 stsp for (;;) {
2683 2ab43947 2020-03-18 stsp struct got_object_id *id;
2684 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2685 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2686 2ab43947 2020-03-18 stsp if (err) {
2687 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2688 2ab43947 2020-03-18 stsp err = NULL;
2689 2ab43947 2020-03-18 stsp break;
2690 2ab43947 2020-03-18 stsp }
2691 2ab43947 2020-03-18 stsp
2692 2ab43947 2020-03-18 stsp if (id) {
2693 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2694 2ab43947 2020-03-18 stsp break;
2695 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2696 2ab43947 2020-03-18 stsp is_same_branch = 1;
2697 2ab43947 2020-03-18 stsp break;
2698 2ab43947 2020-03-18 stsp }
2699 2ab43947 2020-03-18 stsp }
2700 2ab43947 2020-03-18 stsp }
2701 2ab43947 2020-03-18 stsp done:
2702 2ab43947 2020-03-18 stsp if (graph)
2703 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2704 2ab43947 2020-03-18 stsp free(head_commit_id);
2705 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2706 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2707 2ab43947 2020-03-18 stsp return err;
2708 a367ff0f 2019-05-14 stsp }
2709 8069f636 2019-01-12 stsp
2710 4ed7e80c 2018-05-20 stsp static const struct got_error *
2711 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2712 4b6c9460 2020-03-05 stsp {
2713 4b6c9460 2020-03-05 stsp static char msg[512];
2714 4b6c9460 2020-03-05 stsp const char *branch_name;
2715 4b6c9460 2020-03-05 stsp
2716 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2717 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2718 4b6c9460 2020-03-05 stsp else
2719 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2720 4b6c9460 2020-03-05 stsp
2721 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2722 4b6c9460 2020-03-05 stsp branch_name += 11;
2723 4b6c9460 2020-03-05 stsp
2724 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2725 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2726 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2727 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2728 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2729 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2730 4b6c9460 2020-03-05 stsp
2731 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2732 4b6c9460 2020-03-05 stsp }
2733 4b6c9460 2020-03-05 stsp
2734 4b6c9460 2020-03-05 stsp static const struct got_error *
2735 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2736 c09a553d 2018-03-12 stsp {
2737 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2738 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2739 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2740 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2741 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2742 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2743 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2744 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2745 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2746 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2747 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2748 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2749 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2750 f2ea84fa 2019-07-27 stsp
2751 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2752 c09a553d 2018-03-12 stsp
2753 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2754 0bb8a95e 2018-03-12 stsp switch (ch) {
2755 08573d5b 2019-05-14 stsp case 'b':
2756 08573d5b 2019-05-14 stsp branch_name = optarg;
2757 08573d5b 2019-05-14 stsp break;
2758 8069f636 2019-01-12 stsp case 'c':
2759 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2760 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2761 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2762 bb51a5b4 2020-01-13 stsp break;
2763 bb51a5b4 2020-01-13 stsp case 'E':
2764 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2765 8069f636 2019-01-12 stsp break;
2766 0bb8a95e 2018-03-12 stsp case 'p':
2767 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2768 0bb8a95e 2018-03-12 stsp break;
2769 0bb8a95e 2018-03-12 stsp default:
2770 2deda0b9 2019-03-07 stsp usage_checkout();
2771 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2772 0bb8a95e 2018-03-12 stsp }
2773 0bb8a95e 2018-03-12 stsp }
2774 0bb8a95e 2018-03-12 stsp
2775 0bb8a95e 2018-03-12 stsp argc -= optind;
2776 0bb8a95e 2018-03-12 stsp argv += optind;
2777 0bb8a95e 2018-03-12 stsp
2778 6715a751 2018-03-16 stsp #ifndef PROFILE
2779 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2780 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2781 c09a553d 2018-03-12 stsp err(1, "pledge");
2782 6715a751 2018-03-16 stsp #endif
2783 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2784 c47340da 2020-09-20 stsp char *base, *dotgit;
2785 f0207f6a 2020-10-19 stsp const char *path;
2786 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2787 76089277 2018-04-01 stsp if (repo_path == NULL)
2788 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2789 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2790 76089277 2018-04-01 stsp if (cwd == NULL) {
2791 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2792 76089277 2018-04-01 stsp goto done;
2793 76089277 2018-04-01 stsp }
2794 c47340da 2020-09-20 stsp if (path_prefix[0])
2795 f0207f6a 2020-10-19 stsp path = path_prefix;
2796 c47340da 2020-09-20 stsp else
2797 f0207f6a 2020-10-19 stsp path = repo_path;
2798 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2799 f0207f6a 2020-10-19 stsp if (error)
2800 c47340da 2020-09-20 stsp goto done;
2801 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2802 c09a553d 2018-03-12 stsp if (dotgit)
2803 c09a553d 2018-03-12 stsp *dotgit = '\0';
2804 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2805 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2806 f0207f6a 2020-10-19 stsp free(base);
2807 76089277 2018-04-01 stsp goto done;
2808 c09a553d 2018-03-12 stsp }
2809 f0207f6a 2020-10-19 stsp free(base);
2810 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2811 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2812 76089277 2018-04-01 stsp if (repo_path == NULL) {
2813 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2814 76089277 2018-04-01 stsp goto done;
2815 76089277 2018-04-01 stsp }
2816 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2817 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2818 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2819 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2820 b4b3a7dd 2019-07-22 stsp argv[1]);
2821 b4b3a7dd 2019-07-22 stsp goto done;
2822 b4b3a7dd 2019-07-22 stsp }
2823 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2824 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2825 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2826 b4b3a7dd 2019-07-22 stsp goto done;
2827 b4b3a7dd 2019-07-22 stsp }
2828 76089277 2018-04-01 stsp }
2829 c09a553d 2018-03-12 stsp } else
2830 c09a553d 2018-03-12 stsp usage_checkout();
2831 c09a553d 2018-03-12 stsp
2832 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2833 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2834 13bfb272 2019-05-10 jcs
2835 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2836 c09a553d 2018-03-12 stsp if (error != NULL)
2837 c02c541e 2019-03-29 stsp goto done;
2838 c02c541e 2019-03-29 stsp
2839 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2840 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2841 c530dc23 2019-07-23 stsp if (error) {
2842 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2843 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2844 c530dc23 2019-07-23 stsp goto done;
2845 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2846 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2847 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2848 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2849 c530dc23 2019-07-23 stsp goto done;
2850 c530dc23 2019-07-23 stsp }
2851 c530dc23 2019-07-23 stsp }
2852 c530dc23 2019-07-23 stsp
2853 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2854 c02c541e 2019-03-29 stsp if (error)
2855 c09a553d 2018-03-12 stsp goto done;
2856 8069f636 2019-01-12 stsp
2857 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2858 c09a553d 2018-03-12 stsp if (error != NULL)
2859 c09a553d 2018-03-12 stsp goto done;
2860 c09a553d 2018-03-12 stsp
2861 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2862 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2863 c09a553d 2018-03-12 stsp goto done;
2864 c09a553d 2018-03-12 stsp
2865 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2866 c09a553d 2018-03-12 stsp if (error != NULL)
2867 c09a553d 2018-03-12 stsp goto done;
2868 c09a553d 2018-03-12 stsp
2869 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2870 e5dc7198 2018-12-29 stsp path_prefix);
2871 e5dc7198 2018-12-29 stsp if (error != NULL)
2872 e5dc7198 2018-12-29 stsp goto done;
2873 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2874 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2875 49520a32 2018-12-29 stsp goto done;
2876 49520a32 2018-12-29 stsp }
2877 49520a32 2018-12-29 stsp
2878 8069f636 2019-01-12 stsp if (commit_id_str) {
2879 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2880 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2881 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2882 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2883 84de9106 2020-12-26 stsp NULL);
2884 84de9106 2020-12-26 stsp if (error)
2885 84de9106 2020-12-26 stsp goto done;
2886 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2887 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2888 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2889 30837e32 2019-07-25 stsp if (error)
2890 8069f636 2019-01-12 stsp goto done;
2891 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2892 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2893 8069f636 2019-01-12 stsp if (error != NULL) {
2894 8069f636 2019-01-12 stsp free(commit_id);
2895 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2896 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2897 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2898 4b6c9460 2020-03-05 stsp }
2899 8069f636 2019-01-12 stsp goto done;
2900 8069f636 2019-01-12 stsp }
2901 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2902 4b6c9460 2020-03-05 stsp if (error) {
2903 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2904 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2905 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2906 4b6c9460 2020-03-05 stsp }
2907 45d344f6 2019-05-14 stsp goto done;
2908 4b6c9460 2020-03-05 stsp }
2909 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2910 8069f636 2019-01-12 stsp commit_id);
2911 8069f636 2019-01-12 stsp free(commit_id);
2912 8069f636 2019-01-12 stsp if (error)
2913 8069f636 2019-01-12 stsp goto done;
2914 8069f636 2019-01-12 stsp }
2915 8069f636 2019-01-12 stsp
2916 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2917 f2ea84fa 2019-07-27 stsp if (error)
2918 f2ea84fa 2019-07-27 stsp goto done;
2919 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2920 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2921 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2922 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2923 c09a553d 2018-03-12 stsp if (error != NULL)
2924 c09a553d 2018-03-12 stsp goto done;
2925 c09a553d 2018-03-12 stsp
2926 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2927 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2928 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2929 c09a553d 2018-03-12 stsp done:
2930 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2931 8069f636 2019-01-12 stsp free(commit_id_str);
2932 76089277 2018-04-01 stsp free(repo_path);
2933 507dc3bb 2018-12-29 stsp free(worktree_path);
2934 c47340da 2020-09-20 stsp free(cwd);
2935 507dc3bb 2018-12-29 stsp return error;
2936 9627c110 2020-04-18 stsp }
2937 9627c110 2020-04-18 stsp
2938 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2939 9627c110 2020-04-18 stsp int did_something;
2940 9627c110 2020-04-18 stsp int conflicts;
2941 9627c110 2020-04-18 stsp int obstructed;
2942 9627c110 2020-04-18 stsp int not_updated;
2943 9627c110 2020-04-18 stsp };
2944 9627c110 2020-04-18 stsp
2945 9627c110 2020-04-18 stsp void
2946 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2947 9627c110 2020-04-18 stsp {
2948 9627c110 2020-04-18 stsp if (!upa->did_something)
2949 9627c110 2020-04-18 stsp return;
2950 9627c110 2020-04-18 stsp
2951 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2952 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2953 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2954 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2955 9627c110 2020-04-18 stsp upa->obstructed);
2956 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2957 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2958 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2959 507dc3bb 2018-12-29 stsp }
2960 507dc3bb 2018-12-29 stsp
2961 507dc3bb 2018-12-29 stsp __dead static void
2962 507dc3bb 2018-12-29 stsp usage_update(void)
2963 507dc3bb 2018-12-29 stsp {
2964 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2965 507dc3bb 2018-12-29 stsp getprogname());
2966 507dc3bb 2018-12-29 stsp exit(1);
2967 507dc3bb 2018-12-29 stsp }
2968 507dc3bb 2018-12-29 stsp
2969 1ee397ad 2019-07-12 stsp static const struct got_error *
2970 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2971 507dc3bb 2018-12-29 stsp {
2972 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2973 784955db 2019-01-12 stsp
2974 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2975 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2976 1ee397ad 2019-07-12 stsp return NULL;
2977 507dc3bb 2018-12-29 stsp
2978 9627c110 2020-04-18 stsp upa->did_something = 1;
2979 a484d721 2019-06-10 stsp
2980 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2981 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2982 1ee397ad 2019-07-12 stsp return NULL;
2983 a484d721 2019-06-10 stsp
2984 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2985 9627c110 2020-04-18 stsp upa->conflicts++;
2986 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2987 9627c110 2020-04-18 stsp upa->obstructed++;
2988 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2989 9627c110 2020-04-18 stsp upa->not_updated++;
2990 9627c110 2020-04-18 stsp
2991 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2992 507dc3bb 2018-12-29 stsp path++;
2993 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2994 1ee397ad 2019-07-12 stsp return NULL;
2995 be7061eb 2018-12-30 stsp }
2996 be7061eb 2018-12-30 stsp
2997 be7061eb 2018-12-30 stsp static const struct got_error *
2998 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2999 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3000 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3001 a1fb16d8 2019-05-24 stsp {
3002 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3003 a1fb16d8 2019-05-24 stsp char *base_id_str;
3004 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3005 a1fb16d8 2019-05-24 stsp
3006 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3007 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3008 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3009 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3010 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3011 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3012 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3013 a1fb16d8 2019-05-24 stsp }
3014 a1fb16d8 2019-05-24 stsp
3015 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3016 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3017 a1fb16d8 2019-05-24 stsp if (err) {
3018 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3019 a1fb16d8 2019-05-24 stsp return err;
3020 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3021 a1fb16d8 2019-05-24 stsp }
3022 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3023 a1fb16d8 2019-05-24 stsp return NULL;
3024 a1fb16d8 2019-05-24 stsp
3025 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3026 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3027 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3028 a1fb16d8 2019-05-24 stsp if (err)
3029 a1fb16d8 2019-05-24 stsp return err;
3030 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3031 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3032 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3033 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3034 0ebf8283 2019-07-24 stsp return NULL;
3035 0ebf8283 2019-07-24 stsp }
3036 0ebf8283 2019-07-24 stsp
3037 0ebf8283 2019-07-24 stsp static const struct got_error *
3038 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3039 0ebf8283 2019-07-24 stsp {
3040 0ebf8283 2019-07-24 stsp const struct got_error *err;
3041 0ebf8283 2019-07-24 stsp int in_progress;
3042 0ebf8283 2019-07-24 stsp
3043 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3044 0ebf8283 2019-07-24 stsp if (err)
3045 0ebf8283 2019-07-24 stsp return err;
3046 0ebf8283 2019-07-24 stsp if (in_progress)
3047 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3048 0ebf8283 2019-07-24 stsp
3049 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3050 0ebf8283 2019-07-24 stsp if (err)
3051 0ebf8283 2019-07-24 stsp return err;
3052 0ebf8283 2019-07-24 stsp if (in_progress)
3053 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3054 0ebf8283 2019-07-24 stsp
3055 a1fb16d8 2019-05-24 stsp return NULL;
3056 a5edda0a 2019-07-27 stsp }
3057 a5edda0a 2019-07-27 stsp
3058 a5edda0a 2019-07-27 stsp static const struct got_error *
3059 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3060 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3061 a5edda0a 2019-07-27 stsp {
3062 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3063 a5edda0a 2019-07-27 stsp char *path;
3064 a5edda0a 2019-07-27 stsp int i;
3065 a5edda0a 2019-07-27 stsp
3066 a5edda0a 2019-07-27 stsp if (argc == 0) {
3067 a5edda0a 2019-07-27 stsp path = strdup("");
3068 a5edda0a 2019-07-27 stsp if (path == NULL)
3069 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3070 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3071 a5edda0a 2019-07-27 stsp }
3072 a5edda0a 2019-07-27 stsp
3073 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3074 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3075 a5edda0a 2019-07-27 stsp if (err)
3076 a5edda0a 2019-07-27 stsp break;
3077 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3078 a5edda0a 2019-07-27 stsp if (err) {
3079 a5edda0a 2019-07-27 stsp free(path);
3080 a5edda0a 2019-07-27 stsp break;
3081 a5edda0a 2019-07-27 stsp }
3082 a5edda0a 2019-07-27 stsp }
3083 a5edda0a 2019-07-27 stsp
3084 a5edda0a 2019-07-27 stsp return err;
3085 a1fb16d8 2019-05-24 stsp }
3086 a1fb16d8 2019-05-24 stsp
3087 a1fb16d8 2019-05-24 stsp static const struct got_error *
3088 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3089 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3090 fa51e947 2020-03-27 stsp {
3091 fa51e947 2020-03-27 stsp const struct got_error *err;
3092 fa51e947 2020-03-27 stsp struct got_repository *repo;
3093 fa51e947 2020-03-27 stsp static char msg[512];
3094 fa51e947 2020-03-27 stsp
3095 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3096 fa51e947 2020-03-27 stsp if (err)
3097 fa51e947 2020-03-27 stsp return orig_err;
3098 fa51e947 2020-03-27 stsp
3099 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3100 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3101 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3102 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3103 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3104 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3105 fa51e947 2020-03-27 stsp got_repo_close(repo);
3106 fa51e947 2020-03-27 stsp return err;
3107 fa51e947 2020-03-27 stsp }
3108 fa51e947 2020-03-27 stsp
3109 fa51e947 2020-03-27 stsp static const struct got_error *
3110 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3111 507dc3bb 2018-12-29 stsp {
3112 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3113 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3114 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3115 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3116 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3117 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3118 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3119 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3120 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3121 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3122 9627c110 2020-04-18 stsp int ch;
3123 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3124 507dc3bb 2018-12-29 stsp
3125 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3126 f2ea84fa 2019-07-27 stsp
3127 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3128 507dc3bb 2018-12-29 stsp switch (ch) {
3129 024e9686 2019-05-14 stsp case 'b':
3130 024e9686 2019-05-14 stsp branch_name = optarg;
3131 024e9686 2019-05-14 stsp break;
3132 507dc3bb 2018-12-29 stsp case 'c':
3133 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3134 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3135 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3136 507dc3bb 2018-12-29 stsp break;
3137 507dc3bb 2018-12-29 stsp default:
3138 2deda0b9 2019-03-07 stsp usage_update();
3139 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3140 507dc3bb 2018-12-29 stsp }
3141 507dc3bb 2018-12-29 stsp }
3142 507dc3bb 2018-12-29 stsp
3143 507dc3bb 2018-12-29 stsp argc -= optind;
3144 507dc3bb 2018-12-29 stsp argv += optind;
3145 507dc3bb 2018-12-29 stsp
3146 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3147 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3148 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3149 507dc3bb 2018-12-29 stsp err(1, "pledge");
3150 507dc3bb 2018-12-29 stsp #endif
3151 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3152 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3153 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3154 c4cdcb68 2019-04-03 stsp goto done;
3155 c4cdcb68 2019-04-03 stsp }
3156 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3157 fa51e947 2020-03-27 stsp if (error) {
3158 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3159 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3160 fa51e947 2020-03-27 stsp worktree_path);
3161 7d5807f4 2019-07-11 stsp goto done;
3162 fa51e947 2020-03-27 stsp }
3163 7d5807f4 2019-07-11 stsp
3164 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3165 f2ea84fa 2019-07-27 stsp if (error)
3166 f2ea84fa 2019-07-27 stsp goto done;
3167 507dc3bb 2018-12-29 stsp
3168 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3169 c9956ddf 2019-09-08 stsp NULL);
3170 507dc3bb 2018-12-29 stsp if (error != NULL)
3171 507dc3bb 2018-12-29 stsp goto done;
3172 507dc3bb 2018-12-29 stsp
3173 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3174 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3175 087fb88c 2019-08-04 stsp if (error)
3176 087fb88c 2019-08-04 stsp goto done;
3177 087fb88c 2019-08-04 stsp
3178 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3179 0266afb7 2019-01-04 stsp if (error)
3180 0266afb7 2019-01-04 stsp goto done;
3181 0266afb7 2019-01-04 stsp
3182 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3183 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3184 024e9686 2019-05-14 stsp if (error != NULL)
3185 024e9686 2019-05-14 stsp goto done;
3186 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3187 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3188 9c4b8182 2019-01-02 stsp if (error != NULL)
3189 9c4b8182 2019-01-02 stsp goto done;
3190 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3191 507dc3bb 2018-12-29 stsp if (error != NULL)
3192 507dc3bb 2018-12-29 stsp goto done;
3193 507dc3bb 2018-12-29 stsp } else {
3194 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3195 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3196 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3197 84de9106 2020-12-26 stsp NULL);
3198 84de9106 2020-12-26 stsp if (error)
3199 84de9106 2020-12-26 stsp goto done;
3200 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3201 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3202 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3203 04f57cb3 2019-07-25 stsp free(commit_id_str);
3204 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3205 30837e32 2019-07-25 stsp if (error)
3206 a297e751 2019-07-11 stsp goto done;
3207 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3208 a297e751 2019-07-11 stsp if (error)
3209 507dc3bb 2018-12-29 stsp goto done;
3210 507dc3bb 2018-12-29 stsp }
3211 35c965b2 2018-12-29 stsp
3212 a1fb16d8 2019-05-24 stsp if (branch_name) {
3213 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3214 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3215 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3216 f2ea84fa 2019-07-27 stsp continue;
3217 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3218 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3219 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3220 024e9686 2019-05-14 stsp goto done;
3221 024e9686 2019-05-14 stsp }
3222 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3223 024e9686 2019-05-14 stsp if (error)
3224 024e9686 2019-05-14 stsp goto done;
3225 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3226 3aef623b 2019-10-15 stsp repo);
3227 a367ff0f 2019-05-14 stsp free(head_commit_id);
3228 024e9686 2019-05-14 stsp if (error != NULL)
3229 024e9686 2019-05-14 stsp goto done;
3230 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3231 a367ff0f 2019-05-14 stsp if (error)
3232 a367ff0f 2019-05-14 stsp goto done;
3233 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3234 024e9686 2019-05-14 stsp if (error)
3235 024e9686 2019-05-14 stsp goto done;
3236 024e9686 2019-05-14 stsp } else {
3237 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3238 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3239 a367ff0f 2019-05-14 stsp if (error != NULL) {
3240 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3241 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3242 024e9686 2019-05-14 stsp goto done;
3243 a367ff0f 2019-05-14 stsp }
3244 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3245 a367ff0f 2019-05-14 stsp if (error)
3246 a367ff0f 2019-05-14 stsp goto done;
3247 024e9686 2019-05-14 stsp }
3248 507dc3bb 2018-12-29 stsp
3249 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3250 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3251 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3252 507dc3bb 2018-12-29 stsp commit_id);
3253 507dc3bb 2018-12-29 stsp if (error)
3254 507dc3bb 2018-12-29 stsp goto done;
3255 507dc3bb 2018-12-29 stsp }
3256 507dc3bb 2018-12-29 stsp
3257 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3258 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3259 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3260 507dc3bb 2018-12-29 stsp if (error != NULL)
3261 507dc3bb 2018-12-29 stsp goto done;
3262 9c4b8182 2019-01-02 stsp
3263 9627c110 2020-04-18 stsp if (upa.did_something)
3264 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3265 784955db 2019-01-12 stsp else
3266 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3267 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3268 507dc3bb 2018-12-29 stsp done:
3269 c09a553d 2018-03-12 stsp free(worktree_path);
3270 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3271 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3272 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3273 507dc3bb 2018-12-29 stsp free(commit_id);
3274 9c4b8182 2019-01-02 stsp free(commit_id_str);
3275 c09a553d 2018-03-12 stsp return error;
3276 c09a553d 2018-03-12 stsp }
3277 c09a553d 2018-03-12 stsp
3278 f42b1b34 2018-03-12 stsp static const struct got_error *
3279 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3280 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3281 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3282 5c860e29 2018-03-12 stsp {
3283 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3284 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3285 79109fed 2018-03-27 stsp
3286 44392932 2019-08-25 stsp if (blob_id1) {
3287 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3288 44392932 2019-08-25 stsp if (err)
3289 44392932 2019-08-25 stsp goto done;
3290 44392932 2019-08-25 stsp }
3291 79109fed 2018-03-27 stsp
3292 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3293 44392932 2019-08-25 stsp if (err)
3294 44392932 2019-08-25 stsp goto done;
3295 79109fed 2018-03-27 stsp
3296 44392932 2019-08-25 stsp while (path[0] == '/')
3297 44392932 2019-08-25 stsp path++;
3298 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3299 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3300 44392932 2019-08-25 stsp done:
3301 44392932 2019-08-25 stsp if (blob1)
3302 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3303 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3304 44392932 2019-08-25 stsp return err;
3305 44392932 2019-08-25 stsp }
3306 44392932 2019-08-25 stsp
3307 44392932 2019-08-25 stsp static const struct got_error *
3308 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3309 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3310 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3311 44392932 2019-08-25 stsp {
3312 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3313 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3314 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3315 44392932 2019-08-25 stsp
3316 44392932 2019-08-25 stsp if (tree_id1) {
3317 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3318 79109fed 2018-03-27 stsp if (err)
3319 44392932 2019-08-25 stsp goto done;
3320 79109fed 2018-03-27 stsp }
3321 79109fed 2018-03-27 stsp
3322 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3323 0f2b3dca 2018-12-22 stsp if (err)
3324 0f2b3dca 2018-12-22 stsp goto done;
3325 0f2b3dca 2018-12-22 stsp
3326 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3327 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3328 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3329 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3330 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3331 fe621944 2020-11-10 stsp arg.nlines = 0;
3332 44392932 2019-08-25 stsp while (path[0] == '/')
3333 44392932 2019-08-25 stsp path++;
3334 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3335 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3336 0f2b3dca 2018-12-22 stsp done:
3337 79109fed 2018-03-27 stsp if (tree1)
3338 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3339 366e0a5f 2019-10-10 stsp if (tree2)
3340 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3341 44392932 2019-08-25 stsp return err;
3342 44392932 2019-08-25 stsp }
3343 44392932 2019-08-25 stsp
3344 44392932 2019-08-25 stsp static const struct got_error *
3345 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3346 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3347 0208f208 2020-05-05 stsp {
3348 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3349 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3350 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3351 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3352 0208f208 2020-05-05 stsp
3353 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3354 0208f208 2020-05-05 stsp if (qid != NULL) {
3355 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3356 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3357 0208f208 2020-05-05 stsp qid->id);
3358 0208f208 2020-05-05 stsp if (err)
3359 0208f208 2020-05-05 stsp return err;
3360 0208f208 2020-05-05 stsp
3361 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3362 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3363 0208f208 2020-05-05 stsp
3364 0208f208 2020-05-05 stsp }
3365 0208f208 2020-05-05 stsp
3366 0208f208 2020-05-05 stsp if (tree_id1) {
3367 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3368 0208f208 2020-05-05 stsp if (err)
3369 0208f208 2020-05-05 stsp goto done;
3370 0208f208 2020-05-05 stsp }
3371 0208f208 2020-05-05 stsp
3372 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3373 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3374 0208f208 2020-05-05 stsp if (err)
3375 0208f208 2020-05-05 stsp goto done;
3376 0208f208 2020-05-05 stsp
3377 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3378 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3379 0208f208 2020-05-05 stsp done:
3380 0208f208 2020-05-05 stsp if (tree1)
3381 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3382 0208f208 2020-05-05 stsp if (tree2)
3383 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3384 0208f208 2020-05-05 stsp return err;
3385 0208f208 2020-05-05 stsp }
3386 0208f208 2020-05-05 stsp
3387 0208f208 2020-05-05 stsp static const struct got_error *
3388 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3389 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3390 44392932 2019-08-25 stsp {
3391 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3392 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3393 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3394 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3395 44392932 2019-08-25 stsp struct got_object_qid *qid;
3396 44392932 2019-08-25 stsp
3397 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3398 44392932 2019-08-25 stsp if (qid != NULL) {
3399 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3400 44392932 2019-08-25 stsp qid->id);
3401 44392932 2019-08-25 stsp if (err)
3402 44392932 2019-08-25 stsp return err;
3403 44392932 2019-08-25 stsp }
3404 44392932 2019-08-25 stsp
3405 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3406 44392932 2019-08-25 stsp int obj_type;
3407 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3408 44392932 2019-08-25 stsp if (err)
3409 44392932 2019-08-25 stsp goto done;
3410 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3411 44392932 2019-08-25 stsp if (err) {
3412 44392932 2019-08-25 stsp free(obj_id2);
3413 44392932 2019-08-25 stsp goto done;
3414 44392932 2019-08-25 stsp }
3415 44392932 2019-08-25 stsp if (pcommit) {
3416 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3417 44392932 2019-08-25 stsp qid->id, path);
3418 44392932 2019-08-25 stsp if (err) {
3419 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3420 2e8c69d1 2020-05-04 stsp free(obj_id2);
3421 2e8c69d1 2020-05-04 stsp goto done;
3422 2e8c69d1 2020-05-04 stsp }
3423 2e8c69d1 2020-05-04 stsp } else {
3424 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3425 2e8c69d1 2020-05-04 stsp if (err) {
3426 2e8c69d1 2020-05-04 stsp free(obj_id2);
3427 2e8c69d1 2020-05-04 stsp goto done;
3428 2e8c69d1 2020-05-04 stsp }
3429 44392932 2019-08-25 stsp }
3430 44392932 2019-08-25 stsp }
3431 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3432 44392932 2019-08-25 stsp if (err) {
3433 44392932 2019-08-25 stsp free(obj_id2);
3434 44392932 2019-08-25 stsp goto done;
3435 44392932 2019-08-25 stsp }
3436 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3437 44392932 2019-08-25 stsp switch (obj_type) {
3438 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3439 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3440 64453f7e 2020-11-21 stsp 0, 0, repo);
3441 44392932 2019-08-25 stsp break;
3442 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3443 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3444 64453f7e 2020-11-21 stsp 0, 0, repo);
3445 44392932 2019-08-25 stsp break;
3446 44392932 2019-08-25 stsp default:
3447 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3448 44392932 2019-08-25 stsp break;
3449 44392932 2019-08-25 stsp }
3450 44392932 2019-08-25 stsp free(obj_id1);
3451 44392932 2019-08-25 stsp free(obj_id2);
3452 44392932 2019-08-25 stsp } else {
3453 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3454 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3455 44392932 2019-08-25 stsp if (err)
3456 44392932 2019-08-25 stsp goto done;
3457 579bd556 2020-10-24 stsp if (pcommit) {
3458 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3459 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3460 579bd556 2020-10-24 stsp if (err)
3461 579bd556 2020-10-24 stsp goto done;
3462 579bd556 2020-10-24 stsp }
3463 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3464 64453f7e 2020-11-21 stsp id_str2);
3465 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3466 64453f7e 2020-11-21 stsp repo);
3467 44392932 2019-08-25 stsp }
3468 44392932 2019-08-25 stsp done:
3469 0f2b3dca 2018-12-22 stsp free(id_str1);
3470 0f2b3dca 2018-12-22 stsp free(id_str2);
3471 44392932 2019-08-25 stsp if (pcommit)
3472 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3473 79109fed 2018-03-27 stsp return err;
3474 79109fed 2018-03-27 stsp }
3475 79109fed 2018-03-27 stsp
3476 4bb494d5 2018-06-16 stsp static char *
3477 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3478 6c281f94 2018-06-11 stsp {
3479 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3480 09867e48 2019-08-13 stsp char *p, *s;
3481 09867e48 2019-08-13 stsp
3482 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3483 09867e48 2019-08-13 stsp if (tm == NULL)
3484 09867e48 2019-08-13 stsp return NULL;
3485 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3486 09867e48 2019-08-13 stsp if (s == NULL)
3487 09867e48 2019-08-13 stsp return NULL;
3488 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3489 6c281f94 2018-06-11 stsp if (p)
3490 6c281f94 2018-06-11 stsp *p = '\0';
3491 6c281f94 2018-06-11 stsp return s;
3492 6c281f94 2018-06-11 stsp }
3493 dc424a06 2019-08-07 stsp
3494 6841bf13 2019-11-29 kn static const struct got_error *
3495 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3496 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3497 6841bf13 2019-11-29 kn {
3498 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3499 6841bf13 2019-11-29 kn regmatch_t regmatch;
3500 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3501 6841bf13 2019-11-29 kn
3502 6841bf13 2019-11-29 kn *have_match = 0;
3503 6841bf13 2019-11-29 kn
3504 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3505 6841bf13 2019-11-29 kn if (err)
3506 6841bf13 2019-11-29 kn return err;
3507 6841bf13 2019-11-29 kn
3508 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3509 6841bf13 2019-11-29 kn if (err)
3510 6841bf13 2019-11-29 kn goto done;
3511 6841bf13 2019-11-29 kn
3512 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3513 6841bf13 2019-11-29 kn *have_match = 1;
3514 6841bf13 2019-11-29 kn done:
3515 6841bf13 2019-11-29 kn free(id_str);
3516 6841bf13 2019-11-29 kn free(logmsg);
3517 6841bf13 2019-11-29 kn return err;
3518 6841bf13 2019-11-29 kn }
3519 6841bf13 2019-11-29 kn
3520 0208f208 2020-05-05 stsp static void
3521 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3522 0208f208 2020-05-05 stsp regex_t *regex)
3523 0208f208 2020-05-05 stsp {
3524 0208f208 2020-05-05 stsp regmatch_t regmatch;
3525 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3526 0208f208 2020-05-05 stsp
3527 0208f208 2020-05-05 stsp *have_match = 0;
3528 0208f208 2020-05-05 stsp
3529 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3530 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3531 0208f208 2020-05-05 stsp *have_match = 1;
3532 0208f208 2020-05-05 stsp break;
3533 0208f208 2020-05-05 stsp }
3534 0208f208 2020-05-05 stsp }
3535 0208f208 2020-05-05 stsp }
3536 0208f208 2020-05-05 stsp
3537 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3538 6c281f94 2018-06-11 stsp
3539 888b7d99 2020-12-26 stsp static const struct got_error*
3540 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3541 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3542 888b7d99 2020-12-26 stsp {
3543 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3544 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3545 888b7d99 2020-12-26 stsp char *s;
3546 888b7d99 2020-12-26 stsp const char *name;
3547 5c860e29 2018-03-12 stsp
3548 888b7d99 2020-12-26 stsp *refs_str = NULL;
3549 888b7d99 2020-12-26 stsp
3550 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3551 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3552 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3553 a436ad14 2019-08-13 stsp int cmp;
3554 a436ad14 2019-08-13 stsp
3555 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3556 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3557 d9498b20 2019-02-05 stsp continue;
3558 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3559 199a4027 2019-02-02 stsp name += 5;
3560 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3561 7143d404 2019-03-12 stsp continue;
3562 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3563 e34f9ed6 2019-02-02 stsp name += 6;
3564 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3565 141c2bff 2019-02-04 stsp name += 8;
3566 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3567 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3568 4343a07f 2020-04-24 stsp continue;
3569 4343a07f 2020-04-24 stsp }
3570 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3571 48cae60d 2020-09-22 stsp if (err)
3572 888b7d99 2020-12-26 stsp break;
3573 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3574 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3575 5d844a1e 2019-08-13 stsp if (err) {
3576 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3577 48cae60d 2020-09-22 stsp free(ref_id);
3578 888b7d99 2020-12-26 stsp break;
3579 48cae60d 2020-09-22 stsp }
3580 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3581 5d844a1e 2019-08-13 stsp err = NULL;
3582 5d844a1e 2019-08-13 stsp tag = NULL;
3583 5d844a1e 2019-08-13 stsp }
3584 a436ad14 2019-08-13 stsp }
3585 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3586 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3587 48cae60d 2020-09-22 stsp free(ref_id);
3588 a436ad14 2019-08-13 stsp if (tag)
3589 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3590 a436ad14 2019-08-13 stsp if (cmp != 0)
3591 a436ad14 2019-08-13 stsp continue;
3592 888b7d99 2020-12-26 stsp s = *refs_str;
3593 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3594 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3595 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3596 199a4027 2019-02-02 stsp free(s);
3597 888b7d99 2020-12-26 stsp *refs_str = NULL;
3598 888b7d99 2020-12-26 stsp break;
3599 199a4027 2019-02-02 stsp }
3600 199a4027 2019-02-02 stsp free(s);
3601 199a4027 2019-02-02 stsp }
3602 888b7d99 2020-12-26 stsp
3603 888b7d99 2020-12-26 stsp return err;
3604 888b7d99 2020-12-26 stsp }
3605 888b7d99 2020-12-26 stsp
3606 888b7d99 2020-12-26 stsp static const struct got_error *
3607 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3608 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3609 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3610 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3611 e600f124 2021-03-21 stsp const char *custom_refs_str)
3612 888b7d99 2020-12-26 stsp {
3613 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3614 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3615 888b7d99 2020-12-26 stsp char datebuf[26];
3616 888b7d99 2020-12-26 stsp time_t committer_time;
3617 888b7d99 2020-12-26 stsp const char *author, *committer;
3618 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3619 888b7d99 2020-12-26 stsp
3620 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3621 8bf5b3c9 2018-03-17 stsp if (err)
3622 8bf5b3c9 2018-03-17 stsp return err;
3623 788c352e 2018-06-16 stsp
3624 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3625 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3626 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3627 e600f124 2021-03-21 stsp if (refs) {
3628 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3629 e600f124 2021-03-21 stsp if (err)
3630 e600f124 2021-03-21 stsp goto done;
3631 e600f124 2021-03-21 stsp }
3632 888b7d99 2020-12-26 stsp }
3633 888b7d99 2020-12-26 stsp
3634 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3635 e600f124 2021-03-21 stsp if (custom_refs_str)
3636 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3637 e600f124 2021-03-21 stsp else
3638 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3639 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3640 832c249c 2018-06-10 stsp free(id_str);
3641 d3d493d7 2019-02-21 stsp id_str = NULL;
3642 d3d493d7 2019-02-21 stsp free(refs_str);
3643 d3d493d7 2019-02-21 stsp refs_str = NULL;
3644 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3645 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3646 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3647 09867e48 2019-08-13 stsp if (datestr)
3648 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3649 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3650 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3651 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3652 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3653 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3654 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3655 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3656 3fe1abad 2018-06-10 stsp int n = 1;
3657 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3658 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3659 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3660 a0603db2 2018-06-10 stsp if (err)
3661 888b7d99 2020-12-26 stsp goto done;
3662 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3663 a0603db2 2018-06-10 stsp free(id_str);
3664 888b7d99 2020-12-26 stsp id_str = NULL;
3665 a0603db2 2018-06-10 stsp }
3666 a0603db2 2018-06-10 stsp }
3667 832c249c 2018-06-10 stsp
3668 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3669 5943eee2 2019-08-13 stsp if (err)
3670 888b7d99 2020-12-26 stsp goto done;
3671 8bf5b3c9 2018-03-17 stsp
3672 621015ac 2018-07-23 stsp logmsg = logmsg0;
3673 832c249c 2018-06-10 stsp do {
3674 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3675 832c249c 2018-06-10 stsp if (line)
3676 832c249c 2018-06-10 stsp printf(" %s\n", line);
3677 832c249c 2018-06-10 stsp } while (line);
3678 621015ac 2018-07-23 stsp free(logmsg0);
3679 832c249c 2018-06-10 stsp
3680 0208f208 2020-05-05 stsp if (changed_paths) {
3681 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3682 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3683 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3684 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3685 0208f208 2020-05-05 stsp }
3686 0208f208 2020-05-05 stsp printf("\n");
3687 0208f208 2020-05-05 stsp }
3688 971751ac 2018-03-27 stsp if (show_patch) {
3689 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3690 971751ac 2018-03-27 stsp if (err == 0)
3691 971751ac 2018-03-27 stsp printf("\n");
3692 971751ac 2018-03-27 stsp }
3693 07862c20 2018-09-15 stsp
3694 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3695 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3696 888b7d99 2020-12-26 stsp done:
3697 888b7d99 2020-12-26 stsp free(id_str);
3698 888b7d99 2020-12-26 stsp free(refs_str);
3699 07862c20 2018-09-15 stsp return err;
3700 f42b1b34 2018-03-12 stsp }
3701 5c860e29 2018-03-12 stsp
3702 f42b1b34 2018-03-12 stsp static const struct got_error *
3703 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3704 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3705 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3706 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3707 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3708 f42b1b34 2018-03-12 stsp {
3709 f42b1b34 2018-03-12 stsp const struct got_error *err;
3710 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3711 6841bf13 2019-11-29 kn regex_t regex;
3712 6841bf13 2019-11-29 kn int have_match;
3713 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3714 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3715 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3716 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3717 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3718 dbec59df 2020-04-18 stsp
3719 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3720 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3721 372ccdbb 2018-06-10 stsp
3722 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3723 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3724 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3725 6841bf13 2019-11-29 kn
3726 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3727 f42b1b34 2018-03-12 stsp if (err)
3728 f42b1b34 2018-03-12 stsp return err;
3729 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3730 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3731 372ccdbb 2018-06-10 stsp if (err)
3732 fcc85cad 2018-07-22 stsp goto done;
3733 656b1f76 2019-05-11 jcs for (;;) {
3734 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3735 8bf5b3c9 2018-03-17 stsp
3736 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3737 84453469 2018-11-11 stsp break;
3738 84453469 2018-11-11 stsp
3739 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3740 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3741 372ccdbb 2018-06-10 stsp if (err) {
3742 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3743 9ba79e04 2018-06-11 stsp err = NULL;
3744 ee780d5c 2020-01-04 stsp break;
3745 7e665116 2018-04-02 stsp }
3746 b43fbaa0 2018-06-11 stsp if (id == NULL)
3747 7e665116 2018-04-02 stsp break;
3748 b43fbaa0 2018-06-11 stsp
3749 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3750 b43fbaa0 2018-06-11 stsp if (err)
3751 fcc85cad 2018-07-22 stsp break;
3752 6841bf13 2019-11-29 kn
3753 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3754 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3755 0208f208 2020-05-05 stsp if (err)
3756 0208f208 2020-05-05 stsp break;
3757 0208f208 2020-05-05 stsp }
3758 0208f208 2020-05-05 stsp
3759 6841bf13 2019-11-29 kn if (search_pattern) {
3760 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3761 6841bf13 2019-11-29 kn if (err) {
3762 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3763 6841bf13 2019-11-29 kn break;
3764 6841bf13 2019-11-29 kn }
3765 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3766 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3767 0208f208 2020-05-05 stsp &changed_paths, &regex);
3768 6841bf13 2019-11-29 kn if (have_match == 0) {
3769 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3770 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3771 0208f208 2020-05-05 stsp free((char *)pe->path);
3772 0208f208 2020-05-05 stsp free(pe->data);
3773 0208f208 2020-05-05 stsp }
3774 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3775 6841bf13 2019-11-29 kn continue;
3776 6841bf13 2019-11-29 kn }
3777 6841bf13 2019-11-29 kn }
3778 6841bf13 2019-11-29 kn
3779 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3780 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3781 dbec59df 2020-04-18 stsp if (err)
3782 dbec59df 2020-04-18 stsp break;
3783 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3784 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3785 dbec59df 2020-04-18 stsp } else {
3786 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3787 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3788 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3789 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3790 dbec59df 2020-04-18 stsp if (err)
3791 dbec59df 2020-04-18 stsp break;
3792 dbec59df 2020-04-18 stsp }
3793 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3794 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3795 7e665116 2018-04-02 stsp break;
3796 0208f208 2020-05-05 stsp
3797 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3798 0208f208 2020-05-05 stsp free((char *)pe->path);
3799 0208f208 2020-05-05 stsp free(pe->data);
3800 0208f208 2020-05-05 stsp }
3801 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3802 31cedeaf 2018-09-15 stsp }
3803 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3804 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3805 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3806 dbec59df 2020-04-18 stsp if (err)
3807 dbec59df 2020-04-18 stsp break;
3808 502b9684 2020-07-31 stsp if (show_changed_paths) {
3809 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3810 502b9684 2020-07-31 stsp commit, repo);
3811 502b9684 2020-07-31 stsp if (err)
3812 502b9684 2020-07-31 stsp break;
3813 502b9684 2020-07-31 stsp }
3814 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3815 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3816 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3817 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3818 dbec59df 2020-04-18 stsp if (err)
3819 dbec59df 2020-04-18 stsp break;
3820 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3821 502b9684 2020-07-31 stsp free((char *)pe->path);
3822 502b9684 2020-07-31 stsp free(pe->data);
3823 502b9684 2020-07-31 stsp }
3824 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3825 dbec59df 2020-04-18 stsp }
3826 dbec59df 2020-04-18 stsp }
3827 fcc85cad 2018-07-22 stsp done:
3828 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
3829 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
3830 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3831 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3832 dbec59df 2020-04-18 stsp }
3833 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3834 0208f208 2020-05-05 stsp free((char *)pe->path);
3835 0208f208 2020-05-05 stsp free(pe->data);
3836 0208f208 2020-05-05 stsp }
3837 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3838 6841bf13 2019-11-29 kn if (search_pattern)
3839 6841bf13 2019-11-29 kn regfree(&regex);
3840 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3841 f42b1b34 2018-03-12 stsp return err;
3842 f42b1b34 2018-03-12 stsp }
3843 5c860e29 2018-03-12 stsp
3844 4ed7e80c 2018-05-20 stsp __dead static void
3845 6f3d1eb0 2018-03-12 stsp usage_log(void)
3846 6f3d1eb0 2018-03-12 stsp {
3847 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3848 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3849 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3850 6f3d1eb0 2018-03-12 stsp exit(1);
3851 b1ebc001 2019-08-13 stsp }
3852 b1ebc001 2019-08-13 stsp
3853 b1ebc001 2019-08-13 stsp static int
3854 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3855 b1ebc001 2019-08-13 stsp {
3856 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3857 b1ebc001 2019-08-13 stsp long long n;
3858 b1ebc001 2019-08-13 stsp const char *errstr;
3859 b1ebc001 2019-08-13 stsp
3860 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3861 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3862 b1ebc001 2019-08-13 stsp return 0;
3863 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3864 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3865 b1ebc001 2019-08-13 stsp return 0;
3866 b1ebc001 2019-08-13 stsp return n;
3867 6f3d1eb0 2018-03-12 stsp }
3868 6f3d1eb0 2018-03-12 stsp
3869 4ed7e80c 2018-05-20 stsp static const struct got_error *
3870 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3871 f42b1b34 2018-03-12 stsp {
3872 f42b1b34 2018-03-12 stsp const struct got_error *error;
3873 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3874 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3875 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3876 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3877 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3878 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3879 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3880 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3881 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3882 64a96a6d 2018-04-01 stsp const char *errstr;
3883 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3884 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3885 1b3893a2 2019-03-18 stsp
3886 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3887 5c860e29 2018-03-12 stsp
3888 6715a751 2018-03-16 stsp #ifndef PROFILE
3889 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3890 6098196c 2019-01-04 stsp NULL)
3891 ad242220 2018-09-08 stsp == -1)
3892 f42b1b34 2018-03-12 stsp err(1, "pledge");
3893 6715a751 2018-03-16 stsp #endif
3894 79109fed 2018-03-27 stsp
3895 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3896 b1ebc001 2019-08-13 stsp
3897 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3898 79109fed 2018-03-27 stsp switch (ch) {
3899 79109fed 2018-03-27 stsp case 'p':
3900 79109fed 2018-03-27 stsp show_patch = 1;
3901 d142fc45 2018-04-01 stsp break;
3902 0208f208 2020-05-05 stsp case 'P':
3903 0208f208 2020-05-05 stsp show_changed_paths = 1;
3904 0208f208 2020-05-05 stsp break;
3905 d142fc45 2018-04-01 stsp case 'c':
3906 d142fc45 2018-04-01 stsp start_commit = optarg;
3907 64a96a6d 2018-04-01 stsp break;
3908 c0cc5c62 2018-10-18 stsp case 'C':
3909 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3910 4a8520aa 2018-10-18 stsp &errstr);
3911 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3912 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3913 c0cc5c62 2018-10-18 stsp break;
3914 64a96a6d 2018-04-01 stsp case 'l':
3915 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3916 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3917 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3918 cc54c501 2019-07-15 stsp break;
3919 48c8c60d 2020-01-27 stsp case 'b':
3920 48c8c60d 2020-01-27 stsp log_branches = 1;
3921 a0603db2 2018-06-10 stsp break;
3922 04ca23f4 2018-07-16 stsp case 'r':
3923 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3924 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3925 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3926 9ba1d308 2019-10-21 stsp optarg);
3927 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3928 04ca23f4 2018-07-16 stsp break;
3929 dbec59df 2020-04-18 stsp case 'R':
3930 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3931 dbec59df 2020-04-18 stsp break;
3932 6841bf13 2019-11-29 kn case 's':
3933 6841bf13 2019-11-29 kn search_pattern = optarg;
3934 6841bf13 2019-11-29 kn break;
3935 d1fe46f9 2020-04-18 stsp case 'x':
3936 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3937 d1fe46f9 2020-04-18 stsp break;
3938 79109fed 2018-03-27 stsp default:
3939 2deda0b9 2019-03-07 stsp usage_log();
3940 79109fed 2018-03-27 stsp /* NOTREACHED */
3941 79109fed 2018-03-27 stsp }
3942 79109fed 2018-03-27 stsp }
3943 79109fed 2018-03-27 stsp
3944 79109fed 2018-03-27 stsp argc -= optind;
3945 79109fed 2018-03-27 stsp argv += optind;
3946 f42b1b34 2018-03-12 stsp
3947 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3948 dc1edbfa 2019-11-29 kn diff_context = 3;
3949 dc1edbfa 2019-11-29 kn else if (!show_patch)
3950 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3951 dc1edbfa 2019-11-29 kn
3952 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3953 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3954 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3955 04ca23f4 2018-07-16 stsp goto done;
3956 04ca23f4 2018-07-16 stsp }
3957 cffc0aa4 2019-02-05 stsp
3958 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3959 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3960 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3961 50f2fada 2020-04-24 stsp goto done;
3962 50f2fada 2020-04-24 stsp error = NULL;
3963 50f2fada 2020-04-24 stsp }
3964 cffc0aa4 2019-02-05 stsp
3965 603cdeb0 2020-10-22 stsp if (argc == 1) {
3966 e7301579 2019-03-18 stsp if (worktree) {
3967 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3968 e7301579 2019-03-18 stsp argv[0]);
3969 e7301579 2019-03-18 stsp if (error)
3970 e7301579 2019-03-18 stsp goto done;
3971 e7301579 2019-03-18 stsp } else {
3972 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3973 e7301579 2019-03-18 stsp if (path == NULL) {
3974 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3975 e7301579 2019-03-18 stsp goto done;
3976 e7301579 2019-03-18 stsp }
3977 e7301579 2019-03-18 stsp }
3978 603cdeb0 2020-10-22 stsp } else if (argc != 0)
3979 cbd1af7a 2019-03-18 stsp usage_log();
3980 cbd1af7a 2019-03-18 stsp
3981 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3982 5486daa2 2019-05-11 stsp repo_path = worktree ?
3983 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3984 5486daa2 2019-05-11 stsp }
3985 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3986 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3987 cffc0aa4 2019-02-05 stsp goto done;
3988 04ca23f4 2018-07-16 stsp }
3989 6098196c 2019-01-04 stsp
3990 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3991 d7d4f210 2018-03-12 stsp if (error != NULL)
3992 04ca23f4 2018-07-16 stsp goto done;
3993 f42b1b34 2018-03-12 stsp
3994 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3995 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3996 c02c541e 2019-03-29 stsp if (error)
3997 c02c541e 2019-03-29 stsp goto done;
3998 c02c541e 2019-03-29 stsp
3999 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4000 84de9106 2020-12-26 stsp if (error)
4001 84de9106 2020-12-26 stsp goto done;
4002 84de9106 2020-12-26 stsp
4003 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4004 888b7d99 2020-12-26 stsp if (error)
4005 888b7d99 2020-12-26 stsp goto done;
4006 888b7d99 2020-12-26 stsp
4007 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4008 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4009 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4010 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4011 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4012 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4013 3235492e 2018-04-01 stsp if (error != NULL)
4014 d1fe46f9 2020-04-18 stsp goto done;
4015 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4016 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4017 3235492e 2018-04-01 stsp if (error != NULL)
4018 d1fe46f9 2020-04-18 stsp goto done;
4019 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4020 d1fe46f9 2020-04-18 stsp start_id);
4021 d1fe46f9 2020-04-18 stsp if (error != NULL)
4022 d1fe46f9 2020-04-18 stsp goto done;
4023 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4024 3235492e 2018-04-01 stsp } else {
4025 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4026 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4027 d1fe46f9 2020-04-18 stsp if (error != NULL)
4028 d1fe46f9 2020-04-18 stsp goto done;
4029 3235492e 2018-04-01 stsp }
4030 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4031 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4032 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4033 d1fe46f9 2020-04-18 stsp if (error != NULL)
4034 d1fe46f9 2020-04-18 stsp goto done;
4035 d1fe46f9 2020-04-18 stsp }
4036 04ca23f4 2018-07-16 stsp
4037 deeabeae 2019-08-27 stsp if (worktree) {
4038 603cdeb0 2020-10-22 stsp /*
4039 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4040 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4041 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4042 603cdeb0 2020-10-22 stsp */
4043 603cdeb0 2020-10-22 stsp if (path) {
4044 603cdeb0 2020-10-22 stsp const char *prefix;
4045 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4046 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4047 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4048 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4049 603cdeb0 2020-10-22 stsp goto done;
4050 603cdeb0 2020-10-22 stsp }
4051 603cdeb0 2020-10-22 stsp }
4052 deeabeae 2019-08-27 stsp } else
4053 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4054 8fa913ec 2020-11-14 stsp path ? path : "");
4055 04ca23f4 2018-07-16 stsp if (error != NULL)
4056 04ca23f4 2018-07-16 stsp goto done;
4057 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4058 04ca23f4 2018-07-16 stsp free(path);
4059 04ca23f4 2018-07-16 stsp path = in_repo_path;
4060 04ca23f4 2018-07-16 stsp }
4061 199a4027 2019-02-02 stsp
4062 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4063 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4064 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4065 04ca23f4 2018-07-16 stsp done:
4066 04ca23f4 2018-07-16 stsp free(path);
4067 04ca23f4 2018-07-16 stsp free(repo_path);
4068 04ca23f4 2018-07-16 stsp free(cwd);
4069 cffc0aa4 2019-02-05 stsp if (worktree)
4070 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4071 ad242220 2018-09-08 stsp if (repo) {
4072 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4073 ad242220 2018-09-08 stsp if (error == NULL)
4074 1d0f4054 2021-06-17 stsp error = close_err;
4075 ad242220 2018-09-08 stsp }
4076 888b7d99 2020-12-26 stsp if (refs_idmap)
4077 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4078 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4079 8bf5b3c9 2018-03-17 stsp return error;
4080 5c860e29 2018-03-12 stsp }
4081 5c860e29 2018-03-12 stsp
4082 4ed7e80c 2018-05-20 stsp __dead static void
4083 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4084 3f8b7d6a 2018-04-01 stsp {
4085 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4086 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4087 3f8b7d6a 2018-04-01 stsp exit(1);
4088 b00d56cd 2018-04-01 stsp }
4089 b00d56cd 2018-04-01 stsp
4090 b72f483a 2019-02-05 stsp struct print_diff_arg {
4091 b72f483a 2019-02-05 stsp struct got_repository *repo;
4092 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4093 b72f483a 2019-02-05 stsp int diff_context;
4094 3fc0c068 2019-02-10 stsp const char *id_str;
4095 3fc0c068 2019-02-10 stsp int header_shown;
4096 98eaaa12 2019-08-03 stsp int diff_staged;
4097 63035f9f 2019-10-06 stsp int ignore_whitespace;
4098 64453f7e 2020-11-21 stsp int force_text_diff;
4099 b72f483a 2019-02-05 stsp };
4100 39449a05 2020-07-23 stsp
4101 39449a05 2020-07-23 stsp /*
4102 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4103 39449a05 2020-07-23 stsp * it as content to the diff engine.
4104 39449a05 2020-07-23 stsp */
4105 39449a05 2020-07-23 stsp static const struct got_error *
4106 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4107 39449a05 2020-07-23 stsp const char *abspath)
4108 39449a05 2020-07-23 stsp {
4109 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4110 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4111 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4112 39449a05 2020-07-23 stsp
4113 39449a05 2020-07-23 stsp *fd = -1;
4114 39449a05 2020-07-23 stsp
4115 39449a05 2020-07-23 stsp if (dirfd != -1) {
4116 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4117 39449a05 2020-07-23 stsp if (target_len == -1)
4118 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4119 39449a05 2020-07-23 stsp } else {
4120 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4121 39449a05 2020-07-23 stsp if (target_len == -1)
4122 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4123 39449a05 2020-07-23 stsp }
4124 39449a05 2020-07-23 stsp
4125 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4126 39449a05 2020-07-23 stsp if (*fd == -1)
4127 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4128 39449a05 2020-07-23 stsp
4129 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4130 39449a05 2020-07-23 stsp if (outlen == -1) {
4131 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4132 39449a05 2020-07-23 stsp goto done;
4133 39449a05 2020-07-23 stsp }
4134 39449a05 2020-07-23 stsp
4135 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4136 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4137 39449a05 2020-07-23 stsp goto done;
4138 39449a05 2020-07-23 stsp }
4139 39449a05 2020-07-23 stsp done:
4140 39449a05 2020-07-23 stsp if (err) {
4141 39449a05 2020-07-23 stsp close(*fd);
4142 39449a05 2020-07-23 stsp *fd = -1;
4143 39449a05 2020-07-23 stsp }
4144 39449a05 2020-07-23 stsp return err;
4145 39449a05 2020-07-23 stsp }
4146 b72f483a 2019-02-05 stsp
4147 4ed7e80c 2018-05-20 stsp static const struct got_error *
4148 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4149 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4150 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4151 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4152 b72f483a 2019-02-05 stsp {
4153 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4154 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4155 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4156 12463d8b 2019-12-13 stsp int fd = -1;
4157 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4158 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4159 b72f483a 2019-02-05 stsp struct stat sb;
4160 b72f483a 2019-02-05 stsp
4161 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4162 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4163 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4164 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4165 98eaaa12 2019-08-03 stsp return NULL;
4166 98eaaa12 2019-08-03 stsp } else {
4167 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4168 98eaaa12 2019-08-03 stsp return NULL;
4169 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4170 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4171 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4172 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4173 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4174 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4175 98eaaa12 2019-08-03 stsp return NULL;
4176 98eaaa12 2019-08-03 stsp }
4177 3fc0c068 2019-02-10 stsp
4178 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4179 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4180 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4181 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4182 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4183 3fc0c068 2019-02-10 stsp }
4184 b72f483a 2019-02-05 stsp
4185 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4186 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4187 98eaaa12 2019-08-03 stsp switch (staged_status) {
4188 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4189 98eaaa12 2019-08-03 stsp label1 = path;
4190 98eaaa12 2019-08-03 stsp label2 = path;
4191 98eaaa12 2019-08-03 stsp break;
4192 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4193 98eaaa12 2019-08-03 stsp label2 = path;
4194 98eaaa12 2019-08-03 stsp break;
4195 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4196 98eaaa12 2019-08-03 stsp label1 = path;
4197 98eaaa12 2019-08-03 stsp break;
4198 98eaaa12 2019-08-03 stsp default:
4199 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4200 98eaaa12 2019-08-03 stsp }
4201 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4202 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4203 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4204 98eaaa12 2019-08-03 stsp }
4205 98eaaa12 2019-08-03 stsp
4206 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4207 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4208 4ce46740 2019-08-08 stsp char *id_str;
4209 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4210 408b4ebc 2019-08-03 stsp 8192);
4211 4ce46740 2019-08-08 stsp if (err)
4212 4ce46740 2019-08-08 stsp goto done;
4213 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4214 4ce46740 2019-08-08 stsp if (err)
4215 4ce46740 2019-08-08 stsp goto done;
4216 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4217 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4218 4ce46740 2019-08-08 stsp free(id_str);
4219 4ce46740 2019-08-08 stsp goto done;
4220 4ce46740 2019-08-08 stsp }
4221 4ce46740 2019-08-08 stsp free(id_str);
4222 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4223 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4224 4ce46740 2019-08-08 stsp if (err)
4225 4ce46740 2019-08-08 stsp goto done;
4226 4ce46740 2019-08-08 stsp }
4227 d00136be 2019-03-26 stsp
4228 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4229 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4230 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4231 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4232 2ec1f75b 2019-03-26 stsp goto done;
4233 2ec1f75b 2019-03-26 stsp }
4234 b72f483a 2019-02-05 stsp
4235 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4236 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4237 12463d8b 2019-12-13 stsp if (fd == -1) {
4238 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4239 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4240 39449a05 2020-07-23 stsp abspath);
4241 39449a05 2020-07-23 stsp goto done;
4242 39449a05 2020-07-23 stsp }
4243 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4244 39449a05 2020-07-23 stsp de_name, abspath);
4245 39449a05 2020-07-23 stsp if (err)
4246 39449a05 2020-07-23 stsp goto done;
4247 12463d8b 2019-12-13 stsp }
4248 12463d8b 2019-12-13 stsp } else {
4249 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4250 12463d8b 2019-12-13 stsp if (fd == -1) {
4251 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4252 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4253 39449a05 2020-07-23 stsp abspath);
4254 39449a05 2020-07-23 stsp goto done;
4255 39449a05 2020-07-23 stsp }
4256 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4257 39449a05 2020-07-23 stsp de_name, abspath);
4258 39449a05 2020-07-23 stsp if (err)
4259 39449a05 2020-07-23 stsp goto done;
4260 12463d8b 2019-12-13 stsp }
4261 12463d8b 2019-12-13 stsp }
4262 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4263 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4264 2ec1f75b 2019-03-26 stsp goto done;
4265 2ec1f75b 2019-03-26 stsp }
4266 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4267 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4268 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4269 2ec1f75b 2019-03-26 stsp goto done;
4270 2ec1f75b 2019-03-26 stsp }
4271 12463d8b 2019-12-13 stsp fd = -1;
4272 2ec1f75b 2019-03-26 stsp } else
4273 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4274 b72f483a 2019-02-05 stsp
4275 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4276 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4277 b72f483a 2019-02-05 stsp done:
4278 b72f483a 2019-02-05 stsp if (blob1)
4279 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4280 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4281 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4282 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4283 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4284 b72f483a 2019-02-05 stsp free(abspath);
4285 927df6b7 2019-02-10 stsp return err;
4286 927df6b7 2019-02-10 stsp }
4287 927df6b7 2019-02-10 stsp
4288 927df6b7 2019-02-10 stsp static const struct got_error *
4289 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4290 b00d56cd 2018-04-01 stsp {
4291 b00d56cd 2018-04-01 stsp const struct got_error *error;
4292 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4293 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4294 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4295 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4296 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4297 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4298 15a94983 2018-12-23 stsp int type1, type2;
4299 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4300 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4301 c0cc5c62 2018-10-18 stsp const char *errstr;
4302 927df6b7 2019-02-10 stsp char *path = NULL;
4303 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4304 b00d56cd 2018-04-01 stsp
4305 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4306 84de9106 2020-12-26 stsp
4307 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4308 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4309 25eccc22 2019-01-04 stsp NULL) == -1)
4310 b00d56cd 2018-04-01 stsp err(1, "pledge");
4311 b00d56cd 2018-04-01 stsp #endif
4312 b00d56cd 2018-04-01 stsp
4313 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4314 b00d56cd 2018-04-01 stsp switch (ch) {
4315 64453f7e 2020-11-21 stsp case 'a':
4316 64453f7e 2020-11-21 stsp force_text_diff = 1;
4317 64453f7e 2020-11-21 stsp break;
4318 c0cc5c62 2018-10-18 stsp case 'C':
4319 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4320 dfcab68b 2019-11-29 kn &errstr);
4321 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4322 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4323 c0cc5c62 2018-10-18 stsp break;
4324 b72f483a 2019-02-05 stsp case 'r':
4325 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4326 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4327 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4328 9ba1d308 2019-10-21 stsp optarg);
4329 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4330 b72f483a 2019-02-05 stsp break;
4331 98eaaa12 2019-08-03 stsp case 's':
4332 98eaaa12 2019-08-03 stsp diff_staged = 1;
4333 63035f9f 2019-10-06 stsp break;
4334 63035f9f 2019-10-06 stsp case 'w':
4335 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4336 98eaaa12 2019-08-03 stsp break;
4337 b00d56cd 2018-04-01 stsp default:
4338 2deda0b9 2019-03-07 stsp usage_diff();
4339 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4340 b00d56cd 2018-04-01 stsp }
4341 b00d56cd 2018-04-01 stsp }
4342 b00d56cd 2018-04-01 stsp
4343 b00d56cd 2018-04-01 stsp argc -= optind;
4344 b00d56cd 2018-04-01 stsp argv += optind;
4345 b00d56cd 2018-04-01 stsp
4346 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4347 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4348 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4349 b72f483a 2019-02-05 stsp goto done;
4350 b72f483a 2019-02-05 stsp }
4351 927df6b7 2019-02-10 stsp if (argc <= 1) {
4352 b72f483a 2019-02-05 stsp if (repo_path)
4353 b72f483a 2019-02-05 stsp errx(1,
4354 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4355 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4356 fa51e947 2020-03-27 stsp if (error) {
4357 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4358 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4359 fa51e947 2020-03-27 stsp cwd);
4360 1f03b8da 2020-03-20 stsp goto done;
4361 fa51e947 2020-03-27 stsp }
4362 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4363 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4364 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4365 927df6b7 2019-02-10 stsp goto done;
4366 927df6b7 2019-02-10 stsp }
4367 927df6b7 2019-02-10 stsp if (argc == 1) {
4368 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4369 cbd1af7a 2019-03-18 stsp argv[0]);
4370 927df6b7 2019-02-10 stsp if (error)
4371 927df6b7 2019-02-10 stsp goto done;
4372 927df6b7 2019-02-10 stsp } else {
4373 927df6b7 2019-02-10 stsp path = strdup("");
4374 927df6b7 2019-02-10 stsp if (path == NULL) {
4375 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4376 927df6b7 2019-02-10 stsp goto done;
4377 927df6b7 2019-02-10 stsp }
4378 927df6b7 2019-02-10 stsp }
4379 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4380 98eaaa12 2019-08-03 stsp if (diff_staged)
4381 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4382 98eaaa12 2019-08-03 stsp "objects in repository");
4383 15a94983 2018-12-23 stsp id_str1 = argv[0];
4384 15a94983 2018-12-23 stsp id_str2 = argv[1];
4385 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4386 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4387 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4388 30db809c 2019-06-05 stsp goto done;
4389 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4390 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4391 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4392 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4393 1a1242a9 2021-04-01 kn goto done;
4394 30db809c 2019-06-05 stsp }
4395 30db809c 2019-06-05 stsp }
4396 b00d56cd 2018-04-01 stsp } else
4397 b00d56cd 2018-04-01 stsp usage_diff();
4398 b00d56cd 2018-04-01 stsp
4399 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4400 76089277 2018-04-01 stsp free(repo_path);
4401 b00d56cd 2018-04-01 stsp if (error != NULL)
4402 b00d56cd 2018-04-01 stsp goto done;
4403 b00d56cd 2018-04-01 stsp
4404 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4405 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4406 c02c541e 2019-03-29 stsp if (error)
4407 c02c541e 2019-03-29 stsp goto done;
4408 c02c541e 2019-03-29 stsp
4409 30db809c 2019-06-05 stsp if (argc <= 1) {
4410 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4411 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4412 b72f483a 2019-02-05 stsp char *id_str;
4413 72ea6654 2019-07-27 stsp
4414 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4415 72ea6654 2019-07-27 stsp
4416 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4417 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4418 b72f483a 2019-02-05 stsp if (error)
4419 b72f483a 2019-02-05 stsp goto done;
4420 b72f483a 2019-02-05 stsp arg.repo = repo;
4421 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4422 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4423 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4424 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4425 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4426 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4427 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4428 b72f483a 2019-02-05 stsp
4429 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4430 72ea6654 2019-07-27 stsp if (error)
4431 72ea6654 2019-07-27 stsp goto done;
4432 72ea6654 2019-07-27 stsp
4433 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4434 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4435 b72f483a 2019-02-05 stsp free(id_str);
4436 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4437 b72f483a 2019-02-05 stsp goto done;
4438 b72f483a 2019-02-05 stsp }
4439 84de9106 2020-12-26 stsp
4440 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4441 84de9106 2020-12-26 stsp if (error)
4442 84de9106 2020-12-26 stsp return error;
4443 b72f483a 2019-02-05 stsp
4444 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4445 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4446 0adb7196 2019-08-11 stsp if (error)
4447 0adb7196 2019-08-11 stsp goto done;
4448 b00d56cd 2018-04-01 stsp
4449 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4450 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4451 0adb7196 2019-08-11 stsp if (error)
4452 0adb7196 2019-08-11 stsp goto done;
4453 b00d56cd 2018-04-01 stsp
4454 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4455 15a94983 2018-12-23 stsp if (error)
4456 15a94983 2018-12-23 stsp goto done;
4457 15a94983 2018-12-23 stsp
4458 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4459 15a94983 2018-12-23 stsp if (error)
4460 15a94983 2018-12-23 stsp goto done;
4461 15a94983 2018-12-23 stsp
4462 15a94983 2018-12-23 stsp if (type1 != type2) {
4463 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4464 b00d56cd 2018-04-01 stsp goto done;
4465 b00d56cd 2018-04-01 stsp }
4466 b00d56cd 2018-04-01 stsp
4467 15a94983 2018-12-23 stsp switch (type1) {
4468 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4469 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4470 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4471 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4472 b00d56cd 2018-04-01 stsp break;
4473 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4474 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4475 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4476 64453f7e 2020-11-21 stsp repo, stdout);
4477 b00d56cd 2018-04-01 stsp break;
4478 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4479 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4480 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4481 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4482 64453f7e 2020-11-21 stsp stdout);
4483 b00d56cd 2018-04-01 stsp break;
4484 b00d56cd 2018-04-01 stsp default:
4485 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4486 b00d56cd 2018-04-01 stsp }
4487 b00d56cd 2018-04-01 stsp done:
4488 5e70831e 2019-05-28 stsp free(label1);
4489 5e70831e 2019-05-28 stsp free(label2);
4490 15a94983 2018-12-23 stsp free(id1);
4491 15a94983 2018-12-23 stsp free(id2);
4492 927df6b7 2019-02-10 stsp free(path);
4493 b72f483a 2019-02-05 stsp if (worktree)
4494 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4495 ad242220 2018-09-08 stsp if (repo) {
4496 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4497 ad242220 2018-09-08 stsp if (error == NULL)
4498 1d0f4054 2021-06-17 stsp error = close_err;
4499 ad242220 2018-09-08 stsp }
4500 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4501 b00d56cd 2018-04-01 stsp return error;
4502 404c43c4 2018-06-21 stsp }
4503 404c43c4 2018-06-21 stsp
4504 404c43c4 2018-06-21 stsp __dead static void
4505 404c43c4 2018-06-21 stsp usage_blame(void)
4506 404c43c4 2018-06-21 stsp {
4507 9270e621 2019-02-05 stsp fprintf(stderr,
4508 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4509 404c43c4 2018-06-21 stsp getprogname());
4510 404c43c4 2018-06-21 stsp exit(1);
4511 b00d56cd 2018-04-01 stsp }
4512 b00d56cd 2018-04-01 stsp
4513 28315671 2019-08-14 stsp struct blame_line {
4514 28315671 2019-08-14 stsp int annotated;
4515 28315671 2019-08-14 stsp char *id_str;
4516 82f6abb8 2019-08-14 stsp char *committer;
4517 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4518 28315671 2019-08-14 stsp };
4519 28315671 2019-08-14 stsp
4520 28315671 2019-08-14 stsp struct blame_cb_args {
4521 28315671 2019-08-14 stsp struct blame_line *lines;
4522 28315671 2019-08-14 stsp int nlines;
4523 7ef28ff8 2019-08-14 stsp int nlines_prec;
4524 28315671 2019-08-14 stsp int lineno_cur;
4525 28315671 2019-08-14 stsp off_t *line_offsets;
4526 28315671 2019-08-14 stsp FILE *f;
4527 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4528 28315671 2019-08-14 stsp };
4529 28315671 2019-08-14 stsp
4530 404c43c4 2018-06-21 stsp static const struct got_error *
4531 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4532 28315671 2019-08-14 stsp {
4533 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4534 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4535 28315671 2019-08-14 stsp struct blame_line *bline;
4536 82f6abb8 2019-08-14 stsp char *line = NULL;
4537 28315671 2019-08-14 stsp size_t linesize = 0;
4538 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4539 28315671 2019-08-14 stsp off_t offset;
4540 bcb49d15 2019-08-14 stsp struct tm tm;
4541 bcb49d15 2019-08-14 stsp time_t committer_time;
4542 28315671 2019-08-14 stsp
4543 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4544 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4545 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4546 28315671 2019-08-14 stsp
4547 28315671 2019-08-14 stsp if (sigint_received)
4548 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4549 28315671 2019-08-14 stsp
4550 2839f8b9 2019-08-18 stsp if (lineno == -1)
4551 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4552 2839f8b9 2019-08-18 stsp
4553 28315671 2019-08-14 stsp /* Annotate this line. */
4554 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4555 28315671 2019-08-14 stsp if (bline->annotated)
4556 28315671 2019-08-14 stsp return NULL;
4557 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4558 28315671 2019-08-14 stsp if (err)
4559 28315671 2019-08-14 stsp return err;
4560 82f6abb8 2019-08-14 stsp
4561 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4562 82f6abb8 2019-08-14 stsp if (err)
4563 82f6abb8 2019-08-14 stsp goto done;
4564 82f6abb8 2019-08-14 stsp
4565 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4566 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4567 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4568 bcb49d15 2019-08-14 stsp goto done;
4569 bcb49d15 2019-08-14 stsp }
4570 bcb49d15 2019-08-14 stsp
4571 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4572 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4573 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4574 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4575 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4576 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4577 82f6abb8 2019-08-14 stsp goto done;
4578 82f6abb8 2019-08-14 stsp }
4579 28315671 2019-08-14 stsp bline->annotated = 1;
4580 28315671 2019-08-14 stsp
4581 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4582 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4583 28315671 2019-08-14 stsp if (!bline->annotated)
4584 82f6abb8 2019-08-14 stsp goto done;
4585 28315671 2019-08-14 stsp
4586 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4587 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4588 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4589 82f6abb8 2019-08-14 stsp goto done;
4590 82f6abb8 2019-08-14 stsp }
4591 28315671 2019-08-14 stsp
4592 28315671 2019-08-14 stsp while (bline->annotated) {
4593 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4594 82f6abb8 2019-08-14 stsp size_t len;
4595 82f6abb8 2019-08-14 stsp
4596 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4597 28315671 2019-08-14 stsp if (ferror(a->f))
4598 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4599 28315671 2019-08-14 stsp break;
4600 28315671 2019-08-14 stsp }
4601 82f6abb8 2019-08-14 stsp
4602 82f6abb8 2019-08-14 stsp committer = bline->committer;
4603 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4604 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4605 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4606 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4607 82f6abb8 2019-08-14 stsp if (at)
4608 82f6abb8 2019-08-14 stsp *at = '\0';
4609 82f6abb8 2019-08-14 stsp len = strlen(committer);
4610 82f6abb8 2019-08-14 stsp if (len >= 9)
4611 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4612 28315671 2019-08-14 stsp
4613 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4614 28315671 2019-08-14 stsp if (nl)
4615 28315671 2019-08-14 stsp *nl = '\0';
4616 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4617 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4618 28315671 2019-08-14 stsp
4619 28315671 2019-08-14 stsp a->lineno_cur++;
4620 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4621 28315671 2019-08-14 stsp }
4622 82f6abb8 2019-08-14 stsp done:
4623 82f6abb8 2019-08-14 stsp if (commit)
4624 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4625 28315671 2019-08-14 stsp free(line);
4626 28315671 2019-08-14 stsp return err;
4627 28315671 2019-08-14 stsp }
4628 28315671 2019-08-14 stsp
4629 28315671 2019-08-14 stsp static const struct got_error *
4630 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4631 404c43c4 2018-06-21 stsp {
4632 404c43c4 2018-06-21 stsp const struct got_error *error;
4633 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4634 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4635 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4636 0587e10c 2020-07-23 stsp char *link_target = NULL;
4637 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4638 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4639 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4640 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4641 28315671 2019-08-14 stsp struct blame_cb_args bca;
4642 28315671 2019-08-14 stsp int ch, obj_type, i;
4643 be659d10 2020-11-18 stsp off_t filesize;
4644 90f3c347 2019-08-19 stsp
4645 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4646 404c43c4 2018-06-21 stsp
4647 404c43c4 2018-06-21 stsp #ifndef PROFILE
4648 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4649 36e2fb66 2019-01-04 stsp NULL) == -1)
4650 404c43c4 2018-06-21 stsp err(1, "pledge");
4651 404c43c4 2018-06-21 stsp #endif
4652 404c43c4 2018-06-21 stsp
4653 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4654 404c43c4 2018-06-21 stsp switch (ch) {
4655 404c43c4 2018-06-21 stsp case 'c':
4656 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4657 404c43c4 2018-06-21 stsp break;
4658 66bea077 2018-08-02 stsp case 'r':
4659 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4660 66bea077 2018-08-02 stsp if (repo_path == NULL)
4661 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4662 9ba1d308 2019-10-21 stsp optarg);
4663 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4664 66bea077 2018-08-02 stsp break;
4665 404c43c4 2018-06-21 stsp default:
4666 2deda0b9 2019-03-07 stsp usage_blame();
4667 404c43c4 2018-06-21 stsp /* NOTREACHED */
4668 404c43c4 2018-06-21 stsp }
4669 404c43c4 2018-06-21 stsp }
4670 404c43c4 2018-06-21 stsp
4671 404c43c4 2018-06-21 stsp argc -= optind;
4672 404c43c4 2018-06-21 stsp argv += optind;
4673 404c43c4 2018-06-21 stsp
4674 a39318fd 2018-08-02 stsp if (argc == 1)
4675 404c43c4 2018-06-21 stsp path = argv[0];
4676 a39318fd 2018-08-02 stsp else
4677 404c43c4 2018-06-21 stsp usage_blame();
4678 404c43c4 2018-06-21 stsp
4679 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4680 66bea077 2018-08-02 stsp if (cwd == NULL) {
4681 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4682 66bea077 2018-08-02 stsp goto done;
4683 66bea077 2018-08-02 stsp }
4684 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4685 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4686 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4687 66bea077 2018-08-02 stsp goto done;
4688 0c06baac 2019-02-05 stsp else
4689 0c06baac 2019-02-05 stsp error = NULL;
4690 0c06baac 2019-02-05 stsp if (worktree) {
4691 0c06baac 2019-02-05 stsp repo_path =
4692 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4693 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4694 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4695 1f03b8da 2020-03-20 stsp if (error)
4696 1f03b8da 2020-03-20 stsp goto done;
4697 1f03b8da 2020-03-20 stsp }
4698 0c06baac 2019-02-05 stsp } else {
4699 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4700 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4701 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4702 0c06baac 2019-02-05 stsp goto done;
4703 0c06baac 2019-02-05 stsp }
4704 66bea077 2018-08-02 stsp }
4705 66bea077 2018-08-02 stsp }
4706 36e2fb66 2019-01-04 stsp
4707 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4708 c02c541e 2019-03-29 stsp if (error != NULL)
4709 404c43c4 2018-06-21 stsp goto done;
4710 404c43c4 2018-06-21 stsp
4711 0c06baac 2019-02-05 stsp if (worktree) {
4712 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4713 01740607 2020-11-04 stsp char *p;
4714 01740607 2020-11-04 stsp
4715 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4716 01740607 2020-11-04 stsp if (error)
4717 01740607 2020-11-04 stsp goto done;
4718 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4719 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4720 01740607 2020-11-04 stsp p) == -1) {
4721 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4722 01740607 2020-11-04 stsp free(p);
4723 0c06baac 2019-02-05 stsp goto done;
4724 0c06baac 2019-02-05 stsp }
4725 0c06baac 2019-02-05 stsp free(p);
4726 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4727 0c06baac 2019-02-05 stsp } else {
4728 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4729 01740607 2020-11-04 stsp if (error)
4730 01740607 2020-11-04 stsp goto done;
4731 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4732 0c06baac 2019-02-05 stsp }
4733 0c06baac 2019-02-05 stsp if (error)
4734 66bea077 2018-08-02 stsp goto done;
4735 66bea077 2018-08-02 stsp
4736 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4737 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4738 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4739 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4740 404c43c4 2018-06-21 stsp if (error != NULL)
4741 66bea077 2018-08-02 stsp goto done;
4742 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4743 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4744 404c43c4 2018-06-21 stsp if (error != NULL)
4745 66bea077 2018-08-02 stsp goto done;
4746 404c43c4 2018-06-21 stsp } else {
4747 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4748 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4749 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4750 84de9106 2020-12-26 stsp NULL);
4751 84de9106 2020-12-26 stsp if (error)
4752 84de9106 2020-12-26 stsp goto done;
4753 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4754 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4755 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4756 30837e32 2019-07-25 stsp if (error)
4757 66bea077 2018-08-02 stsp goto done;
4758 404c43c4 2018-06-21 stsp }
4759 404c43c4 2018-06-21 stsp
4760 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4761 0587e10c 2020-07-23 stsp commit_id, repo);
4762 0587e10c 2020-07-23 stsp if (error)
4763 0587e10c 2020-07-23 stsp goto done;
4764 0587e10c 2020-07-23 stsp
4765 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4766 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4767 28315671 2019-08-14 stsp if (error)
4768 28315671 2019-08-14 stsp goto done;
4769 28315671 2019-08-14 stsp
4770 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4771 28315671 2019-08-14 stsp if (error)
4772 28315671 2019-08-14 stsp goto done;
4773 28315671 2019-08-14 stsp
4774 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4775 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4776 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4777 28315671 2019-08-14 stsp goto done;
4778 28315671 2019-08-14 stsp }
4779 28315671 2019-08-14 stsp
4780 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4781 28315671 2019-08-14 stsp if (error)
4782 28315671 2019-08-14 stsp goto done;
4783 28315671 2019-08-14 stsp bca.f = got_opentemp();
4784 28315671 2019-08-14 stsp if (bca.f == NULL) {
4785 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4786 28315671 2019-08-14 stsp goto done;
4787 28315671 2019-08-14 stsp }
4788 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4789 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4790 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4791 28315671 2019-08-14 stsp goto done;
4792 28315671 2019-08-14 stsp
4793 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4794 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4795 b02560ec 2019-08-19 stsp bca.nlines--;
4796 b02560ec 2019-08-19 stsp
4797 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4798 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4799 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4800 28315671 2019-08-14 stsp goto done;
4801 28315671 2019-08-14 stsp }
4802 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4803 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4804 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4805 7ef28ff8 2019-08-14 stsp while (i > 0) {
4806 7ef28ff8 2019-08-14 stsp i /= 10;
4807 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4808 7ef28ff8 2019-08-14 stsp }
4809 82f6abb8 2019-08-14 stsp bca.repo = repo;
4810 7ef28ff8 2019-08-14 stsp
4811 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4812 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4813 404c43c4 2018-06-21 stsp done:
4814 66bea077 2018-08-02 stsp free(in_repo_path);
4815 0587e10c 2020-07-23 stsp free(link_target);
4816 66bea077 2018-08-02 stsp free(repo_path);
4817 66bea077 2018-08-02 stsp free(cwd);
4818 404c43c4 2018-06-21 stsp free(commit_id);
4819 28315671 2019-08-14 stsp free(obj_id);
4820 28315671 2019-08-14 stsp if (blob)
4821 28315671 2019-08-14 stsp got_object_blob_close(blob);
4822 0c06baac 2019-02-05 stsp if (worktree)
4823 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4824 ad242220 2018-09-08 stsp if (repo) {
4825 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4826 ad242220 2018-09-08 stsp if (error == NULL)
4827 1d0f4054 2021-06-17 stsp error = close_err;
4828 ad242220 2018-09-08 stsp }
4829 3affba96 2019-09-22 stsp if (bca.lines) {
4830 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4831 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4832 3affba96 2019-09-22 stsp free(bline->id_str);
4833 3affba96 2019-09-22 stsp free(bline->committer);
4834 3affba96 2019-09-22 stsp }
4835 3affba96 2019-09-22 stsp free(bca.lines);
4836 28315671 2019-08-14 stsp }
4837 28315671 2019-08-14 stsp free(bca.line_offsets);
4838 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4839 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4840 404c43c4 2018-06-21 stsp return error;
4841 5de5890b 2018-10-18 stsp }
4842 5de5890b 2018-10-18 stsp
4843 5de5890b 2018-10-18 stsp __dead static void
4844 5de5890b 2018-10-18 stsp usage_tree(void)
4845 5de5890b 2018-10-18 stsp {
4846 c1669e2e 2019-01-09 stsp fprintf(stderr,
4847 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4848 5de5890b 2018-10-18 stsp getprogname());
4849 5de5890b 2018-10-18 stsp exit(1);
4850 5de5890b 2018-10-18 stsp }
4851 5de5890b 2018-10-18 stsp
4852 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4853 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4854 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4855 c1669e2e 2019-01-09 stsp {
4856 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4857 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4858 848d6979 2019-08-12 stsp const char *modestr = "";
4859 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4860 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4861 5de5890b 2018-10-18 stsp
4862 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4863 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4864 c1669e2e 2019-01-09 stsp path++;
4865 c1669e2e 2019-01-09 stsp
4866 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4867 63c5ca5d 2019-08-24 stsp modestr = "$";
4868 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4869 0d6c6ee3 2020-05-20 stsp int i;
4870 0d6c6ee3 2020-05-20 stsp
4871 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4872 0d6c6ee3 2020-05-20 stsp if (err)
4873 0d6c6ee3 2020-05-20 stsp return err;
4874 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4875 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4876 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4877 0d6c6ee3 2020-05-20 stsp }
4878 0d6c6ee3 2020-05-20 stsp
4879 848d6979 2019-08-12 stsp modestr = "@";
4880 0d6c6ee3 2020-05-20 stsp }
4881 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4882 848d6979 2019-08-12 stsp modestr = "/";
4883 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4884 848d6979 2019-08-12 stsp modestr = "*";
4885 848d6979 2019-08-12 stsp
4886 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4887 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4888 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4889 0d6c6ee3 2020-05-20 stsp
4890 0d6c6ee3 2020-05-20 stsp free(link_target);
4891 0d6c6ee3 2020-05-20 stsp return NULL;
4892 c1669e2e 2019-01-09 stsp }
4893 c1669e2e 2019-01-09 stsp
4894 5de5890b 2018-10-18 stsp static const struct got_error *
4895 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4896 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4897 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4898 5de5890b 2018-10-18 stsp {
4899 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4900 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4901 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4902 56e0773d 2019-11-28 stsp int nentries, i;
4903 5de5890b 2018-10-18 stsp
4904 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4905 5de5890b 2018-10-18 stsp if (err)
4906 5de5890b 2018-10-18 stsp goto done;
4907 5de5890b 2018-10-18 stsp
4908 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4909 5de5890b 2018-10-18 stsp if (err)
4910 5de5890b 2018-10-18 stsp goto done;
4911 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4912 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4913 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4914 5de5890b 2018-10-18 stsp char *id = NULL;
4915 84453469 2018-11-11 stsp
4916 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4917 84453469 2018-11-11 stsp break;
4918 84453469 2018-11-11 stsp
4919 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4920 5de5890b 2018-10-18 stsp if (show_ids) {
4921 5de5890b 2018-10-18 stsp char *id_str;
4922 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4923 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4924 5de5890b 2018-10-18 stsp if (err)
4925 5de5890b 2018-10-18 stsp goto done;
4926 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4927 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4928 5de5890b 2018-10-18 stsp free(id_str);
4929 5de5890b 2018-10-18 stsp goto done;
4930 5de5890b 2018-10-18 stsp }
4931 5de5890b 2018-10-18 stsp free(id_str);
4932 5de5890b 2018-10-18 stsp }
4933 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4934 5de5890b 2018-10-18 stsp free(id);
4935 0d6c6ee3 2020-05-20 stsp if (err)
4936 0d6c6ee3 2020-05-20 stsp goto done;
4937 c1669e2e 2019-01-09 stsp
4938 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4939 c1669e2e 2019-01-09 stsp char *child_path;
4940 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4941 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4942 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4943 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4944 c1669e2e 2019-01-09 stsp goto done;
4945 c1669e2e 2019-01-09 stsp }
4946 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4947 c1669e2e 2019-01-09 stsp root_path, repo);
4948 c1669e2e 2019-01-09 stsp free(child_path);
4949 c1669e2e 2019-01-09 stsp if (err)
4950 c1669e2e 2019-01-09 stsp goto done;
4951 c1669e2e 2019-01-09 stsp }
4952 5de5890b 2018-10-18 stsp }
4953 5de5890b 2018-10-18 stsp done:
4954 5de5890b 2018-10-18 stsp if (tree)
4955 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4956 5de5890b 2018-10-18 stsp free(tree_id);
4957 5de5890b 2018-10-18 stsp return err;
4958 404c43c4 2018-06-21 stsp }
4959 404c43c4 2018-06-21 stsp
4960 5de5890b 2018-10-18 stsp static const struct got_error *
4961 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4962 5de5890b 2018-10-18 stsp {
4963 5de5890b 2018-10-18 stsp const struct got_error *error;
4964 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4965 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4966 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4967 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4968 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4969 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4970 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4971 5de5890b 2018-10-18 stsp int ch;
4972 5de5890b 2018-10-18 stsp
4973 5de5890b 2018-10-18 stsp #ifndef PROFILE
4974 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4975 0f8d269b 2019-01-04 stsp NULL) == -1)
4976 5de5890b 2018-10-18 stsp err(1, "pledge");
4977 5de5890b 2018-10-18 stsp #endif
4978 5de5890b 2018-10-18 stsp
4979 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4980 5de5890b 2018-10-18 stsp switch (ch) {
4981 5de5890b 2018-10-18 stsp case 'c':
4982 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4983 5de5890b 2018-10-18 stsp break;
4984 5de5890b 2018-10-18 stsp case 'r':
4985 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4986 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4987 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4988 9ba1d308 2019-10-21 stsp optarg);
4989 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4990 5de5890b 2018-10-18 stsp break;
4991 5de5890b 2018-10-18 stsp case 'i':
4992 5de5890b 2018-10-18 stsp show_ids = 1;
4993 5de5890b 2018-10-18 stsp break;
4994 c1669e2e 2019-01-09 stsp case 'R':
4995 c1669e2e 2019-01-09 stsp recurse = 1;
4996 c1669e2e 2019-01-09 stsp break;
4997 5de5890b 2018-10-18 stsp default:
4998 2deda0b9 2019-03-07 stsp usage_tree();
4999 5de5890b 2018-10-18 stsp /* NOTREACHED */
5000 5de5890b 2018-10-18 stsp }
5001 5de5890b 2018-10-18 stsp }
5002 5de5890b 2018-10-18 stsp
5003 5de5890b 2018-10-18 stsp argc -= optind;
5004 5de5890b 2018-10-18 stsp argv += optind;
5005 5de5890b 2018-10-18 stsp
5006 5de5890b 2018-10-18 stsp if (argc == 1)
5007 5de5890b 2018-10-18 stsp path = argv[0];
5008 5de5890b 2018-10-18 stsp else if (argc > 1)
5009 5de5890b 2018-10-18 stsp usage_tree();
5010 5de5890b 2018-10-18 stsp else
5011 7a2c19d6 2019-02-05 stsp path = NULL;
5012 7a2c19d6 2019-02-05 stsp
5013 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5014 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5015 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5016 9bf7a39b 2019-02-05 stsp goto done;
5017 9bf7a39b 2019-02-05 stsp }
5018 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5019 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5020 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5021 7a2c19d6 2019-02-05 stsp goto done;
5022 7a2c19d6 2019-02-05 stsp else
5023 7a2c19d6 2019-02-05 stsp error = NULL;
5024 7a2c19d6 2019-02-05 stsp if (worktree) {
5025 7a2c19d6 2019-02-05 stsp repo_path =
5026 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5027 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5028 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5029 7a2c19d6 2019-02-05 stsp if (error)
5030 7a2c19d6 2019-02-05 stsp goto done;
5031 7a2c19d6 2019-02-05 stsp } else {
5032 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5033 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5034 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5035 7a2c19d6 2019-02-05 stsp goto done;
5036 7a2c19d6 2019-02-05 stsp }
5037 5de5890b 2018-10-18 stsp }
5038 5de5890b 2018-10-18 stsp }
5039 5de5890b 2018-10-18 stsp
5040 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5041 5de5890b 2018-10-18 stsp if (error != NULL)
5042 c02c541e 2019-03-29 stsp goto done;
5043 c02c541e 2019-03-29 stsp
5044 4fedbf4c 2020-11-07 stsp if (worktree) {
5045 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5046 4fedbf4c 2020-11-07 stsp char *p;
5047 5de5890b 2018-10-18 stsp
5048 4fedbf4c 2020-11-07 stsp if (path == NULL)
5049 4fedbf4c 2020-11-07 stsp path = "";
5050 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5051 4fedbf4c 2020-11-07 stsp if (error)
5052 4fedbf4c 2020-11-07 stsp goto done;
5053 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5054 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5055 4fedbf4c 2020-11-07 stsp p) == -1) {
5056 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5057 9bf7a39b 2019-02-05 stsp free(p);
5058 4fedbf4c 2020-11-07 stsp goto done;
5059 4fedbf4c 2020-11-07 stsp }
5060 4fedbf4c 2020-11-07 stsp free(p);
5061 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5062 4fedbf4c 2020-11-07 stsp if (error)
5063 4fedbf4c 2020-11-07 stsp goto done;
5064 4fedbf4c 2020-11-07 stsp } else {
5065 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5066 4fedbf4c 2020-11-07 stsp if (error)
5067 4fedbf4c 2020-11-07 stsp goto done;
5068 4fedbf4c 2020-11-07 stsp if (path == NULL)
5069 9bf7a39b 2019-02-05 stsp path = "/";
5070 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5071 9bf7a39b 2019-02-05 stsp if (error != NULL)
5072 9bf7a39b 2019-02-05 stsp goto done;
5073 9bf7a39b 2019-02-05 stsp }
5074 5de5890b 2018-10-18 stsp
5075 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5076 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5077 4e0a20a4 2020-03-23 tracey if (worktree)
5078 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5079 4e0a20a4 2020-03-23 tracey else
5080 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5081 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5082 5de5890b 2018-10-18 stsp if (error != NULL)
5083 5de5890b 2018-10-18 stsp goto done;
5084 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5085 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5086 5de5890b 2018-10-18 stsp if (error != NULL)
5087 5de5890b 2018-10-18 stsp goto done;
5088 5de5890b 2018-10-18 stsp } else {
5089 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5090 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5091 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5092 84de9106 2020-12-26 stsp NULL);
5093 84de9106 2020-12-26 stsp if (error)
5094 84de9106 2020-12-26 stsp goto done;
5095 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5096 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5097 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5098 30837e32 2019-07-25 stsp if (error)
5099 5de5890b 2018-10-18 stsp goto done;
5100 5de5890b 2018-10-18 stsp }
5101 5de5890b 2018-10-18 stsp
5102 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5103 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5104 5de5890b 2018-10-18 stsp done:
5105 5de5890b 2018-10-18 stsp free(in_repo_path);
5106 5de5890b 2018-10-18 stsp free(repo_path);
5107 5de5890b 2018-10-18 stsp free(cwd);
5108 5de5890b 2018-10-18 stsp free(commit_id);
5109 7a2c19d6 2019-02-05 stsp if (worktree)
5110 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5111 5de5890b 2018-10-18 stsp if (repo) {
5112 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5113 5de5890b 2018-10-18 stsp if (error == NULL)
5114 1d0f4054 2021-06-17 stsp error = close_err;
5115 5de5890b 2018-10-18 stsp }
5116 5de5890b 2018-10-18 stsp return error;
5117 5de5890b 2018-10-18 stsp }
5118 5de5890b 2018-10-18 stsp
5119 6bad629b 2019-02-04 stsp __dead static void
5120 6bad629b 2019-02-04 stsp usage_status(void)
5121 6bad629b 2019-02-04 stsp {
5122 f6343036 2021-06-22 stsp fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5123 081470ac 2020-08-13 stsp getprogname());
5124 6bad629b 2019-02-04 stsp exit(1);
5125 6bad629b 2019-02-04 stsp }
5126 5c860e29 2018-03-12 stsp
5127 b72f483a 2019-02-05 stsp static const struct got_error *
5128 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5129 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5130 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5131 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5132 6bad629b 2019-02-04 stsp {
5133 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5134 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5135 081470ac 2020-08-13 stsp if (arg) {
5136 081470ac 2020-08-13 stsp char *status_codes = arg;
5137 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5138 081470ac 2020-08-13 stsp int i;
5139 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5140 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5141 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5142 081470ac 2020-08-13 stsp break;
5143 081470ac 2020-08-13 stsp }
5144 081470ac 2020-08-13 stsp if (i == ncodes)
5145 081470ac 2020-08-13 stsp return NULL;
5146 081470ac 2020-08-13 stsp }
5147 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5148 b72f483a 2019-02-05 stsp return NULL;
5149 6bad629b 2019-02-04 stsp }
5150 5c860e29 2018-03-12 stsp
5151 6bad629b 2019-02-04 stsp static const struct got_error *
5152 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5153 6bad629b 2019-02-04 stsp {
5154 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5155 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5156 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5157 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5158 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5159 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5160 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5161 5c860e29 2018-03-12 stsp
5162 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5163 72ea6654 2019-07-27 stsp
5164 f6343036 2021-06-22 stsp while ((ch = getopt(argc, argv, "Is:")) != -1) {
5165 6bad629b 2019-02-04 stsp switch (ch) {
5166 f6343036 2021-06-22 stsp case 'I':
5167 f6343036 2021-06-22 stsp no_ignores = 1;
5168 f6343036 2021-06-22 stsp break;
5169 081470ac 2020-08-13 stsp case 's':
5170 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5171 081470ac 2020-08-13 stsp switch (optarg[i]) {
5172 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5173 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5174 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5175 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5176 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5177 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5178 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5179 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5180 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5181 081470ac 2020-08-13 stsp break;
5182 081470ac 2020-08-13 stsp default:
5183 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5184 081470ac 2020-08-13 stsp optarg[i]);
5185 081470ac 2020-08-13 stsp }
5186 081470ac 2020-08-13 stsp }
5187 081470ac 2020-08-13 stsp status_codes = optarg;
5188 081470ac 2020-08-13 stsp break;
5189 5c860e29 2018-03-12 stsp default:
5190 2deda0b9 2019-03-07 stsp usage_status();
5191 6bad629b 2019-02-04 stsp /* NOTREACHED */
5192 5c860e29 2018-03-12 stsp }
5193 5c860e29 2018-03-12 stsp }
5194 5c860e29 2018-03-12 stsp
5195 6bad629b 2019-02-04 stsp argc -= optind;
5196 6bad629b 2019-02-04 stsp argv += optind;
5197 5c860e29 2018-03-12 stsp
5198 6bad629b 2019-02-04 stsp #ifndef PROFILE
5199 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5200 6bad629b 2019-02-04 stsp NULL) == -1)
5201 6bad629b 2019-02-04 stsp err(1, "pledge");
5202 f42b1b34 2018-03-12 stsp #endif
5203 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5204 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5205 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5206 927df6b7 2019-02-10 stsp goto done;
5207 927df6b7 2019-02-10 stsp }
5208 927df6b7 2019-02-10 stsp
5209 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5210 fa51e947 2020-03-27 stsp if (error) {
5211 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5212 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5213 a5edda0a 2019-07-27 stsp goto done;
5214 fa51e947 2020-03-27 stsp }
5215 6bad629b 2019-02-04 stsp
5216 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5217 c9956ddf 2019-09-08 stsp NULL);
5218 6bad629b 2019-02-04 stsp if (error != NULL)
5219 6bad629b 2019-02-04 stsp goto done;
5220 6bad629b 2019-02-04 stsp
5221 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5222 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5223 087fb88c 2019-08-04 stsp if (error)
5224 087fb88c 2019-08-04 stsp goto done;
5225 087fb88c 2019-08-04 stsp
5226 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5227 6bad629b 2019-02-04 stsp if (error)
5228 6bad629b 2019-02-04 stsp goto done;
5229 6bad629b 2019-02-04 stsp
5230 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5231 f6343036 2021-06-22 stsp print_status, status_codes, check_cancelled, NULL);
5232 6bad629b 2019-02-04 stsp done:
5233 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5234 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5235 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5236 927df6b7 2019-02-10 stsp free(cwd);
5237 d0eebce4 2019-03-11 stsp return error;
5238 d0eebce4 2019-03-11 stsp }
5239 d0eebce4 2019-03-11 stsp
5240 d0eebce4 2019-03-11 stsp __dead static void
5241 d0eebce4 2019-03-11 stsp usage_ref(void)
5242 d0eebce4 2019-03-11 stsp {
5243 d0eebce4 2019-03-11 stsp fprintf(stderr,
5244 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5245 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5246 d0eebce4 2019-03-11 stsp getprogname());
5247 d0eebce4 2019-03-11 stsp exit(1);
5248 d0eebce4 2019-03-11 stsp }
5249 d0eebce4 2019-03-11 stsp
5250 d0eebce4 2019-03-11 stsp static const struct got_error *
5251 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5252 d0eebce4 2019-03-11 stsp {
5253 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5254 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5255 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5256 d0eebce4 2019-03-11 stsp
5257 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5258 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5259 d0eebce4 2019-03-11 stsp if (err)
5260 d0eebce4 2019-03-11 stsp return err;
5261 d0eebce4 2019-03-11 stsp
5262 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5263 d0eebce4 2019-03-11 stsp char *refstr;
5264 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5265 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5266 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5267 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5268 d0eebce4 2019-03-11 stsp free(refstr);
5269 d0eebce4 2019-03-11 stsp }
5270 d0eebce4 2019-03-11 stsp
5271 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5272 d0eebce4 2019-03-11 stsp return NULL;
5273 d0eebce4 2019-03-11 stsp }
5274 d0eebce4 2019-03-11 stsp
5275 d0eebce4 2019-03-11 stsp static const struct got_error *
5276 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5277 d0eebce4 2019-03-11 stsp {
5278 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5279 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5280 993f033b 2021-07-16 stsp struct got_object_id *id = NULL;
5281 993f033b 2021-07-16 stsp char *id_str = NULL;
5282 da630daa 2021-07-16 stsp const char *target;
5283 d0eebce4 2019-03-11 stsp
5284 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5285 d0eebce4 2019-03-11 stsp if (err)
5286 d0eebce4 2019-03-11 stsp return err;
5287 993f033b 2021-07-16 stsp
5288 da630daa 2021-07-16 stsp if (got_ref_is_symbolic(ref)) {
5289 da630daa 2021-07-16 stsp target = got_ref_get_symref_target(ref);
5290 da630daa 2021-07-16 stsp } else {
5291 da630daa 2021-07-16 stsp err = got_ref_resolve(&id, repo, ref);
5292 da630daa 2021-07-16 stsp if (err)
5293 da630daa 2021-07-16 stsp goto done;
5294 da630daa 2021-07-16 stsp err = got_object_id_str(&id_str, id);
5295 da630daa 2021-07-16 stsp if (err)
5296 da630daa 2021-07-16 stsp goto done;
5297 da630daa 2021-07-16 stsp target = id_str;
5298 da630daa 2021-07-16 stsp }
5299 d0eebce4 2019-03-11 stsp
5300 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5301 90830082 2021-07-16 stsp if (err)
5302 90830082 2021-07-16 stsp goto done;
5303 90830082 2021-07-16 stsp
5304 da630daa 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
5305 993f033b 2021-07-16 stsp done:
5306 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5307 993f033b 2021-07-16 stsp free(id);
5308 993f033b 2021-07-16 stsp free(id_str);
5309 d0eebce4 2019-03-11 stsp return err;
5310 d0eebce4 2019-03-11 stsp }
5311 d0eebce4 2019-03-11 stsp
5312 d0eebce4 2019-03-11 stsp static const struct got_error *
5313 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5314 d0eebce4 2019-03-11 stsp {
5315 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5316 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5317 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5318 d1644381 2019-07-14 stsp
5319 d1644381 2019-07-14 stsp /*
5320 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5321 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5322 d1644381 2019-07-14 stsp * an unintended typo.
5323 d1644381 2019-07-14 stsp */
5324 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5325 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5326 d0eebce4 2019-03-11 stsp
5327 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5328 dd88155e 2019-06-29 stsp repo);
5329 d83d9d5c 2019-05-13 stsp if (err) {
5330 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5331 d0eebce4 2019-03-11 stsp
5332 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5333 d83d9d5c 2019-05-13 stsp return err;
5334 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5335 d83d9d5c 2019-05-13 stsp if (err)
5336 d83d9d5c 2019-05-13 stsp return err;
5337 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5338 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5339 d83d9d5c 2019-05-13 stsp if (err)
5340 d83d9d5c 2019-05-13 stsp return err;
5341 d83d9d5c 2019-05-13 stsp }
5342 d83d9d5c 2019-05-13 stsp
5343 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5344 d0eebce4 2019-03-11 stsp if (err)
5345 d0eebce4 2019-03-11 stsp goto done;
5346 d0eebce4 2019-03-11 stsp
5347 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5348 d0eebce4 2019-03-11 stsp done:
5349 d0eebce4 2019-03-11 stsp if (ref)
5350 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5351 d0eebce4 2019-03-11 stsp free(id);
5352 d1c1ae5f 2019-08-12 stsp return err;
5353 d1c1ae5f 2019-08-12 stsp }
5354 d1c1ae5f 2019-08-12 stsp
5355 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5356 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5357 d1c1ae5f 2019-08-12 stsp {
5358 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5359 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5360 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5361 d1c1ae5f 2019-08-12 stsp
5362 d1c1ae5f 2019-08-12 stsp /*
5363 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5364 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5365 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5366 d1c1ae5f 2019-08-12 stsp */
5367 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5368 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5369 d1c1ae5f 2019-08-12 stsp
5370 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5371 d1c1ae5f 2019-08-12 stsp if (err)
5372 d1c1ae5f 2019-08-12 stsp return err;
5373 d1c1ae5f 2019-08-12 stsp
5374 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5375 d1c1ae5f 2019-08-12 stsp if (err)
5376 d1c1ae5f 2019-08-12 stsp goto done;
5377 d1c1ae5f 2019-08-12 stsp
5378 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5379 d1c1ae5f 2019-08-12 stsp done:
5380 d1c1ae5f 2019-08-12 stsp if (target_ref)
5381 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5382 d1c1ae5f 2019-08-12 stsp if (ref)
5383 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5384 d0eebce4 2019-03-11 stsp return err;
5385 d0eebce4 2019-03-11 stsp }
5386 d0eebce4 2019-03-11 stsp
5387 d0eebce4 2019-03-11 stsp static const struct got_error *
5388 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5389 d0eebce4 2019-03-11 stsp {
5390 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5391 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5392 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5393 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5394 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5395 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5396 b2070a3f 2020-03-22 stsp char *refname = NULL;
5397 d0eebce4 2019-03-11 stsp
5398 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5399 d0eebce4 2019-03-11 stsp switch (ch) {
5400 e31abbf2 2020-03-22 stsp case 'c':
5401 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5402 e31abbf2 2020-03-22 stsp break;
5403 d0eebce4 2019-03-11 stsp case 'd':
5404 e31abbf2 2020-03-22 stsp do_delete = 1;
5405 d0eebce4 2019-03-11 stsp break;
5406 d0eebce4 2019-03-11 stsp case 'r':
5407 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5408 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5409 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5410 9ba1d308 2019-10-21 stsp optarg);
5411 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5412 d0eebce4 2019-03-11 stsp break;
5413 d0eebce4 2019-03-11 stsp case 'l':
5414 d0eebce4 2019-03-11 stsp do_list = 1;
5415 d1c1ae5f 2019-08-12 stsp break;
5416 d1c1ae5f 2019-08-12 stsp case 's':
5417 e31abbf2 2020-03-22 stsp symref_target = optarg;
5418 d0eebce4 2019-03-11 stsp break;
5419 d0eebce4 2019-03-11 stsp default:
5420 d0eebce4 2019-03-11 stsp usage_ref();
5421 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5422 d0eebce4 2019-03-11 stsp }
5423 d0eebce4 2019-03-11 stsp }
5424 d0eebce4 2019-03-11 stsp
5425 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5426 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5427 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5428 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5429 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5430 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5431 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5432 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5433 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5434 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5435 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5436 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5437 d0eebce4 2019-03-11 stsp
5438 d0eebce4 2019-03-11 stsp argc -= optind;
5439 d0eebce4 2019-03-11 stsp argv += optind;
5440 d0eebce4 2019-03-11 stsp
5441 e31abbf2 2020-03-22 stsp if (do_list) {
5442 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5443 d0eebce4 2019-03-11 stsp usage_ref();
5444 b2070a3f 2020-03-22 stsp if (argc == 1) {
5445 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5446 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5447 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5448 b2070a3f 2020-03-22 stsp goto done;
5449 b2070a3f 2020-03-22 stsp }
5450 b2070a3f 2020-03-22 stsp }
5451 e31abbf2 2020-03-22 stsp } else {
5452 e31abbf2 2020-03-22 stsp if (argc != 1)
5453 e31abbf2 2020-03-22 stsp usage_ref();
5454 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5455 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5456 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5457 b2070a3f 2020-03-22 stsp goto done;
5458 b2070a3f 2020-03-22 stsp }
5459 e31abbf2 2020-03-22 stsp }
5460 b2070a3f 2020-03-22 stsp
5461 b2070a3f 2020-03-22 stsp if (refname)
5462 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5463 d0eebce4 2019-03-11 stsp
5464 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5465 e0b57350 2019-03-12 stsp if (do_list) {
5466 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5467 e0b57350 2019-03-12 stsp NULL) == -1)
5468 e0b57350 2019-03-12 stsp err(1, "pledge");
5469 e0b57350 2019-03-12 stsp } else {
5470 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5471 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5472 e0b57350 2019-03-12 stsp err(1, "pledge");
5473 e0b57350 2019-03-12 stsp }
5474 d0eebce4 2019-03-11 stsp #endif
5475 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5476 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5477 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5478 d0eebce4 2019-03-11 stsp goto done;
5479 d0eebce4 2019-03-11 stsp }
5480 d0eebce4 2019-03-11 stsp
5481 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5482 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5483 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5484 d0eebce4 2019-03-11 stsp goto done;
5485 d0eebce4 2019-03-11 stsp else
5486 d0eebce4 2019-03-11 stsp error = NULL;
5487 d0eebce4 2019-03-11 stsp if (worktree) {
5488 d0eebce4 2019-03-11 stsp repo_path =
5489 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5490 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5491 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5492 d0eebce4 2019-03-11 stsp if (error)
5493 d0eebce4 2019-03-11 stsp goto done;
5494 d0eebce4 2019-03-11 stsp } else {
5495 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5496 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5497 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5498 d0eebce4 2019-03-11 stsp goto done;
5499 d0eebce4 2019-03-11 stsp }
5500 d0eebce4 2019-03-11 stsp }
5501 d0eebce4 2019-03-11 stsp }
5502 d0eebce4 2019-03-11 stsp
5503 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5504 d0eebce4 2019-03-11 stsp if (error != NULL)
5505 d0eebce4 2019-03-11 stsp goto done;
5506 d0eebce4 2019-03-11 stsp
5507 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5508 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5509 c02c541e 2019-03-29 stsp if (error)
5510 c02c541e 2019-03-29 stsp goto done;
5511 c02c541e 2019-03-29 stsp
5512 d0eebce4 2019-03-11 stsp if (do_list)
5513 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5514 e31abbf2 2020-03-22 stsp else if (do_delete)
5515 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5516 e31abbf2 2020-03-22 stsp else if (symref_target)
5517 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5518 e31abbf2 2020-03-22 stsp else {
5519 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5520 e31abbf2 2020-03-22 stsp usage_ref();
5521 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5522 e31abbf2 2020-03-22 stsp }
5523 4e759de4 2019-06-26 stsp done:
5524 b2070a3f 2020-03-22 stsp free(refname);
5525 1d0f4054 2021-06-17 stsp if (repo) {
5526 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5527 1d0f4054 2021-06-17 stsp if (error == NULL)
5528 1d0f4054 2021-06-17 stsp error = close_err;
5529 1d0f4054 2021-06-17 stsp }
5530 4e759de4 2019-06-26 stsp if (worktree)
5531 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5532 4e759de4 2019-06-26 stsp free(cwd);
5533 4e759de4 2019-06-26 stsp free(repo_path);
5534 4e759de4 2019-06-26 stsp return error;
5535 4e759de4 2019-06-26 stsp }
5536 4e759de4 2019-06-26 stsp
5537 4e759de4 2019-06-26 stsp __dead static void
5538 4e759de4 2019-06-26 stsp usage_branch(void)
5539 4e759de4 2019-06-26 stsp {
5540 4e759de4 2019-06-26 stsp fprintf(stderr,
5541 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5542 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5543 4e759de4 2019-06-26 stsp exit(1);
5544 4e759de4 2019-06-26 stsp }
5545 4e759de4 2019-06-26 stsp
5546 4e759de4 2019-06-26 stsp static const struct got_error *
5547 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5548 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5549 4e99b47e 2019-10-04 stsp {
5550 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5551 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5552 4e99b47e 2019-10-04 stsp char *refstr;
5553 4e99b47e 2019-10-04 stsp
5554 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5555 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5556 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5557 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5558 4e99b47e 2019-10-04 stsp
5559 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5560 4e99b47e 2019-10-04 stsp if (err)
5561 4e99b47e 2019-10-04 stsp return err;
5562 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5563 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5564 4e99b47e 2019-10-04 stsp marker = "* ";
5565 4e99b47e 2019-10-04 stsp else
5566 4e99b47e 2019-10-04 stsp marker = "~ ";
5567 4e99b47e 2019-10-04 stsp free(id);
5568 4e99b47e 2019-10-04 stsp }
5569 4e99b47e 2019-10-04 stsp
5570 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5571 4e99b47e 2019-10-04 stsp refname += 11;
5572 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5573 4e99b47e 2019-10-04 stsp refname += 18;
5574 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5575 34d4e04c 2021-02-08 stsp refname += 13;
5576 4e99b47e 2019-10-04 stsp
5577 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5578 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5579 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5580 4e99b47e 2019-10-04 stsp
5581 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5582 4e99b47e 2019-10-04 stsp free(refstr);
5583 4e99b47e 2019-10-04 stsp return NULL;
5584 4e99b47e 2019-10-04 stsp }
5585 4e99b47e 2019-10-04 stsp
5586 4e99b47e 2019-10-04 stsp static const struct got_error *
5587 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5588 ad89fa31 2019-10-04 stsp {
5589 ad89fa31 2019-10-04 stsp const char *refname;
5590 ad89fa31 2019-10-04 stsp
5591 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5592 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5593 ad89fa31 2019-10-04 stsp
5594 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5595 ad89fa31 2019-10-04 stsp
5596 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5597 ad89fa31 2019-10-04 stsp refname += 11;
5598 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5599 ad89fa31 2019-10-04 stsp refname += 18;
5600 ad89fa31 2019-10-04 stsp
5601 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5602 ad89fa31 2019-10-04 stsp
5603 ad89fa31 2019-10-04 stsp return NULL;
5604 ad89fa31 2019-10-04 stsp }
5605 ad89fa31 2019-10-04 stsp
5606 ad89fa31 2019-10-04 stsp static const struct got_error *
5607 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5608 4e759de4 2019-06-26 stsp {
5609 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5610 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5611 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5612 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5613 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5614 4e759de4 2019-06-26 stsp
5615 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5616 ba882ee3 2019-07-11 stsp
5617 4e99b47e 2019-10-04 stsp if (worktree) {
5618 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5619 4e99b47e 2019-10-04 stsp worktree);
5620 4e99b47e 2019-10-04 stsp if (err)
5621 4e99b47e 2019-10-04 stsp return err;
5622 4e99b47e 2019-10-04 stsp
5623 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5624 4e99b47e 2019-10-04 stsp worktree);
5625 4e99b47e 2019-10-04 stsp if (err)
5626 4e99b47e 2019-10-04 stsp return err;
5627 4e99b47e 2019-10-04 stsp
5628 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5629 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5630 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5631 4e99b47e 2019-10-04 stsp if (err)
5632 4e99b47e 2019-10-04 stsp return err;
5633 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5634 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5635 4e99b47e 2019-10-04 stsp }
5636 4e99b47e 2019-10-04 stsp }
5637 4e99b47e 2019-10-04 stsp
5638 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5639 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5640 4e759de4 2019-06-26 stsp if (err)
5641 4e759de4 2019-06-26 stsp return err;
5642 4e759de4 2019-06-26 stsp
5643 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5644 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5645 4e759de4 2019-06-26 stsp
5646 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5647 34d4e04c 2021-02-08 stsp
5648 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5649 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5650 34d4e04c 2021-02-08 stsp if (err)
5651 34d4e04c 2021-02-08 stsp return err;
5652 34d4e04c 2021-02-08 stsp
5653 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5654 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5655 34d4e04c 2021-02-08 stsp
5656 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5657 34d4e04c 2021-02-08 stsp
5658 4e759de4 2019-06-26 stsp return NULL;
5659 4e759de4 2019-06-26 stsp }
5660 4e759de4 2019-06-26 stsp
5661 4e759de4 2019-06-26 stsp static const struct got_error *
5662 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5663 45cd4e47 2019-08-25 stsp const char *branch_name)
5664 4e759de4 2019-06-26 stsp {
5665 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5666 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5667 4e759de4 2019-06-26 stsp char *refname;
5668 4e759de4 2019-06-26 stsp
5669 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5670 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5671 4e759de4 2019-06-26 stsp
5672 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5673 4e759de4 2019-06-26 stsp if (err)
5674 4e759de4 2019-06-26 stsp goto done;
5675 4e759de4 2019-06-26 stsp
5676 45cd4e47 2019-08-25 stsp if (worktree &&
5677 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5678 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5679 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5680 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5681 45cd4e47 2019-08-25 stsp goto done;
5682 45cd4e47 2019-08-25 stsp }
5683 45cd4e47 2019-08-25 stsp
5684 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5685 4e759de4 2019-06-26 stsp done:
5686 45cd4e47 2019-08-25 stsp if (ref)
5687 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5688 4e759de4 2019-06-26 stsp free(refname);
5689 4e759de4 2019-06-26 stsp return err;
5690 4e759de4 2019-06-26 stsp }
5691 4e759de4 2019-06-26 stsp
5692 4e759de4 2019-06-26 stsp static const struct got_error *
5693 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5694 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5695 4e759de4 2019-06-26 stsp {
5696 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5697 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5698 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5699 d3f84d51 2019-07-11 stsp
5700 d3f84d51 2019-07-11 stsp /*
5701 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5702 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5703 d3f84d51 2019-07-11 stsp * an unintended typo.
5704 d3f84d51 2019-07-11 stsp */
5705 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5706 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5707 4e759de4 2019-06-26 stsp
5708 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5709 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5710 62d463ca 2020-10-20 naddy goto done;
5711 4e759de4 2019-06-26 stsp }
5712 4e759de4 2019-06-26 stsp
5713 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5714 4e759de4 2019-06-26 stsp if (err == NULL) {
5715 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5716 4e759de4 2019-06-26 stsp goto done;
5717 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5718 4e759de4 2019-06-26 stsp goto done;
5719 4e759de4 2019-06-26 stsp
5720 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5721 4e759de4 2019-06-26 stsp if (err)
5722 4e759de4 2019-06-26 stsp goto done;
5723 4e759de4 2019-06-26 stsp
5724 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5725 d0eebce4 2019-03-11 stsp done:
5726 4e759de4 2019-06-26 stsp if (ref)
5727 4e759de4 2019-06-26 stsp got_ref_close(ref);
5728 4e759de4 2019-06-26 stsp free(base_refname);
5729 4e759de4 2019-06-26 stsp free(refname);
5730 4e759de4 2019-06-26 stsp return err;
5731 4e759de4 2019-06-26 stsp }
5732 4e759de4 2019-06-26 stsp
5733 4e759de4 2019-06-26 stsp static const struct got_error *
5734 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5735 4e759de4 2019-06-26 stsp {
5736 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5737 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5738 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5739 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5740 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5741 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5742 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5743 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5744 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5745 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5746 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5747 4e759de4 2019-06-26 stsp
5748 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5749 da76fce2 2020-02-24 stsp
5750 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5751 4e759de4 2019-06-26 stsp switch (ch) {
5752 a74f7e83 2019-11-10 stsp case 'c':
5753 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5754 a74f7e83 2019-11-10 stsp break;
5755 4e759de4 2019-06-26 stsp case 'd':
5756 4e759de4 2019-06-26 stsp delref = optarg;
5757 4e759de4 2019-06-26 stsp break;
5758 4e759de4 2019-06-26 stsp case 'r':
5759 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5760 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5761 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5762 9ba1d308 2019-10-21 stsp optarg);
5763 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5764 4e759de4 2019-06-26 stsp break;
5765 4e759de4 2019-06-26 stsp case 'l':
5766 4e759de4 2019-06-26 stsp do_list = 1;
5767 da76fce2 2020-02-24 stsp break;
5768 da76fce2 2020-02-24 stsp case 'n':
5769 da76fce2 2020-02-24 stsp do_update = 0;
5770 4e759de4 2019-06-26 stsp break;
5771 4e759de4 2019-06-26 stsp default:
5772 4e759de4 2019-06-26 stsp usage_branch();
5773 4e759de4 2019-06-26 stsp /* NOTREACHED */
5774 4e759de4 2019-06-26 stsp }
5775 4e759de4 2019-06-26 stsp }
5776 4e759de4 2019-06-26 stsp
5777 4e759de4 2019-06-26 stsp if (do_list && delref)
5778 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5779 4e759de4 2019-06-26 stsp
5780 4e759de4 2019-06-26 stsp argc -= optind;
5781 4e759de4 2019-06-26 stsp argv += optind;
5782 ad89fa31 2019-10-04 stsp
5783 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5784 ad89fa31 2019-10-04 stsp do_show = 1;
5785 4e759de4 2019-06-26 stsp
5786 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5787 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5788 a74f7e83 2019-11-10 stsp
5789 4e759de4 2019-06-26 stsp if (do_list || delref) {
5790 4e759de4 2019-06-26 stsp if (argc > 0)
5791 4e759de4 2019-06-26 stsp usage_branch();
5792 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5793 4e759de4 2019-06-26 stsp usage_branch();
5794 4e759de4 2019-06-26 stsp
5795 4e759de4 2019-06-26 stsp #ifndef PROFILE
5796 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5797 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5798 4e759de4 2019-06-26 stsp NULL) == -1)
5799 4e759de4 2019-06-26 stsp err(1, "pledge");
5800 4e759de4 2019-06-26 stsp } else {
5801 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5802 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5803 4e759de4 2019-06-26 stsp err(1, "pledge");
5804 4e759de4 2019-06-26 stsp }
5805 4e759de4 2019-06-26 stsp #endif
5806 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5807 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5808 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5809 4e759de4 2019-06-26 stsp goto done;
5810 4e759de4 2019-06-26 stsp }
5811 4e759de4 2019-06-26 stsp
5812 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5813 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5814 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5815 4e759de4 2019-06-26 stsp goto done;
5816 4e759de4 2019-06-26 stsp else
5817 4e759de4 2019-06-26 stsp error = NULL;
5818 4e759de4 2019-06-26 stsp if (worktree) {
5819 4e759de4 2019-06-26 stsp repo_path =
5820 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5821 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5822 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5823 4e759de4 2019-06-26 stsp if (error)
5824 4e759de4 2019-06-26 stsp goto done;
5825 4e759de4 2019-06-26 stsp } else {
5826 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5827 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5828 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5829 4e759de4 2019-06-26 stsp goto done;
5830 4e759de4 2019-06-26 stsp }
5831 4e759de4 2019-06-26 stsp }
5832 4e759de4 2019-06-26 stsp }
5833 4e759de4 2019-06-26 stsp
5834 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5835 4e759de4 2019-06-26 stsp if (error != NULL)
5836 4e759de4 2019-06-26 stsp goto done;
5837 4e759de4 2019-06-26 stsp
5838 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5839 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5840 4e759de4 2019-06-26 stsp if (error)
5841 4e759de4 2019-06-26 stsp goto done;
5842 4e759de4 2019-06-26 stsp
5843 ad89fa31 2019-10-04 stsp if (do_show)
5844 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5845 ad89fa31 2019-10-04 stsp else if (do_list)
5846 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5847 4e759de4 2019-06-26 stsp else if (delref)
5848 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5849 4e759de4 2019-06-26 stsp else {
5850 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5851 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5852 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5853 84de9106 2020-12-26 stsp NULL);
5854 84de9106 2020-12-26 stsp if (error)
5855 84de9106 2020-12-26 stsp goto done;
5856 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5857 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5858 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5859 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5860 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5861 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5862 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5863 a74f7e83 2019-11-10 stsp if (error)
5864 a74f7e83 2019-11-10 stsp goto done;
5865 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5866 da76fce2 2020-02-24 stsp if (error)
5867 da76fce2 2020-02-24 stsp goto done;
5868 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5869 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5870 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5871 da76fce2 2020-02-24 stsp
5872 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5873 da76fce2 2020-02-24 stsp if (error)
5874 da76fce2 2020-02-24 stsp goto done;
5875 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5876 da76fce2 2020-02-24 stsp worktree);
5877 da76fce2 2020-02-24 stsp if (error)
5878 da76fce2 2020-02-24 stsp goto done;
5879 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5880 da76fce2 2020-02-24 stsp == -1) {
5881 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5882 da76fce2 2020-02-24 stsp goto done;
5883 da76fce2 2020-02-24 stsp }
5884 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5885 da76fce2 2020-02-24 stsp free(branch_refname);
5886 da76fce2 2020-02-24 stsp if (error)
5887 da76fce2 2020-02-24 stsp goto done;
5888 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5889 da76fce2 2020-02-24 stsp repo);
5890 da76fce2 2020-02-24 stsp if (error)
5891 da76fce2 2020-02-24 stsp goto done;
5892 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5893 da76fce2 2020-02-24 stsp commit_id);
5894 da76fce2 2020-02-24 stsp if (error)
5895 da76fce2 2020-02-24 stsp goto done;
5896 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5897 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5898 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5899 9627c110 2020-04-18 stsp NULL);
5900 da76fce2 2020-02-24 stsp if (error)
5901 da76fce2 2020-02-24 stsp goto done;
5902 9627c110 2020-04-18 stsp if (upa.did_something)
5903 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5904 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5905 da76fce2 2020-02-24 stsp }
5906 4e759de4 2019-06-26 stsp }
5907 4e759de4 2019-06-26 stsp done:
5908 da76fce2 2020-02-24 stsp if (ref)
5909 da76fce2 2020-02-24 stsp got_ref_close(ref);
5910 1d0f4054 2021-06-17 stsp if (repo) {
5911 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5912 1d0f4054 2021-06-17 stsp if (error == NULL)
5913 1d0f4054 2021-06-17 stsp error = close_err;
5914 1d0f4054 2021-06-17 stsp }
5915 d0eebce4 2019-03-11 stsp if (worktree)
5916 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5917 d0eebce4 2019-03-11 stsp free(cwd);
5918 d0eebce4 2019-03-11 stsp free(repo_path);
5919 da76fce2 2020-02-24 stsp free(commit_id);
5920 da76fce2 2020-02-24 stsp free(commit_id_str);
5921 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5922 da76fce2 2020-02-24 stsp free((char *)pe->path);
5923 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5924 d00136be 2019-03-26 stsp return error;
5925 d00136be 2019-03-26 stsp }
5926 d00136be 2019-03-26 stsp
5927 8e7bd50a 2019-08-22 stsp
5928 d00136be 2019-03-26 stsp __dead static void
5929 8e7bd50a 2019-08-22 stsp usage_tag(void)
5930 8e7bd50a 2019-08-22 stsp {
5931 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5932 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5933 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5934 8e7bd50a 2019-08-22 stsp exit(1);
5935 b8bad2ba 2019-08-23 stsp }
5936 b8bad2ba 2019-08-23 stsp
5937 b8bad2ba 2019-08-23 stsp #if 0
5938 b8bad2ba 2019-08-23 stsp static const struct got_error *
5939 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5940 b8bad2ba 2019-08-23 stsp {
5941 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5942 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5943 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5944 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5945 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5946 b8bad2ba 2019-08-23 stsp
5947 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
5948 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
5949 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5950 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5951 b8bad2ba 2019-08-23 stsp if (err)
5952 b8bad2ba 2019-08-23 stsp return err;
5953 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
5954 b8bad2ba 2019-08-23 stsp continue;
5955 b8bad2ba 2019-08-23 stsp } else {
5956 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5957 b8bad2ba 2019-08-23 stsp if (err)
5958 b8bad2ba 2019-08-23 stsp break;
5959 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5960 b8bad2ba 2019-08-23 stsp free(re_id);
5961 b8bad2ba 2019-08-23 stsp if (err)
5962 b8bad2ba 2019-08-23 stsp break;
5963 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5964 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5965 b8bad2ba 2019-08-23 stsp }
5966 b8bad2ba 2019-08-23 stsp
5967 b8bad2ba 2019-08-23 stsp while (se) {
5968 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5969 b8bad2ba 2019-08-23 stsp if (err)
5970 b8bad2ba 2019-08-23 stsp break;
5971 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5972 b8bad2ba 2019-08-23 stsp free(se_id);
5973 b8bad2ba 2019-08-23 stsp if (err)
5974 b8bad2ba 2019-08-23 stsp break;
5975 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5976 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5977 b8bad2ba 2019-08-23 stsp
5978 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5979 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5980 b8bad2ba 2019-08-23 stsp if (err)
5981 b8bad2ba 2019-08-23 stsp return err;
5982 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
5983 b8bad2ba 2019-08-23 stsp break;
5984 b8bad2ba 2019-08-23 stsp }
5985 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
5986 b8bad2ba 2019-08-23 stsp continue;
5987 b8bad2ba 2019-08-23 stsp }
5988 b8bad2ba 2019-08-23 stsp }
5989 b8bad2ba 2019-08-23 stsp done:
5990 b8bad2ba 2019-08-23 stsp return err;
5991 8e7bd50a 2019-08-22 stsp }
5992 b8bad2ba 2019-08-23 stsp #endif
5993 b8bad2ba 2019-08-23 stsp
5994 b8bad2ba 2019-08-23 stsp static const struct got_error *
5995 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5996 8e7bd50a 2019-08-22 stsp {
5997 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5998 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5999 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6000 8e7bd50a 2019-08-22 stsp
6001 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6002 8e7bd50a 2019-08-22 stsp
6003 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6004 8e7bd50a 2019-08-22 stsp if (err)
6005 8e7bd50a 2019-08-22 stsp return err;
6006 8e7bd50a 2019-08-22 stsp
6007 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6008 8e7bd50a 2019-08-22 stsp const char *refname;
6009 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6010 8e7bd50a 2019-08-22 stsp char datebuf[26];
6011 d4efa91b 2020-01-14 stsp const char *tagger;
6012 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6013 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6014 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6015 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6016 8e7bd50a 2019-08-22 stsp
6017 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6018 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6019 8e7bd50a 2019-08-22 stsp continue;
6020 8e7bd50a 2019-08-22 stsp refname += 10;
6021 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6022 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6023 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6024 8e7bd50a 2019-08-22 stsp break;
6025 8e7bd50a 2019-08-22 stsp }
6026 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6027 8e7bd50a 2019-08-22 stsp free(refstr);
6028 8e7bd50a 2019-08-22 stsp
6029 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6030 8e7bd50a 2019-08-22 stsp if (err)
6031 8e7bd50a 2019-08-22 stsp break;
6032 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6033 d4efa91b 2020-01-14 stsp if (err) {
6034 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6035 d4efa91b 2020-01-14 stsp free(id);
6036 d4efa91b 2020-01-14 stsp break;
6037 d4efa91b 2020-01-14 stsp }
6038 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6039 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6040 d4efa91b 2020-01-14 stsp if (err) {
6041 d4efa91b 2020-01-14 stsp free(id);
6042 d4efa91b 2020-01-14 stsp break;
6043 d4efa91b 2020-01-14 stsp }
6044 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6045 d4efa91b 2020-01-14 stsp tagger_time =
6046 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6047 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6048 d4efa91b 2020-01-14 stsp free(id);
6049 d4efa91b 2020-01-14 stsp if (err)
6050 d4efa91b 2020-01-14 stsp break;
6051 d4efa91b 2020-01-14 stsp } else {
6052 d4efa91b 2020-01-14 stsp free(id);
6053 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6054 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6055 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6056 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6057 d4efa91b 2020-01-14 stsp if (err)
6058 d4efa91b 2020-01-14 stsp break;
6059 d4efa91b 2020-01-14 stsp }
6060 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6061 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6062 2417344c 2019-08-23 stsp if (datestr)
6063 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6064 d4efa91b 2020-01-14 stsp if (commit)
6065 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6066 d4efa91b 2020-01-14 stsp else {
6067 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6068 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6069 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6070 d4efa91b 2020-01-14 stsp id_str);
6071 d4efa91b 2020-01-14 stsp break;
6072 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6073 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6074 d4efa91b 2020-01-14 stsp id_str);
6075 d4efa91b 2020-01-14 stsp break;
6076 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6077 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6078 d4efa91b 2020-01-14 stsp id_str);
6079 d4efa91b 2020-01-14 stsp break;
6080 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6081 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6082 d4efa91b 2020-01-14 stsp id_str);
6083 d4efa91b 2020-01-14 stsp break;
6084 d4efa91b 2020-01-14 stsp default:
6085 d4efa91b 2020-01-14 stsp break;
6086 d4efa91b 2020-01-14 stsp }
6087 8e7bd50a 2019-08-22 stsp }
6088 8e7bd50a 2019-08-22 stsp free(id_str);
6089 d4efa91b 2020-01-14 stsp if (commit) {
6090 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6091 d4efa91b 2020-01-14 stsp if (err)
6092 d4efa91b 2020-01-14 stsp break;
6093 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6094 d4efa91b 2020-01-14 stsp } else {
6095 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6096 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6097 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6098 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6099 d4efa91b 2020-01-14 stsp break;
6100 d4efa91b 2020-01-14 stsp }
6101 8e7bd50a 2019-08-22 stsp }
6102 8e7bd50a 2019-08-22 stsp
6103 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6104 8e7bd50a 2019-08-22 stsp do {
6105 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6106 8e7bd50a 2019-08-22 stsp if (line)
6107 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6108 8e7bd50a 2019-08-22 stsp } while (line);
6109 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6110 8e7bd50a 2019-08-22 stsp }
6111 8e7bd50a 2019-08-22 stsp
6112 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6113 8e7bd50a 2019-08-22 stsp return NULL;
6114 8e7bd50a 2019-08-22 stsp }
6115 8e7bd50a 2019-08-22 stsp
6116 8e7bd50a 2019-08-22 stsp static const struct got_error *
6117 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6118 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6119 8e7bd50a 2019-08-22 stsp {
6120 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6121 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6122 f372d5cd 2019-10-21 stsp char *editor = NULL;
6123 1601cb9f 2020-09-11 naddy int initial_content_len;
6124 8e7bd50a 2019-08-22 stsp int fd = -1;
6125 8e7bd50a 2019-08-22 stsp
6126 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6127 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6128 8e7bd50a 2019-08-22 stsp goto done;
6129 8e7bd50a 2019-08-22 stsp }
6130 8e7bd50a 2019-08-22 stsp
6131 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6132 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6133 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6134 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6135 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6136 8e7bd50a 2019-08-22 stsp goto done;
6137 8e7bd50a 2019-08-22 stsp }
6138 8e7bd50a 2019-08-22 stsp
6139 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6140 8e7bd50a 2019-08-22 stsp if (err)
6141 8e7bd50a 2019-08-22 stsp goto done;
6142 8e7bd50a 2019-08-22 stsp
6143 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6144 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6145 97972933 2020-09-11 stsp goto done;
6146 97972933 2020-09-11 stsp }
6147 8e7bd50a 2019-08-22 stsp
6148 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6149 8e7bd50a 2019-08-22 stsp if (err)
6150 8e7bd50a 2019-08-22 stsp goto done;
6151 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6152 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6153 8e7bd50a 2019-08-22 stsp done:
6154 8e7bd50a 2019-08-22 stsp free(initial_content);
6155 8e7bd50a 2019-08-22 stsp free(template);
6156 8e7bd50a 2019-08-22 stsp free(editor);
6157 8e7bd50a 2019-08-22 stsp
6158 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6159 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6160 97972933 2020-09-11 stsp
6161 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6162 59f86c76 2020-09-11 stsp if (err == NULL)
6163 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6164 59f86c76 2020-09-11 stsp if (err) {
6165 59f86c76 2020-09-11 stsp free(*tagmsg);
6166 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6167 8e7bd50a 2019-08-22 stsp }
6168 8e7bd50a 2019-08-22 stsp return err;
6169 8e7bd50a 2019-08-22 stsp }
6170 8e7bd50a 2019-08-22 stsp
6171 8e7bd50a 2019-08-22 stsp static const struct got_error *
6172 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6173 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6174 8e7bd50a 2019-08-22 stsp {
6175 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6176 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6177 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6178 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6179 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6180 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6181 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6182 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6183 8e7bd50a 2019-08-22 stsp
6184 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6185 84de9106 2020-12-26 stsp
6186 8e7bd50a 2019-08-22 stsp /*
6187 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6188 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6189 8e7bd50a 2019-08-22 stsp * an unintended typo.
6190 8e7bd50a 2019-08-22 stsp */
6191 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6192 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6193 8e7bd50a 2019-08-22 stsp
6194 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6195 8e7bd50a 2019-08-22 stsp if (err)
6196 8e7bd50a 2019-08-22 stsp return err;
6197 8e7bd50a 2019-08-22 stsp
6198 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6199 84de9106 2020-12-26 stsp if (err)
6200 84de9106 2020-12-26 stsp goto done;
6201 84de9106 2020-12-26 stsp
6202 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6203 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6204 8e7bd50a 2019-08-22 stsp if (err)
6205 8e7bd50a 2019-08-22 stsp goto done;
6206 8e7bd50a 2019-08-22 stsp
6207 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6208 8e7bd50a 2019-08-22 stsp if (err)
6209 8e7bd50a 2019-08-22 stsp goto done;
6210 8e7bd50a 2019-08-22 stsp
6211 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6212 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6213 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6214 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6215 62d463ca 2020-10-20 naddy goto done;
6216 8e7bd50a 2019-08-22 stsp }
6217 8e7bd50a 2019-08-22 stsp tag_name += 10;
6218 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6219 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6220 62d463ca 2020-10-20 naddy goto done;
6221 8e7bd50a 2019-08-22 stsp }
6222 8e7bd50a 2019-08-22 stsp
6223 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6224 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6225 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6226 8e7bd50a 2019-08-22 stsp goto done;
6227 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6228 8e7bd50a 2019-08-22 stsp goto done;
6229 8e7bd50a 2019-08-22 stsp
6230 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6231 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6232 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6233 f372d5cd 2019-10-21 stsp if (err) {
6234 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6235 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6236 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6237 8e7bd50a 2019-08-22 stsp goto done;
6238 f372d5cd 2019-10-21 stsp }
6239 8e7bd50a 2019-08-22 stsp }
6240 8e7bd50a 2019-08-22 stsp
6241 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6242 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6243 f372d5cd 2019-10-21 stsp if (err) {
6244 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6245 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6246 8e7bd50a 2019-08-22 stsp goto done;
6247 f372d5cd 2019-10-21 stsp }
6248 8e7bd50a 2019-08-22 stsp
6249 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6250 f372d5cd 2019-10-21 stsp if (err) {
6251 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6252 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6253 8e7bd50a 2019-08-22 stsp goto done;
6254 f372d5cd 2019-10-21 stsp }
6255 8e7bd50a 2019-08-22 stsp
6256 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6257 f372d5cd 2019-10-21 stsp if (err) {
6258 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6259 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6260 f372d5cd 2019-10-21 stsp goto done;
6261 f372d5cd 2019-10-21 stsp }
6262 8e7bd50a 2019-08-22 stsp
6263 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6264 f372d5cd 2019-10-21 stsp if (err) {
6265 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6266 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6267 f372d5cd 2019-10-21 stsp goto done;
6268 8e7bd50a 2019-08-22 stsp }
6269 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6270 8e7bd50a 2019-08-22 stsp done:
6271 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6272 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6273 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6274 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6275 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6276 f372d5cd 2019-10-21 stsp free(tag_id_str);
6277 8e7bd50a 2019-08-22 stsp if (ref)
6278 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6279 8e7bd50a 2019-08-22 stsp free(commit_id);
6280 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6281 8e7bd50a 2019-08-22 stsp free(refname);
6282 8e7bd50a 2019-08-22 stsp free(tagmsg);
6283 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6284 aba9c984 2019-09-08 stsp free(tagger);
6285 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6286 8e7bd50a 2019-08-22 stsp return err;
6287 8e7bd50a 2019-08-22 stsp }
6288 8e7bd50a 2019-08-22 stsp
6289 8e7bd50a 2019-08-22 stsp static const struct got_error *
6290 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6291 8e7bd50a 2019-08-22 stsp {
6292 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6293 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6294 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6295 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6296 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6297 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6298 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6299 8e7bd50a 2019-08-22 stsp
6300 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6301 8e7bd50a 2019-08-22 stsp switch (ch) {
6302 80106605 2020-02-24 stsp case 'c':
6303 80106605 2020-02-24 stsp commit_id_arg = optarg;
6304 80106605 2020-02-24 stsp break;
6305 8e7bd50a 2019-08-22 stsp case 'm':
6306 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6307 8e7bd50a 2019-08-22 stsp break;
6308 8e7bd50a 2019-08-22 stsp case 'r':
6309 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6310 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6311 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6312 9ba1d308 2019-10-21 stsp optarg);
6313 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6314 8e7bd50a 2019-08-22 stsp break;
6315 8e7bd50a 2019-08-22 stsp case 'l':
6316 8e7bd50a 2019-08-22 stsp do_list = 1;
6317 8e7bd50a 2019-08-22 stsp break;
6318 8e7bd50a 2019-08-22 stsp default:
6319 8e7bd50a 2019-08-22 stsp usage_tag();
6320 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6321 8e7bd50a 2019-08-22 stsp }
6322 8e7bd50a 2019-08-22 stsp }
6323 8e7bd50a 2019-08-22 stsp
6324 8e7bd50a 2019-08-22 stsp argc -= optind;
6325 8e7bd50a 2019-08-22 stsp argv += optind;
6326 8e7bd50a 2019-08-22 stsp
6327 8e7bd50a 2019-08-22 stsp if (do_list) {
6328 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6329 775ce909 2020-03-22 stsp errx(1,
6330 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6331 8e7bd50a 2019-08-22 stsp if (tagmsg)
6332 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6333 8e7bd50a 2019-08-22 stsp if (argc > 0)
6334 8e7bd50a 2019-08-22 stsp usage_tag();
6335 80106605 2020-02-24 stsp } else if (argc != 1)
6336 8e7bd50a 2019-08-22 stsp usage_tag();
6337 80106605 2020-02-24 stsp
6338 a2887370 2019-08-23 stsp tag_name = argv[0];
6339 8e7bd50a 2019-08-22 stsp
6340 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6341 8e7bd50a 2019-08-22 stsp if (do_list) {
6342 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6343 8e7bd50a 2019-08-22 stsp NULL) == -1)
6344 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6345 8e7bd50a 2019-08-22 stsp } else {
6346 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6347 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6348 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6349 8e7bd50a 2019-08-22 stsp }
6350 8e7bd50a 2019-08-22 stsp #endif
6351 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6352 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6353 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6354 8e7bd50a 2019-08-22 stsp goto done;
6355 8e7bd50a 2019-08-22 stsp }
6356 8e7bd50a 2019-08-22 stsp
6357 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6358 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6359 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6360 8e7bd50a 2019-08-22 stsp goto done;
6361 8e7bd50a 2019-08-22 stsp else
6362 8e7bd50a 2019-08-22 stsp error = NULL;
6363 8e7bd50a 2019-08-22 stsp if (worktree) {
6364 8e7bd50a 2019-08-22 stsp repo_path =
6365 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6366 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6367 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6368 8e7bd50a 2019-08-22 stsp if (error)
6369 8e7bd50a 2019-08-22 stsp goto done;
6370 8e7bd50a 2019-08-22 stsp } else {
6371 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6372 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6373 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6374 8e7bd50a 2019-08-22 stsp goto done;
6375 8e7bd50a 2019-08-22 stsp }
6376 8e7bd50a 2019-08-22 stsp }
6377 8e7bd50a 2019-08-22 stsp }
6378 8e7bd50a 2019-08-22 stsp
6379 8e7bd50a 2019-08-22 stsp if (do_list) {
6380 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6381 c9956ddf 2019-09-08 stsp if (error != NULL)
6382 c9956ddf 2019-09-08 stsp goto done;
6383 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6384 8e7bd50a 2019-08-22 stsp if (error)
6385 8e7bd50a 2019-08-22 stsp goto done;
6386 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6387 8e7bd50a 2019-08-22 stsp } else {
6388 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6389 c9956ddf 2019-09-08 stsp if (error)
6390 c9956ddf 2019-09-08 stsp goto done;
6391 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6392 c9956ddf 2019-09-08 stsp if (error != NULL)
6393 c9956ddf 2019-09-08 stsp goto done;
6394 c9956ddf 2019-09-08 stsp
6395 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6396 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6397 8e7bd50a 2019-08-22 stsp if (error)
6398 8e7bd50a 2019-08-22 stsp goto done;
6399 8e7bd50a 2019-08-22 stsp }
6400 8e7bd50a 2019-08-22 stsp
6401 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6402 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6403 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6404 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6405 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6406 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6407 8e7bd50a 2019-08-22 stsp if (error)
6408 8e7bd50a 2019-08-22 stsp goto done;
6409 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6410 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6411 8e7bd50a 2019-08-22 stsp if (error)
6412 8e7bd50a 2019-08-22 stsp goto done;
6413 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6414 8e7bd50a 2019-08-22 stsp free(commit_id);
6415 8e7bd50a 2019-08-22 stsp if (error)
6416 8e7bd50a 2019-08-22 stsp goto done;
6417 8e7bd50a 2019-08-22 stsp }
6418 8e7bd50a 2019-08-22 stsp
6419 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6420 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6421 8e7bd50a 2019-08-22 stsp }
6422 8e7bd50a 2019-08-22 stsp done:
6423 1d0f4054 2021-06-17 stsp if (repo) {
6424 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6425 1d0f4054 2021-06-17 stsp if (error == NULL)
6426 1d0f4054 2021-06-17 stsp error = close_err;
6427 1d0f4054 2021-06-17 stsp }
6428 8e7bd50a 2019-08-22 stsp if (worktree)
6429 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6430 8e7bd50a 2019-08-22 stsp free(cwd);
6431 8e7bd50a 2019-08-22 stsp free(repo_path);
6432 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6433 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6434 8e7bd50a 2019-08-22 stsp return error;
6435 8e7bd50a 2019-08-22 stsp }
6436 8e7bd50a 2019-08-22 stsp
6437 8e7bd50a 2019-08-22 stsp __dead static void
6438 d00136be 2019-03-26 stsp usage_add(void)
6439 d00136be 2019-03-26 stsp {
6440 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6441 022fae89 2019-12-06 tracey getprogname());
6442 d00136be 2019-03-26 stsp exit(1);
6443 d00136be 2019-03-26 stsp }
6444 d00136be 2019-03-26 stsp
6445 d00136be 2019-03-26 stsp static const struct got_error *
6446 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6447 4e68cba3 2019-11-23 stsp {
6448 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6449 4e68cba3 2019-11-23 stsp path++;
6450 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6451 4e68cba3 2019-11-23 stsp return NULL;
6452 4e68cba3 2019-11-23 stsp }
6453 4e68cba3 2019-11-23 stsp
6454 4e68cba3 2019-11-23 stsp static const struct got_error *
6455 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6456 d00136be 2019-03-26 stsp {
6457 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6458 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6459 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6460 1dd54920 2019-05-11 stsp char *cwd = NULL;
6461 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6462 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6463 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6464 1dd54920 2019-05-11 stsp
6465 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6466 d00136be 2019-03-26 stsp
6467 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6468 d00136be 2019-03-26 stsp switch (ch) {
6469 022fae89 2019-12-06 tracey case 'I':
6470 022fae89 2019-12-06 tracey no_ignores = 1;
6471 022fae89 2019-12-06 tracey break;
6472 4e68cba3 2019-11-23 stsp case 'R':
6473 4e68cba3 2019-11-23 stsp can_recurse = 1;
6474 4e68cba3 2019-11-23 stsp break;
6475 d00136be 2019-03-26 stsp default:
6476 d00136be 2019-03-26 stsp usage_add();
6477 d00136be 2019-03-26 stsp /* NOTREACHED */
6478 d00136be 2019-03-26 stsp }
6479 d00136be 2019-03-26 stsp }
6480 d00136be 2019-03-26 stsp
6481 d00136be 2019-03-26 stsp argc -= optind;
6482 d00136be 2019-03-26 stsp argv += optind;
6483 d00136be 2019-03-26 stsp
6484 43012d58 2019-07-14 stsp #ifndef PROFILE
6485 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6486 43012d58 2019-07-14 stsp NULL) == -1)
6487 43012d58 2019-07-14 stsp err(1, "pledge");
6488 43012d58 2019-07-14 stsp #endif
6489 723c305c 2019-05-11 jcs if (argc < 1)
6490 d00136be 2019-03-26 stsp usage_add();
6491 d00136be 2019-03-26 stsp
6492 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6493 d00136be 2019-03-26 stsp if (cwd == NULL) {
6494 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6495 d00136be 2019-03-26 stsp goto done;
6496 d00136be 2019-03-26 stsp }
6497 723c305c 2019-05-11 jcs
6498 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6499 fa51e947 2020-03-27 stsp if (error) {
6500 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6501 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6502 d00136be 2019-03-26 stsp goto done;
6503 fa51e947 2020-03-27 stsp }
6504 d00136be 2019-03-26 stsp
6505 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6506 c9956ddf 2019-09-08 stsp NULL);
6507 031a5338 2019-03-26 stsp if (error != NULL)
6508 031a5338 2019-03-26 stsp goto done;
6509 031a5338 2019-03-26 stsp
6510 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6511 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6512 d00136be 2019-03-26 stsp if (error)
6513 d00136be 2019-03-26 stsp goto done;
6514 d00136be 2019-03-26 stsp
6515 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6516 6d022e97 2019-08-04 stsp if (error)
6517 022fae89 2019-12-06 tracey goto done;
6518 022fae89 2019-12-06 tracey
6519 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6520 4e68cba3 2019-11-23 stsp char *ondisk_path;
6521 4e68cba3 2019-11-23 stsp struct stat sb;
6522 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6523 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6524 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6525 62d463ca 2020-10-20 naddy pe->path) == -1) {
6526 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6527 4e68cba3 2019-11-23 stsp goto done;
6528 4e68cba3 2019-11-23 stsp }
6529 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6530 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6531 4e68cba3 2019-11-23 stsp free(ondisk_path);
6532 4e68cba3 2019-11-23 stsp continue;
6533 4e68cba3 2019-11-23 stsp }
6534 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6535 4e68cba3 2019-11-23 stsp ondisk_path);
6536 4e68cba3 2019-11-23 stsp free(ondisk_path);
6537 4e68cba3 2019-11-23 stsp goto done;
6538 4e68cba3 2019-11-23 stsp }
6539 4e68cba3 2019-11-23 stsp free(ondisk_path);
6540 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6541 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6542 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6543 4e68cba3 2019-11-23 stsp goto done;
6544 4e68cba3 2019-11-23 stsp }
6545 4e68cba3 2019-11-23 stsp }
6546 4e68cba3 2019-11-23 stsp }
6547 022fae89 2019-12-06 tracey
6548 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6549 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6550 d00136be 2019-03-26 stsp done:
6551 1d0f4054 2021-06-17 stsp if (repo) {
6552 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6553 1d0f4054 2021-06-17 stsp if (error == NULL)
6554 1d0f4054 2021-06-17 stsp error = close_err;
6555 1d0f4054 2021-06-17 stsp }
6556 d00136be 2019-03-26 stsp if (worktree)
6557 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6558 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6559 1dd54920 2019-05-11 stsp free((char *)pe->path);
6560 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6561 2ec1f75b 2019-03-26 stsp free(cwd);
6562 2ec1f75b 2019-03-26 stsp return error;
6563 2ec1f75b 2019-03-26 stsp }
6564 2ec1f75b 2019-03-26 stsp
6565 2ec1f75b 2019-03-26 stsp __dead static void
6566 648e4ef7 2019-07-09 stsp usage_remove(void)
6567 2ec1f75b 2019-03-26 stsp {
6568 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6569 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6570 2ec1f75b 2019-03-26 stsp exit(1);
6571 2a06fe5f 2019-08-24 stsp }
6572 2a06fe5f 2019-08-24 stsp
6573 2a06fe5f 2019-08-24 stsp static const struct got_error *
6574 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6575 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6576 2a06fe5f 2019-08-24 stsp {
6577 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6578 f2a9dc41 2019-12-13 tracey path++;
6579 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6580 2a06fe5f 2019-08-24 stsp return NULL;
6581 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6582 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6583 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6584 2a06fe5f 2019-08-24 stsp return NULL;
6585 2ec1f75b 2019-03-26 stsp }
6586 2ec1f75b 2019-03-26 stsp
6587 2ec1f75b 2019-03-26 stsp static const struct got_error *
6588 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6589 2ec1f75b 2019-03-26 stsp {
6590 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6591 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6592 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6593 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6594 17ed4618 2019-06-02 stsp char *cwd = NULL;
6595 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6596 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6597 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6598 2ec1f75b 2019-03-26 stsp
6599 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6600 17ed4618 2019-06-02 stsp
6601 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6602 2ec1f75b 2019-03-26 stsp switch (ch) {
6603 2ec1f75b 2019-03-26 stsp case 'f':
6604 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6605 2ec1f75b 2019-03-26 stsp break;
6606 70e3e7f5 2019-12-13 tracey case 'k':
6607 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6608 70e3e7f5 2019-12-13 tracey break;
6609 f2a9dc41 2019-12-13 tracey case 'R':
6610 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6611 f2a9dc41 2019-12-13 tracey break;
6612 766841c2 2020-08-13 stsp case 's':
6613 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6614 766841c2 2020-08-13 stsp switch (optarg[i]) {
6615 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6616 766841c2 2020-08-13 stsp delete_local_mods = 1;
6617 766841c2 2020-08-13 stsp break;
6618 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6619 766841c2 2020-08-13 stsp break;
6620 766841c2 2020-08-13 stsp default:
6621 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6622 766841c2 2020-08-13 stsp optarg[i]);
6623 766841c2 2020-08-13 stsp }
6624 766841c2 2020-08-13 stsp }
6625 766841c2 2020-08-13 stsp status_codes = optarg;
6626 766841c2 2020-08-13 stsp break;
6627 2ec1f75b 2019-03-26 stsp default:
6628 f2a9dc41 2019-12-13 tracey usage_remove();
6629 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6630 2ec1f75b 2019-03-26 stsp }
6631 2ec1f75b 2019-03-26 stsp }
6632 2ec1f75b 2019-03-26 stsp
6633 2ec1f75b 2019-03-26 stsp argc -= optind;
6634 2ec1f75b 2019-03-26 stsp argv += optind;
6635 2ec1f75b 2019-03-26 stsp
6636 43012d58 2019-07-14 stsp #ifndef PROFILE
6637 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6638 43012d58 2019-07-14 stsp NULL) == -1)
6639 43012d58 2019-07-14 stsp err(1, "pledge");
6640 43012d58 2019-07-14 stsp #endif
6641 17ed4618 2019-06-02 stsp if (argc < 1)
6642 648e4ef7 2019-07-09 stsp usage_remove();
6643 2ec1f75b 2019-03-26 stsp
6644 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6645 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6646 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6647 2ec1f75b 2019-03-26 stsp goto done;
6648 2ec1f75b 2019-03-26 stsp }
6649 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6650 fa51e947 2020-03-27 stsp if (error) {
6651 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6652 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6653 2ec1f75b 2019-03-26 stsp goto done;
6654 fa51e947 2020-03-27 stsp }
6655 2ec1f75b 2019-03-26 stsp
6656 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6657 c9956ddf 2019-09-08 stsp NULL);
6658 2af4a041 2019-05-11 jcs if (error)
6659 2ec1f75b 2019-03-26 stsp goto done;
6660 2ec1f75b 2019-03-26 stsp
6661 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6662 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6663 2ec1f75b 2019-03-26 stsp if (error)
6664 2ec1f75b 2019-03-26 stsp goto done;
6665 2ec1f75b 2019-03-26 stsp
6666 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6667 6d022e97 2019-08-04 stsp if (error)
6668 6d022e97 2019-08-04 stsp goto done;
6669 17ed4618 2019-06-02 stsp
6670 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6671 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6672 f2a9dc41 2019-12-13 tracey struct stat sb;
6673 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6674 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6675 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6676 62d463ca 2020-10-20 naddy pe->path) == -1) {
6677 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6678 f2a9dc41 2019-12-13 tracey goto done;
6679 f2a9dc41 2019-12-13 tracey }
6680 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6681 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6682 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6683 f2a9dc41 2019-12-13 tracey continue;
6684 f2a9dc41 2019-12-13 tracey }
6685 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6686 f2a9dc41 2019-12-13 tracey ondisk_path);
6687 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6688 f2a9dc41 2019-12-13 tracey goto done;
6689 f2a9dc41 2019-12-13 tracey }
6690 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6691 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6692 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6693 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6694 f2a9dc41 2019-12-13 tracey goto done;
6695 f2a9dc41 2019-12-13 tracey }
6696 f2a9dc41 2019-12-13 tracey }
6697 f2a9dc41 2019-12-13 tracey }
6698 f2a9dc41 2019-12-13 tracey
6699 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6700 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6701 766841c2 2020-08-13 stsp repo, keep_on_disk);
6702 a129376b 2019-03-28 stsp done:
6703 1d0f4054 2021-06-17 stsp if (repo) {
6704 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6705 1d0f4054 2021-06-17 stsp if (error == NULL)
6706 1d0f4054 2021-06-17 stsp error = close_err;
6707 1d0f4054 2021-06-17 stsp }
6708 a129376b 2019-03-28 stsp if (worktree)
6709 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6710 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6711 17ed4618 2019-06-02 stsp free((char *)pe->path);
6712 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6713 a129376b 2019-03-28 stsp free(cwd);
6714 a129376b 2019-03-28 stsp return error;
6715 a129376b 2019-03-28 stsp }
6716 a129376b 2019-03-28 stsp
6717 a129376b 2019-03-28 stsp __dead static void
6718 a129376b 2019-03-28 stsp usage_revert(void)
6719 a129376b 2019-03-28 stsp {
6720 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6721 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6722 a129376b 2019-03-28 stsp exit(1);
6723 a129376b 2019-03-28 stsp }
6724 a129376b 2019-03-28 stsp
6725 1ee397ad 2019-07-12 stsp static const struct got_error *
6726 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6727 a129376b 2019-03-28 stsp {
6728 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6729 fb9704af 2020-01-27 stsp return NULL;
6730 fb9704af 2020-01-27 stsp
6731 a129376b 2019-03-28 stsp while (path[0] == '/')
6732 a129376b 2019-03-28 stsp path++;
6733 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6734 33aa809d 2019-08-08 stsp return NULL;
6735 33aa809d 2019-08-08 stsp }
6736 33aa809d 2019-08-08 stsp
6737 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6738 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6739 33aa809d 2019-08-08 stsp const char *action;
6740 33aa809d 2019-08-08 stsp };
6741 33aa809d 2019-08-08 stsp
6742 33aa809d 2019-08-08 stsp static const struct got_error *
6743 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6744 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6745 33aa809d 2019-08-08 stsp {
6746 33aa809d 2019-08-08 stsp char *line = NULL;
6747 33aa809d 2019-08-08 stsp size_t linesize = 0;
6748 33aa809d 2019-08-08 stsp ssize_t linelen;
6749 33aa809d 2019-08-08 stsp
6750 33aa809d 2019-08-08 stsp switch (status) {
6751 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6752 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6753 33aa809d 2019-08-08 stsp break;
6754 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6755 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6756 33aa809d 2019-08-08 stsp break;
6757 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6758 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6759 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6760 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6761 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6762 33aa809d 2019-08-08 stsp printf("%s", line);
6763 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6764 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6765 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6766 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6767 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6768 33aa809d 2019-08-08 stsp break;
6769 33aa809d 2019-08-08 stsp default:
6770 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6771 33aa809d 2019-08-08 stsp }
6772 33aa809d 2019-08-08 stsp
6773 33aa809d 2019-08-08 stsp return NULL;
6774 33aa809d 2019-08-08 stsp }
6775 33aa809d 2019-08-08 stsp
6776 33aa809d 2019-08-08 stsp static const struct got_error *
6777 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6778 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6779 33aa809d 2019-08-08 stsp {
6780 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6781 33aa809d 2019-08-08 stsp char *line = NULL;
6782 33aa809d 2019-08-08 stsp size_t linesize = 0;
6783 33aa809d 2019-08-08 stsp ssize_t linelen;
6784 33aa809d 2019-08-08 stsp int resp = ' ';
6785 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6786 33aa809d 2019-08-08 stsp
6787 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6788 33aa809d 2019-08-08 stsp
6789 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6790 33aa809d 2019-08-08 stsp char *nl;
6791 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6792 33aa809d 2019-08-08 stsp a->action);
6793 33aa809d 2019-08-08 stsp if (err)
6794 33aa809d 2019-08-08 stsp return err;
6795 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6796 33aa809d 2019-08-08 stsp if (linelen == -1) {
6797 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6798 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6799 33aa809d 2019-08-08 stsp return NULL;
6800 33aa809d 2019-08-08 stsp }
6801 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6802 33aa809d 2019-08-08 stsp if (nl)
6803 33aa809d 2019-08-08 stsp *nl = '\0';
6804 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6805 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6806 33aa809d 2019-08-08 stsp printf("y\n");
6807 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6808 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6809 33aa809d 2019-08-08 stsp printf("n\n");
6810 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6811 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6812 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6813 33aa809d 2019-08-08 stsp printf("q\n");
6814 33aa809d 2019-08-08 stsp } else
6815 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6816 33aa809d 2019-08-08 stsp free(line);
6817 33aa809d 2019-08-08 stsp return NULL;
6818 33aa809d 2019-08-08 stsp }
6819 33aa809d 2019-08-08 stsp
6820 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6821 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6822 33aa809d 2019-08-08 stsp a->action);
6823 33aa809d 2019-08-08 stsp if (err)
6824 33aa809d 2019-08-08 stsp return err;
6825 33aa809d 2019-08-08 stsp resp = getchar();
6826 33aa809d 2019-08-08 stsp if (resp == '\n')
6827 33aa809d 2019-08-08 stsp resp = getchar();
6828 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6829 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6830 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6831 33aa809d 2019-08-08 stsp resp = ' ';
6832 33aa809d 2019-08-08 stsp }
6833 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6834 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6835 33aa809d 2019-08-08 stsp resp = ' ';
6836 33aa809d 2019-08-08 stsp }
6837 33aa809d 2019-08-08 stsp }
6838 33aa809d 2019-08-08 stsp
6839 33aa809d 2019-08-08 stsp if (resp == 'y')
6840 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6841 33aa809d 2019-08-08 stsp else if (resp == 'n')
6842 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6843 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6844 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6845 33aa809d 2019-08-08 stsp
6846 1ee397ad 2019-07-12 stsp return NULL;
6847 a129376b 2019-03-28 stsp }
6848 a129376b 2019-03-28 stsp
6849 33aa809d 2019-08-08 stsp
6850 a129376b 2019-03-28 stsp static const struct got_error *
6851 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6852 a129376b 2019-03-28 stsp {
6853 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6854 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6855 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6856 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6857 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6858 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6859 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6860 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6861 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6862 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6863 a129376b 2019-03-28 stsp
6864 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6865 e20a8b6f 2019-06-04 stsp
6866 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6867 a129376b 2019-03-28 stsp switch (ch) {
6868 33aa809d 2019-08-08 stsp case 'p':
6869 33aa809d 2019-08-08 stsp pflag = 1;
6870 33aa809d 2019-08-08 stsp break;
6871 33aa809d 2019-08-08 stsp case 'F':
6872 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6873 33aa809d 2019-08-08 stsp break;
6874 0f6d7415 2019-08-08 stsp case 'R':
6875 0f6d7415 2019-08-08 stsp can_recurse = 1;
6876 0f6d7415 2019-08-08 stsp break;
6877 a129376b 2019-03-28 stsp default:
6878 a129376b 2019-03-28 stsp usage_revert();
6879 a129376b 2019-03-28 stsp /* NOTREACHED */
6880 a129376b 2019-03-28 stsp }
6881 a129376b 2019-03-28 stsp }
6882 a129376b 2019-03-28 stsp
6883 a129376b 2019-03-28 stsp argc -= optind;
6884 a129376b 2019-03-28 stsp argv += optind;
6885 a129376b 2019-03-28 stsp
6886 43012d58 2019-07-14 stsp #ifndef PROFILE
6887 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6888 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6889 43012d58 2019-07-14 stsp err(1, "pledge");
6890 43012d58 2019-07-14 stsp #endif
6891 e20a8b6f 2019-06-04 stsp if (argc < 1)
6892 a129376b 2019-03-28 stsp usage_revert();
6893 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6894 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6895 a129376b 2019-03-28 stsp
6896 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6897 a129376b 2019-03-28 stsp if (cwd == NULL) {
6898 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6899 a129376b 2019-03-28 stsp goto done;
6900 a129376b 2019-03-28 stsp }
6901 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6902 fa51e947 2020-03-27 stsp if (error) {
6903 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6904 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6905 2ec1f75b 2019-03-26 stsp goto done;
6906 fa51e947 2020-03-27 stsp }
6907 a129376b 2019-03-28 stsp
6908 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6909 c9956ddf 2019-09-08 stsp NULL);
6910 a129376b 2019-03-28 stsp if (error != NULL)
6911 a129376b 2019-03-28 stsp goto done;
6912 a129376b 2019-03-28 stsp
6913 33aa809d 2019-08-08 stsp if (patch_script_path) {
6914 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6915 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6916 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6917 33aa809d 2019-08-08 stsp patch_script_path);
6918 33aa809d 2019-08-08 stsp goto done;
6919 33aa809d 2019-08-08 stsp }
6920 33aa809d 2019-08-08 stsp }
6921 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6922 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6923 a129376b 2019-03-28 stsp if (error)
6924 a129376b 2019-03-28 stsp goto done;
6925 a129376b 2019-03-28 stsp
6926 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6927 6d022e97 2019-08-04 stsp if (error)
6928 6d022e97 2019-08-04 stsp goto done;
6929 00db391e 2019-08-03 stsp
6930 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6931 0f6d7415 2019-08-08 stsp char *ondisk_path;
6932 0f6d7415 2019-08-08 stsp struct stat sb;
6933 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6934 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6935 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6936 62d463ca 2020-10-20 naddy pe->path) == -1) {
6937 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6938 0f6d7415 2019-08-08 stsp goto done;
6939 0f6d7415 2019-08-08 stsp }
6940 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6941 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6942 0f6d7415 2019-08-08 stsp free(ondisk_path);
6943 0f6d7415 2019-08-08 stsp continue;
6944 0f6d7415 2019-08-08 stsp }
6945 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6946 0f6d7415 2019-08-08 stsp ondisk_path);
6947 0f6d7415 2019-08-08 stsp free(ondisk_path);
6948 0f6d7415 2019-08-08 stsp goto done;
6949 0f6d7415 2019-08-08 stsp }
6950 0f6d7415 2019-08-08 stsp free(ondisk_path);
6951 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6952 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6953 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6954 0f6d7415 2019-08-08 stsp goto done;
6955 0f6d7415 2019-08-08 stsp }
6956 0f6d7415 2019-08-08 stsp }
6957 0f6d7415 2019-08-08 stsp }
6958 0f6d7415 2019-08-08 stsp
6959 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6960 33aa809d 2019-08-08 stsp cpa.action = "revert";
6961 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6962 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6963 2ec1f75b 2019-03-26 stsp done:
6964 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6965 33aa809d 2019-08-08 stsp error == NULL)
6966 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6967 1d0f4054 2021-06-17 stsp if (repo) {
6968 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6969 1d0f4054 2021-06-17 stsp if (error == NULL)
6970 1d0f4054 2021-06-17 stsp error = close_err;
6971 1d0f4054 2021-06-17 stsp }
6972 2ec1f75b 2019-03-26 stsp if (worktree)
6973 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6974 2ec1f75b 2019-03-26 stsp free(path);
6975 d00136be 2019-03-26 stsp free(cwd);
6976 6bad629b 2019-02-04 stsp return error;
6977 c4296144 2019-05-09 stsp }
6978 c4296144 2019-05-09 stsp
6979 c4296144 2019-05-09 stsp __dead static void
6980 c4296144 2019-05-09 stsp usage_commit(void)
6981 c4296144 2019-05-09 stsp {
6982 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
6983 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
6984 c4296144 2019-05-09 stsp exit(1);
6985 33ad4cbe 2019-05-12 jcs }
6986 33ad4cbe 2019-05-12 jcs
6987 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6988 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6989 28cf319f 2021-01-28 stsp const char *prepared_log;
6990 28cf319f 2021-01-28 stsp int non_interactive;
6991 e2ba3d07 2019-05-13 stsp const char *editor;
6992 e0870e44 2019-05-13 stsp const char *worktree_path;
6993 76d98825 2019-06-03 stsp const char *branch_name;
6994 314a6357 2019-05-13 stsp const char *repo_path;
6995 e0870e44 2019-05-13 stsp char *logmsg_path;
6996 e2ba3d07 2019-05-13 stsp
6997 e2ba3d07 2019-05-13 stsp };
6998 28cf319f 2021-01-28 stsp
6999 28cf319f 2021-01-28 stsp static const struct got_error *
7000 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7001 28cf319f 2021-01-28 stsp {
7002 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7003 28cf319f 2021-01-28 stsp FILE *f = NULL;
7004 28cf319f 2021-01-28 stsp struct stat sb;
7005 28cf319f 2021-01-28 stsp size_t r;
7006 28cf319f 2021-01-28 stsp
7007 28cf319f 2021-01-28 stsp *logmsg = NULL;
7008 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7009 28cf319f 2021-01-28 stsp
7010 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7011 28cf319f 2021-01-28 stsp if (f == NULL)
7012 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7013 28cf319f 2021-01-28 stsp
7014 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7015 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7016 28cf319f 2021-01-28 stsp goto done;
7017 28cf319f 2021-01-28 stsp }
7018 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7019 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7020 28cf319f 2021-01-28 stsp goto done;
7021 28cf319f 2021-01-28 stsp }
7022 28cf319f 2021-01-28 stsp
7023 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7024 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7025 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7026 28cf319f 2021-01-28 stsp goto done;
7027 28cf319f 2021-01-28 stsp }
7028 28cf319f 2021-01-28 stsp
7029 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7030 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7031 28cf319f 2021-01-28 stsp if (ferror(f))
7032 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7033 28cf319f 2021-01-28 stsp else
7034 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7035 28cf319f 2021-01-28 stsp goto done;
7036 28cf319f 2021-01-28 stsp }
7037 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7038 28cf319f 2021-01-28 stsp done:
7039 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7040 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7041 28cf319f 2021-01-28 stsp if (err) {
7042 28cf319f 2021-01-28 stsp free(*logmsg);
7043 28cf319f 2021-01-28 stsp *logmsg = NULL;
7044 28cf319f 2021-01-28 stsp }
7045 28cf319f 2021-01-28 stsp return err;
7046 28cf319f 2021-01-28 stsp
7047 28cf319f 2021-01-28 stsp }
7048 e0870e44 2019-05-13 stsp
7049 33ad4cbe 2019-05-12 jcs static const struct got_error *
7050 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7051 33ad4cbe 2019-05-12 jcs void *arg)
7052 33ad4cbe 2019-05-12 jcs {
7053 76d98825 2019-06-03 stsp char *initial_content = NULL;
7054 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7055 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7056 e0870e44 2019-05-13 stsp char *template = NULL;
7057 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7058 1601cb9f 2020-09-11 naddy int initial_content_len;
7059 97972933 2020-09-11 stsp int fd = -1;
7060 33ad4cbe 2019-05-12 jcs size_t len;
7061 33ad4cbe 2019-05-12 jcs
7062 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7063 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7064 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7065 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7066 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7067 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7068 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7069 33ad4cbe 2019-05-12 jcs return NULL;
7070 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7071 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7072 33ad4cbe 2019-05-12 jcs
7073 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7074 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7075 76d98825 2019-06-03 stsp
7076 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7077 28cf319f 2021-01-28 stsp if (err)
7078 28cf319f 2021-01-28 stsp goto done;
7079 28cf319f 2021-01-28 stsp
7080 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7081 28cf319f 2021-01-28 stsp char *msg;
7082 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7083 28cf319f 2021-01-28 stsp if (err)
7084 28cf319f 2021-01-28 stsp goto done;
7085 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7086 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7087 28cf319f 2021-01-28 stsp free(msg);
7088 28cf319f 2021-01-28 stsp goto done;
7089 28cf319f 2021-01-28 stsp }
7090 28cf319f 2021-01-28 stsp free(msg);
7091 28cf319f 2021-01-28 stsp }
7092 28cf319f 2021-01-28 stsp
7093 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7094 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7095 1601cb9f 2020-09-11 naddy a->branch_name);
7096 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7097 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7098 e0870e44 2019-05-13 stsp goto done;
7099 28cf319f 2021-01-28 stsp }
7100 33ad4cbe 2019-05-12 jcs
7101 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7102 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7103 97972933 2020-09-11 stsp goto done;
7104 97972933 2020-09-11 stsp }
7105 33ad4cbe 2019-05-12 jcs
7106 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7107 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7108 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7109 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7110 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7111 33ad4cbe 2019-05-12 jcs }
7112 33ad4cbe 2019-05-12 jcs
7113 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7114 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7115 33ad4cbe 2019-05-12 jcs done:
7116 76d98825 2019-06-03 stsp free(initial_content);
7117 e0870e44 2019-05-13 stsp free(template);
7118 314a6357 2019-05-13 stsp
7119 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7120 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7121 97972933 2020-09-11 stsp
7122 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7123 59f86c76 2020-09-11 stsp if (err == NULL)
7124 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7125 59f86c76 2020-09-11 stsp if (err) {
7126 59f86c76 2020-09-11 stsp free(*logmsg);
7127 59f86c76 2020-09-11 stsp *logmsg = NULL;
7128 3ce1b845 2019-07-15 stsp }
7129 33ad4cbe 2019-05-12 jcs return err;
7130 6bad629b 2019-02-04 stsp }
7131 c4296144 2019-05-09 stsp
7132 c4296144 2019-05-09 stsp static const struct got_error *
7133 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7134 c4296144 2019-05-09 stsp {
7135 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7136 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7137 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7138 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7139 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7140 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7141 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7142 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7143 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7144 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7145 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7146 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7147 5c1e53bc 2019-07-28 stsp
7148 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7149 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7150 c4296144 2019-05-09 stsp
7151 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7152 c4296144 2019-05-09 stsp switch (ch) {
7153 28cf319f 2021-01-28 stsp case 'F':
7154 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7155 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7156 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7157 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7158 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7159 28cf319f 2021-01-28 stsp optarg);
7160 28cf319f 2021-01-28 stsp break;
7161 c4296144 2019-05-09 stsp case 'm':
7162 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7163 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7164 c4296144 2019-05-09 stsp logmsg = optarg;
7165 28cf319f 2021-01-28 stsp break;
7166 28cf319f 2021-01-28 stsp case 'N':
7167 28cf319f 2021-01-28 stsp non_interactive = 1;
7168 c4296144 2019-05-09 stsp break;
7169 35213c7c 2020-07-23 stsp case 'S':
7170 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7171 35213c7c 2020-07-23 stsp break;
7172 c4296144 2019-05-09 stsp default:
7173 c4296144 2019-05-09 stsp usage_commit();
7174 c4296144 2019-05-09 stsp /* NOTREACHED */
7175 c4296144 2019-05-09 stsp }
7176 c4296144 2019-05-09 stsp }
7177 c4296144 2019-05-09 stsp
7178 c4296144 2019-05-09 stsp argc -= optind;
7179 c4296144 2019-05-09 stsp argv += optind;
7180 c4296144 2019-05-09 stsp
7181 43012d58 2019-07-14 stsp #ifndef PROFILE
7182 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7183 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7184 43012d58 2019-07-14 stsp err(1, "pledge");
7185 43012d58 2019-07-14 stsp #endif
7186 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7187 c4296144 2019-05-09 stsp if (cwd == NULL) {
7188 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7189 c4296144 2019-05-09 stsp goto done;
7190 c4296144 2019-05-09 stsp }
7191 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7192 fa51e947 2020-03-27 stsp if (error) {
7193 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7194 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7195 7d5807f4 2019-07-11 stsp goto done;
7196 fa51e947 2020-03-27 stsp }
7197 7d5807f4 2019-07-11 stsp
7198 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7199 c4296144 2019-05-09 stsp if (error)
7200 a698f62e 2019-07-25 stsp goto done;
7201 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7202 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7203 c4296144 2019-05-09 stsp goto done;
7204 a698f62e 2019-07-25 stsp }
7205 5c1e53bc 2019-07-28 stsp
7206 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7207 916f288c 2019-07-30 stsp worktree);
7208 5c1e53bc 2019-07-28 stsp if (error)
7209 5c1e53bc 2019-07-28 stsp goto done;
7210 c4296144 2019-05-09 stsp
7211 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7212 c9956ddf 2019-09-08 stsp if (error)
7213 c9956ddf 2019-09-08 stsp goto done;
7214 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7215 c9956ddf 2019-09-08 stsp gitconfig_path);
7216 c4296144 2019-05-09 stsp if (error != NULL)
7217 c4296144 2019-05-09 stsp goto done;
7218 c4296144 2019-05-09 stsp
7219 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7220 aba9c984 2019-09-08 stsp if (error)
7221 aba9c984 2019-09-08 stsp return error;
7222 aba9c984 2019-09-08 stsp
7223 314a6357 2019-05-13 stsp /*
7224 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7225 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7226 314a6357 2019-05-13 stsp */
7227 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7228 314a6357 2019-05-13 stsp error = get_editor(&editor);
7229 314a6357 2019-05-13 stsp else
7230 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7231 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7232 c4296144 2019-05-09 stsp if (error)
7233 c4296144 2019-05-09 stsp goto done;
7234 c4296144 2019-05-09 stsp
7235 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7236 087fb88c 2019-08-04 stsp if (error)
7237 087fb88c 2019-08-04 stsp goto done;
7238 087fb88c 2019-08-04 stsp
7239 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7240 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7241 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7242 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7243 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7244 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7245 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7246 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7247 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7248 916f288c 2019-07-30 stsp goto done;
7249 916f288c 2019-07-30 stsp }
7250 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7251 916f288c 2019-07-30 stsp }
7252 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7253 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7254 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7255 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7256 e0870e44 2019-05-13 stsp if (error) {
7257 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7258 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7259 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7260 c4296144 2019-05-09 stsp goto done;
7261 e0870e44 2019-05-13 stsp }
7262 c4296144 2019-05-09 stsp
7263 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7264 c4296144 2019-05-09 stsp if (error)
7265 c4296144 2019-05-09 stsp goto done;
7266 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7267 c4296144 2019-05-09 stsp done:
7268 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7269 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7270 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7271 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7272 7266f21f 2019-10-21 stsp error == NULL)
7273 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7274 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7275 1d0f4054 2021-06-17 stsp if (repo) {
7276 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7277 1d0f4054 2021-06-17 stsp if (error == NULL)
7278 1d0f4054 2021-06-17 stsp error = close_err;
7279 1d0f4054 2021-06-17 stsp }
7280 c4296144 2019-05-09 stsp if (worktree)
7281 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7282 c4296144 2019-05-09 stsp free(cwd);
7283 c4296144 2019-05-09 stsp free(id_str);
7284 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7285 e2ba3d07 2019-05-13 stsp free(editor);
7286 aba9c984 2019-09-08 stsp free(author);
7287 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7288 234035bc 2019-06-01 stsp return error;
7289 234035bc 2019-06-01 stsp }
7290 234035bc 2019-06-01 stsp
7291 234035bc 2019-06-01 stsp __dead static void
7292 234035bc 2019-06-01 stsp usage_cherrypick(void)
7293 234035bc 2019-06-01 stsp {
7294 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7295 234035bc 2019-06-01 stsp exit(1);
7296 234035bc 2019-06-01 stsp }
7297 234035bc 2019-06-01 stsp
7298 234035bc 2019-06-01 stsp static const struct got_error *
7299 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7300 234035bc 2019-06-01 stsp {
7301 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7302 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7303 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7304 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7305 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7306 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7307 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7308 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
7309 9627c110 2020-04-18 stsp int ch;
7310 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7311 234035bc 2019-06-01 stsp
7312 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7313 234035bc 2019-06-01 stsp switch (ch) {
7314 234035bc 2019-06-01 stsp default:
7315 234035bc 2019-06-01 stsp usage_cherrypick();
7316 234035bc 2019-06-01 stsp /* NOTREACHED */
7317 234035bc 2019-06-01 stsp }
7318 234035bc 2019-06-01 stsp }
7319 234035bc 2019-06-01 stsp
7320 234035bc 2019-06-01 stsp argc -= optind;
7321 234035bc 2019-06-01 stsp argv += optind;
7322 234035bc 2019-06-01 stsp
7323 43012d58 2019-07-14 stsp #ifndef PROFILE
7324 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7325 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7326 43012d58 2019-07-14 stsp err(1, "pledge");
7327 43012d58 2019-07-14 stsp #endif
7328 234035bc 2019-06-01 stsp if (argc != 1)
7329 234035bc 2019-06-01 stsp usage_cherrypick();
7330 234035bc 2019-06-01 stsp
7331 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7332 234035bc 2019-06-01 stsp if (cwd == NULL) {
7333 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7334 234035bc 2019-06-01 stsp goto done;
7335 234035bc 2019-06-01 stsp }
7336 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7337 fa51e947 2020-03-27 stsp if (error) {
7338 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7339 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7340 fa51e947 2020-03-27 stsp cwd);
7341 234035bc 2019-06-01 stsp goto done;
7342 fa51e947 2020-03-27 stsp }
7343 234035bc 2019-06-01 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 234035bc 2019-06-01 stsp if (error != NULL)
7347 234035bc 2019-06-01 stsp goto done;
7348 234035bc 2019-06-01 stsp
7349 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7350 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7351 234035bc 2019-06-01 stsp if (error)
7352 234035bc 2019-06-01 stsp goto done;
7353 234035bc 2019-06-01 stsp
7354 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7355 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7356 234035bc 2019-06-01 stsp if (error != NULL) {
7357 234035bc 2019-06-01 stsp struct got_reference *ref;
7358 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7359 234035bc 2019-06-01 stsp goto done;
7360 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7361 234035bc 2019-06-01 stsp if (error != NULL)
7362 234035bc 2019-06-01 stsp goto done;
7363 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7364 234035bc 2019-06-01 stsp got_ref_close(ref);
7365 234035bc 2019-06-01 stsp if (error != NULL)
7366 234035bc 2019-06-01 stsp goto done;
7367 234035bc 2019-06-01 stsp }
7368 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7369 234035bc 2019-06-01 stsp if (error)
7370 234035bc 2019-06-01 stsp goto done;
7371 234035bc 2019-06-01 stsp
7372 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7373 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7374 234035bc 2019-06-01 stsp if (error != NULL)
7375 234035bc 2019-06-01 stsp goto done;
7376 234035bc 2019-06-01 stsp
7377 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7378 234035bc 2019-06-01 stsp if (error) {
7379 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7380 234035bc 2019-06-01 stsp goto done;
7381 234035bc 2019-06-01 stsp error = NULL;
7382 234035bc 2019-06-01 stsp } else {
7383 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7384 234035bc 2019-06-01 stsp goto done;
7385 234035bc 2019-06-01 stsp }
7386 234035bc 2019-06-01 stsp
7387 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7388 234035bc 2019-06-01 stsp if (error)
7389 234035bc 2019-06-01 stsp goto done;
7390 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7391 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7392 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7393 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7394 03415a1a 2019-06-02 stsp NULL);
7395 234035bc 2019-06-01 stsp if (error != NULL)
7396 234035bc 2019-06-01 stsp goto done;
7397 234035bc 2019-06-01 stsp
7398 9627c110 2020-04-18 stsp if (upa.did_something)
7399 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7400 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7401 234035bc 2019-06-01 stsp done:
7402 234035bc 2019-06-01 stsp if (commit)
7403 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7404 234035bc 2019-06-01 stsp free(commit_id_str);
7405 234035bc 2019-06-01 stsp if (head_ref)
7406 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7407 234035bc 2019-06-01 stsp if (worktree)
7408 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7409 1d0f4054 2021-06-17 stsp if (repo) {
7410 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7411 1d0f4054 2021-06-17 stsp if (error == NULL)
7412 1d0f4054 2021-06-17 stsp error = close_err;
7413 1d0f4054 2021-06-17 stsp }
7414 c4296144 2019-05-09 stsp return error;
7415 c4296144 2019-05-09 stsp }
7416 5ef14e63 2019-06-02 stsp
7417 5ef14e63 2019-06-02 stsp __dead static void
7418 5ef14e63 2019-06-02 stsp usage_backout(void)
7419 5ef14e63 2019-06-02 stsp {
7420 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7421 5ef14e63 2019-06-02 stsp exit(1);
7422 5ef14e63 2019-06-02 stsp }
7423 5ef14e63 2019-06-02 stsp
7424 5ef14e63 2019-06-02 stsp static const struct got_error *
7425 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7426 5ef14e63 2019-06-02 stsp {
7427 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7428 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7429 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7430 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7431 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7432 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7433 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7434 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7435 9627c110 2020-04-18 stsp int ch;
7436 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7437 5ef14e63 2019-06-02 stsp
7438 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7439 5ef14e63 2019-06-02 stsp switch (ch) {
7440 5ef14e63 2019-06-02 stsp default:
7441 5ef14e63 2019-06-02 stsp usage_backout();
7442 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7443 5ef14e63 2019-06-02 stsp }
7444 5ef14e63 2019-06-02 stsp }
7445 5ef14e63 2019-06-02 stsp
7446 5ef14e63 2019-06-02 stsp argc -= optind;
7447 5ef14e63 2019-06-02 stsp argv += optind;
7448 5ef14e63 2019-06-02 stsp
7449 43012d58 2019-07-14 stsp #ifndef PROFILE
7450 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7451 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7452 43012d58 2019-07-14 stsp err(1, "pledge");
7453 43012d58 2019-07-14 stsp #endif
7454 5ef14e63 2019-06-02 stsp if (argc != 1)
7455 5ef14e63 2019-06-02 stsp usage_backout();
7456 5ef14e63 2019-06-02 stsp
7457 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7458 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7459 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7460 5ef14e63 2019-06-02 stsp goto done;
7461 5ef14e63 2019-06-02 stsp }
7462 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7463 fa51e947 2020-03-27 stsp if (error) {
7464 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7465 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7466 5ef14e63 2019-06-02 stsp goto done;
7467 fa51e947 2020-03-27 stsp }
7468 5ef14e63 2019-06-02 stsp
7469 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7470 c9956ddf 2019-09-08 stsp NULL);
7471 5ef14e63 2019-06-02 stsp if (error != NULL)
7472 5ef14e63 2019-06-02 stsp goto done;
7473 5ef14e63 2019-06-02 stsp
7474 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7475 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7476 5ef14e63 2019-06-02 stsp if (error)
7477 5ef14e63 2019-06-02 stsp goto done;
7478 5ef14e63 2019-06-02 stsp
7479 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7480 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7481 5ef14e63 2019-06-02 stsp if (error != NULL) {
7482 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7483 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7484 5ef14e63 2019-06-02 stsp goto done;
7485 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7486 5ef14e63 2019-06-02 stsp if (error != NULL)
7487 5ef14e63 2019-06-02 stsp goto done;
7488 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7489 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7490 5ef14e63 2019-06-02 stsp if (error != NULL)
7491 5ef14e63 2019-06-02 stsp goto done;
7492 5ef14e63 2019-06-02 stsp }
7493 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7494 5ef14e63 2019-06-02 stsp if (error)
7495 5ef14e63 2019-06-02 stsp goto done;
7496 5ef14e63 2019-06-02 stsp
7497 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7498 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7499 5ef14e63 2019-06-02 stsp if (error != NULL)
7500 5ef14e63 2019-06-02 stsp goto done;
7501 5ef14e63 2019-06-02 stsp
7502 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7503 5ef14e63 2019-06-02 stsp if (error)
7504 5ef14e63 2019-06-02 stsp goto done;
7505 5ef14e63 2019-06-02 stsp
7506 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7507 5ef14e63 2019-06-02 stsp if (error)
7508 5ef14e63 2019-06-02 stsp goto done;
7509 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7510 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7511 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7512 5ef14e63 2019-06-02 stsp goto done;
7513 5ef14e63 2019-06-02 stsp }
7514 5ef14e63 2019-06-02 stsp
7515 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7516 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7517 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7518 5ef14e63 2019-06-02 stsp if (error != NULL)
7519 5ef14e63 2019-06-02 stsp goto done;
7520 5ef14e63 2019-06-02 stsp
7521 9627c110 2020-04-18 stsp if (upa.did_something)
7522 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7523 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7524 5ef14e63 2019-06-02 stsp done:
7525 5ef14e63 2019-06-02 stsp if (commit)
7526 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7527 5ef14e63 2019-06-02 stsp free(commit_id_str);
7528 5ef14e63 2019-06-02 stsp if (head_ref)
7529 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7530 818c7501 2019-07-11 stsp if (worktree)
7531 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7532 1d0f4054 2021-06-17 stsp if (repo) {
7533 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7534 1d0f4054 2021-06-17 stsp if (error == NULL)
7535 1d0f4054 2021-06-17 stsp error = close_err;
7536 1d0f4054 2021-06-17 stsp }
7537 818c7501 2019-07-11 stsp return error;
7538 818c7501 2019-07-11 stsp }
7539 818c7501 2019-07-11 stsp
7540 818c7501 2019-07-11 stsp __dead static void
7541 818c7501 2019-07-11 stsp usage_rebase(void)
7542 818c7501 2019-07-11 stsp {
7543 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
7544 af54c8f8 2019-07-11 stsp getprogname());
7545 818c7501 2019-07-11 stsp exit(1);
7546 0ebf8283 2019-07-24 stsp }
7547 0ebf8283 2019-07-24 stsp
7548 0ebf8283 2019-07-24 stsp void
7549 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7550 0ebf8283 2019-07-24 stsp {
7551 0ebf8283 2019-07-24 stsp char *nl;
7552 0ebf8283 2019-07-24 stsp size_t len;
7553 0ebf8283 2019-07-24 stsp
7554 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7555 0ebf8283 2019-07-24 stsp if (len > limit)
7556 0ebf8283 2019-07-24 stsp len = limit;
7557 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7558 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7559 0ebf8283 2019-07-24 stsp if (nl)
7560 0ebf8283 2019-07-24 stsp *nl = '\0';
7561 0ebf8283 2019-07-24 stsp }
7562 0ebf8283 2019-07-24 stsp
7563 0ebf8283 2019-07-24 stsp static const struct got_error *
7564 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7565 0ebf8283 2019-07-24 stsp {
7566 5943eee2 2019-08-13 stsp const struct got_error *err;
7567 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7568 5943eee2 2019-08-13 stsp const char *s;
7569 0ebf8283 2019-07-24 stsp
7570 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7571 5943eee2 2019-08-13 stsp if (err)
7572 5943eee2 2019-08-13 stsp return err;
7573 0ebf8283 2019-07-24 stsp
7574 5943eee2 2019-08-13 stsp s = logmsg0;
7575 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7576 5943eee2 2019-08-13 stsp s++;
7577 5943eee2 2019-08-13 stsp
7578 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7579 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7580 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7581 5943eee2 2019-08-13 stsp goto done;
7582 5943eee2 2019-08-13 stsp }
7583 0ebf8283 2019-07-24 stsp
7584 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7585 5943eee2 2019-08-13 stsp done:
7586 5943eee2 2019-08-13 stsp free(logmsg0);
7587 a0ea4fc0 2020-02-28 stsp return err;
7588 a0ea4fc0 2020-02-28 stsp }
7589 a0ea4fc0 2020-02-28 stsp
7590 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7591 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7592 a0ea4fc0 2020-02-28 stsp {
7593 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7594 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7595 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7596 a0ea4fc0 2020-02-28 stsp
7597 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7598 a0ea4fc0 2020-02-28 stsp if (err)
7599 a0ea4fc0 2020-02-28 stsp return err;
7600 a0ea4fc0 2020-02-28 stsp
7601 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7602 a0ea4fc0 2020-02-28 stsp if (err)
7603 a0ea4fc0 2020-02-28 stsp goto done;
7604 a0ea4fc0 2020-02-28 stsp
7605 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7606 a0ea4fc0 2020-02-28 stsp
7607 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7608 a0ea4fc0 2020-02-28 stsp if (err)
7609 a0ea4fc0 2020-02-28 stsp goto done;
7610 a0ea4fc0 2020-02-28 stsp
7611 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7612 a0ea4fc0 2020-02-28 stsp done:
7613 a0ea4fc0 2020-02-28 stsp free(id_str);
7614 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7615 a0ea4fc0 2020-02-28 stsp free(logmsg);
7616 5943eee2 2019-08-13 stsp return err;
7617 818c7501 2019-07-11 stsp }
7618 818c7501 2019-07-11 stsp
7619 818c7501 2019-07-11 stsp static const struct got_error *
7620 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7621 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7622 818c7501 2019-07-11 stsp {
7623 818c7501 2019-07-11 stsp const struct got_error *err;
7624 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7625 818c7501 2019-07-11 stsp
7626 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7627 818c7501 2019-07-11 stsp if (err)
7628 818c7501 2019-07-11 stsp goto done;
7629 818c7501 2019-07-11 stsp
7630 ff0d2220 2019-07-11 stsp if (new_id) {
7631 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7632 ff0d2220 2019-07-11 stsp if (err)
7633 ff0d2220 2019-07-11 stsp goto done;
7634 ff0d2220 2019-07-11 stsp }
7635 818c7501 2019-07-11 stsp
7636 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7637 ff0d2220 2019-07-11 stsp if (new_id_str)
7638 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7639 0ebf8283 2019-07-24 stsp
7640 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7641 0ebf8283 2019-07-24 stsp if (err)
7642 0ebf8283 2019-07-24 stsp goto done;
7643 0ebf8283 2019-07-24 stsp
7644 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7645 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7646 818c7501 2019-07-11 stsp done:
7647 818c7501 2019-07-11 stsp free(old_id_str);
7648 818c7501 2019-07-11 stsp free(new_id_str);
7649 272a1371 2020-02-28 stsp free(logmsg);
7650 818c7501 2019-07-11 stsp return err;
7651 818c7501 2019-07-11 stsp }
7652 818c7501 2019-07-11 stsp
7653 1ee397ad 2019-07-12 stsp static const struct got_error *
7654 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7655 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7656 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
7657 e600f124 2021-03-21 stsp int create_backup)
7658 818c7501 2019-07-11 stsp {
7659 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7660 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7661 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
7662 818c7501 2019-07-11 stsp }
7663 818c7501 2019-07-11 stsp
7664 818c7501 2019-07-11 stsp static const struct got_error *
7665 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7666 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7667 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7668 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7669 ff0d2220 2019-07-11 stsp {
7670 ff0d2220 2019-07-11 stsp const struct got_error *error;
7671 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7672 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7673 ff0d2220 2019-07-11 stsp
7674 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7675 ff0d2220 2019-07-11 stsp if (error)
7676 ff0d2220 2019-07-11 stsp return error;
7677 ff0d2220 2019-07-11 stsp
7678 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7679 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7680 ff0d2220 2019-07-11 stsp if (error) {
7681 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7682 ff0d2220 2019-07-11 stsp goto done;
7683 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7684 ff0d2220 2019-07-11 stsp } else {
7685 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7686 ff0d2220 2019-07-11 stsp free(new_commit_id);
7687 ff0d2220 2019-07-11 stsp }
7688 ff0d2220 2019-07-11 stsp done:
7689 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7690 ff0d2220 2019-07-11 stsp return error;
7691 64c6d990 2019-07-11 stsp }
7692 64c6d990 2019-07-11 stsp
7693 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7694 64c6d990 2019-07-11 stsp const char *path_prefix;
7695 64c6d990 2019-07-11 stsp size_t len;
7696 8ca9bd68 2019-07-25 stsp int errcode;
7697 64c6d990 2019-07-11 stsp };
7698 64c6d990 2019-07-11 stsp
7699 64c6d990 2019-07-11 stsp static const struct got_error *
7700 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7701 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7702 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7703 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7704 64c6d990 2019-07-11 stsp {
7705 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7706 64c6d990 2019-07-11 stsp
7707 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7708 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7709 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7710 64c6d990 2019-07-11 stsp
7711 64c6d990 2019-07-11 stsp return NULL;
7712 64c6d990 2019-07-11 stsp }
7713 64c6d990 2019-07-11 stsp
7714 64c6d990 2019-07-11 stsp static const struct got_error *
7715 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7716 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7717 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7718 64c6d990 2019-07-11 stsp {
7719 64c6d990 2019-07-11 stsp const struct got_error *err;
7720 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7721 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7722 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7723 64c6d990 2019-07-11 stsp
7724 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7725 64c6d990 2019-07-11 stsp return NULL;
7726 64c6d990 2019-07-11 stsp
7727 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7728 64c6d990 2019-07-11 stsp if (err)
7729 64c6d990 2019-07-11 stsp goto done;
7730 64c6d990 2019-07-11 stsp
7731 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7732 64c6d990 2019-07-11 stsp if (err)
7733 64c6d990 2019-07-11 stsp goto done;
7734 64c6d990 2019-07-11 stsp
7735 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7736 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7737 64c6d990 2019-07-11 stsp if (err)
7738 64c6d990 2019-07-11 stsp goto done;
7739 64c6d990 2019-07-11 stsp
7740 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7741 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7742 64c6d990 2019-07-11 stsp if (err)
7743 64c6d990 2019-07-11 stsp goto done;
7744 64c6d990 2019-07-11 stsp
7745 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7746 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7747 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7748 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7749 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7750 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7751 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7752 64c6d990 2019-07-11 stsp done:
7753 64c6d990 2019-07-11 stsp if (tree1)
7754 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7755 64c6d990 2019-07-11 stsp if (tree2)
7756 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7757 64c6d990 2019-07-11 stsp if (commit)
7758 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7759 64c6d990 2019-07-11 stsp if (parent_commit)
7760 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7761 64c6d990 2019-07-11 stsp return err;
7762 ff0d2220 2019-07-11 stsp }
7763 ff0d2220 2019-07-11 stsp
7764 ff0d2220 2019-07-11 stsp static const struct got_error *
7765 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7766 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7767 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7768 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7769 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7770 af61c510 2019-07-19 stsp {
7771 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7772 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7773 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7774 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7775 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7776 af61c510 2019-07-19 stsp
7777 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7778 af61c510 2019-07-19 stsp if (err)
7779 af61c510 2019-07-19 stsp return err;
7780 af61c510 2019-07-19 stsp
7781 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7782 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7783 af61c510 2019-07-19 stsp if (err)
7784 af61c510 2019-07-19 stsp goto done;
7785 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7786 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7787 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7788 af61c510 2019-07-19 stsp if (err) {
7789 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7790 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7791 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7792 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7793 af61c510 2019-07-19 stsp "been reached?!?");
7794 ee780d5c 2020-01-04 stsp }
7795 ee780d5c 2020-01-04 stsp goto done;
7796 af61c510 2019-07-19 stsp } else {
7797 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7798 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7799 af61c510 2019-07-19 stsp if (err)
7800 af61c510 2019-07-19 stsp goto done;
7801 af61c510 2019-07-19 stsp
7802 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7803 af61c510 2019-07-19 stsp if (err)
7804 af61c510 2019-07-19 stsp goto done;
7805 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
7806 af61c510 2019-07-19 stsp commit_id = parent_id;
7807 af61c510 2019-07-19 stsp }
7808 af61c510 2019-07-19 stsp }
7809 af61c510 2019-07-19 stsp done:
7810 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7811 af61c510 2019-07-19 stsp return err;
7812 e600f124 2021-03-21 stsp }
7813 e600f124 2021-03-21 stsp
7814 e600f124 2021-03-21 stsp static const struct got_error *
7815 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
7816 e600f124 2021-03-21 stsp {
7817 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
7818 e600f124 2021-03-21 stsp time_t committer_time;
7819 e600f124 2021-03-21 stsp struct tm tm;
7820 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
7821 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
7822 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
7823 e600f124 2021-03-21 stsp
7824 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
7825 e600f124 2021-03-21 stsp if (localtime_r(&committer_time, &tm) == NULL)
7826 e600f124 2021-03-21 stsp return got_error_from_errno("localtime_r");
7827 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
7828 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
7829 e600f124 2021-03-21 stsp
7830 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
7831 e600f124 2021-03-21 stsp if (author0 == NULL)
7832 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
7833 e600f124 2021-03-21 stsp author = author0;
7834 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
7835 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
7836 e600f124 2021-03-21 stsp author = smallerthan + 1;
7837 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
7838 e600f124 2021-03-21 stsp
7839 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7840 e600f124 2021-03-21 stsp if (err)
7841 e600f124 2021-03-21 stsp goto done;
7842 e600f124 2021-03-21 stsp logmsg = logmsg0;
7843 e600f124 2021-03-21 stsp while (*logmsg == '\n')
7844 e600f124 2021-03-21 stsp logmsg++;
7845 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
7846 e600f124 2021-03-21 stsp if (newline)
7847 e600f124 2021-03-21 stsp *newline = '\0';
7848 e600f124 2021-03-21 stsp
7849 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
7850 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
7851 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7852 e600f124 2021-03-21 stsp done:
7853 e600f124 2021-03-21 stsp free(author0);
7854 e600f124 2021-03-21 stsp free(logmsg0);
7855 643b85bc 2021-07-16 stsp return err;
7856 643b85bc 2021-07-16 stsp }
7857 643b85bc 2021-07-16 stsp
7858 643b85bc 2021-07-16 stsp static const struct got_error *
7859 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
7860 643b85bc 2021-07-16 stsp struct got_repository *repo)
7861 643b85bc 2021-07-16 stsp {
7862 643b85bc 2021-07-16 stsp const struct got_error *err;
7863 643b85bc 2021-07-16 stsp char *id_str;
7864 643b85bc 2021-07-16 stsp
7865 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
7866 643b85bc 2021-07-16 stsp if (err)
7867 643b85bc 2021-07-16 stsp return err;
7868 643b85bc 2021-07-16 stsp
7869 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
7870 643b85bc 2021-07-16 stsp if (err)
7871 643b85bc 2021-07-16 stsp goto done;
7872 643b85bc 2021-07-16 stsp
7873 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
7874 643b85bc 2021-07-16 stsp done:
7875 643b85bc 2021-07-16 stsp free(id_str);
7876 e600f124 2021-03-21 stsp return err;
7877 e600f124 2021-03-21 stsp }
7878 e600f124 2021-03-21 stsp
7879 e600f124 2021-03-21 stsp static const struct got_error *
7880 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
7881 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
7882 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
7883 e600f124 2021-03-21 stsp struct got_repository *repo)
7884 e600f124 2021-03-21 stsp {
7885 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
7886 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
7887 e600f124 2021-03-21 stsp char *refs_str = NULL;
7888 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
7889 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
7890 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
7891 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
7892 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
7893 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
7894 e600f124 2021-03-21 stsp char *custom_refs_str;
7895 e600f124 2021-03-21 stsp
7896 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
7897 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
7898 e600f124 2021-03-21 stsp
7899 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
7900 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
7901 e600f124 2021-03-21 stsp if (err)
7902 e600f124 2021-03-21 stsp goto done;
7903 e600f124 2021-03-21 stsp
7904 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
7905 e600f124 2021-03-21 stsp if (err)
7906 e600f124 2021-03-21 stsp goto done;
7907 e600f124 2021-03-21 stsp
7908 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
7909 e600f124 2021-03-21 stsp if (refs) {
7910 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
7911 e600f124 2021-03-21 stsp if (err)
7912 e600f124 2021-03-21 stsp goto done;
7913 e600f124 2021-03-21 stsp }
7914 e600f124 2021-03-21 stsp
7915 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
7916 e600f124 2021-03-21 stsp if (err)
7917 e600f124 2021-03-21 stsp goto done;
7918 e600f124 2021-03-21 stsp
7919 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
7920 e600f124 2021-03-21 stsp if (err)
7921 e600f124 2021-03-21 stsp goto done;
7922 e600f124 2021-03-21 stsp
7923 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7924 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
7925 e600f124 2021-03-21 stsp if (err)
7926 e600f124 2021-03-21 stsp goto done;
7927 e600f124 2021-03-21 stsp
7928 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
7929 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
7930 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
7931 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
7932 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
7933 e600f124 2021-03-21 stsp free(refs_str);
7934 e600f124 2021-03-21 stsp refs_str = NULL;
7935 e600f124 2021-03-21 stsp
7936 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
7937 e600f124 2021-03-21 stsp if (err)
7938 e600f124 2021-03-21 stsp goto done;
7939 e600f124 2021-03-21 stsp
7940 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
7941 e600f124 2021-03-21 stsp if (err)
7942 e600f124 2021-03-21 stsp goto done;
7943 e600f124 2021-03-21 stsp
7944 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
7945 e600f124 2021-03-21 stsp if (err)
7946 e600f124 2021-03-21 stsp goto done;
7947 e600f124 2021-03-21 stsp
7948 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
7949 e600f124 2021-03-21 stsp if (refs) {
7950 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
7951 e600f124 2021-03-21 stsp if (err)
7952 e600f124 2021-03-21 stsp goto done;
7953 e600f124 2021-03-21 stsp }
7954 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
7955 e600f124 2021-03-21 stsp yca_id_str,
7956 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
7957 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
7958 e600f124 2021-03-21 stsp }
7959 e600f124 2021-03-21 stsp done:
7960 e600f124 2021-03-21 stsp free(custom_refs_str);
7961 e600f124 2021-03-21 stsp free(new_commit_id);
7962 e600f124 2021-03-21 stsp free(refs_str);
7963 e600f124 2021-03-21 stsp free(yca_id);
7964 e600f124 2021-03-21 stsp free(yca_id_str);
7965 e600f124 2021-03-21 stsp free(yca_brief_str);
7966 e600f124 2021-03-21 stsp if (new_commit)
7967 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
7968 e600f124 2021-03-21 stsp if (yca_commit)
7969 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
7970 e600f124 2021-03-21 stsp
7971 e600f124 2021-03-21 stsp return NULL;
7972 af61c510 2019-07-19 stsp }
7973 af61c510 2019-07-19 stsp
7974 af61c510 2019-07-19 stsp static const struct got_error *
7975 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
7976 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
7977 e600f124 2021-03-21 stsp {
7978 e600f124 2021-03-21 stsp const struct got_error *err;
7979 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
7980 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
7981 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
7982 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7983 e600f124 2021-03-21 stsp char *branch_name = NULL;
7984 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
7985 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
7986 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
7987 e600f124 2021-03-21 stsp
7988 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
7989 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
7990 e600f124 2021-03-21 stsp
7991 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7992 e600f124 2021-03-21 stsp if (err)
7993 e600f124 2021-03-21 stsp return err;
7994 e600f124 2021-03-21 stsp
7995 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
7996 e600f124 2021-03-21 stsp if (err)
7997 e600f124 2021-03-21 stsp goto done;
7998 e600f124 2021-03-21 stsp
7999 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8000 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8001 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8002 e600f124 2021-03-21 stsp }
8003 e600f124 2021-03-21 stsp
8004 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8005 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8006 e600f124 2021-03-21 stsp if (err)
8007 e600f124 2021-03-21 stsp goto done;
8008 e600f124 2021-03-21 stsp
8009 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8010 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8011 e600f124 2021-03-21 stsp char *slash;
8012 e600f124 2021-03-21 stsp
8013 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8014 643b85bc 2021-07-16 stsp if (err)
8015 643b85bc 2021-07-16 stsp break;
8016 643b85bc 2021-07-16 stsp
8017 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8018 e600f124 2021-03-21 stsp if (err)
8019 e600f124 2021-03-21 stsp break;
8020 e600f124 2021-03-21 stsp
8021 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8022 e600f124 2021-03-21 stsp old_commit_id);
8023 e600f124 2021-03-21 stsp if (err)
8024 e600f124 2021-03-21 stsp break;
8025 e600f124 2021-03-21 stsp
8026 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8027 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8028 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8029 e600f124 2021-03-21 stsp
8030 e600f124 2021-03-21 stsp while (refname[0] == '/')
8031 e600f124 2021-03-21 stsp refname++;
8032 e600f124 2021-03-21 stsp
8033 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8034 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8035 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8036 e600f124 2021-03-21 stsp break;
8037 e600f124 2021-03-21 stsp }
8038 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8039 e600f124 2021-03-21 stsp if (slash) {
8040 e600f124 2021-03-21 stsp *slash = '\0';
8041 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8042 e600f124 2021-03-21 stsp }
8043 e600f124 2021-03-21 stsp
8044 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8045 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8046 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8047 643b85bc 2021-07-16 stsp if (delete) {
8048 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8049 643b85bc 2021-07-16 stsp old_commit_id, repo);
8050 643b85bc 2021-07-16 stsp } else {
8051 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8052 643b85bc 2021-07-16 stsp old_commit_id, old_commit, refs_idmap,
8053 643b85bc 2021-07-16 stsp repo);
8054 643b85bc 2021-07-16 stsp }
8055 e600f124 2021-03-21 stsp if (err)
8056 e600f124 2021-03-21 stsp break;
8057 e600f124 2021-03-21 stsp }
8058 e600f124 2021-03-21 stsp
8059 e600f124 2021-03-21 stsp free(old_commit_id);
8060 e600f124 2021-03-21 stsp old_commit_id = NULL;
8061 e600f124 2021-03-21 stsp free(branch_name);
8062 e600f124 2021-03-21 stsp branch_name = NULL;
8063 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8064 e600f124 2021-03-21 stsp old_commit = NULL;
8065 e600f124 2021-03-21 stsp }
8066 9e822917 2021-03-23 stsp
8067 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8068 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8069 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8070 9e822917 2021-03-23 stsp }
8071 e600f124 2021-03-21 stsp done:
8072 e600f124 2021-03-21 stsp if (refs_idmap)
8073 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8074 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8075 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8076 e600f124 2021-03-21 stsp free(old_commit_id);
8077 e600f124 2021-03-21 stsp free(branch_name);
8078 e600f124 2021-03-21 stsp if (old_commit)
8079 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8080 e600f124 2021-03-21 stsp return err;
8081 e600f124 2021-03-21 stsp }
8082 e600f124 2021-03-21 stsp
8083 e600f124 2021-03-21 stsp static const struct got_error *
8084 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8085 818c7501 2019-07-11 stsp {
8086 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8087 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8088 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8089 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8090 818c7501 2019-07-11 stsp char *cwd = NULL;
8091 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8092 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8093 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8094 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8095 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8096 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8097 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8098 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8099 643b85bc 2021-07-16 stsp int delete_backups = 0;
8100 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8101 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8102 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8103 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8104 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8105 818c7501 2019-07-11 stsp
8106 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8107 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8108 818c7501 2019-07-11 stsp
8109 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8110 818c7501 2019-07-11 stsp switch (ch) {
8111 818c7501 2019-07-11 stsp case 'a':
8112 818c7501 2019-07-11 stsp abort_rebase = 1;
8113 818c7501 2019-07-11 stsp break;
8114 818c7501 2019-07-11 stsp case 'c':
8115 818c7501 2019-07-11 stsp continue_rebase = 1;
8116 818c7501 2019-07-11 stsp break;
8117 e600f124 2021-03-21 stsp case 'l':
8118 e600f124 2021-03-21 stsp list_backups = 1;
8119 e600f124 2021-03-21 stsp break;
8120 643b85bc 2021-07-16 stsp case 'X':
8121 643b85bc 2021-07-16 stsp delete_backups = 1;
8122 643b85bc 2021-07-16 stsp break;
8123 818c7501 2019-07-11 stsp default:
8124 818c7501 2019-07-11 stsp usage_rebase();
8125 818c7501 2019-07-11 stsp /* NOTREACHED */
8126 818c7501 2019-07-11 stsp }
8127 818c7501 2019-07-11 stsp }
8128 818c7501 2019-07-11 stsp
8129 818c7501 2019-07-11 stsp argc -= optind;
8130 818c7501 2019-07-11 stsp argv += optind;
8131 818c7501 2019-07-11 stsp
8132 43012d58 2019-07-14 stsp #ifndef PROFILE
8133 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8134 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8135 43012d58 2019-07-14 stsp err(1, "pledge");
8136 43012d58 2019-07-14 stsp #endif
8137 e600f124 2021-03-21 stsp if (list_backups) {
8138 e600f124 2021-03-21 stsp if (abort_rebase)
8139 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8140 e600f124 2021-03-21 stsp if (continue_rebase)
8141 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8142 643b85bc 2021-07-16 stsp if (delete_backups)
8143 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8144 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8145 643b85bc 2021-07-16 stsp usage_rebase();
8146 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8147 643b85bc 2021-07-16 stsp if (abort_rebase)
8148 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8149 643b85bc 2021-07-16 stsp if (continue_rebase)
8150 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8151 643b85bc 2021-07-16 stsp if (list_backups)
8152 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8153 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8154 818c7501 2019-07-11 stsp usage_rebase();
8155 e600f124 2021-03-21 stsp } else {
8156 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8157 e600f124 2021-03-21 stsp usage_rebase();
8158 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8159 e600f124 2021-03-21 stsp if (argc != 0)
8160 e600f124 2021-03-21 stsp usage_rebase();
8161 e600f124 2021-03-21 stsp } else if (argc != 1)
8162 e600f124 2021-03-21 stsp usage_rebase();
8163 e600f124 2021-03-21 stsp }
8164 818c7501 2019-07-11 stsp
8165 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8166 818c7501 2019-07-11 stsp if (cwd == NULL) {
8167 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8168 818c7501 2019-07-11 stsp goto done;
8169 818c7501 2019-07-11 stsp }
8170 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8171 fa51e947 2020-03-27 stsp if (error) {
8172 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8173 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8174 e600f124 2021-03-21 stsp goto done;
8175 e600f124 2021-03-21 stsp } else {
8176 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8177 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8178 e600f124 2021-03-21 stsp "rebase", cwd);
8179 e600f124 2021-03-21 stsp goto done;
8180 e600f124 2021-03-21 stsp }
8181 fa51e947 2020-03-27 stsp }
8182 818c7501 2019-07-11 stsp
8183 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8184 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8185 818c7501 2019-07-11 stsp if (error != NULL)
8186 818c7501 2019-07-11 stsp goto done;
8187 818c7501 2019-07-11 stsp
8188 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8189 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8190 7ef62c4e 2020-02-24 stsp if (error)
8191 7ef62c4e 2020-02-24 stsp goto done;
8192 7ef62c4e 2020-02-24 stsp
8193 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8194 643b85bc 2021-07-16 stsp error = process_backup_refs(
8195 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8196 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8197 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8198 e600f124 2021-03-21 stsp }
8199 e600f124 2021-03-21 stsp
8200 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8201 7ef62c4e 2020-02-24 stsp worktree);
8202 818c7501 2019-07-11 stsp if (error)
8203 7ef62c4e 2020-02-24 stsp goto done;
8204 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8205 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8206 818c7501 2019-07-11 stsp goto done;
8207 7ef62c4e 2020-02-24 stsp }
8208 818c7501 2019-07-11 stsp
8209 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8210 818c7501 2019-07-11 stsp if (error)
8211 818c7501 2019-07-11 stsp goto done;
8212 818c7501 2019-07-11 stsp
8213 f6794adc 2019-07-23 stsp if (abort_rebase) {
8214 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8215 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8216 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8217 f6794adc 2019-07-23 stsp goto done;
8218 f6794adc 2019-07-23 stsp }
8219 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8220 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8221 3e3a69f1 2019-07-25 stsp worktree, repo);
8222 818c7501 2019-07-11 stsp if (error)
8223 818c7501 2019-07-11 stsp goto done;
8224 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8225 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8226 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8227 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8228 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8229 818c7501 2019-07-11 stsp if (error)
8230 818c7501 2019-07-11 stsp goto done;
8231 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8232 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8233 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8234 818c7501 2019-07-11 stsp }
8235 818c7501 2019-07-11 stsp
8236 818c7501 2019-07-11 stsp if (continue_rebase) {
8237 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8238 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8239 f6794adc 2019-07-23 stsp goto done;
8240 f6794adc 2019-07-23 stsp }
8241 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8242 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8243 3e3a69f1 2019-07-25 stsp worktree, repo);
8244 818c7501 2019-07-11 stsp if (error)
8245 818c7501 2019-07-11 stsp goto done;
8246 818c7501 2019-07-11 stsp
8247 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8248 01757395 2019-07-12 stsp resume_commit_id, repo);
8249 818c7501 2019-07-11 stsp if (error)
8250 818c7501 2019-07-11 stsp goto done;
8251 818c7501 2019-07-11 stsp
8252 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8253 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8254 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8255 818c7501 2019-07-11 stsp goto done;
8256 818c7501 2019-07-11 stsp }
8257 818c7501 2019-07-11 stsp } else {
8258 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8259 818c7501 2019-07-11 stsp if (error != NULL)
8260 818c7501 2019-07-11 stsp goto done;
8261 ff0d2220 2019-07-11 stsp }
8262 818c7501 2019-07-11 stsp
8263 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8264 ff0d2220 2019-07-11 stsp if (error)
8265 ff0d2220 2019-07-11 stsp goto done;
8266 ff0d2220 2019-07-11 stsp
8267 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8268 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8269 a51a74b3 2019-07-27 stsp
8270 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8271 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8272 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8273 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8274 818c7501 2019-07-11 stsp if (error)
8275 818c7501 2019-07-11 stsp goto done;
8276 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8277 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8278 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8279 818c7501 2019-07-11 stsp "with work tree's branch");
8280 818c7501 2019-07-11 stsp goto done;
8281 818c7501 2019-07-11 stsp }
8282 818c7501 2019-07-11 stsp
8283 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8284 a51a74b3 2019-07-27 stsp if (error) {
8285 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8286 a51a74b3 2019-07-27 stsp goto done;
8287 a51a74b3 2019-07-27 stsp error = NULL;
8288 a51a74b3 2019-07-27 stsp } else {
8289 df3ed485 2021-01-31 stsp static char msg[128];
8290 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8291 df3ed485 2021-01-31 stsp "%s is already based on %s",
8292 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8293 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8294 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8295 a51a74b3 2019-07-27 stsp goto done;
8296 a51a74b3 2019-07-27 stsp }
8297 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8298 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8299 818c7501 2019-07-11 stsp if (error)
8300 818c7501 2019-07-11 stsp goto done;
8301 818c7501 2019-07-11 stsp }
8302 818c7501 2019-07-11 stsp
8303 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8304 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8305 818c7501 2019-07-11 stsp if (error)
8306 818c7501 2019-07-11 stsp goto done;
8307 818c7501 2019-07-11 stsp
8308 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8309 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
8310 fc66b545 2019-08-12 stsp if (pid == NULL) {
8311 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8312 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8313 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8314 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8315 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8316 fc66b545 2019-08-12 stsp if (error)
8317 fc66b545 2019-08-12 stsp goto done;
8318 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8319 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8320 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8321 9627c110 2020-04-18 stsp
8322 fc66b545 2019-08-12 stsp }
8323 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8324 fc66b545 2019-08-12 stsp goto done;
8325 fc66b545 2019-08-12 stsp }
8326 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8327 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8328 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8329 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8330 818c7501 2019-07-11 stsp commit = NULL;
8331 818c7501 2019-07-11 stsp if (error)
8332 818c7501 2019-07-11 stsp goto done;
8333 64c6d990 2019-07-11 stsp
8334 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
8335 38b0338b 2019-11-29 stsp if (continue_rebase) {
8336 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8337 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8338 e600f124 2021-03-21 stsp create_backup);
8339 38b0338b 2019-11-29 stsp goto done;
8340 38b0338b 2019-11-29 stsp } else {
8341 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8342 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8343 38b0338b 2019-11-29 stsp char *id_str;
8344 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8345 38b0338b 2019-11-29 stsp new_base_branch);
8346 38b0338b 2019-11-29 stsp if (error)
8347 38b0338b 2019-11-29 stsp goto done;
8348 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8349 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8350 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8351 38b0338b 2019-11-29 stsp free(id_str);
8352 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8353 38b0338b 2019-11-29 stsp new_head_commit_id);
8354 38b0338b 2019-11-29 stsp if (error)
8355 38b0338b 2019-11-29 stsp goto done;
8356 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8357 e600f124 2021-03-21 stsp create_backup = 0;
8358 38b0338b 2019-11-29 stsp }
8359 818c7501 2019-07-11 stsp }
8360 818c7501 2019-07-11 stsp
8361 818c7501 2019-07-11 stsp pid = NULL;
8362 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
8363 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8364 9627c110 2020-04-18 stsp
8365 818c7501 2019-07-11 stsp commit_id = qid->id;
8366 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
8367 818c7501 2019-07-11 stsp pid = qid;
8368 818c7501 2019-07-11 stsp
8369 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8370 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
8371 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
8372 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8373 818c7501 2019-07-11 stsp if (error)
8374 818c7501 2019-07-11 stsp goto done;
8375 9627c110 2020-04-18 stsp
8376 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8377 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8378 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8379 818c7501 2019-07-11 stsp
8380 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8381 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
8382 a0ea4fc0 2020-02-28 stsp if (error)
8383 a0ea4fc0 2020-02-28 stsp goto done;
8384 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8385 818c7501 2019-07-11 stsp break;
8386 01757395 2019-07-12 stsp }
8387 818c7501 2019-07-11 stsp
8388 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
8389 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
8390 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8391 818c7501 2019-07-11 stsp if (error)
8392 818c7501 2019-07-11 stsp goto done;
8393 818c7501 2019-07-11 stsp }
8394 818c7501 2019-07-11 stsp
8395 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8396 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
8397 818c7501 2019-07-11 stsp if (error)
8398 818c7501 2019-07-11 stsp goto done;
8399 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8400 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
8401 818c7501 2019-07-11 stsp } else
8402 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
8403 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
8404 818c7501 2019-07-11 stsp done:
8405 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
8406 818c7501 2019-07-11 stsp free(branch_head_commit_id);
8407 818c7501 2019-07-11 stsp free(resume_commit_id);
8408 818c7501 2019-07-11 stsp free(yca_id);
8409 818c7501 2019-07-11 stsp if (commit)
8410 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8411 818c7501 2019-07-11 stsp if (branch)
8412 818c7501 2019-07-11 stsp got_ref_close(branch);
8413 818c7501 2019-07-11 stsp if (new_base_branch)
8414 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
8415 818c7501 2019-07-11 stsp if (tmp_branch)
8416 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
8417 5ef14e63 2019-06-02 stsp if (worktree)
8418 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
8419 1d0f4054 2021-06-17 stsp if (repo) {
8420 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8421 1d0f4054 2021-06-17 stsp if (error == NULL)
8422 1d0f4054 2021-06-17 stsp error = close_err;
8423 1d0f4054 2021-06-17 stsp }
8424 5ef14e63 2019-06-02 stsp return error;
8425 0ebf8283 2019-07-24 stsp }
8426 0ebf8283 2019-07-24 stsp
8427 0ebf8283 2019-07-24 stsp __dead static void
8428 0ebf8283 2019-07-24 stsp usage_histedit(void)
8429 0ebf8283 2019-07-24 stsp {
8430 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8431 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
8432 643b85bc 2021-07-16 stsp getprogname());
8433 0ebf8283 2019-07-24 stsp exit(1);
8434 0ebf8283 2019-07-24 stsp }
8435 0ebf8283 2019-07-24 stsp
8436 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
8437 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
8438 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
8439 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
8440 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
8441 0ebf8283 2019-07-24 stsp
8442 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
8443 0ebf8283 2019-07-24 stsp unsigned char code;
8444 0ebf8283 2019-07-24 stsp const char *name;
8445 0ebf8283 2019-07-24 stsp const char *desc;
8446 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
8447 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
8448 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
8449 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
8450 82997472 2020-01-29 stsp "be used" },
8451 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
8452 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
8453 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
8454 0ebf8283 2019-07-24 stsp };
8455 0ebf8283 2019-07-24 stsp
8456 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
8457 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
8458 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
8459 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8460 0ebf8283 2019-07-24 stsp char *logmsg;
8461 0ebf8283 2019-07-24 stsp };
8462 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
8463 0ebf8283 2019-07-24 stsp
8464 0ebf8283 2019-07-24 stsp static const struct got_error *
8465 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8466 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8467 0ebf8283 2019-07-24 stsp {
8468 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8469 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
8470 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8471 8138f3e1 2019-08-11 stsp int n;
8472 0ebf8283 2019-07-24 stsp
8473 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8474 0ebf8283 2019-07-24 stsp if (err)
8475 0ebf8283 2019-07-24 stsp goto done;
8476 0ebf8283 2019-07-24 stsp
8477 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
8478 0ebf8283 2019-07-24 stsp if (err)
8479 0ebf8283 2019-07-24 stsp goto done;
8480 0ebf8283 2019-07-24 stsp
8481 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
8482 0ebf8283 2019-07-24 stsp if (err)
8483 0ebf8283 2019-07-24 stsp goto done;
8484 0ebf8283 2019-07-24 stsp
8485 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8486 0ebf8283 2019-07-24 stsp if (n < 0)
8487 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8488 0ebf8283 2019-07-24 stsp done:
8489 0ebf8283 2019-07-24 stsp if (commit)
8490 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8491 0ebf8283 2019-07-24 stsp free(id_str);
8492 0ebf8283 2019-07-24 stsp free(logmsg);
8493 0ebf8283 2019-07-24 stsp return err;
8494 0ebf8283 2019-07-24 stsp }
8495 0ebf8283 2019-07-24 stsp
8496 0ebf8283 2019-07-24 stsp static const struct got_error *
8497 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
8498 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8499 0ebf8283 2019-07-24 stsp {
8500 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8501 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
8502 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
8503 0ebf8283 2019-07-24 stsp
8504 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
8505 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8506 0ebf8283 2019-07-24 stsp
8507 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
8508 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
8509 dbdddfee 2021-06-23 naddy if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
8510 466785b9 2020-12-10 jrick histedit_cmd = "fold";
8511 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8512 0ebf8283 2019-07-24 stsp if (err)
8513 0ebf8283 2019-07-24 stsp break;
8514 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8515 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8516 083957f4 2020-02-24 stsp if (n < 0) {
8517 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
8518 083957f4 2020-02-24 stsp break;
8519 083957f4 2020-02-24 stsp }
8520 083957f4 2020-02-24 stsp }
8521 0ebf8283 2019-07-24 stsp }
8522 0ebf8283 2019-07-24 stsp
8523 0ebf8283 2019-07-24 stsp return err;
8524 0ebf8283 2019-07-24 stsp }
8525 0ebf8283 2019-07-24 stsp
8526 0ebf8283 2019-07-24 stsp static const struct got_error *
8527 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
8528 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
8529 0ebf8283 2019-07-24 stsp {
8530 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8531 6059809a 2020-12-17 stsp size_t i;
8532 6059809a 2020-12-17 stsp int n;
8533 514f2ffe 2020-01-29 stsp char *id_str;
8534 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
8535 514f2ffe 2020-01-29 stsp
8536 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
8537 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
8538 514f2ffe 2020-01-29 stsp if (err)
8539 514f2ffe 2020-01-29 stsp return err;
8540 514f2ffe 2020-01-29 stsp
8541 514f2ffe 2020-01-29 stsp n = fprintf(f,
8542 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
8543 514f2ffe 2020-01-29 stsp "# commit %s\n"
8544 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
8545 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
8546 514f2ffe 2020-01-29 stsp if (n < 0) {
8547 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8548 514f2ffe 2020-01-29 stsp goto done;
8549 514f2ffe 2020-01-29 stsp }
8550 0ebf8283 2019-07-24 stsp
8551 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
8552 514f2ffe 2020-01-29 stsp if (n < 0) {
8553 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8554 514f2ffe 2020-01-29 stsp goto done;
8555 514f2ffe 2020-01-29 stsp }
8556 0ebf8283 2019-07-24 stsp
8557 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8558 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8559 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8560 0ebf8283 2019-07-24 stsp cmd->desc);
8561 0ebf8283 2019-07-24 stsp if (n < 0) {
8562 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8563 0ebf8283 2019-07-24 stsp break;
8564 0ebf8283 2019-07-24 stsp }
8565 0ebf8283 2019-07-24 stsp }
8566 514f2ffe 2020-01-29 stsp done:
8567 514f2ffe 2020-01-29 stsp free(id_str);
8568 0ebf8283 2019-07-24 stsp return err;
8569 0ebf8283 2019-07-24 stsp }
8570 0ebf8283 2019-07-24 stsp
8571 0ebf8283 2019-07-24 stsp static const struct got_error *
8572 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
8573 0ebf8283 2019-07-24 stsp {
8574 0ebf8283 2019-07-24 stsp static char msg[42];
8575 0ebf8283 2019-07-24 stsp int ret;
8576 0ebf8283 2019-07-24 stsp
8577 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8578 0ebf8283 2019-07-24 stsp lineno);
8579 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
8580 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8581 0ebf8283 2019-07-24 stsp
8582 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8583 0ebf8283 2019-07-24 stsp }
8584 0ebf8283 2019-07-24 stsp
8585 0ebf8283 2019-07-24 stsp static const struct got_error *
8586 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8587 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
8588 0ebf8283 2019-07-24 stsp {
8589 0ebf8283 2019-07-24 stsp const struct got_error *err;
8590 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
8591 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
8592 0ebf8283 2019-07-24 stsp
8593 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8594 0ebf8283 2019-07-24 stsp if (err)
8595 0ebf8283 2019-07-24 stsp return err;
8596 0ebf8283 2019-07-24 stsp
8597 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8598 0ebf8283 2019-07-24 stsp if (err)
8599 0ebf8283 2019-07-24 stsp goto done;
8600 0ebf8283 2019-07-24 stsp
8601 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8602 5943eee2 2019-08-13 stsp if (err)
8603 5943eee2 2019-08-13 stsp goto done;
8604 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8605 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8606 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
8607 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8608 0ebf8283 2019-07-24 stsp }
8609 0ebf8283 2019-07-24 stsp done:
8610 0ebf8283 2019-07-24 stsp if (folded_commit)
8611 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
8612 0ebf8283 2019-07-24 stsp free(id_str);
8613 5943eee2 2019-08-13 stsp free(folded_logmsg);
8614 0ebf8283 2019-07-24 stsp return err;
8615 0ebf8283 2019-07-24 stsp }
8616 0ebf8283 2019-07-24 stsp
8617 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
8618 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
8619 0ebf8283 2019-07-24 stsp {
8620 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
8621 0ebf8283 2019-07-24 stsp
8622 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
8623 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8624 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
8625 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8626 3f9de99f 2019-07-24 stsp folded = prev;
8627 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
8628 0ebf8283 2019-07-24 stsp }
8629 0ebf8283 2019-07-24 stsp
8630 0ebf8283 2019-07-24 stsp return folded;
8631 0ebf8283 2019-07-24 stsp }
8632 0ebf8283 2019-07-24 stsp
8633 0ebf8283 2019-07-24 stsp static const struct got_error *
8634 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8635 0ebf8283 2019-07-24 stsp struct got_repository *repo)
8636 0ebf8283 2019-07-24 stsp {
8637 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8638 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8639 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8640 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8641 1601cb9f 2020-09-11 naddy int logmsg_len;
8642 0ebf8283 2019-07-24 stsp int fd;
8643 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
8644 0ebf8283 2019-07-24 stsp
8645 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8646 0ebf8283 2019-07-24 stsp if (err)
8647 0ebf8283 2019-07-24 stsp return err;
8648 0ebf8283 2019-07-24 stsp
8649 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
8650 0ebf8283 2019-07-24 stsp if (folded) {
8651 0ebf8283 2019-07-24 stsp while (folded != hle) {
8652 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8653 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8654 3f9de99f 2019-07-24 stsp continue;
8655 3f9de99f 2019-07-24 stsp }
8656 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
8657 0ebf8283 2019-07-24 stsp logmsg, repo);
8658 0ebf8283 2019-07-24 stsp if (err)
8659 0ebf8283 2019-07-24 stsp goto done;
8660 0ebf8283 2019-07-24 stsp free(logmsg);
8661 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8662 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8663 0ebf8283 2019-07-24 stsp }
8664 0ebf8283 2019-07-24 stsp }
8665 0ebf8283 2019-07-24 stsp
8666 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8667 0ebf8283 2019-07-24 stsp if (err)
8668 0ebf8283 2019-07-24 stsp goto done;
8669 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8670 5943eee2 2019-08-13 stsp if (err)
8671 5943eee2 2019-08-13 stsp goto done;
8672 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8673 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8674 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8675 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8676 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8677 0ebf8283 2019-07-24 stsp goto done;
8678 0ebf8283 2019-07-24 stsp }
8679 0ebf8283 2019-07-24 stsp free(logmsg);
8680 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8681 0ebf8283 2019-07-24 stsp
8682 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8683 0ebf8283 2019-07-24 stsp if (err)
8684 0ebf8283 2019-07-24 stsp goto done;
8685 0ebf8283 2019-07-24 stsp
8686 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8687 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8688 0ebf8283 2019-07-24 stsp if (err)
8689 0ebf8283 2019-07-24 stsp goto done;
8690 0ebf8283 2019-07-24 stsp
8691 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8692 0ebf8283 2019-07-24 stsp close(fd);
8693 0ebf8283 2019-07-24 stsp
8694 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8695 0ebf8283 2019-07-24 stsp if (err)
8696 0ebf8283 2019-07-24 stsp goto done;
8697 0ebf8283 2019-07-24 stsp
8698 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8699 0d5bb276 2020-12-15 stsp logmsg_len, 0);
8700 0ebf8283 2019-07-24 stsp if (err) {
8701 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8702 0ebf8283 2019-07-24 stsp goto done;
8703 71392a05 2020-12-13 stsp err = NULL;
8704 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
8705 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
8706 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
8707 0ebf8283 2019-07-24 stsp }
8708 0ebf8283 2019-07-24 stsp done:
8709 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8710 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8711 0ebf8283 2019-07-24 stsp free(logmsg_path);
8712 0ebf8283 2019-07-24 stsp free(logmsg);
8713 5943eee2 2019-08-13 stsp free(orig_logmsg);
8714 0ebf8283 2019-07-24 stsp free(editor);
8715 0ebf8283 2019-07-24 stsp if (commit)
8716 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8717 0ebf8283 2019-07-24 stsp return err;
8718 5ef14e63 2019-06-02 stsp }
8719 0ebf8283 2019-07-24 stsp
8720 0ebf8283 2019-07-24 stsp static const struct got_error *
8721 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8722 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8723 0ebf8283 2019-07-24 stsp {
8724 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8725 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8726 6059809a 2020-12-17 stsp size_t i, size;
8727 0ebf8283 2019-07-24 stsp ssize_t len;
8728 6059809a 2020-12-17 stsp int lineno = 0;
8729 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8730 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8731 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8732 0ebf8283 2019-07-24 stsp
8733 0ebf8283 2019-07-24 stsp for (;;) {
8734 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8735 0ebf8283 2019-07-24 stsp if (len == -1) {
8736 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8737 0ebf8283 2019-07-24 stsp if (feof(f))
8738 0ebf8283 2019-07-24 stsp break;
8739 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8740 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8741 0ebf8283 2019-07-24 stsp break;
8742 0ebf8283 2019-07-24 stsp }
8743 0ebf8283 2019-07-24 stsp lineno++;
8744 0ebf8283 2019-07-24 stsp p = line;
8745 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8746 0ebf8283 2019-07-24 stsp p++;
8747 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8748 0ebf8283 2019-07-24 stsp free(line);
8749 0ebf8283 2019-07-24 stsp line = NULL;
8750 0ebf8283 2019-07-24 stsp continue;
8751 0ebf8283 2019-07-24 stsp }
8752 0ebf8283 2019-07-24 stsp cmd = NULL;
8753 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8754 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8755 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8756 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8757 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8758 0ebf8283 2019-07-24 stsp break;
8759 0ebf8283 2019-07-24 stsp }
8760 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8761 0ebf8283 2019-07-24 stsp p++;
8762 0ebf8283 2019-07-24 stsp break;
8763 0ebf8283 2019-07-24 stsp }
8764 0ebf8283 2019-07-24 stsp }
8765 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8766 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8767 0ebf8283 2019-07-24 stsp break;
8768 0ebf8283 2019-07-24 stsp }
8769 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8770 0ebf8283 2019-07-24 stsp p++;
8771 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8772 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8773 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8774 0ebf8283 2019-07-24 stsp break;
8775 0ebf8283 2019-07-24 stsp }
8776 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8777 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8778 0ebf8283 2019-07-24 stsp if (err)
8779 0ebf8283 2019-07-24 stsp break;
8780 0ebf8283 2019-07-24 stsp } else {
8781 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8782 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8783 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8784 0ebf8283 2019-07-24 stsp break;
8785 0ebf8283 2019-07-24 stsp }
8786 0ebf8283 2019-07-24 stsp }
8787 0ebf8283 2019-07-24 stsp free(line);
8788 0ebf8283 2019-07-24 stsp line = NULL;
8789 0ebf8283 2019-07-24 stsp continue;
8790 0ebf8283 2019-07-24 stsp } else {
8791 0ebf8283 2019-07-24 stsp end = p;
8792 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8793 0ebf8283 2019-07-24 stsp end++;
8794 0ebf8283 2019-07-24 stsp *end = '\0';
8795 0ebf8283 2019-07-24 stsp
8796 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8797 0ebf8283 2019-07-24 stsp if (err) {
8798 0ebf8283 2019-07-24 stsp /* override error code */
8799 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8800 0ebf8283 2019-07-24 stsp break;
8801 0ebf8283 2019-07-24 stsp }
8802 0ebf8283 2019-07-24 stsp }
8803 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8804 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8805 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8806 0ebf8283 2019-07-24 stsp break;
8807 0ebf8283 2019-07-24 stsp }
8808 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8809 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8810 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8811 0ebf8283 2019-07-24 stsp commit_id = NULL;
8812 0ebf8283 2019-07-24 stsp free(line);
8813 0ebf8283 2019-07-24 stsp line = NULL;
8814 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8815 0ebf8283 2019-07-24 stsp }
8816 0ebf8283 2019-07-24 stsp
8817 0ebf8283 2019-07-24 stsp free(line);
8818 0ebf8283 2019-07-24 stsp free(commit_id);
8819 0ebf8283 2019-07-24 stsp return err;
8820 0ebf8283 2019-07-24 stsp }
8821 0ebf8283 2019-07-24 stsp
8822 0ebf8283 2019-07-24 stsp static const struct got_error *
8823 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8824 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8825 bfce7f83 2019-07-27 stsp {
8826 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8827 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8828 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8829 5b87815e 2020-03-05 stsp static char msg[92];
8830 bfce7f83 2019-07-27 stsp char *id_str;
8831 bfce7f83 2019-07-27 stsp
8832 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8833 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8834 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8835 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
8836 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8837 5b87815e 2020-03-05 stsp
8838 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8839 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8840 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8841 5b87815e 2020-03-05 stsp if (hle == hle2)
8842 5b87815e 2020-03-05 stsp continue;
8843 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8844 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8845 5b87815e 2020-03-05 stsp continue;
8846 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8847 5b87815e 2020-03-05 stsp if (err)
8848 5b87815e 2020-03-05 stsp return err;
8849 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8850 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8851 5b87815e 2020-03-05 stsp free(id_str);
8852 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8853 5b87815e 2020-03-05 stsp }
8854 5b87815e 2020-03-05 stsp }
8855 bfce7f83 2019-07-27 stsp
8856 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
8857 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8858 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8859 bfce7f83 2019-07-27 stsp break;
8860 bfce7f83 2019-07-27 stsp }
8861 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8862 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8863 bfce7f83 2019-07-27 stsp if (err)
8864 bfce7f83 2019-07-27 stsp return err;
8865 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8866 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8867 bfce7f83 2019-07-27 stsp free(id_str);
8868 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8869 bfce7f83 2019-07-27 stsp }
8870 bfce7f83 2019-07-27 stsp }
8871 bfce7f83 2019-07-27 stsp
8872 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8873 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8874 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8875 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8876 bfce7f83 2019-07-27 stsp
8877 bfce7f83 2019-07-27 stsp return NULL;
8878 bfce7f83 2019-07-27 stsp }
8879 bfce7f83 2019-07-27 stsp
8880 bfce7f83 2019-07-27 stsp static const struct got_error *
8881 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8882 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8883 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8884 0ebf8283 2019-07-24 stsp {
8885 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8886 0ebf8283 2019-07-24 stsp char *editor;
8887 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8888 0ebf8283 2019-07-24 stsp
8889 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8890 0ebf8283 2019-07-24 stsp if (err)
8891 0ebf8283 2019-07-24 stsp return err;
8892 0ebf8283 2019-07-24 stsp
8893 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8894 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8895 0ebf8283 2019-07-24 stsp goto done;
8896 0ebf8283 2019-07-24 stsp }
8897 0ebf8283 2019-07-24 stsp
8898 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8899 0ebf8283 2019-07-24 stsp if (f == NULL) {
8900 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8901 0ebf8283 2019-07-24 stsp goto done;
8902 0ebf8283 2019-07-24 stsp }
8903 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8904 bfce7f83 2019-07-27 stsp if (err)
8905 bfce7f83 2019-07-27 stsp goto done;
8906 bfce7f83 2019-07-27 stsp
8907 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8908 0ebf8283 2019-07-24 stsp done:
8909 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8910 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8911 0ebf8283 2019-07-24 stsp free(editor);
8912 0ebf8283 2019-07-24 stsp return err;
8913 0ebf8283 2019-07-24 stsp }
8914 0ebf8283 2019-07-24 stsp
8915 0ebf8283 2019-07-24 stsp static const struct got_error *
8916 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8917 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8918 514f2ffe 2020-01-29 stsp struct got_repository *);
8919 0ebf8283 2019-07-24 stsp
8920 0ebf8283 2019-07-24 stsp static const struct got_error *
8921 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8922 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8923 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
8924 0ebf8283 2019-07-24 stsp {
8925 0ebf8283 2019-07-24 stsp const struct got_error *err;
8926 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8927 0ebf8283 2019-07-24 stsp char *path = NULL;
8928 0ebf8283 2019-07-24 stsp
8929 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8930 0ebf8283 2019-07-24 stsp if (err)
8931 0ebf8283 2019-07-24 stsp return err;
8932 0ebf8283 2019-07-24 stsp
8933 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8934 0ebf8283 2019-07-24 stsp if (err)
8935 0ebf8283 2019-07-24 stsp goto done;
8936 0ebf8283 2019-07-24 stsp
8937 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8938 466785b9 2020-12-10 jrick fold_only, repo);
8939 0ebf8283 2019-07-24 stsp if (err)
8940 0ebf8283 2019-07-24 stsp goto done;
8941 0ebf8283 2019-07-24 stsp
8942 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
8943 083957f4 2020-02-24 stsp rewind(f);
8944 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8945 083957f4 2020-02-24 stsp } else {
8946 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
8947 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8948 0ebf8283 2019-07-24 stsp goto done;
8949 083957f4 2020-02-24 stsp }
8950 083957f4 2020-02-24 stsp f = NULL;
8951 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8952 083957f4 2020-02-24 stsp if (err) {
8953 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8954 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8955 083957f4 2020-02-24 stsp goto done;
8956 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8957 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8958 083957f4 2020-02-24 stsp }
8959 0ebf8283 2019-07-24 stsp }
8960 0ebf8283 2019-07-24 stsp done:
8961 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8962 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8963 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8964 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8965 0ebf8283 2019-07-24 stsp free(path);
8966 0ebf8283 2019-07-24 stsp return err;
8967 0ebf8283 2019-07-24 stsp }
8968 0ebf8283 2019-07-24 stsp
8969 0ebf8283 2019-07-24 stsp static const struct got_error *
8970 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8971 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8972 0ebf8283 2019-07-24 stsp {
8973 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8974 0ebf8283 2019-07-24 stsp char *path = NULL;
8975 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8976 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8977 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8978 0ebf8283 2019-07-24 stsp
8979 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8980 0ebf8283 2019-07-24 stsp if (err)
8981 0ebf8283 2019-07-24 stsp return err;
8982 0ebf8283 2019-07-24 stsp
8983 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8984 0ebf8283 2019-07-24 stsp if (f == NULL) {
8985 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8986 0ebf8283 2019-07-24 stsp goto done;
8987 0ebf8283 2019-07-24 stsp }
8988 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8989 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8990 0ebf8283 2019-07-24 stsp repo);
8991 0ebf8283 2019-07-24 stsp if (err)
8992 0ebf8283 2019-07-24 stsp break;
8993 0ebf8283 2019-07-24 stsp
8994 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8995 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8996 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8997 0ebf8283 2019-07-24 stsp if (n < 0) {
8998 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8999 0ebf8283 2019-07-24 stsp break;
9000 0ebf8283 2019-07-24 stsp }
9001 0ebf8283 2019-07-24 stsp }
9002 0ebf8283 2019-07-24 stsp }
9003 0ebf8283 2019-07-24 stsp done:
9004 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9005 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9006 0ebf8283 2019-07-24 stsp free(path);
9007 0ebf8283 2019-07-24 stsp if (commit)
9008 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9009 0ebf8283 2019-07-24 stsp return err;
9010 0ebf8283 2019-07-24 stsp }
9011 0ebf8283 2019-07-24 stsp
9012 bfce7f83 2019-07-27 stsp void
9013 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9014 bfce7f83 2019-07-27 stsp {
9015 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9016 bfce7f83 2019-07-27 stsp
9017 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9018 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9019 bfce7f83 2019-07-27 stsp free(hle);
9020 bfce7f83 2019-07-27 stsp }
9021 bfce7f83 2019-07-27 stsp }
9022 bfce7f83 2019-07-27 stsp
9023 0ebf8283 2019-07-24 stsp static const struct got_error *
9024 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9025 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9026 0ebf8283 2019-07-24 stsp {
9027 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9028 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9029 0ebf8283 2019-07-24 stsp
9030 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9031 0ebf8283 2019-07-24 stsp if (f == NULL) {
9032 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9033 0ebf8283 2019-07-24 stsp goto done;
9034 0ebf8283 2019-07-24 stsp }
9035 0ebf8283 2019-07-24 stsp
9036 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9037 0ebf8283 2019-07-24 stsp done:
9038 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9039 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9040 0ebf8283 2019-07-24 stsp return err;
9041 0ebf8283 2019-07-24 stsp }
9042 0ebf8283 2019-07-24 stsp
9043 0ebf8283 2019-07-24 stsp static const struct got_error *
9044 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9045 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9046 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9047 0ebf8283 2019-07-24 stsp {
9048 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9049 0ebf8283 2019-07-24 stsp int resp = ' ';
9050 0ebf8283 2019-07-24 stsp
9051 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9052 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9053 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9054 0ebf8283 2019-07-24 stsp resp = getchar();
9055 a61a4414 2019-08-07 stsp if (resp == '\n')
9056 a61a4414 2019-08-07 stsp resp = getchar();
9057 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9058 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9059 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9060 bfce7f83 2019-07-27 stsp repo);
9061 426ebf2e 2019-07-25 stsp if (err) {
9062 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9063 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9064 426ebf2e 2019-07-25 stsp break;
9065 bfce7f83 2019-07-27 stsp prev_err = err;
9066 426ebf2e 2019-07-25 stsp resp = ' ';
9067 426ebf2e 2019-07-25 stsp continue;
9068 426ebf2e 2019-07-25 stsp }
9069 bfce7f83 2019-07-27 stsp break;
9070 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9071 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9072 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9073 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
9074 426ebf2e 2019-07-25 stsp if (err) {
9075 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9076 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9077 426ebf2e 2019-07-25 stsp break;
9078 bfce7f83 2019-07-27 stsp prev_err = err;
9079 426ebf2e 2019-07-25 stsp resp = ' ';
9080 426ebf2e 2019-07-25 stsp continue;
9081 426ebf2e 2019-07-25 stsp }
9082 bfce7f83 2019-07-27 stsp break;
9083 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9084 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9085 0ebf8283 2019-07-24 stsp break;
9086 426ebf2e 2019-07-25 stsp } else
9087 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9088 0ebf8283 2019-07-24 stsp }
9089 0ebf8283 2019-07-24 stsp
9090 0ebf8283 2019-07-24 stsp return err;
9091 0ebf8283 2019-07-24 stsp }
9092 0ebf8283 2019-07-24 stsp
9093 0ebf8283 2019-07-24 stsp static const struct got_error *
9094 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9095 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9096 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9097 0ebf8283 2019-07-24 stsp {
9098 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9099 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9100 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9101 3e3a69f1 2019-07-25 stsp branch, repo);
9102 0ebf8283 2019-07-24 stsp }
9103 0ebf8283 2019-07-24 stsp
9104 0ebf8283 2019-07-24 stsp static const struct got_error *
9105 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9106 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9107 0ebf8283 2019-07-24 stsp {
9108 0ebf8283 2019-07-24 stsp const struct got_error *err;
9109 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9110 0ebf8283 2019-07-24 stsp
9111 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9112 0ebf8283 2019-07-24 stsp if (err)
9113 0ebf8283 2019-07-24 stsp goto done;
9114 0ebf8283 2019-07-24 stsp
9115 0ebf8283 2019-07-24 stsp if (new_id) {
9116 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9117 0ebf8283 2019-07-24 stsp if (err)
9118 0ebf8283 2019-07-24 stsp goto done;
9119 0ebf8283 2019-07-24 stsp }
9120 0ebf8283 2019-07-24 stsp
9121 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9122 0ebf8283 2019-07-24 stsp if (new_id_str)
9123 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9124 0ebf8283 2019-07-24 stsp
9125 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9126 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9127 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9128 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9129 0ebf8283 2019-07-24 stsp goto done;
9130 0ebf8283 2019-07-24 stsp }
9131 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9132 0ebf8283 2019-07-24 stsp } else {
9133 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9134 0ebf8283 2019-07-24 stsp if (err)
9135 0ebf8283 2019-07-24 stsp goto done;
9136 0ebf8283 2019-07-24 stsp }
9137 0ebf8283 2019-07-24 stsp
9138 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9139 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9140 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9141 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9142 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9143 0ebf8283 2019-07-24 stsp break;
9144 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9145 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9146 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9147 0ebf8283 2019-07-24 stsp logmsg);
9148 0ebf8283 2019-07-24 stsp break;
9149 0ebf8283 2019-07-24 stsp default:
9150 0ebf8283 2019-07-24 stsp break;
9151 0ebf8283 2019-07-24 stsp }
9152 0ebf8283 2019-07-24 stsp done:
9153 0ebf8283 2019-07-24 stsp free(old_id_str);
9154 0ebf8283 2019-07-24 stsp free(new_id_str);
9155 0ebf8283 2019-07-24 stsp return err;
9156 0ebf8283 2019-07-24 stsp }
9157 0ebf8283 2019-07-24 stsp
9158 0ebf8283 2019-07-24 stsp static const struct got_error *
9159 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9160 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9161 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9162 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9163 0ebf8283 2019-07-24 stsp {
9164 0ebf8283 2019-07-24 stsp const struct got_error *err;
9165 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9166 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9167 0ebf8283 2019-07-24 stsp
9168 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9169 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9170 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9171 0ebf8283 2019-07-24 stsp if (err)
9172 0ebf8283 2019-07-24 stsp return err;
9173 0ebf8283 2019-07-24 stsp }
9174 0ebf8283 2019-07-24 stsp
9175 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9176 0ebf8283 2019-07-24 stsp if (err)
9177 0ebf8283 2019-07-24 stsp return err;
9178 0ebf8283 2019-07-24 stsp
9179 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9180 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9181 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9182 0ebf8283 2019-07-24 stsp if (err) {
9183 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9184 0ebf8283 2019-07-24 stsp goto done;
9185 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9186 0ebf8283 2019-07-24 stsp } else {
9187 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9188 0ebf8283 2019-07-24 stsp free(new_commit_id);
9189 0ebf8283 2019-07-24 stsp }
9190 0ebf8283 2019-07-24 stsp done:
9191 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9192 0ebf8283 2019-07-24 stsp return err;
9193 0ebf8283 2019-07-24 stsp }
9194 0ebf8283 2019-07-24 stsp
9195 0ebf8283 2019-07-24 stsp static const struct got_error *
9196 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9197 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9198 0ebf8283 2019-07-24 stsp {
9199 0ebf8283 2019-07-24 stsp const struct got_error *error;
9200 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9201 0ebf8283 2019-07-24 stsp
9202 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9203 0ebf8283 2019-07-24 stsp repo);
9204 0ebf8283 2019-07-24 stsp if (error)
9205 0ebf8283 2019-07-24 stsp return error;
9206 0ebf8283 2019-07-24 stsp
9207 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9208 0ebf8283 2019-07-24 stsp if (error)
9209 0ebf8283 2019-07-24 stsp return error;
9210 0ebf8283 2019-07-24 stsp
9211 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9212 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9213 0ebf8283 2019-07-24 stsp return error;
9214 ab20a43a 2020-01-29 stsp }
9215 ab20a43a 2020-01-29 stsp
9216 ab20a43a 2020-01-29 stsp static const struct got_error *
9217 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9218 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9219 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9220 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9221 ab20a43a 2020-01-29 stsp {
9222 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9223 ab20a43a 2020-01-29 stsp
9224 ab20a43a 2020-01-29 stsp switch (status) {
9225 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9226 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9227 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9228 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9229 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9230 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9231 ab20a43a 2020-01-29 stsp default:
9232 ab20a43a 2020-01-29 stsp break;
9233 ab20a43a 2020-01-29 stsp }
9234 ab20a43a 2020-01-29 stsp
9235 ab20a43a 2020-01-29 stsp switch (staged_status) {
9236 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9237 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9238 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9239 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9240 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9241 ab20a43a 2020-01-29 stsp default:
9242 ab20a43a 2020-01-29 stsp break;
9243 ab20a43a 2020-01-29 stsp }
9244 ab20a43a 2020-01-29 stsp
9245 ab20a43a 2020-01-29 stsp return NULL;
9246 0ebf8283 2019-07-24 stsp }
9247 0ebf8283 2019-07-24 stsp
9248 0ebf8283 2019-07-24 stsp static const struct got_error *
9249 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9250 0ebf8283 2019-07-24 stsp {
9251 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9252 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9253 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9254 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9255 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9256 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9257 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9258 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9259 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9260 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9261 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9262 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9263 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9264 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9265 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9266 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
9267 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9268 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9269 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9270 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9271 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9272 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9273 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9274 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9275 0ebf8283 2019-07-24 stsp
9276 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9277 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9278 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9279 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9280 0ebf8283 2019-07-24 stsp
9281 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9282 0ebf8283 2019-07-24 stsp switch (ch) {
9283 0ebf8283 2019-07-24 stsp case 'a':
9284 0ebf8283 2019-07-24 stsp abort_edit = 1;
9285 0ebf8283 2019-07-24 stsp break;
9286 0ebf8283 2019-07-24 stsp case 'c':
9287 0ebf8283 2019-07-24 stsp continue_edit = 1;
9288 0ebf8283 2019-07-24 stsp break;
9289 466785b9 2020-12-10 jrick case 'f':
9290 466785b9 2020-12-10 jrick fold_only = 1;
9291 466785b9 2020-12-10 jrick break;
9292 0ebf8283 2019-07-24 stsp case 'F':
9293 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9294 0ebf8283 2019-07-24 stsp break;
9295 083957f4 2020-02-24 stsp case 'm':
9296 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9297 083957f4 2020-02-24 stsp break;
9298 e600f124 2021-03-21 stsp case 'l':
9299 e600f124 2021-03-21 stsp list_backups = 1;
9300 e600f124 2021-03-21 stsp break;
9301 643b85bc 2021-07-16 stsp case 'X':
9302 643b85bc 2021-07-16 stsp delete_backups = 1;
9303 643b85bc 2021-07-16 stsp break;
9304 0ebf8283 2019-07-24 stsp default:
9305 0ebf8283 2019-07-24 stsp usage_histedit();
9306 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9307 0ebf8283 2019-07-24 stsp }
9308 0ebf8283 2019-07-24 stsp }
9309 0ebf8283 2019-07-24 stsp
9310 0ebf8283 2019-07-24 stsp argc -= optind;
9311 0ebf8283 2019-07-24 stsp argv += optind;
9312 0ebf8283 2019-07-24 stsp
9313 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9314 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9315 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9316 0ebf8283 2019-07-24 stsp err(1, "pledge");
9317 0ebf8283 2019-07-24 stsp #endif
9318 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9319 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9320 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9321 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9322 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9323 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9324 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9325 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9326 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9327 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9328 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9329 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9330 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9331 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9332 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9333 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9334 e600f124 2021-03-21 stsp if (list_backups) {
9335 e600f124 2021-03-21 stsp if (abort_edit)
9336 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9337 e600f124 2021-03-21 stsp if (continue_edit)
9338 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9339 e600f124 2021-03-21 stsp if (edit_script_path)
9340 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9341 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9342 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9343 e600f124 2021-03-21 stsp if (fold_only)
9344 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9345 643b85bc 2021-07-16 stsp if (delete_backups)
9346 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9347 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9348 e600f124 2021-03-21 stsp usage_histedit();
9349 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9350 643b85bc 2021-07-16 stsp if (abort_edit)
9351 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9352 643b85bc 2021-07-16 stsp if (continue_edit)
9353 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9354 643b85bc 2021-07-16 stsp if (edit_script_path)
9355 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
9356 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
9357 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
9358 643b85bc 2021-07-16 stsp if (fold_only)
9359 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
9360 643b85bc 2021-07-16 stsp if (list_backups)
9361 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
9362 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9363 643b85bc 2021-07-16 stsp usage_histedit();
9364 e600f124 2021-03-21 stsp } else if (argc != 0)
9365 0ebf8283 2019-07-24 stsp usage_histedit();
9366 0ebf8283 2019-07-24 stsp
9367 0ebf8283 2019-07-24 stsp /*
9368 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
9369 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
9370 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
9371 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
9372 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
9373 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
9374 0ebf8283 2019-07-24 stsp */
9375 0ebf8283 2019-07-24 stsp
9376 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
9377 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
9378 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
9379 0ebf8283 2019-07-24 stsp goto done;
9380 0ebf8283 2019-07-24 stsp }
9381 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
9382 fa51e947 2020-03-27 stsp if (error) {
9383 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9384 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9385 e600f124 2021-03-21 stsp goto done;
9386 e600f124 2021-03-21 stsp } else {
9387 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9388 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9389 e600f124 2021-03-21 stsp "histedit", cwd);
9390 e600f124 2021-03-21 stsp goto done;
9391 e600f124 2021-03-21 stsp }
9392 e600f124 2021-03-21 stsp }
9393 e600f124 2021-03-21 stsp
9394 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9395 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9396 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
9397 e600f124 2021-03-21 stsp NULL);
9398 e600f124 2021-03-21 stsp if (error != NULL)
9399 e600f124 2021-03-21 stsp goto done;
9400 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9401 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9402 e600f124 2021-03-21 stsp if (error)
9403 e600f124 2021-03-21 stsp goto done;
9404 643b85bc 2021-07-16 stsp error = process_backup_refs(
9405 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9406 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9407 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9408 fa51e947 2020-03-27 stsp }
9409 0ebf8283 2019-07-24 stsp
9410 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9411 c9956ddf 2019-09-08 stsp NULL);
9412 0ebf8283 2019-07-24 stsp if (error != NULL)
9413 0ebf8283 2019-07-24 stsp goto done;
9414 0ebf8283 2019-07-24 stsp
9415 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9416 0ebf8283 2019-07-24 stsp if (error)
9417 0ebf8283 2019-07-24 stsp goto done;
9418 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
9419 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
9420 0ebf8283 2019-07-24 stsp goto done;
9421 0ebf8283 2019-07-24 stsp }
9422 0ebf8283 2019-07-24 stsp
9423 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
9424 0ebf8283 2019-07-24 stsp if (error)
9425 0ebf8283 2019-07-24 stsp goto done;
9426 0ebf8283 2019-07-24 stsp
9427 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
9428 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9429 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
9430 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
9431 083957f4 2020-02-24 stsp "before the -m option can be used");
9432 083957f4 2020-02-24 stsp goto done;
9433 083957f4 2020-02-24 stsp }
9434 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
9435 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9436 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
9437 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
9438 466785b9 2020-12-10 jrick "before the -f option can be used");
9439 466785b9 2020-12-10 jrick goto done;
9440 466785b9 2020-12-10 jrick }
9441 083957f4 2020-02-24 stsp
9442 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
9443 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
9444 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
9445 3e3a69f1 2019-07-25 stsp worktree, repo);
9446 0ebf8283 2019-07-24 stsp if (error)
9447 0ebf8283 2019-07-24 stsp goto done;
9448 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9449 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9450 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
9451 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
9452 0ebf8283 2019-07-24 stsp if (error)
9453 0ebf8283 2019-07-24 stsp goto done;
9454 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
9455 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9456 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9457 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
9458 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
9459 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
9460 0ebf8283 2019-07-24 stsp goto done;
9461 0ebf8283 2019-07-24 stsp }
9462 0ebf8283 2019-07-24 stsp
9463 0ebf8283 2019-07-24 stsp if (continue_edit) {
9464 0ebf8283 2019-07-24 stsp char *path;
9465 0ebf8283 2019-07-24 stsp
9466 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
9467 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
9468 0ebf8283 2019-07-24 stsp goto done;
9469 0ebf8283 2019-07-24 stsp }
9470 0ebf8283 2019-07-24 stsp
9471 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
9472 0ebf8283 2019-07-24 stsp if (error)
9473 0ebf8283 2019-07-24 stsp goto done;
9474 0ebf8283 2019-07-24 stsp
9475 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
9476 0ebf8283 2019-07-24 stsp free(path);
9477 0ebf8283 2019-07-24 stsp if (error)
9478 0ebf8283 2019-07-24 stsp goto done;
9479 0ebf8283 2019-07-24 stsp
9480 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
9481 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
9482 3e3a69f1 2019-07-25 stsp worktree, repo);
9483 0ebf8283 2019-07-24 stsp if (error)
9484 0ebf8283 2019-07-24 stsp goto done;
9485 0ebf8283 2019-07-24 stsp
9486 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
9487 0ebf8283 2019-07-24 stsp if (error)
9488 0ebf8283 2019-07-24 stsp goto done;
9489 0ebf8283 2019-07-24 stsp
9490 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9491 0ebf8283 2019-07-24 stsp head_commit_id);
9492 0ebf8283 2019-07-24 stsp if (error)
9493 0ebf8283 2019-07-24 stsp goto done;
9494 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9495 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9496 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
9497 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9498 3aac7cf7 2019-07-25 stsp goto done;
9499 3aac7cf7 2019-07-25 stsp }
9500 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
9501 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
9502 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9503 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9504 0ebf8283 2019-07-24 stsp commit = NULL;
9505 0ebf8283 2019-07-24 stsp if (error)
9506 0ebf8283 2019-07-24 stsp goto done;
9507 0ebf8283 2019-07-24 stsp } else {
9508 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
9509 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
9510 0ebf8283 2019-07-24 stsp goto done;
9511 0ebf8283 2019-07-24 stsp }
9512 0ebf8283 2019-07-24 stsp
9513 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
9514 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
9515 0ebf8283 2019-07-24 stsp if (error != NULL)
9516 0ebf8283 2019-07-24 stsp goto done;
9517 0ebf8283 2019-07-24 stsp
9518 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
9519 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
9520 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
9521 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
9522 c7d20a3f 2019-07-30 stsp goto done;
9523 c7d20a3f 2019-07-30 stsp }
9524 c7d20a3f 2019-07-30 stsp
9525 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
9526 bfce7f83 2019-07-27 stsp got_ref_close(branch);
9527 bfce7f83 2019-07-27 stsp branch = NULL;
9528 0ebf8283 2019-07-24 stsp if (error)
9529 0ebf8283 2019-07-24 stsp goto done;
9530 0ebf8283 2019-07-24 stsp
9531 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9532 0ebf8283 2019-07-24 stsp head_commit_id);
9533 0ebf8283 2019-07-24 stsp if (error)
9534 0ebf8283 2019-07-24 stsp goto done;
9535 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9536 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9537 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
9538 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9539 3aac7cf7 2019-07-25 stsp goto done;
9540 3aac7cf7 2019-07-25 stsp }
9541 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
9542 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
9543 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
9544 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9545 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9546 0ebf8283 2019-07-24 stsp commit = NULL;
9547 0ebf8283 2019-07-24 stsp if (error)
9548 0ebf8283 2019-07-24 stsp goto done;
9549 0ebf8283 2019-07-24 stsp
9550 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
9551 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9552 c5996fff 2020-01-29 stsp goto done;
9553 c5996fff 2020-01-29 stsp }
9554 c5996fff 2020-01-29 stsp
9555 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9556 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
9557 bfce7f83 2019-07-27 stsp if (error)
9558 bfce7f83 2019-07-27 stsp goto done;
9559 bfce7f83 2019-07-27 stsp
9560 0ebf8283 2019-07-24 stsp if (edit_script_path) {
9561 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
9562 0ebf8283 2019-07-24 stsp edit_script_path, repo);
9563 6ba2bea2 2019-07-27 stsp if (error) {
9564 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9565 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9566 9627c110 2020-04-18 stsp update_progress, &upa);
9567 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9568 0ebf8283 2019-07-24 stsp goto done;
9569 6ba2bea2 2019-07-27 stsp }
9570 0ebf8283 2019-07-24 stsp } else {
9571 514f2ffe 2020-01-29 stsp const char *branch_name;
9572 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
9573 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
9574 514f2ffe 2020-01-29 stsp branch_name += 11;
9575 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
9576 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
9577 6ba2bea2 2019-07-27 stsp if (error) {
9578 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9579 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9580 9627c110 2020-04-18 stsp update_progress, &upa);
9581 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9582 0ebf8283 2019-07-24 stsp goto done;
9583 6ba2bea2 2019-07-27 stsp }
9584 0ebf8283 2019-07-24 stsp
9585 0ebf8283 2019-07-24 stsp }
9586 0ebf8283 2019-07-24 stsp
9587 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
9588 0ebf8283 2019-07-24 stsp repo);
9589 6ba2bea2 2019-07-27 stsp if (error) {
9590 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9591 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9592 9627c110 2020-04-18 stsp update_progress, &upa);
9593 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9594 0ebf8283 2019-07-24 stsp goto done;
9595 6ba2bea2 2019-07-27 stsp }
9596 0ebf8283 2019-07-24 stsp
9597 0ebf8283 2019-07-24 stsp }
9598 0ebf8283 2019-07-24 stsp
9599 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
9600 4ec14e60 2019-08-28 hiltjo if (error)
9601 0ebf8283 2019-07-24 stsp goto done;
9602 0ebf8283 2019-07-24 stsp
9603 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9604 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
9605 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
9606 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
9607 0ebf8283 2019-07-24 stsp continue;
9608 0ebf8283 2019-07-24 stsp
9609 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
9610 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9611 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
9612 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
9613 62d463ca 2020-10-20 naddy repo);
9614 ab20a43a 2020-01-29 stsp if (error)
9615 ab20a43a 2020-01-29 stsp goto done;
9616 0ebf8283 2019-07-24 stsp } else {
9617 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
9618 ab20a43a 2020-01-29 stsp int have_changes = 0;
9619 ab20a43a 2020-01-29 stsp
9620 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
9621 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
9622 ab20a43a 2020-01-29 stsp if (error)
9623 ab20a43a 2020-01-29 stsp goto done;
9624 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
9625 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
9626 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
9627 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
9628 ab20a43a 2020-01-29 stsp if (error) {
9629 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
9630 ab20a43a 2020-01-29 stsp goto done;
9631 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
9632 ab20a43a 2020-01-29 stsp goto done;
9633 ab20a43a 2020-01-29 stsp }
9634 ab20a43a 2020-01-29 stsp if (have_changes) {
9635 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
9636 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
9637 ab20a43a 2020-01-29 stsp if (error)
9638 ab20a43a 2020-01-29 stsp goto done;
9639 ab20a43a 2020-01-29 stsp } else {
9640 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
9641 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
9642 ab20a43a 2020-01-29 stsp if (error)
9643 ab20a43a 2020-01-29 stsp goto done;
9644 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
9645 ab20a43a 2020-01-29 stsp hle, NULL);
9646 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
9647 ab20a43a 2020-01-29 stsp commit = NULL;
9648 ab20a43a 2020-01-29 stsp if (error)
9649 ab20a43a 2020-01-29 stsp goto done;
9650 ab20a43a 2020-01-29 stsp }
9651 0ebf8283 2019-07-24 stsp }
9652 0ebf8283 2019-07-24 stsp continue;
9653 0ebf8283 2019-07-24 stsp }
9654 0ebf8283 2019-07-24 stsp
9655 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9656 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9657 0ebf8283 2019-07-24 stsp if (error)
9658 0ebf8283 2019-07-24 stsp goto done;
9659 0ebf8283 2019-07-24 stsp continue;
9660 0ebf8283 2019-07-24 stsp }
9661 0ebf8283 2019-07-24 stsp
9662 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9663 0ebf8283 2019-07-24 stsp hle->commit_id);
9664 0ebf8283 2019-07-24 stsp if (error)
9665 0ebf8283 2019-07-24 stsp goto done;
9666 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9667 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9668 0ebf8283 2019-07-24 stsp
9669 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
9670 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
9671 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9672 0ebf8283 2019-07-24 stsp if (error)
9673 0ebf8283 2019-07-24 stsp goto done;
9674 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9675 0ebf8283 2019-07-24 stsp commit = NULL;
9676 0ebf8283 2019-07-24 stsp
9677 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9678 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
9679 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
9680 9627c110 2020-04-18 stsp
9681 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9682 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
9683 a0ea4fc0 2020-02-28 stsp repo);
9684 a0ea4fc0 2020-02-28 stsp if (error)
9685 a0ea4fc0 2020-02-28 stsp goto done;
9686 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9687 0ebf8283 2019-07-24 stsp break;
9688 0ebf8283 2019-07-24 stsp }
9689 0ebf8283 2019-07-24 stsp
9690 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9691 0ebf8283 2019-07-24 stsp char *id_str;
9692 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
9693 0ebf8283 2019-07-24 stsp if (error)
9694 0ebf8283 2019-07-24 stsp goto done;
9695 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
9696 0ebf8283 2019-07-24 stsp id_str);
9697 0ebf8283 2019-07-24 stsp free(id_str);
9698 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9699 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
9700 3e3a69f1 2019-07-25 stsp fileindex);
9701 0ebf8283 2019-07-24 stsp goto done;
9702 0ebf8283 2019-07-24 stsp }
9703 0ebf8283 2019-07-24 stsp
9704 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9705 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9706 0ebf8283 2019-07-24 stsp if (error)
9707 0ebf8283 2019-07-24 stsp goto done;
9708 0ebf8283 2019-07-24 stsp continue;
9709 0ebf8283 2019-07-24 stsp }
9710 0ebf8283 2019-07-24 stsp
9711 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
9712 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
9713 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9714 0ebf8283 2019-07-24 stsp if (error)
9715 0ebf8283 2019-07-24 stsp goto done;
9716 0ebf8283 2019-07-24 stsp }
9717 0ebf8283 2019-07-24 stsp
9718 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9719 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
9720 0ebf8283 2019-07-24 stsp if (error)
9721 0ebf8283 2019-07-24 stsp goto done;
9722 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9723 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
9724 0ebf8283 2019-07-24 stsp } else
9725 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
9726 3e3a69f1 2019-07-25 stsp branch, repo);
9727 0ebf8283 2019-07-24 stsp done:
9728 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
9729 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
9730 0ebf8283 2019-07-24 stsp free(head_commit_id);
9731 0ebf8283 2019-07-24 stsp free(base_commit_id);
9732 0ebf8283 2019-07-24 stsp free(resume_commit_id);
9733 0ebf8283 2019-07-24 stsp if (commit)
9734 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9735 0ebf8283 2019-07-24 stsp if (branch)
9736 0ebf8283 2019-07-24 stsp got_ref_close(branch);
9737 0ebf8283 2019-07-24 stsp if (tmp_branch)
9738 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
9739 0ebf8283 2019-07-24 stsp if (worktree)
9740 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
9741 1d0f4054 2021-06-17 stsp if (repo) {
9742 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9743 1d0f4054 2021-06-17 stsp if (error == NULL)
9744 1d0f4054 2021-06-17 stsp error = close_err;
9745 1d0f4054 2021-06-17 stsp }
9746 2822a352 2019-10-15 stsp return error;
9747 2822a352 2019-10-15 stsp }
9748 2822a352 2019-10-15 stsp
9749 2822a352 2019-10-15 stsp __dead static void
9750 2822a352 2019-10-15 stsp usage_integrate(void)
9751 2822a352 2019-10-15 stsp {
9752 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9753 2822a352 2019-10-15 stsp exit(1);
9754 2822a352 2019-10-15 stsp }
9755 2822a352 2019-10-15 stsp
9756 2822a352 2019-10-15 stsp static const struct got_error *
9757 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9758 2822a352 2019-10-15 stsp {
9759 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9760 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9761 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9762 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9763 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9764 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9765 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9766 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9767 9627c110 2020-04-18 stsp int ch;
9768 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9769 2822a352 2019-10-15 stsp
9770 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9771 2822a352 2019-10-15 stsp switch (ch) {
9772 2822a352 2019-10-15 stsp default:
9773 2822a352 2019-10-15 stsp usage_integrate();
9774 2822a352 2019-10-15 stsp /* NOTREACHED */
9775 2822a352 2019-10-15 stsp }
9776 2822a352 2019-10-15 stsp }
9777 2822a352 2019-10-15 stsp
9778 2822a352 2019-10-15 stsp argc -= optind;
9779 2822a352 2019-10-15 stsp argv += optind;
9780 2822a352 2019-10-15 stsp
9781 2822a352 2019-10-15 stsp if (argc != 1)
9782 2822a352 2019-10-15 stsp usage_integrate();
9783 2822a352 2019-10-15 stsp branch_arg = argv[0];
9784 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9785 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9786 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9787 2822a352 2019-10-15 stsp err(1, "pledge");
9788 b08a0ccd 2020-09-24 stsp #endif
9789 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9790 2822a352 2019-10-15 stsp if (cwd == NULL) {
9791 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9792 2822a352 2019-10-15 stsp goto done;
9793 2822a352 2019-10-15 stsp }
9794 2822a352 2019-10-15 stsp
9795 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9796 fa51e947 2020-03-27 stsp if (error) {
9797 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9798 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9799 fa51e947 2020-03-27 stsp cwd);
9800 2822a352 2019-10-15 stsp goto done;
9801 fa51e947 2020-03-27 stsp }
9802 2822a352 2019-10-15 stsp
9803 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9804 2822a352 2019-10-15 stsp if (error)
9805 2822a352 2019-10-15 stsp goto done;
9806 2822a352 2019-10-15 stsp
9807 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9808 2822a352 2019-10-15 stsp NULL);
9809 2822a352 2019-10-15 stsp if (error != NULL)
9810 2822a352 2019-10-15 stsp goto done;
9811 2822a352 2019-10-15 stsp
9812 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9813 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9814 2822a352 2019-10-15 stsp if (error)
9815 2822a352 2019-10-15 stsp goto done;
9816 2822a352 2019-10-15 stsp
9817 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9818 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9819 2822a352 2019-10-15 stsp goto done;
9820 2822a352 2019-10-15 stsp }
9821 2822a352 2019-10-15 stsp
9822 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9823 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9824 2822a352 2019-10-15 stsp if (error)
9825 2822a352 2019-10-15 stsp goto done;
9826 2822a352 2019-10-15 stsp
9827 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9828 2822a352 2019-10-15 stsp if (refname == NULL) {
9829 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9830 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9831 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9832 2822a352 2019-10-15 stsp goto done;
9833 2822a352 2019-10-15 stsp }
9834 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9835 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9836 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9837 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9838 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9839 2822a352 2019-10-15 stsp goto done;
9840 2822a352 2019-10-15 stsp }
9841 2822a352 2019-10-15 stsp
9842 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9843 2822a352 2019-10-15 stsp if (error)
9844 2822a352 2019-10-15 stsp goto done;
9845 2822a352 2019-10-15 stsp
9846 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9847 2822a352 2019-10-15 stsp if (error)
9848 2822a352 2019-10-15 stsp goto done;
9849 2822a352 2019-10-15 stsp
9850 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9851 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9852 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9853 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9854 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9855 2822a352 2019-10-15 stsp goto done;
9856 2822a352 2019-10-15 stsp }
9857 2822a352 2019-10-15 stsp
9858 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9859 2822a352 2019-10-15 stsp if (error) {
9860 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9861 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9862 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9863 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9864 2822a352 2019-10-15 stsp goto done;
9865 2822a352 2019-10-15 stsp }
9866 2822a352 2019-10-15 stsp
9867 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9868 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9869 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9870 2822a352 2019-10-15 stsp check_cancelled, NULL);
9871 2822a352 2019-10-15 stsp if (error)
9872 2822a352 2019-10-15 stsp goto done;
9873 2822a352 2019-10-15 stsp
9874 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9875 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9876 2822a352 2019-10-15 stsp done:
9877 1d0f4054 2021-06-17 stsp if (repo) {
9878 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9879 1d0f4054 2021-06-17 stsp if (error == NULL)
9880 1d0f4054 2021-06-17 stsp error = close_err;
9881 1d0f4054 2021-06-17 stsp }
9882 2822a352 2019-10-15 stsp if (worktree)
9883 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9884 2822a352 2019-10-15 stsp free(cwd);
9885 2822a352 2019-10-15 stsp free(base_commit_id);
9886 2822a352 2019-10-15 stsp free(commit_id);
9887 2822a352 2019-10-15 stsp free(refname);
9888 2822a352 2019-10-15 stsp free(base_refname);
9889 715dc77e 2019-08-03 stsp return error;
9890 715dc77e 2019-08-03 stsp }
9891 715dc77e 2019-08-03 stsp
9892 715dc77e 2019-08-03 stsp __dead static void
9893 715dc77e 2019-08-03 stsp usage_stage(void)
9894 715dc77e 2019-08-03 stsp {
9895 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9896 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9897 715dc77e 2019-08-03 stsp getprogname());
9898 715dc77e 2019-08-03 stsp exit(1);
9899 715dc77e 2019-08-03 stsp }
9900 715dc77e 2019-08-03 stsp
9901 715dc77e 2019-08-03 stsp static const struct got_error *
9902 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9903 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9904 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9905 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9906 408b4ebc 2019-08-03 stsp {
9907 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9908 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9909 408b4ebc 2019-08-03 stsp
9910 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9911 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9912 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9913 408b4ebc 2019-08-03 stsp return NULL;
9914 408b4ebc 2019-08-03 stsp
9915 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9916 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9917 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9918 408b4ebc 2019-08-03 stsp else
9919 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9920 408b4ebc 2019-08-03 stsp if (err)
9921 408b4ebc 2019-08-03 stsp return err;
9922 408b4ebc 2019-08-03 stsp
9923 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9924 408b4ebc 2019-08-03 stsp free(id_str);
9925 dc424a06 2019-08-07 stsp return NULL;
9926 dc424a06 2019-08-07 stsp }
9927 2e1f37b0 2019-08-08 stsp
9928 dc424a06 2019-08-07 stsp static const struct got_error *
9929 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9930 715dc77e 2019-08-03 stsp {
9931 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9932 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9933 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9934 715dc77e 2019-08-03 stsp char *cwd = NULL;
9935 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9936 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9937 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9938 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9939 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9940 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9941 715dc77e 2019-08-03 stsp
9942 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9943 715dc77e 2019-08-03 stsp
9944 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9945 715dc77e 2019-08-03 stsp switch (ch) {
9946 408b4ebc 2019-08-03 stsp case 'l':
9947 408b4ebc 2019-08-03 stsp list_stage = 1;
9948 408b4ebc 2019-08-03 stsp break;
9949 dc424a06 2019-08-07 stsp case 'p':
9950 dc424a06 2019-08-07 stsp pflag = 1;
9951 dc424a06 2019-08-07 stsp break;
9952 dc424a06 2019-08-07 stsp case 'F':
9953 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9954 dc424a06 2019-08-07 stsp break;
9955 35213c7c 2020-07-23 stsp case 'S':
9956 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9957 35213c7c 2020-07-23 stsp break;
9958 715dc77e 2019-08-03 stsp default:
9959 715dc77e 2019-08-03 stsp usage_stage();
9960 715dc77e 2019-08-03 stsp /* NOTREACHED */
9961 715dc77e 2019-08-03 stsp }
9962 715dc77e 2019-08-03 stsp }
9963 715dc77e 2019-08-03 stsp
9964 715dc77e 2019-08-03 stsp argc -= optind;
9965 715dc77e 2019-08-03 stsp argv += optind;
9966 715dc77e 2019-08-03 stsp
9967 715dc77e 2019-08-03 stsp #ifndef PROFILE
9968 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9969 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9970 715dc77e 2019-08-03 stsp err(1, "pledge");
9971 715dc77e 2019-08-03 stsp #endif
9972 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9973 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9974 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9975 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9976 715dc77e 2019-08-03 stsp
9977 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9978 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9979 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9980 715dc77e 2019-08-03 stsp goto done;
9981 715dc77e 2019-08-03 stsp }
9982 715dc77e 2019-08-03 stsp
9983 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9984 fa51e947 2020-03-27 stsp if (error) {
9985 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9986 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9987 715dc77e 2019-08-03 stsp goto done;
9988 fa51e947 2020-03-27 stsp }
9989 715dc77e 2019-08-03 stsp
9990 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9991 c9956ddf 2019-09-08 stsp NULL);
9992 715dc77e 2019-08-03 stsp if (error != NULL)
9993 715dc77e 2019-08-03 stsp goto done;
9994 715dc77e 2019-08-03 stsp
9995 dc424a06 2019-08-07 stsp if (patch_script_path) {
9996 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9997 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9998 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9999 dc424a06 2019-08-07 stsp patch_script_path);
10000 dc424a06 2019-08-07 stsp goto done;
10001 dc424a06 2019-08-07 stsp }
10002 dc424a06 2019-08-07 stsp }
10003 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10004 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
10005 715dc77e 2019-08-03 stsp if (error)
10006 715dc77e 2019-08-03 stsp goto done;
10007 715dc77e 2019-08-03 stsp
10008 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10009 6d022e97 2019-08-04 stsp if (error)
10010 6d022e97 2019-08-04 stsp goto done;
10011 715dc77e 2019-08-03 stsp
10012 6d022e97 2019-08-04 stsp if (list_stage)
10013 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
10014 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
10015 2e1f37b0 2019-08-08 stsp else {
10016 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10017 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
10018 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
10019 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
10020 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
10021 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
10022 2e1f37b0 2019-08-08 stsp }
10023 ad493afc 2019-08-03 stsp done:
10024 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10025 2e1f37b0 2019-08-08 stsp error == NULL)
10026 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10027 1d0f4054 2021-06-17 stsp if (repo) {
10028 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10029 1d0f4054 2021-06-17 stsp if (error == NULL)
10030 1d0f4054 2021-06-17 stsp error = close_err;
10031 1d0f4054 2021-06-17 stsp }
10032 ad493afc 2019-08-03 stsp if (worktree)
10033 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
10034 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10035 ad493afc 2019-08-03 stsp free((char *)pe->path);
10036 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
10037 ad493afc 2019-08-03 stsp free(cwd);
10038 ad493afc 2019-08-03 stsp return error;
10039 ad493afc 2019-08-03 stsp }
10040 ad493afc 2019-08-03 stsp
10041 ad493afc 2019-08-03 stsp __dead static void
10042 ad493afc 2019-08-03 stsp usage_unstage(void)
10043 ad493afc 2019-08-03 stsp {
10044 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10045 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
10046 ad493afc 2019-08-03 stsp getprogname());
10047 ad493afc 2019-08-03 stsp exit(1);
10048 ad493afc 2019-08-03 stsp }
10049 ad493afc 2019-08-03 stsp
10050 ad493afc 2019-08-03 stsp
10051 ad493afc 2019-08-03 stsp static const struct got_error *
10052 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
10053 ad493afc 2019-08-03 stsp {
10054 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
10055 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
10056 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
10057 ad493afc 2019-08-03 stsp char *cwd = NULL;
10058 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
10059 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
10060 9627c110 2020-04-18 stsp int ch, pflag = 0;
10061 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10062 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
10063 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10064 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10065 ad493afc 2019-08-03 stsp
10066 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
10067 ad493afc 2019-08-03 stsp
10068 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
10069 ad493afc 2019-08-03 stsp switch (ch) {
10070 2e1f37b0 2019-08-08 stsp case 'p':
10071 2e1f37b0 2019-08-08 stsp pflag = 1;
10072 2e1f37b0 2019-08-08 stsp break;
10073 2e1f37b0 2019-08-08 stsp case 'F':
10074 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
10075 2e1f37b0 2019-08-08 stsp break;
10076 ad493afc 2019-08-03 stsp default:
10077 ad493afc 2019-08-03 stsp usage_unstage();
10078 ad493afc 2019-08-03 stsp /* NOTREACHED */
10079 ad493afc 2019-08-03 stsp }
10080 ad493afc 2019-08-03 stsp }
10081 ad493afc 2019-08-03 stsp
10082 ad493afc 2019-08-03 stsp argc -= optind;
10083 ad493afc 2019-08-03 stsp argv += optind;
10084 ad493afc 2019-08-03 stsp
10085 ad493afc 2019-08-03 stsp #ifndef PROFILE
10086 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10087 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
10088 ad493afc 2019-08-03 stsp err(1, "pledge");
10089 ad493afc 2019-08-03 stsp #endif
10090 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
10091 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
10092 2e1f37b0 2019-08-08 stsp
10093 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
10094 ad493afc 2019-08-03 stsp if (cwd == NULL) {
10095 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
10096 ad493afc 2019-08-03 stsp goto done;
10097 ad493afc 2019-08-03 stsp }
10098 ad493afc 2019-08-03 stsp
10099 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10100 fa51e947 2020-03-27 stsp if (error) {
10101 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10102 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
10103 ad493afc 2019-08-03 stsp goto done;
10104 fa51e947 2020-03-27 stsp }
10105 ad493afc 2019-08-03 stsp
10106 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10107 c9956ddf 2019-09-08 stsp NULL);
10108 ad493afc 2019-08-03 stsp if (error != NULL)
10109 ad493afc 2019-08-03 stsp goto done;
10110 ad493afc 2019-08-03 stsp
10111 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
10112 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
10113 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
10114 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
10115 2e1f37b0 2019-08-08 stsp patch_script_path);
10116 2e1f37b0 2019-08-08 stsp goto done;
10117 2e1f37b0 2019-08-08 stsp }
10118 2e1f37b0 2019-08-08 stsp }
10119 2e1f37b0 2019-08-08 stsp
10120 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10121 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10122 ad493afc 2019-08-03 stsp if (error)
10123 ad493afc 2019-08-03 stsp goto done;
10124 ad493afc 2019-08-03 stsp
10125 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10126 ad493afc 2019-08-03 stsp if (error)
10127 ad493afc 2019-08-03 stsp goto done;
10128 ad493afc 2019-08-03 stsp
10129 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10130 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10131 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10132 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10133 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10134 9627c110 2020-04-18 stsp if (!error)
10135 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10136 715dc77e 2019-08-03 stsp done:
10137 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10138 2e1f37b0 2019-08-08 stsp error == NULL)
10139 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10140 1d0f4054 2021-06-17 stsp if (repo) {
10141 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10142 1d0f4054 2021-06-17 stsp if (error == NULL)
10143 1d0f4054 2021-06-17 stsp error = close_err;
10144 1d0f4054 2021-06-17 stsp }
10145 715dc77e 2019-08-03 stsp if (worktree)
10146 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10147 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10148 715dc77e 2019-08-03 stsp free((char *)pe->path);
10149 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10150 715dc77e 2019-08-03 stsp free(cwd);
10151 01073a5d 2019-08-22 stsp return error;
10152 01073a5d 2019-08-22 stsp }
10153 01073a5d 2019-08-22 stsp
10154 01073a5d 2019-08-22 stsp __dead static void
10155 01073a5d 2019-08-22 stsp usage_cat(void)
10156 01073a5d 2019-08-22 stsp {
10157 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10158 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10159 01073a5d 2019-08-22 stsp exit(1);
10160 01073a5d 2019-08-22 stsp }
10161 01073a5d 2019-08-22 stsp
10162 01073a5d 2019-08-22 stsp static const struct got_error *
10163 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10164 01073a5d 2019-08-22 stsp {
10165 01073a5d 2019-08-22 stsp const struct got_error *err;
10166 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10167 01073a5d 2019-08-22 stsp
10168 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10169 01073a5d 2019-08-22 stsp if (err)
10170 01073a5d 2019-08-22 stsp return err;
10171 01073a5d 2019-08-22 stsp
10172 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10173 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10174 01073a5d 2019-08-22 stsp return err;
10175 01073a5d 2019-08-22 stsp }
10176 01073a5d 2019-08-22 stsp
10177 01073a5d 2019-08-22 stsp static const struct got_error *
10178 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10179 01073a5d 2019-08-22 stsp {
10180 01073a5d 2019-08-22 stsp const struct got_error *err;
10181 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10182 56e0773d 2019-11-28 stsp int nentries, i;
10183 01073a5d 2019-08-22 stsp
10184 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10185 01073a5d 2019-08-22 stsp if (err)
10186 01073a5d 2019-08-22 stsp return err;
10187 01073a5d 2019-08-22 stsp
10188 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10189 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10190 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10191 01073a5d 2019-08-22 stsp char *id_str;
10192 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10193 01073a5d 2019-08-22 stsp break;
10194 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10195 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10196 01073a5d 2019-08-22 stsp if (err)
10197 01073a5d 2019-08-22 stsp break;
10198 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10199 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10200 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10201 01073a5d 2019-08-22 stsp free(id_str);
10202 01073a5d 2019-08-22 stsp }
10203 01073a5d 2019-08-22 stsp
10204 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10205 01073a5d 2019-08-22 stsp return err;
10206 01073a5d 2019-08-22 stsp }
10207 01073a5d 2019-08-22 stsp
10208 01073a5d 2019-08-22 stsp static const struct got_error *
10209 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10210 01073a5d 2019-08-22 stsp {
10211 01073a5d 2019-08-22 stsp const struct got_error *err;
10212 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10213 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10214 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10215 01073a5d 2019-08-22 stsp char *id_str = NULL;
10216 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10217 01073a5d 2019-08-22 stsp
10218 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10219 01073a5d 2019-08-22 stsp if (err)
10220 01073a5d 2019-08-22 stsp return err;
10221 01073a5d 2019-08-22 stsp
10222 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10223 01073a5d 2019-08-22 stsp if (err)
10224 01073a5d 2019-08-22 stsp goto done;
10225 01073a5d 2019-08-22 stsp
10226 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10227 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10228 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10229 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10230 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
10231 01073a5d 2019-08-22 stsp char *pid_str;
10232 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10233 01073a5d 2019-08-22 stsp if (err)
10234 01073a5d 2019-08-22 stsp goto done;
10235 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10236 01073a5d 2019-08-22 stsp free(pid_str);
10237 01073a5d 2019-08-22 stsp }
10238 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10239 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10240 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10241 01073a5d 2019-08-22 stsp
10242 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10243 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10244 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10245 01073a5d 2019-08-22 stsp
10246 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10247 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10248 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10249 01073a5d 2019-08-22 stsp done:
10250 01073a5d 2019-08-22 stsp free(id_str);
10251 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10252 01073a5d 2019-08-22 stsp return err;
10253 01073a5d 2019-08-22 stsp }
10254 01073a5d 2019-08-22 stsp
10255 01073a5d 2019-08-22 stsp static const struct got_error *
10256 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10257 01073a5d 2019-08-22 stsp {
10258 01073a5d 2019-08-22 stsp const struct got_error *err;
10259 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10260 01073a5d 2019-08-22 stsp char *id_str = NULL;
10261 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10262 01073a5d 2019-08-22 stsp
10263 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10264 01073a5d 2019-08-22 stsp if (err)
10265 01073a5d 2019-08-22 stsp return err;
10266 01073a5d 2019-08-22 stsp
10267 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10268 01073a5d 2019-08-22 stsp if (err)
10269 01073a5d 2019-08-22 stsp goto done;
10270 01073a5d 2019-08-22 stsp
10271 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10272 e15d5241 2019-08-22 stsp
10273 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10274 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10275 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10276 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10277 01073a5d 2019-08-22 stsp break;
10278 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10279 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10280 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10281 01073a5d 2019-08-22 stsp break;
10282 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10283 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10284 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10285 01073a5d 2019-08-22 stsp break;
10286 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10287 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10288 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10289 01073a5d 2019-08-22 stsp break;
10290 01073a5d 2019-08-22 stsp default:
10291 01073a5d 2019-08-22 stsp break;
10292 01073a5d 2019-08-22 stsp }
10293 01073a5d 2019-08-22 stsp
10294 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10295 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10296 e15d5241 2019-08-22 stsp
10297 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10298 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10299 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10300 01073a5d 2019-08-22 stsp
10301 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10302 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10303 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10304 01073a5d 2019-08-22 stsp done:
10305 01073a5d 2019-08-22 stsp free(id_str);
10306 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10307 01073a5d 2019-08-22 stsp return err;
10308 01073a5d 2019-08-22 stsp }
10309 01073a5d 2019-08-22 stsp
10310 01073a5d 2019-08-22 stsp static const struct got_error *
10311 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10312 01073a5d 2019-08-22 stsp {
10313 01073a5d 2019-08-22 stsp const struct got_error *error;
10314 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10315 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10316 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10317 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10318 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10319 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10320 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10321 84de9106 2020-12-26 stsp
10322 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10323 01073a5d 2019-08-22 stsp
10324 01073a5d 2019-08-22 stsp #ifndef PROFILE
10325 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10326 01073a5d 2019-08-22 stsp NULL) == -1)
10327 01073a5d 2019-08-22 stsp err(1, "pledge");
10328 01073a5d 2019-08-22 stsp #endif
10329 01073a5d 2019-08-22 stsp
10330 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10331 01073a5d 2019-08-22 stsp switch (ch) {
10332 896e9b6f 2019-08-26 stsp case 'c':
10333 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10334 896e9b6f 2019-08-26 stsp break;
10335 01073a5d 2019-08-22 stsp case 'r':
10336 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10337 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10338 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10339 9ba1d308 2019-10-21 stsp optarg);
10340 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10341 01073a5d 2019-08-22 stsp break;
10342 896e9b6f 2019-08-26 stsp case 'P':
10343 896e9b6f 2019-08-26 stsp force_path = 1;
10344 896e9b6f 2019-08-26 stsp break;
10345 01073a5d 2019-08-22 stsp default:
10346 01073a5d 2019-08-22 stsp usage_cat();
10347 01073a5d 2019-08-22 stsp /* NOTREACHED */
10348 01073a5d 2019-08-22 stsp }
10349 01073a5d 2019-08-22 stsp }
10350 01073a5d 2019-08-22 stsp
10351 01073a5d 2019-08-22 stsp argc -= optind;
10352 01073a5d 2019-08-22 stsp argv += optind;
10353 01073a5d 2019-08-22 stsp
10354 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10355 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10356 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10357 01073a5d 2019-08-22 stsp goto done;
10358 01073a5d 2019-08-22 stsp }
10359 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
10360 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10361 01073a5d 2019-08-22 stsp goto done;
10362 01073a5d 2019-08-22 stsp if (worktree) {
10363 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10364 01073a5d 2019-08-22 stsp repo_path = strdup(
10365 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
10366 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10367 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
10368 01073a5d 2019-08-22 stsp goto done;
10369 01073a5d 2019-08-22 stsp }
10370 01073a5d 2019-08-22 stsp }
10371 01073a5d 2019-08-22 stsp }
10372 01073a5d 2019-08-22 stsp
10373 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10374 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
10375 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10376 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
10377 01073a5d 2019-08-22 stsp }
10378 01073a5d 2019-08-22 stsp
10379 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
10380 01073a5d 2019-08-22 stsp free(repo_path);
10381 01073a5d 2019-08-22 stsp if (error != NULL)
10382 01073a5d 2019-08-22 stsp goto done;
10383 01073a5d 2019-08-22 stsp
10384 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10385 896e9b6f 2019-08-26 stsp if (error)
10386 896e9b6f 2019-08-26 stsp goto done;
10387 896e9b6f 2019-08-26 stsp
10388 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10389 84de9106 2020-12-26 stsp if (error)
10390 84de9106 2020-12-26 stsp goto done;
10391 84de9106 2020-12-26 stsp
10392 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
10393 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
10394 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
10395 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10396 01073a5d 2019-08-22 stsp if (error)
10397 01073a5d 2019-08-22 stsp goto done;
10398 01073a5d 2019-08-22 stsp
10399 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
10400 896e9b6f 2019-08-26 stsp if (force_path) {
10401 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
10402 896e9b6f 2019-08-26 stsp argv[i]);
10403 896e9b6f 2019-08-26 stsp if (error)
10404 896e9b6f 2019-08-26 stsp break;
10405 896e9b6f 2019-08-26 stsp } else {
10406 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
10407 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10408 84de9106 2020-12-26 stsp repo);
10409 896e9b6f 2019-08-26 stsp if (error) {
10410 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10411 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
10412 896e9b6f 2019-08-26 stsp break;
10413 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
10414 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
10415 896e9b6f 2019-08-26 stsp if (error)
10416 896e9b6f 2019-08-26 stsp break;
10417 896e9b6f 2019-08-26 stsp }
10418 896e9b6f 2019-08-26 stsp }
10419 01073a5d 2019-08-22 stsp
10420 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
10421 01073a5d 2019-08-22 stsp if (error)
10422 01073a5d 2019-08-22 stsp break;
10423 01073a5d 2019-08-22 stsp
10424 01073a5d 2019-08-22 stsp switch (obj_type) {
10425 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10426 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
10427 01073a5d 2019-08-22 stsp break;
10428 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10429 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
10430 01073a5d 2019-08-22 stsp break;
10431 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10432 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
10433 01073a5d 2019-08-22 stsp break;
10434 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10435 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
10436 01073a5d 2019-08-22 stsp break;
10437 01073a5d 2019-08-22 stsp default:
10438 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
10439 01073a5d 2019-08-22 stsp break;
10440 01073a5d 2019-08-22 stsp }
10441 01073a5d 2019-08-22 stsp if (error)
10442 01073a5d 2019-08-22 stsp break;
10443 01073a5d 2019-08-22 stsp free(label);
10444 01073a5d 2019-08-22 stsp label = NULL;
10445 01073a5d 2019-08-22 stsp free(id);
10446 01073a5d 2019-08-22 stsp id = NULL;
10447 01073a5d 2019-08-22 stsp }
10448 01073a5d 2019-08-22 stsp done:
10449 01073a5d 2019-08-22 stsp free(label);
10450 01073a5d 2019-08-22 stsp free(id);
10451 896e9b6f 2019-08-26 stsp free(commit_id);
10452 01073a5d 2019-08-22 stsp if (worktree)
10453 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
10454 01073a5d 2019-08-22 stsp if (repo) {
10455 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10456 01073a5d 2019-08-22 stsp if (error == NULL)
10457 1d0f4054 2021-06-17 stsp error = close_err;
10458 b2118c49 2020-07-28 stsp }
10459 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
10460 b2118c49 2020-07-28 stsp return error;
10461 b2118c49 2020-07-28 stsp }
10462 b2118c49 2020-07-28 stsp
10463 b2118c49 2020-07-28 stsp __dead static void
10464 b2118c49 2020-07-28 stsp usage_info(void)
10465 b2118c49 2020-07-28 stsp {
10466 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
10467 b2118c49 2020-07-28 stsp getprogname());
10468 b2118c49 2020-07-28 stsp exit(1);
10469 b2118c49 2020-07-28 stsp }
10470 b2118c49 2020-07-28 stsp
10471 b2118c49 2020-07-28 stsp static const struct got_error *
10472 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
10473 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10474 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
10475 b2118c49 2020-07-28 stsp {
10476 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
10477 b2118c49 2020-07-28 stsp char *id_str = NULL;
10478 b2118c49 2020-07-28 stsp char datebuf[128];
10479 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
10480 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
10481 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10482 b2118c49 2020-07-28 stsp
10483 b2118c49 2020-07-28 stsp /*
10484 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
10485 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
10486 b2118c49 2020-07-28 stsp */
10487 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
10488 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
10489 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
10490 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
10491 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
10492 b2118c49 2020-07-28 stsp }
10493 b2118c49 2020-07-28 stsp
10494 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
10495 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
10496 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
10497 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
10498 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
10499 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
10500 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
10501 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
10502 b2118c49 2020-07-28 stsp else
10503 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
10504 b2118c49 2020-07-28 stsp
10505 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
10506 b2118c49 2020-07-28 stsp if (tm == NULL)
10507 b2118c49 2020-07-28 stsp return NULL;
10508 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
10509 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
10510 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
10511 b2118c49 2020-07-28 stsp
10512 b2118c49 2020-07-28 stsp if (blob_id) {
10513 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
10514 b2118c49 2020-07-28 stsp if (err)
10515 b2118c49 2020-07-28 stsp return err;
10516 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
10517 b2118c49 2020-07-28 stsp free(id_str);
10518 b2118c49 2020-07-28 stsp }
10519 b2118c49 2020-07-28 stsp
10520 b2118c49 2020-07-28 stsp if (staged_blob_id) {
10521 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
10522 b2118c49 2020-07-28 stsp if (err)
10523 b2118c49 2020-07-28 stsp return err;
10524 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
10525 b2118c49 2020-07-28 stsp free(id_str);
10526 b2118c49 2020-07-28 stsp }
10527 b2118c49 2020-07-28 stsp
10528 b2118c49 2020-07-28 stsp if (commit_id) {
10529 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
10530 b2118c49 2020-07-28 stsp if (err)
10531 b2118c49 2020-07-28 stsp return err;
10532 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
10533 b2118c49 2020-07-28 stsp free(id_str);
10534 b2118c49 2020-07-28 stsp }
10535 b2118c49 2020-07-28 stsp
10536 b2118c49 2020-07-28 stsp return NULL;
10537 b2118c49 2020-07-28 stsp }
10538 b2118c49 2020-07-28 stsp
10539 b2118c49 2020-07-28 stsp static const struct got_error *
10540 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
10541 b2118c49 2020-07-28 stsp {
10542 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
10543 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
10544 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
10545 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
10546 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10547 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
10548 b2118c49 2020-07-28 stsp int ch, show_files = 0;
10549 b2118c49 2020-07-28 stsp
10550 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
10551 b2118c49 2020-07-28 stsp
10552 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10553 b2118c49 2020-07-28 stsp switch (ch) {
10554 b2118c49 2020-07-28 stsp default:
10555 b2118c49 2020-07-28 stsp usage_info();
10556 b2118c49 2020-07-28 stsp /* NOTREACHED */
10557 b2118c49 2020-07-28 stsp }
10558 01073a5d 2019-08-22 stsp }
10559 b2118c49 2020-07-28 stsp
10560 b2118c49 2020-07-28 stsp argc -= optind;
10561 b2118c49 2020-07-28 stsp argv += optind;
10562 b2118c49 2020-07-28 stsp
10563 b2118c49 2020-07-28 stsp #ifndef PROFILE
10564 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10565 b2118c49 2020-07-28 stsp NULL) == -1)
10566 b2118c49 2020-07-28 stsp err(1, "pledge");
10567 b2118c49 2020-07-28 stsp #endif
10568 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
10569 b2118c49 2020-07-28 stsp if (cwd == NULL) {
10570 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
10571 b2118c49 2020-07-28 stsp goto done;
10572 b2118c49 2020-07-28 stsp }
10573 b2118c49 2020-07-28 stsp
10574 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
10575 b2118c49 2020-07-28 stsp if (error) {
10576 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10577 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
10578 b2118c49 2020-07-28 stsp goto done;
10579 b2118c49 2020-07-28 stsp }
10580 b2118c49 2020-07-28 stsp
10581 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10582 b2118c49 2020-07-28 stsp if (error)
10583 b2118c49 2020-07-28 stsp goto done;
10584 b2118c49 2020-07-28 stsp
10585 b2118c49 2020-07-28 stsp if (argc >= 1) {
10586 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
10587 b2118c49 2020-07-28 stsp worktree);
10588 b2118c49 2020-07-28 stsp if (error)
10589 b2118c49 2020-07-28 stsp goto done;
10590 b2118c49 2020-07-28 stsp show_files = 1;
10591 b2118c49 2020-07-28 stsp }
10592 b2118c49 2020-07-28 stsp
10593 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
10594 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
10595 b2118c49 2020-07-28 stsp if (error)
10596 b2118c49 2020-07-28 stsp goto done;
10597 b2118c49 2020-07-28 stsp
10598 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
10599 b2118c49 2020-07-28 stsp if (error)
10600 b2118c49 2020-07-28 stsp goto done;
10601 b2118c49 2020-07-28 stsp
10602 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10603 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
10604 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
10605 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
10606 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
10607 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
10608 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
10609 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10610 b2118c49 2020-07-28 stsp
10611 b2118c49 2020-07-28 stsp if (show_files) {
10612 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10613 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10614 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
10615 b2118c49 2020-07-28 stsp continue;
10616 b2118c49 2020-07-28 stsp /*
10617 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
10618 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
10619 b2118c49 2020-07-28 stsp */
10620 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
10621 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
10622 b2118c49 2020-07-28 stsp }
10623 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
10624 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
10625 b2118c49 2020-07-28 stsp if (error)
10626 b2118c49 2020-07-28 stsp goto done;
10627 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10628 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
10629 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
10630 b2118c49 2020-07-28 stsp break;
10631 b2118c49 2020-07-28 stsp }
10632 b2118c49 2020-07-28 stsp }
10633 b2118c49 2020-07-28 stsp }
10634 b2118c49 2020-07-28 stsp done:
10635 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
10636 b2118c49 2020-07-28 stsp free((char *)pe->path);
10637 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
10638 b2118c49 2020-07-28 stsp free(cwd);
10639 b2118c49 2020-07-28 stsp free(id_str);
10640 b2118c49 2020-07-28 stsp free(uuidstr);
10641 0ebf8283 2019-07-24 stsp return error;
10642 0ebf8283 2019-07-24 stsp }