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 5c860e29 2018-03-12 stsp
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 5c860e29 2018-03-12 stsp #endif
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigint_received = 1;
69 99437157 2018-11-11 stsp }
70 99437157 2018-11-11 stsp
71 99437157 2018-11-11 stsp static void
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp {
74 99437157 2018-11-11 stsp sigpipe_received = 1;
75 99437157 2018-11-11 stsp }
76 5c860e29 2018-03-12 stsp
77 99437157 2018-11-11 stsp
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
83 5c860e29 2018-03-12 stsp };
84 5c860e29 2018-03-12 stsp
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
90 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
91 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
94 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
95 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
96 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
97 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
98 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
99 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
100 d00136be 2019-03-26 stsp __dead static void usage_add(void);
101 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
102 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
103 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
104 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
105 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
106 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
107 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
108 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
109 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
110 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
111 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
112 5c860e29 2018-03-12 stsp
113 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
114 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
115 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
116 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
118 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
121 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
122 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
124 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
125 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
126 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
127 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
128 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
129 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
130 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
131 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
132 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
133 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
134 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
135 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
136 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
137 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
138 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
139 5c860e29 2018-03-12 stsp
140 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
141 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
142 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
143 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
144 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
145 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
146 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
147 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
148 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
149 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
150 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
151 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
152 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
153 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
154 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
155 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
156 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
157 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
158 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
159 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
161 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
162 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
163 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
164 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
165 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
166 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
167 5c860e29 2018-03-12 stsp };
168 ce5b7c56 2019-07-09 stsp
169 ce5b7c56 2019-07-09 stsp static void
170 ce5b7c56 2019-07-09 stsp list_commands(void)
171 ce5b7c56 2019-07-09 stsp {
172 ce5b7c56 2019-07-09 stsp int i;
173 ce5b7c56 2019-07-09 stsp
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
175 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
176 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
177 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
178 ce5b7c56 2019-07-09 stsp }
179 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
180 ce5b7c56 2019-07-09 stsp }
181 5c860e29 2018-03-12 stsp
182 5c860e29 2018-03-12 stsp int
183 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
184 5c860e29 2018-03-12 stsp {
185 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
186 5c860e29 2018-03-12 stsp unsigned int i;
187 5c860e29 2018-03-12 stsp int ch;
188 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
189 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
190 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
191 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
192 83cd27f8 2020-01-13 stsp };
193 5c860e29 2018-03-12 stsp
194 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
195 5c860e29 2018-03-12 stsp
196 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 5c860e29 2018-03-12 stsp switch (ch) {
198 1b6b95a8 2018-03-12 stsp case 'h':
199 1b6b95a8 2018-03-12 stsp hflag = 1;
200 53ccebc2 2019-07-30 stsp break;
201 53ccebc2 2019-07-30 stsp case 'V':
202 53ccebc2 2019-07-30 stsp Vflag = 1;
203 1b6b95a8 2018-03-12 stsp break;
204 5c860e29 2018-03-12 stsp default:
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp /* NOTREACHED */
207 5c860e29 2018-03-12 stsp }
208 5c860e29 2018-03-12 stsp }
209 5c860e29 2018-03-12 stsp
210 5c860e29 2018-03-12 stsp argc -= optind;
211 5c860e29 2018-03-12 stsp argv += optind;
212 1e70621d 2018-03-27 stsp optind = 0;
213 53ccebc2 2019-07-30 stsp
214 53ccebc2 2019-07-30 stsp if (Vflag) {
215 53ccebc2 2019-07-30 stsp got_version_print_str();
216 53ccebc2 2019-07-30 stsp return 1;
217 53ccebc2 2019-07-30 stsp }
218 5c860e29 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp if (argc <= 0)
220 ce5b7c56 2019-07-09 stsp usage(hflag);
221 5c860e29 2018-03-12 stsp
222 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
223 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
224 99437157 2018-11-11 stsp
225 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
226 d7d4f210 2018-03-12 stsp const struct got_error *error;
227 d7d4f210 2018-03-12 stsp
228 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
229 5c860e29 2018-03-12 stsp
230 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
232 5c860e29 2018-03-12 stsp continue;
233 5c860e29 2018-03-12 stsp
234 1b6b95a8 2018-03-12 stsp if (hflag)
235 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
236 1b6b95a8 2018-03-12 stsp
237 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
238 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
239 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
240 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
241 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 70015d7a 2019-11-08 stsp !(sigint_received &&
243 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 d7d4f210 2018-03-12 stsp return 1;
246 d7d4f210 2018-03-12 stsp }
247 d7d4f210 2018-03-12 stsp
248 d7d4f210 2018-03-12 stsp return 0;
249 5c860e29 2018-03-12 stsp }
250 5c860e29 2018-03-12 stsp
251 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 ce5b7c56 2019-07-09 stsp list_commands();
253 5c860e29 2018-03-12 stsp return 1;
254 5c860e29 2018-03-12 stsp }
255 5c860e29 2018-03-12 stsp
256 4ed7e80c 2018-05-20 stsp __dead static void
257 ce5b7c56 2019-07-09 stsp usage(int hflag)
258 5c860e29 2018-03-12 stsp {
259 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 53ccebc2 2019-07-30 stsp getprogname());
261 ce5b7c56 2019-07-09 stsp if (hflag)
262 ce5b7c56 2019-07-09 stsp list_commands();
263 5c860e29 2018-03-12 stsp exit(1);
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 0266afb7 2019-01-04 stsp static const struct got_error *
267 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
268 e2ba3d07 2019-05-13 stsp {
269 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
270 e2ba3d07 2019-05-13 stsp const char *editor;
271 8920fa04 2019-08-18 stsp
272 8920fa04 2019-08-18 stsp *abspath = NULL;
273 e2ba3d07 2019-05-13 stsp
274 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
275 e2ba3d07 2019-05-13 stsp if (editor == NULL)
276 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
277 e2ba3d07 2019-05-13 stsp
278 0ee7065d 2019-05-13 stsp if (editor) {
279 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
280 0ee7065d 2019-05-13 stsp if (err)
281 0ee7065d 2019-05-13 stsp return err;
282 0ee7065d 2019-05-13 stsp }
283 e2ba3d07 2019-05-13 stsp
284 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
285 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
286 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
287 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
288 0ee7065d 2019-05-13 stsp }
289 0ee7065d 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp return NULL;
291 e2ba3d07 2019-05-13 stsp }
292 e2ba3d07 2019-05-13 stsp
293 e2ba3d07 2019-05-13 stsp static const struct got_error *
294 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
295 c530dc23 2019-07-23 stsp const char *worktree_path)
296 0266afb7 2019-01-04 stsp {
297 163ce85a 2019-05-13 stsp const struct got_error *err;
298 0266afb7 2019-01-04 stsp
299 37c06ea4 2019-07-15 stsp #ifdef PROFILE
300 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
301 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
302 37c06ea4 2019-07-15 stsp #endif
303 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
305 0266afb7 2019-01-04 stsp
306 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
308 0266afb7 2019-01-04 stsp
309 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
311 0266afb7 2019-01-04 stsp
312 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
313 163ce85a 2019-05-13 stsp if (err != NULL)
314 163ce85a 2019-05-13 stsp return err;
315 0266afb7 2019-01-04 stsp
316 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
318 0266afb7 2019-01-04 stsp
319 0266afb7 2019-01-04 stsp return NULL;
320 2c7829a4 2019-06-17 stsp }
321 2c7829a4 2019-06-17 stsp
322 2c7829a4 2019-06-17 stsp __dead static void
323 2c7829a4 2019-06-17 stsp usage_init(void)
324 2c7829a4 2019-06-17 stsp {
325 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 2c7829a4 2019-06-17 stsp exit(1);
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp
329 2c7829a4 2019-06-17 stsp static const struct got_error *
330 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
331 2c7829a4 2019-06-17 stsp {
332 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
333 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
334 2c7829a4 2019-06-17 stsp int ch;
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
337 2c7829a4 2019-06-17 stsp switch (ch) {
338 2c7829a4 2019-06-17 stsp default:
339 2c7829a4 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp /* NOTREACHED */
341 2c7829a4 2019-06-17 stsp }
342 2c7829a4 2019-06-17 stsp }
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp argc -= optind;
345 2c7829a4 2019-06-17 stsp argv += optind;
346 2c7829a4 2019-06-17 stsp
347 2c7829a4 2019-06-17 stsp #ifndef PROFILE
348 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 2c7829a4 2019-06-17 stsp err(1, "pledge");
350 2c7829a4 2019-06-17 stsp #endif
351 2c7829a4 2019-06-17 stsp if (argc != 1)
352 bc20e173 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
355 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
356 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
361 2c7829a4 2019-06-17 stsp if (error &&
362 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
366 2c7829a4 2019-06-17 stsp if (error)
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
370 3ce1b845 2019-07-15 stsp done:
371 3ce1b845 2019-07-15 stsp free(repo_path);
372 3ce1b845 2019-07-15 stsp return error;
373 3ce1b845 2019-07-15 stsp }
374 3ce1b845 2019-07-15 stsp
375 3ce1b845 2019-07-15 stsp __dead static void
376 3ce1b845 2019-07-15 stsp usage_import(void)
377 3ce1b845 2019-07-15 stsp {
378 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
380 3ce1b845 2019-07-15 stsp exit(1);
381 3ce1b845 2019-07-15 stsp }
382 3ce1b845 2019-07-15 stsp
383 3ce1b845 2019-07-15 stsp int
384 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
385 3ce1b845 2019-07-15 stsp {
386 3ce1b845 2019-07-15 stsp pid_t pid;
387 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
388 3ce1b845 2019-07-15 stsp int st = -1;
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
391 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
392 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
395 3ce1b845 2019-07-15 stsp case -1:
396 3ce1b845 2019-07-15 stsp goto doneediting;
397 3ce1b845 2019-07-15 stsp case 0:
398 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
399 3ce1b845 2019-07-15 stsp _exit(127);
400 3ce1b845 2019-07-15 stsp }
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
403 3ce1b845 2019-07-15 stsp if (errno != EINTR)
404 3ce1b845 2019-07-15 stsp break;
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp doneediting:
407 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
408 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
409 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
412 3ce1b845 2019-07-15 stsp errno = EINTR;
413 3ce1b845 2019-07-15 stsp return -1;
414 3ce1b845 2019-07-15 stsp }
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp static const struct got_error *
420 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 3ce1b845 2019-07-15 stsp const char *initial_content)
422 3ce1b845 2019-07-15 stsp {
423 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
424 3ce1b845 2019-07-15 stsp char buf[1024];
425 3ce1b845 2019-07-15 stsp struct stat st, st2;
426 3ce1b845 2019-07-15 stsp FILE *fp;
427 3ce1b845 2019-07-15 stsp int content_changed = 0;
428 3ce1b845 2019-07-15 stsp size_t len;
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
439 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
446 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
447 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
448 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
449 3ce1b845 2019-07-15 stsp len = 0;
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
452 3ce1b845 2019-07-15 stsp if (fp == NULL) {
453 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
454 3ce1b845 2019-07-15 stsp goto done;
455 3ce1b845 2019-07-15 stsp }
456 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
457 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
458 3ce1b845 2019-07-15 stsp content_changed = 1;
459 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
461 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
462 3ce1b845 2019-07-15 stsp }
463 3ce1b845 2019-07-15 stsp fclose(fp);
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
467 3ce1b845 2019-07-15 stsp len--;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp
470 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
471 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
473 3ce1b845 2019-07-15 stsp done:
474 3ce1b845 2019-07-15 stsp if (err) {
475 3ce1b845 2019-07-15 stsp free(*logmsg);
476 3ce1b845 2019-07-15 stsp *logmsg = NULL;
477 3ce1b845 2019-07-15 stsp }
478 3ce1b845 2019-07-15 stsp return err;
479 3ce1b845 2019-07-15 stsp }
480 3ce1b845 2019-07-15 stsp
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
484 3ce1b845 2019-07-15 stsp {
485 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
486 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
487 3ce1b845 2019-07-15 stsp int fd;
488 3ce1b845 2019-07-15 stsp
489 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
490 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
491 3ce1b845 2019-07-15 stsp branch_name) == -1)
492 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
493 3ce1b845 2019-07-15 stsp
494 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
495 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
496 3ce1b845 2019-07-15 stsp if (err)
497 3ce1b845 2019-07-15 stsp goto done;
498 3ce1b845 2019-07-15 stsp
499 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
500 3ce1b845 2019-07-15 stsp close(fd);
501 3ce1b845 2019-07-15 stsp
502 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 3ce1b845 2019-07-15 stsp done:
504 3ce1b845 2019-07-15 stsp free(initial_content);
505 3ce1b845 2019-07-15 stsp return err;
506 3ce1b845 2019-07-15 stsp }
507 3ce1b845 2019-07-15 stsp
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
510 3ce1b845 2019-07-15 stsp {
511 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
512 3ce1b845 2019-07-15 stsp return NULL;
513 3ce1b845 2019-07-15 stsp }
514 3ce1b845 2019-07-15 stsp
515 3ce1b845 2019-07-15 stsp static const struct got_error *
516 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
517 84792843 2019-08-09 stsp {
518 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
519 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
520 84792843 2019-08-09 stsp
521 84792843 2019-08-09 stsp *author = NULL;
522 aba9c984 2019-09-08 stsp
523 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
528 aba9c984 2019-09-08 stsp return NULL;
529 aba9c984 2019-09-08 stsp }
530 84792843 2019-08-09 stsp
531 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
532 84792843 2019-08-09 stsp if (got_author == NULL) {
533 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
534 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
535 c9956ddf 2019-09-08 stsp if (name && email) {
536 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
537 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
538 c9956ddf 2019-09-08 stsp return NULL;
539 c9956ddf 2019-09-08 stsp }
540 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
541 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
542 84792843 2019-08-09 stsp }
543 84792843 2019-08-09 stsp
544 aba9c984 2019-09-08 stsp *author = strdup(got_author);
545 aba9c984 2019-09-08 stsp if (*author == NULL)
546 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
547 84792843 2019-08-09 stsp
548 84792843 2019-08-09 stsp /*
549 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
550 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
551 84792843 2019-08-09 stsp */
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
553 84792843 2019-08-09 stsp got_author++;
554 aba9c984 2019-09-08 stsp if (*got_author != '<') {
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 aba9c984 2019-09-08 stsp goto done;
557 aba9c984 2019-09-08 stsp }
558 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
559 84792843 2019-08-09 stsp got_author++;
560 aba9c984 2019-09-08 stsp if (*got_author != '@') {
561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 aba9c984 2019-09-08 stsp goto done;
563 aba9c984 2019-09-08 stsp }
564 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
565 84792843 2019-08-09 stsp got_author++;
566 84792843 2019-08-09 stsp if (*got_author != '>')
567 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 aba9c984 2019-09-08 stsp done:
569 aba9c984 2019-09-08 stsp if (err) {
570 aba9c984 2019-09-08 stsp free(*author);
571 aba9c984 2019-09-08 stsp *author = NULL;
572 aba9c984 2019-09-08 stsp }
573 aba9c984 2019-09-08 stsp return err;
574 c9956ddf 2019-09-08 stsp }
575 c9956ddf 2019-09-08 stsp
576 c9956ddf 2019-09-08 stsp static const struct got_error *
577 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
578 c9956ddf 2019-09-08 stsp {
579 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
580 c9956ddf 2019-09-08 stsp
581 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
582 c9956ddf 2019-09-08 stsp if (homedir) {
583 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
585 c9956ddf 2019-09-08 stsp
586 c9956ddf 2019-09-08 stsp }
587 c9956ddf 2019-09-08 stsp return NULL;
588 84792843 2019-08-09 stsp }
589 84792843 2019-08-09 stsp
590 84792843 2019-08-09 stsp static const struct got_error *
591 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
592 3ce1b845 2019-07-15 stsp {
593 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
594 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
597 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
599 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
601 3ce1b845 2019-07-15 stsp int ch;
602 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
603 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
604 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
605 3ce1b845 2019-07-15 stsp
606 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
607 3ce1b845 2019-07-15 stsp
608 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 3ce1b845 2019-07-15 stsp switch (ch) {
610 3ce1b845 2019-07-15 stsp case 'b':
611 3ce1b845 2019-07-15 stsp branch_name = optarg;
612 3ce1b845 2019-07-15 stsp break;
613 3ce1b845 2019-07-15 stsp case 'm':
614 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
615 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
617 3ce1b845 2019-07-15 stsp goto done;
618 3ce1b845 2019-07-15 stsp }
619 3ce1b845 2019-07-15 stsp break;
620 3ce1b845 2019-07-15 stsp case 'r':
621 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
622 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
623 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
624 9ba1d308 2019-10-21 stsp optarg);
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp }
627 3ce1b845 2019-07-15 stsp break;
628 3ce1b845 2019-07-15 stsp case 'I':
629 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
630 3ce1b845 2019-07-15 stsp break;
631 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
632 3ce1b845 2019-07-15 stsp NULL);
633 3ce1b845 2019-07-15 stsp if (error)
634 3ce1b845 2019-07-15 stsp goto done;
635 3ce1b845 2019-07-15 stsp break;
636 3ce1b845 2019-07-15 stsp default:
637 b2b65d18 2019-08-22 stsp usage_import();
638 3ce1b845 2019-07-15 stsp /* NOTREACHED */
639 3ce1b845 2019-07-15 stsp }
640 3ce1b845 2019-07-15 stsp }
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp argc -= optind;
643 3ce1b845 2019-07-15 stsp argv += optind;
644 3ce1b845 2019-07-15 stsp
645 3ce1b845 2019-07-15 stsp #ifndef PROFILE
646 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 aba9c984 2019-09-08 stsp "unveil",
648 3ce1b845 2019-07-15 stsp NULL) == -1)
649 3ce1b845 2019-07-15 stsp err(1, "pledge");
650 3ce1b845 2019-07-15 stsp #endif
651 3ce1b845 2019-07-15 stsp if (argc != 1)
652 3ce1b845 2019-07-15 stsp usage_import();
653 2c7829a4 2019-06-17 stsp
654 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
655 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
657 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
658 3ce1b845 2019-07-15 stsp }
659 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
660 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
661 c9956ddf 2019-09-08 stsp if (error)
662 c9956ddf 2019-09-08 stsp goto done;
663 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
664 3ce1b845 2019-07-15 stsp if (error)
665 3ce1b845 2019-07-15 stsp goto done;
666 aba9c984 2019-09-08 stsp
667 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
668 aba9c984 2019-09-08 stsp if (error)
669 aba9c984 2019-09-08 stsp return error;
670 e560b7e0 2019-11-28 stsp
671 e560b7e0 2019-11-28 stsp /*
672 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
673 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
674 e560b7e0 2019-11-28 stsp * an unintended typo.
675 e560b7e0 2019-11-28 stsp */
676 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
677 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
681 3ce1b845 2019-07-15 stsp goto done;
682 3ce1b845 2019-07-15 stsp }
683 3ce1b845 2019-07-15 stsp
684 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
685 3ce1b845 2019-07-15 stsp if (error) {
686 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp } else {
689 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 3ce1b845 2019-07-15 stsp "import target branch already exists");
691 3ce1b845 2019-07-15 stsp goto done;
692 3ce1b845 2019-07-15 stsp }
693 3ce1b845 2019-07-15 stsp
694 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
695 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
696 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
697 3ce1b845 2019-07-15 stsp goto done;
698 3ce1b845 2019-07-15 stsp }
699 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
700 3ce1b845 2019-07-15 stsp
701 3ce1b845 2019-07-15 stsp /*
702 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
703 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
704 3ce1b845 2019-07-15 stsp */
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
706 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
707 3ce1b845 2019-07-15 stsp if (error)
708 3ce1b845 2019-07-15 stsp goto done;
709 8e158b01 2019-09-22 stsp free(logmsg);
710 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 ef293bdd 2019-10-21 stsp path_dir, refname);
712 ef293bdd 2019-10-21 stsp if (error) {
713 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
715 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
716 3ce1b845 2019-07-15 stsp goto done;
717 ef293bdd 2019-10-21 stsp }
718 3ce1b845 2019-07-15 stsp }
719 3ce1b845 2019-07-15 stsp
720 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
721 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 3ce1b845 2019-07-15 stsp goto done;
725 ef293bdd 2019-10-21 stsp }
726 3ce1b845 2019-07-15 stsp
727 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
731 ef293bdd 2019-10-21 stsp goto done;
732 ef293bdd 2019-10-21 stsp }
733 ef293bdd 2019-10-21 stsp
734 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp goto done;
740 ef293bdd 2019-10-21 stsp }
741 3ce1b845 2019-07-15 stsp
742 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp goto done;
747 ef293bdd 2019-10-21 stsp }
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp goto done;
754 ef293bdd 2019-10-21 stsp }
755 3ce1b845 2019-07-15 stsp
756 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
757 ef293bdd 2019-10-21 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (logmsg_path)
759 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
760 3ce1b845 2019-07-15 stsp goto done;
761 ef293bdd 2019-10-21 stsp }
762 3ce1b845 2019-07-15 stsp
763 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 3ce1b845 2019-07-15 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
766 ef293bdd 2019-10-21 stsp if (logmsg_path)
767 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
768 3ce1b845 2019-07-15 stsp goto done;
769 ef293bdd 2019-10-21 stsp }
770 3ce1b845 2019-07-15 stsp
771 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 3ce1b845 2019-07-15 stsp branch_ref);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp goto done;
777 ef293bdd 2019-10-21 stsp }
778 3ce1b845 2019-07-15 stsp
779 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
780 ef293bdd 2019-10-21 stsp if (error) {
781 ef293bdd 2019-10-21 stsp if (logmsg_path)
782 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp goto done;
784 ef293bdd 2019-10-21 stsp }
785 3ce1b845 2019-07-15 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
788 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
789 2c7829a4 2019-06-17 stsp done:
790 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
791 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
792 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
793 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
795 8e158b01 2019-09-22 stsp free(logmsg);
796 ef293bdd 2019-10-21 stsp free(logmsg_path);
797 2c7829a4 2019-06-17 stsp free(repo_path);
798 3ce1b845 2019-07-15 stsp free(editor);
799 3ce1b845 2019-07-15 stsp free(refname);
800 3ce1b845 2019-07-15 stsp free(new_commit_id);
801 3ce1b845 2019-07-15 stsp free(id_str);
802 aba9c984 2019-09-08 stsp free(author);
803 c9956ddf 2019-09-08 stsp free(gitconfig_path);
804 3ce1b845 2019-07-15 stsp if (branch_ref)
805 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
806 3ce1b845 2019-07-15 stsp if (head_ref)
807 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
808 2c7829a4 2019-06-17 stsp return error;
809 93658fb9 2020-03-18 stsp }
810 93658fb9 2020-03-18 stsp
811 93658fb9 2020-03-18 stsp __dead static void
812 93658fb9 2020-03-18 stsp usage_clone(void)
813 93658fb9 2020-03-18 stsp {
814 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
816 93658fb9 2020-03-18 stsp exit(1);
817 93658fb9 2020-03-18 stsp }
818 892ac3b6 2020-03-18 stsp
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
824 892ac3b6 2020-03-18 stsp };
825 93658fb9 2020-03-18 stsp
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
829 531c3985 2020-03-18 stsp {
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
834 b2409d58 2020-03-18 stsp
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
837 68999b92 2020-03-18 stsp
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
842 b2409d58 2020-03-18 stsp }
843 b2409d58 2020-03-18 stsp
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
852 892ac3b6 2020-03-18 stsp }
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
859 892ac3b6 2020-03-18 stsp }
860 61cc1a7a 2020-03-18 stsp }
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
869 892ac3b6 2020-03-18 stsp }
870 61cc1a7a 2020-03-18 stsp }
871 b2409d58 2020-03-18 stsp
872 d2cdc636 2020-03-18 stsp }
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
883 892ac3b6 2020-03-18 stsp
884 531c3985 2020-03-18 stsp return NULL;
885 531c3985 2020-03-18 stsp }
886 531c3985 2020-03-18 stsp
887 531c3985 2020-03-18 stsp static const struct got_error *
888 30718c93 2020-03-21 stsp create_head_ref(struct got_reference *target_ref, int verbosity,
889 30718c93 2020-03-21 stsp struct got_repository *repo)
890 4ba14133 2020-03-20 stsp {
891 4ba14133 2020-03-20 stsp const struct got_error *err;
892 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
893 4ba14133 2020-03-20 stsp
894 4ba14133 2020-03-20 stsp err = got_ref_alloc_symref(&head_symref, GOT_REF_HEAD, target_ref);
895 4ba14133 2020-03-20 stsp if (err)
896 4ba14133 2020-03-20 stsp return err;
897 4ba14133 2020-03-20 stsp
898 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
899 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
900 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
901 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
903 6338a6a1 2020-03-21 stsp }
904 4ba14133 2020-03-20 stsp return err;
905 4ba14133 2020-03-20 stsp }
906 4ba14133 2020-03-20 stsp
907 4ba14133 2020-03-20 stsp static const struct got_error *
908 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
909 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
910 41b0de12 2020-03-21 stsp {
911 41b0de12 2020-03-21 stsp const struct got_error *err;
912 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
913 41b0de12 2020-03-21 stsp
914 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
915 41b0de12 2020-03-21 stsp const char *refname = pe->path;
916 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
917 41b0de12 2020-03-21 stsp
918 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
919 41b0de12 2020-03-21 stsp }
920 41b0de12 2020-03-21 stsp
921 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
922 41b0de12 2020-03-21 stsp const char *refname = pe->path;
923 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
924 41b0de12 2020-03-21 stsp char *id_str;
925 41b0de12 2020-03-21 stsp
926 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
927 41b0de12 2020-03-21 stsp if (err)
928 41b0de12 2020-03-21 stsp return err;
929 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
930 41b0de12 2020-03-21 stsp free(id_str);
931 41b0de12 2020-03-21 stsp }
932 41b0de12 2020-03-21 stsp
933 41b0de12 2020-03-21 stsp return NULL;
934 6338a6a1 2020-03-21 stsp }
935 6338a6a1 2020-03-21 stsp
936 6338a6a1 2020-03-21 stsp static const struct got_error *
937 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
938 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
939 6338a6a1 2020-03-21 stsp {
940 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
941 6338a6a1 2020-03-21 stsp struct got_reference *ref;
942 6338a6a1 2020-03-21 stsp char *id_str;
943 6338a6a1 2020-03-21 stsp
944 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
945 6338a6a1 2020-03-21 stsp if (err)
946 6338a6a1 2020-03-21 stsp return err;
947 6338a6a1 2020-03-21 stsp
948 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
949 6338a6a1 2020-03-21 stsp if (err)
950 6338a6a1 2020-03-21 stsp goto done;
951 6338a6a1 2020-03-21 stsp
952 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
953 6338a6a1 2020-03-21 stsp got_ref_close(ref);
954 6338a6a1 2020-03-21 stsp
955 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
956 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
957 6338a6a1 2020-03-21 stsp done:
958 6338a6a1 2020-03-21 stsp free(id_str);
959 6338a6a1 2020-03-21 stsp return err;
960 0e4002ca 2020-03-21 stsp }
961 0e4002ca 2020-03-21 stsp
962 0e4002ca 2020-03-21 stsp static int
963 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
964 0e4002ca 2020-03-21 stsp {
965 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
966 0e4002ca 2020-03-21 stsp return 0;
967 0e4002ca 2020-03-21 stsp refname += 5;
968 0e4002ca 2020-03-21 stsp
969 0e4002ca 2020-03-21 stsp /*
970 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
971 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
972 0e4002ca 2020-03-21 stsp */
973 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
974 0e4002ca 2020-03-21 stsp return 0;
975 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
976 0e4002ca 2020-03-21 stsp return 0;
977 0e4002ca 2020-03-21 stsp
978 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
979 0e4002ca 2020-03-21 stsp wanted_ref += 5;
980 0e4002ca 2020-03-21 stsp
981 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
982 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
983 0e4002ca 2020-03-21 stsp return 1;
984 0e4002ca 2020-03-21 stsp
985 0e4002ca 2020-03-21 stsp /* Allow exact match. */
986 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
987 41b0de12 2020-03-21 stsp }
988 41b0de12 2020-03-21 stsp
989 0e4002ca 2020-03-21 stsp static int
990 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
991 0e4002ca 2020-03-21 stsp {
992 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
993 0e4002ca 2020-03-21 stsp
994 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
995 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
996 0e4002ca 2020-03-21 stsp return 1;
997 0e4002ca 2020-03-21 stsp }
998 0e4002ca 2020-03-21 stsp
999 0e4002ca 2020-03-21 stsp return 0;
1000 0e4002ca 2020-03-21 stsp }
1001 0e4002ca 2020-03-21 stsp
1002 41b0de12 2020-03-21 stsp static const struct got_error *
1003 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1004 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1005 0e4002ca 2020-03-21 stsp {
1006 0e4002ca 2020-03-21 stsp const struct got_error *err;
1007 0e4002ca 2020-03-21 stsp char *remote_refname;
1008 0e4002ca 2020-03-21 stsp
1009 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1010 0e4002ca 2020-03-21 stsp refname += 5;
1011 0e4002ca 2020-03-21 stsp
1012 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1014 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1015 0e4002ca 2020-03-21 stsp
1016 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1017 0e4002ca 2020-03-21 stsp free(remote_refname);
1018 0e4002ca 2020-03-21 stsp return err;
1019 0e4002ca 2020-03-21 stsp }
1020 0e4002ca 2020-03-21 stsp
1021 0e4002ca 2020-03-21 stsp static const struct got_error *
1022 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1023 93658fb9 2020-03-18 stsp {
1024 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1025 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1026 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1027 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1028 bb64b798 2020-03-18 stsp const char *repo_path;
1029 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1032 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1033 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1034 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1035 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1036 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1037 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
1038 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
1039 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
1040 b46f3e71 2020-03-18 stsp ssize_t n;
1041 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1043 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1044 93658fb9 2020-03-18 stsp
1045 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1046 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1047 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1048 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1049 d9b4d0c0 2020-03-18 stsp
1050 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1051 93658fb9 2020-03-18 stsp switch (ch) {
1052 659e7fbd 2020-03-20 stsp case 'a':
1053 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1054 4ba14133 2020-03-20 stsp break;
1055 4ba14133 2020-03-20 stsp case 'b':
1056 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1057 4ba14133 2020-03-20 stsp optarg, NULL);
1058 4ba14133 2020-03-20 stsp if (error)
1059 4ba14133 2020-03-20 stsp return error;
1060 659e7fbd 2020-03-20 stsp break;
1061 41b0de12 2020-03-21 stsp case 'l':
1062 41b0de12 2020-03-21 stsp list_refs_only = 1;
1063 41b0de12 2020-03-21 stsp break;
1064 469dd726 2020-03-20 stsp case 'm':
1065 469dd726 2020-03-20 stsp mirror_references = 1;
1066 469dd726 2020-03-20 stsp break;
1067 68999b92 2020-03-18 stsp case 'v':
1068 68999b92 2020-03-18 stsp if (verbosity < 0)
1069 68999b92 2020-03-18 stsp verbosity = 0;
1070 68999b92 2020-03-18 stsp else if (verbosity < 3)
1071 68999b92 2020-03-18 stsp verbosity++;
1072 68999b92 2020-03-18 stsp break;
1073 68999b92 2020-03-18 stsp case 'q':
1074 68999b92 2020-03-18 stsp verbosity = -1;
1075 68999b92 2020-03-18 stsp break;
1076 0e4002ca 2020-03-21 stsp case 'R':
1077 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1078 0e4002ca 2020-03-21 stsp optarg, NULL);
1079 0e4002ca 2020-03-21 stsp if (error)
1080 0e4002ca 2020-03-21 stsp return error;
1081 0e4002ca 2020-03-21 stsp break;
1082 93658fb9 2020-03-18 stsp default:
1083 93658fb9 2020-03-18 stsp usage_clone();
1084 93658fb9 2020-03-18 stsp break;
1085 93658fb9 2020-03-18 stsp }
1086 93658fb9 2020-03-18 stsp }
1087 93658fb9 2020-03-18 stsp argc -= optind;
1088 93658fb9 2020-03-18 stsp argv += optind;
1089 39c64a6a 2020-03-18 stsp
1090 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1092 41b0de12 2020-03-21 stsp if (list_refs_only) {
1093 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1094 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1095 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1096 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1097 41b0de12 2020-03-21 stsp if (mirror_references)
1098 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1099 41b0de12 2020-03-21 stsp if (verbosity == -1)
1100 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1101 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1102 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1103 41b0de12 2020-03-21 stsp }
1104 4ba14133 2020-03-20 stsp
1105 93658fb9 2020-03-18 stsp uri = argv[0];
1106 9df6f38b 2020-03-18 stsp
1107 9df6f38b 2020-03-18 stsp if (argc == 1)
1108 93658fb9 2020-03-18 stsp dirname = NULL;
1109 9df6f38b 2020-03-18 stsp else if (argc == 2)
1110 93658fb9 2020-03-18 stsp dirname = argv[1];
1111 93658fb9 2020-03-18 stsp else
1112 93658fb9 2020-03-18 stsp usage_clone();
1113 09838ffc 2020-03-18 stsp
1114 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1116 39c64a6a 2020-03-18 stsp if (error)
1117 09f63084 2020-03-20 stsp goto done;
1118 09f63084 2020-03-20 stsp
1119 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1121 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1123 09838ffc 2020-03-18 stsp goto done;
1124 09f63084 2020-03-20 stsp }
1125 09838ffc 2020-03-18 stsp
1126 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1127 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1128 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1130 39c64a6a 2020-03-18 stsp err(1, "pledge");
1131 b46f3e71 2020-03-18 stsp #endif
1132 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1133 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1134 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1135 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1137 39c64a6a 2020-03-18 stsp err(1, "pledge");
1138 b46f3e71 2020-03-18 stsp #endif
1139 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1140 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1141 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1142 39c64a6a 2020-03-18 stsp goto done;
1143 39c64a6a 2020-03-18 stsp } else {
1144 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1145 39c64a6a 2020-03-18 stsp goto done;
1146 39c64a6a 2020-03-18 stsp }
1147 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1148 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1150 bb64b798 2020-03-18 stsp goto done;
1151 bb64b798 2020-03-18 stsp }
1152 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1153 bb64b798 2020-03-18 stsp } else
1154 bb64b798 2020-03-18 stsp repo_path = dirname;
1155 bb64b798 2020-03-18 stsp
1156 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1157 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1158 41b0de12 2020-03-21 stsp if (error)
1159 41b0de12 2020-03-21 stsp goto done;
1160 bb64b798 2020-03-18 stsp
1161 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1162 41b0de12 2020-03-21 stsp if (error)
1163 41b0de12 2020-03-21 stsp goto done;
1164 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1165 41b0de12 2020-03-21 stsp if (error)
1166 41b0de12 2020-03-21 stsp goto done;
1167 41b0de12 2020-03-21 stsp }
1168 bb64b798 2020-03-18 stsp
1169 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1172 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1173 ee448f5f 2020-03-18 stsp goto done;
1174 ee448f5f 2020-03-18 stsp }
1175 ee448f5f 2020-03-18 stsp }
1176 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1177 ee448f5f 2020-03-18 stsp if (error)
1178 ee448f5f 2020-03-18 stsp goto done;
1179 ee448f5f 2020-03-18 stsp
1180 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1181 9c52365f 2020-03-21 stsp server_path, verbosity);
1182 39c64a6a 2020-03-18 stsp if (error)
1183 bb64b798 2020-03-18 stsp goto done;
1184 294dfefd 2020-03-18 stsp
1185 68999b92 2020-03-18 stsp if (verbosity >= 0)
1186 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1187 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1188 bb64b798 2020-03-18 stsp
1189 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1190 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1191 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1192 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1193 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1196 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1197 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1198 39c64a6a 2020-03-18 stsp if (error)
1199 d9b4d0c0 2020-03-18 stsp goto done;
1200 d9b4d0c0 2020-03-18 stsp
1201 41b0de12 2020-03-21 stsp if (list_refs_only) {
1202 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1203 41b0de12 2020-03-21 stsp goto done;
1204 41b0de12 2020-03-21 stsp }
1205 41b0de12 2020-03-21 stsp
1206 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1207 39c64a6a 2020-03-18 stsp if (error)
1208 d9b4d0c0 2020-03-18 stsp goto done;
1209 68999b92 2020-03-18 stsp if (verbosity >= 0)
1210 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1211 d9b4d0c0 2020-03-18 stsp free(id_str);
1212 d9b4d0c0 2020-03-18 stsp
1213 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1214 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1215 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1216 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1217 7ebc0570 2020-03-18 stsp char *remote_refname;
1218 0e4002ca 2020-03-21 stsp
1219 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1220 0e4002ca 2020-03-21 stsp !mirror_references) {
1221 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1222 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1224 0e4002ca 2020-03-21 stsp if (error)
1225 0e4002ca 2020-03-21 stsp goto done;
1226 0e4002ca 2020-03-21 stsp continue;
1227 0e4002ca 2020-03-21 stsp }
1228 668a20f6 2020-03-18 stsp
1229 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1230 39c64a6a 2020-03-18 stsp if (error)
1231 d9b4d0c0 2020-03-18 stsp goto done;
1232 d9b4d0c0 2020-03-18 stsp
1233 469dd726 2020-03-20 stsp if (mirror_references)
1234 469dd726 2020-03-20 stsp continue;
1235 469dd726 2020-03-20 stsp
1236 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1237 7ebc0570 2020-03-18 stsp continue;
1238 7ebc0570 2020-03-18 stsp
1239 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1240 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1242 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1243 7ebc0570 2020-03-18 stsp goto done;
1244 7ebc0570 2020-03-18 stsp }
1245 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 39c64a6a 2020-03-18 stsp if (error)
1247 d9b4d0c0 2020-03-18 stsp goto done;
1248 d9b4d0c0 2020-03-18 stsp }
1249 d9b4d0c0 2020-03-18 stsp
1250 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1251 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1252 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1253 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1254 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1255 d9b4d0c0 2020-03-18 stsp
1256 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1257 d9b4d0c0 2020-03-18 stsp continue;
1258 d9b4d0c0 2020-03-18 stsp
1259 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1260 39c64a6a 2020-03-18 stsp if (error) {
1261 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1262 55330abe 2020-03-20 stsp error = NULL;
1263 d9b4d0c0 2020-03-18 stsp continue;
1264 55330abe 2020-03-20 stsp }
1265 d9b4d0c0 2020-03-18 stsp goto done;
1266 d9b4d0c0 2020-03-18 stsp }
1267 d9b4d0c0 2020-03-18 stsp
1268 30718c93 2020-03-21 stsp error = create_head_ref(target_ref, verbosity, repo);
1269 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1270 39c64a6a 2020-03-18 stsp if (error)
1271 d9b4d0c0 2020-03-18 stsp goto done;
1272 4ba14133 2020-03-20 stsp }
1273 4ba14133 2020-03-20 stsp if (pe == NULL) {
1274 4ba14133 2020-03-20 stsp /*
1275 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1276 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1277 4ba14133 2020-03-20 stsp * which could be fetched instead.
1278 4ba14133 2020-03-20 stsp */
1279 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1280 4ba14133 2020-03-20 stsp const char *target = pe->path;
1281 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1282 d9b4d0c0 2020-03-18 stsp
1283 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1284 4ba14133 2020-03-20 stsp if (error) {
1285 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1286 4ba14133 2020-03-20 stsp error = NULL;
1287 4ba14133 2020-03-20 stsp continue;
1288 4ba14133 2020-03-20 stsp }
1289 4ba14133 2020-03-20 stsp goto done;
1290 4ba14133 2020-03-20 stsp }
1291 4ba14133 2020-03-20 stsp
1292 30718c93 2020-03-21 stsp error = create_head_ref(target_ref, verbosity, repo);
1293 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1294 4ba14133 2020-03-20 stsp if (error)
1295 4ba14133 2020-03-20 stsp goto done;
1296 4ba14133 2020-03-20 stsp break;
1297 4ba14133 2020-03-20 stsp }
1298 659e7fbd 2020-03-20 stsp }
1299 659e7fbd 2020-03-20 stsp
1300 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1301 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1302 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1303 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1304 659e7fbd 2020-03-20 stsp goto done;
1305 659e7fbd 2020-03-20 stsp }
1306 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1307 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1308 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1309 659e7fbd 2020-03-20 stsp goto done;
1310 b46f3e71 2020-03-18 stsp }
1311 659e7fbd 2020-03-20 stsp if (mirror_references) {
1312 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1313 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1314 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1315 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1316 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1317 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1318 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1319 659e7fbd 2020-03-20 stsp goto done;
1320 659e7fbd 2020-03-20 stsp }
1321 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1322 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1323 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1324 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1325 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1326 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1327 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1328 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1329 659e7fbd 2020-03-20 stsp goto done;
1330 659e7fbd 2020-03-20 stsp }
1331 659e7fbd 2020-03-20 stsp } else {
1332 659e7fbd 2020-03-20 stsp const char *branchname;
1333 d715f13e 2020-03-19 stsp
1334 659e7fbd 2020-03-20 stsp /*
1335 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1336 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1337 659e7fbd 2020-03-20 stsp */
1338 659e7fbd 2020-03-20 stsp if (head_symref) {
1339 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1340 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1341 659e7fbd 2020-03-20 stsp branchname += 11;
1342 659e7fbd 2020-03-20 stsp } else
1343 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1344 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1345 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1346 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1347 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1348 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1349 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1350 659e7fbd 2020-03-20 stsp branchname) == -1) {
1351 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1352 659e7fbd 2020-03-20 stsp goto done;
1353 659e7fbd 2020-03-20 stsp }
1354 659e7fbd 2020-03-20 stsp }
1355 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1356 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1357 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1358 659e7fbd 2020-03-20 stsp goto done;
1359 659e7fbd 2020-03-20 stsp }
1360 659e7fbd 2020-03-20 stsp
1361 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1362 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1363 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1364 09838ffc 2020-03-18 stsp done:
1365 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1366 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1367 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1368 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1369 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1370 9c52365f 2020-03-21 stsp }
1371 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1372 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1373 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1374 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1375 bb64b798 2020-03-18 stsp if (repo)
1376 bb64b798 2020-03-18 stsp got_repo_close(repo);
1377 659e7fbd 2020-03-20 stsp if (head_symref)
1378 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1379 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1380 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1381 d9b4d0c0 2020-03-18 stsp free(pe->data);
1382 d9b4d0c0 2020-03-18 stsp }
1383 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1384 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1385 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1386 d9b4d0c0 2020-03-18 stsp free(pe->data);
1387 d9b4d0c0 2020-03-18 stsp }
1388 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1389 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1390 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1391 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1392 09838ffc 2020-03-18 stsp free(proto);
1393 09838ffc 2020-03-18 stsp free(host);
1394 09838ffc 2020-03-18 stsp free(port);
1395 09838ffc 2020-03-18 stsp free(server_path);
1396 09838ffc 2020-03-18 stsp free(repo_name);
1397 bb64b798 2020-03-18 stsp free(default_destdir);
1398 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1399 b46f3e71 2020-03-18 stsp free(git_url);
1400 39c64a6a 2020-03-18 stsp return error;
1401 7848a0e1 2020-03-19 stsp }
1402 7848a0e1 2020-03-19 stsp
1403 7848a0e1 2020-03-19 stsp static const struct got_error *
1404 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1405 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1406 7848a0e1 2020-03-19 stsp {
1407 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1408 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1409 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1410 7848a0e1 2020-03-19 stsp
1411 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1412 7848a0e1 2020-03-19 stsp if (err)
1413 7848a0e1 2020-03-19 stsp goto done;
1414 7848a0e1 2020-03-19 stsp
1415 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1416 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1417 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1418 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1419 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1420 db6d8ad8 2020-03-21 stsp }
1421 db6d8ad8 2020-03-21 stsp goto done;
1422 db6d8ad8 2020-03-21 stsp }
1423 db6d8ad8 2020-03-21 stsp
1424 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1425 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1426 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1427 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1428 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1429 688f11b3 2020-03-21 stsp }
1430 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1431 7848a0e1 2020-03-19 stsp if (err)
1432 7848a0e1 2020-03-19 stsp goto done;
1433 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1434 e8a967e0 2020-03-21 stsp if (err)
1435 e8a967e0 2020-03-21 stsp goto done;
1436 7848a0e1 2020-03-19 stsp } else {
1437 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1438 7848a0e1 2020-03-19 stsp if (err)
1439 7848a0e1 2020-03-19 stsp goto done;
1440 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1441 6338a6a1 2020-03-21 stsp goto done;
1442 6338a6a1 2020-03-21 stsp
1443 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1444 6338a6a1 2020-03-21 stsp if (err)
1445 6338a6a1 2020-03-21 stsp goto done;
1446 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1447 6338a6a1 2020-03-21 stsp if (err)
1448 6338a6a1 2020-03-21 stsp goto done;
1449 7848a0e1 2020-03-19 stsp }
1450 6338a6a1 2020-03-21 stsp
1451 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1452 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1453 6338a6a1 2020-03-21 stsp new_id_str);
1454 7848a0e1 2020-03-19 stsp done:
1455 7848a0e1 2020-03-19 stsp free(old_id);
1456 7848a0e1 2020-03-19 stsp free(new_id_str);
1457 7848a0e1 2020-03-19 stsp return err;
1458 2ab43947 2020-03-18 stsp }
1459 2ab43947 2020-03-18 stsp
1460 2ab43947 2020-03-18 stsp __dead static void
1461 7848a0e1 2020-03-19 stsp usage_fetch(void)
1462 7848a0e1 2020-03-19 stsp {
1463 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1464 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1465 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1466 13f12b09 2020-03-21 stsp getprogname());
1467 7848a0e1 2020-03-19 stsp exit(1);
1468 7848a0e1 2020-03-19 stsp }
1469 7848a0e1 2020-03-19 stsp
1470 7848a0e1 2020-03-19 stsp static const struct got_error *
1471 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1472 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1473 f21ec2f0 2020-03-21 stsp {
1474 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1475 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1476 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1477 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1478 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1479 f21ec2f0 2020-03-21 stsp char *id_str;
1480 f21ec2f0 2020-03-21 stsp
1481 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1482 f21ec2f0 2020-03-21 stsp
1483 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1484 f21ec2f0 2020-03-21 stsp if (err)
1485 f21ec2f0 2020-03-21 stsp return err;
1486 f21ec2f0 2020-03-21 stsp
1487 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1488 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1489 f21ec2f0 2020-03-21 stsp
1490 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1491 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1492 f21ec2f0 2020-03-21 stsp continue;
1493 f21ec2f0 2020-03-21 stsp
1494 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1495 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1496 f21ec2f0 2020-03-21 stsp break;
1497 f21ec2f0 2020-03-21 stsp }
1498 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1499 f21ec2f0 2020-03-21 stsp continue;
1500 f21ec2f0 2020-03-21 stsp
1501 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1502 f21ec2f0 2020-03-21 stsp if (err)
1503 f21ec2f0 2020-03-21 stsp break;
1504 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1505 f21ec2f0 2020-03-21 stsp free(id);
1506 f21ec2f0 2020-03-21 stsp if (err)
1507 f21ec2f0 2020-03-21 stsp break;
1508 f21ec2f0 2020-03-21 stsp
1509 f21ec2f0 2020-03-21 stsp free(id_str);
1510 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1511 f21ec2f0 2020-03-21 stsp if (err)
1512 f21ec2f0 2020-03-21 stsp break;
1513 6338a6a1 2020-03-21 stsp if (verbosity >= 0) {
1514 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1515 6338a6a1 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1516 6338a6a1 2020-03-21 stsp }
1517 f21ec2f0 2020-03-21 stsp }
1518 0e4002ca 2020-03-21 stsp
1519 0e4002ca 2020-03-21 stsp return err;
1520 0e4002ca 2020-03-21 stsp }
1521 0e4002ca 2020-03-21 stsp
1522 0e4002ca 2020-03-21 stsp static const struct got_error *
1523 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1524 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1525 0e4002ca 2020-03-21 stsp {
1526 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1527 0e4002ca 2020-03-21 stsp char *remote_refname;
1528 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1529 0e4002ca 2020-03-21 stsp
1530 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1531 0e4002ca 2020-03-21 stsp refname += 5;
1532 0e4002ca 2020-03-21 stsp
1533 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1534 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1535 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1536 f21ec2f0 2020-03-21 stsp
1537 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1538 0e4002ca 2020-03-21 stsp if (err) {
1539 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1540 0e4002ca 2020-03-21 stsp goto done;
1541 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1542 0e4002ca 2020-03-21 stsp } else {
1543 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1544 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1545 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1546 9f142382 2020-03-21 stsp err = unlock_err;
1547 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1548 0e4002ca 2020-03-21 stsp }
1549 0e4002ca 2020-03-21 stsp done:
1550 0e4002ca 2020-03-21 stsp free(remote_refname);
1551 f21ec2f0 2020-03-21 stsp return err;
1552 f21ec2f0 2020-03-21 stsp }
1553 f21ec2f0 2020-03-21 stsp
1554 f21ec2f0 2020-03-21 stsp static const struct got_error *
1555 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1556 7848a0e1 2020-03-19 stsp {
1557 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1558 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1559 7848a0e1 2020-03-19 stsp const char *remote_name;
1560 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1561 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1562 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1563 7848a0e1 2020-03-19 stsp int nremotes;
1564 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1565 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1566 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1567 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1568 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1569 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1570 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1571 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1572 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1573 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1574 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1575 7848a0e1 2020-03-19 stsp
1576 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1577 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1578 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1579 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1580 7848a0e1 2020-03-19 stsp
1581 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1582 7848a0e1 2020-03-19 stsp switch (ch) {
1583 659e7fbd 2020-03-20 stsp case 'a':
1584 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1585 4ba14133 2020-03-20 stsp break;
1586 4ba14133 2020-03-20 stsp case 'b':
1587 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1588 4ba14133 2020-03-20 stsp optarg, NULL);
1589 4ba14133 2020-03-20 stsp if (error)
1590 4ba14133 2020-03-20 stsp return error;
1591 41b0de12 2020-03-21 stsp break;
1592 f21ec2f0 2020-03-21 stsp case 'd':
1593 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1594 f21ec2f0 2020-03-21 stsp break;
1595 41b0de12 2020-03-21 stsp case 'l':
1596 41b0de12 2020-03-21 stsp list_refs_only = 1;
1597 659e7fbd 2020-03-20 stsp break;
1598 7848a0e1 2020-03-19 stsp case 'r':
1599 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1600 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1601 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1602 7848a0e1 2020-03-19 stsp optarg);
1603 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1604 7848a0e1 2020-03-19 stsp break;
1605 db6d8ad8 2020-03-21 stsp case 't':
1606 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1607 db6d8ad8 2020-03-21 stsp break;
1608 7848a0e1 2020-03-19 stsp case 'v':
1609 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1610 7848a0e1 2020-03-19 stsp verbosity = 0;
1611 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1612 7848a0e1 2020-03-19 stsp verbosity++;
1613 7848a0e1 2020-03-19 stsp break;
1614 7848a0e1 2020-03-19 stsp case 'q':
1615 7848a0e1 2020-03-19 stsp verbosity = -1;
1616 7848a0e1 2020-03-19 stsp break;
1617 0e4002ca 2020-03-21 stsp case 'R':
1618 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1619 0e4002ca 2020-03-21 stsp optarg, NULL);
1620 0e4002ca 2020-03-21 stsp if (error)
1621 0e4002ca 2020-03-21 stsp return error;
1622 0e4002ca 2020-03-21 stsp break;
1623 7848a0e1 2020-03-19 stsp default:
1624 7848a0e1 2020-03-19 stsp usage_fetch();
1625 7848a0e1 2020-03-19 stsp break;
1626 7848a0e1 2020-03-19 stsp }
1627 7848a0e1 2020-03-19 stsp }
1628 7848a0e1 2020-03-19 stsp argc -= optind;
1629 7848a0e1 2020-03-19 stsp argv += optind;
1630 7848a0e1 2020-03-19 stsp
1631 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1632 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1633 41b0de12 2020-03-21 stsp if (list_refs_only) {
1634 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1635 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1636 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1637 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1638 f21ec2f0 2020-03-21 stsp if (delete_refs)
1639 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1640 41b0de12 2020-03-21 stsp if (verbosity == -1)
1641 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1642 41b0de12 2020-03-21 stsp }
1643 4ba14133 2020-03-20 stsp
1644 7848a0e1 2020-03-19 stsp if (argc == 0)
1645 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1646 7848a0e1 2020-03-19 stsp else if (argc == 1)
1647 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1648 7848a0e1 2020-03-19 stsp else
1649 7848a0e1 2020-03-19 stsp usage_fetch();
1650 7848a0e1 2020-03-19 stsp
1651 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1652 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1653 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1654 7848a0e1 2020-03-19 stsp goto done;
1655 7848a0e1 2020-03-19 stsp }
1656 7848a0e1 2020-03-19 stsp
1657 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1658 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1659 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1660 7848a0e1 2020-03-19 stsp goto done;
1661 7848a0e1 2020-03-19 stsp else
1662 7848a0e1 2020-03-19 stsp error = NULL;
1663 7848a0e1 2020-03-19 stsp if (worktree) {
1664 7848a0e1 2020-03-19 stsp repo_path =
1665 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1666 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1667 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1668 7848a0e1 2020-03-19 stsp if (error)
1669 7848a0e1 2020-03-19 stsp goto done;
1670 7848a0e1 2020-03-19 stsp } else {
1671 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1672 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1673 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1674 7848a0e1 2020-03-19 stsp goto done;
1675 7848a0e1 2020-03-19 stsp }
1676 7848a0e1 2020-03-19 stsp }
1677 7848a0e1 2020-03-19 stsp }
1678 7848a0e1 2020-03-19 stsp
1679 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1680 7848a0e1 2020-03-19 stsp if (error)
1681 7848a0e1 2020-03-19 stsp goto done;
1682 7848a0e1 2020-03-19 stsp
1683 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1684 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1685 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1686 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1687 7848a0e1 2020-03-19 stsp break;
1688 7848a0e1 2020-03-19 stsp }
1689 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1690 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1691 7848a0e1 2020-03-19 stsp goto done;
1692 7848a0e1 2020-03-19 stsp }
1693 7848a0e1 2020-03-19 stsp
1694 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1695 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1696 7848a0e1 2020-03-19 stsp if (error)
1697 7848a0e1 2020-03-19 stsp goto done;
1698 7848a0e1 2020-03-19 stsp
1699 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1700 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1701 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1702 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1703 7848a0e1 2020-03-19 stsp err(1, "pledge");
1704 7848a0e1 2020-03-19 stsp #endif
1705 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1706 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1707 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1708 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1709 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1710 7848a0e1 2020-03-19 stsp err(1, "pledge");
1711 7848a0e1 2020-03-19 stsp #endif
1712 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1713 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1714 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1715 7848a0e1 2020-03-19 stsp goto done;
1716 7848a0e1 2020-03-19 stsp } else {
1717 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1718 7848a0e1 2020-03-19 stsp goto done;
1719 7848a0e1 2020-03-19 stsp }
1720 7848a0e1 2020-03-19 stsp
1721 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1722 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1723 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1724 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1725 7848a0e1 2020-03-19 stsp goto done;
1726 7848a0e1 2020-03-19 stsp }
1727 7848a0e1 2020-03-19 stsp }
1728 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1729 7848a0e1 2020-03-19 stsp if (error)
1730 7848a0e1 2020-03-19 stsp goto done;
1731 7848a0e1 2020-03-19 stsp
1732 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1733 9c52365f 2020-03-21 stsp server_path, verbosity);
1734 7848a0e1 2020-03-19 stsp if (error)
1735 7848a0e1 2020-03-19 stsp goto done;
1736 7848a0e1 2020-03-19 stsp
1737 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1738 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1739 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1740 7848a0e1 2020-03-19 stsp
1741 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1742 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1743 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1744 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1745 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1746 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1747 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1748 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1749 7848a0e1 2020-03-19 stsp if (error)
1750 7848a0e1 2020-03-19 stsp goto done;
1751 7848a0e1 2020-03-19 stsp
1752 41b0de12 2020-03-21 stsp if (list_refs_only) {
1753 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1754 41b0de12 2020-03-21 stsp goto done;
1755 41b0de12 2020-03-21 stsp }
1756 41b0de12 2020-03-21 stsp
1757 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1758 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1759 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1760 f21ec2f0 2020-03-21 stsp if (delete_refs)
1761 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1762 7848a0e1 2020-03-19 stsp goto done;
1763 7848a0e1 2020-03-19 stsp }
1764 7848a0e1 2020-03-19 stsp
1765 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1766 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1767 984065c8 2020-03-19 stsp if (error)
1768 984065c8 2020-03-19 stsp goto done;
1769 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1770 984065c8 2020-03-19 stsp free(id_str);
1771 984065c8 2020-03-19 stsp id_str = NULL;
1772 984065c8 2020-03-19 stsp }
1773 7848a0e1 2020-03-19 stsp
1774 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1775 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1776 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1777 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1778 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1779 7848a0e1 2020-03-19 stsp char *remote_refname;
1780 7848a0e1 2020-03-19 stsp
1781 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1782 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1783 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1784 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1785 0e4002ca 2020-03-21 stsp if (error)
1786 0e4002ca 2020-03-21 stsp goto done;
1787 0e4002ca 2020-03-21 stsp continue;
1788 0e4002ca 2020-03-21 stsp }
1789 0e4002ca 2020-03-21 stsp
1790 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1791 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1792 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1793 7848a0e1 2020-03-19 stsp if (error) {
1794 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1795 7848a0e1 2020-03-19 stsp goto done;
1796 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1797 6338a6a1 2020-03-21 stsp repo);
1798 7848a0e1 2020-03-19 stsp if (error)
1799 7848a0e1 2020-03-19 stsp goto done;
1800 7848a0e1 2020-03-19 stsp } else {
1801 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1802 db6d8ad8 2020-03-21 stsp verbosity, repo);
1803 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1804 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1805 9f142382 2020-03-21 stsp error = unlock_err;
1806 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1807 7848a0e1 2020-03-19 stsp if (error)
1808 7848a0e1 2020-03-19 stsp goto done;
1809 7848a0e1 2020-03-19 stsp }
1810 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1811 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1812 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1813 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1814 7848a0e1 2020-03-19 stsp goto done;
1815 7848a0e1 2020-03-19 stsp }
1816 7848a0e1 2020-03-19 stsp
1817 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1818 7848a0e1 2020-03-19 stsp if (error) {
1819 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1820 7848a0e1 2020-03-19 stsp goto done;
1821 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1822 688f11b3 2020-03-21 stsp verbosity, repo);
1823 7848a0e1 2020-03-19 stsp if (error)
1824 7848a0e1 2020-03-19 stsp goto done;
1825 7848a0e1 2020-03-19 stsp } else {
1826 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1827 db6d8ad8 2020-03-21 stsp verbosity, repo);
1828 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1829 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1830 9f142382 2020-03-21 stsp error = unlock_err;
1831 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1832 7848a0e1 2020-03-19 stsp if (error)
1833 7848a0e1 2020-03-19 stsp goto done;
1834 7848a0e1 2020-03-19 stsp }
1835 2ec30c80 2020-03-20 stsp
1836 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1837 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1838 2ec30c80 2020-03-20 stsp if (error) {
1839 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1840 2ec30c80 2020-03-20 stsp goto done;
1841 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1842 6338a6a1 2020-03-21 stsp repo);
1843 2ec30c80 2020-03-20 stsp if (error)
1844 2ec30c80 2020-03-20 stsp goto done;
1845 9f142382 2020-03-21 stsp } else {
1846 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1847 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1848 9f142382 2020-03-21 stsp error = unlock_err;
1849 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1850 9f142382 2020-03-21 stsp }
1851 7848a0e1 2020-03-19 stsp }
1852 7848a0e1 2020-03-19 stsp }
1853 f21ec2f0 2020-03-21 stsp if (delete_refs)
1854 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1855 7848a0e1 2020-03-19 stsp done:
1856 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1857 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1858 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1859 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1860 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1861 9c52365f 2020-03-21 stsp }
1862 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1863 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1864 7848a0e1 2020-03-19 stsp if (repo)
1865 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1866 7848a0e1 2020-03-19 stsp if (worktree)
1867 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1868 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1869 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1870 7848a0e1 2020-03-19 stsp free(pe->data);
1871 7848a0e1 2020-03-19 stsp }
1872 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1873 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1874 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1875 7848a0e1 2020-03-19 stsp free(pe->data);
1876 7848a0e1 2020-03-19 stsp }
1877 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1878 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1879 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1880 7848a0e1 2020-03-19 stsp free(id_str);
1881 7848a0e1 2020-03-19 stsp free(cwd);
1882 7848a0e1 2020-03-19 stsp free(repo_path);
1883 7848a0e1 2020-03-19 stsp free(pack_hash);
1884 7848a0e1 2020-03-19 stsp free(proto);
1885 7848a0e1 2020-03-19 stsp free(host);
1886 7848a0e1 2020-03-19 stsp free(port);
1887 7848a0e1 2020-03-19 stsp free(server_path);
1888 7848a0e1 2020-03-19 stsp free(repo_name);
1889 7848a0e1 2020-03-19 stsp return error;
1890 7848a0e1 2020-03-19 stsp }
1891 7848a0e1 2020-03-19 stsp
1892 7848a0e1 2020-03-19 stsp
1893 7848a0e1 2020-03-19 stsp __dead static void
1894 2ab43947 2020-03-18 stsp usage_checkout(void)
1895 2ab43947 2020-03-18 stsp {
1896 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1897 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1898 2ab43947 2020-03-18 stsp exit(1);
1899 2ab43947 2020-03-18 stsp }
1900 2ab43947 2020-03-18 stsp
1901 2ab43947 2020-03-18 stsp static void
1902 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1903 2ab43947 2020-03-18 stsp {
1904 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1905 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1906 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1907 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1908 2ab43947 2020-03-18 stsp getprogname());
1909 2ab43947 2020-03-18 stsp }
1910 2ab43947 2020-03-18 stsp
1911 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1912 2ab43947 2020-03-18 stsp const char *worktree_path;
1913 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1914 2ab43947 2020-03-18 stsp };
1915 2ab43947 2020-03-18 stsp
1916 2ab43947 2020-03-18 stsp static const struct got_error *
1917 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1918 2ab43947 2020-03-18 stsp {
1919 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1920 2ab43947 2020-03-18 stsp
1921 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1922 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1923 2ab43947 2020-03-18 stsp return NULL;
1924 2ab43947 2020-03-18 stsp
1925 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1926 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1927 2ab43947 2020-03-18 stsp return NULL;
1928 2ab43947 2020-03-18 stsp }
1929 2ab43947 2020-03-18 stsp
1930 2ab43947 2020-03-18 stsp while (path[0] == '/')
1931 2ab43947 2020-03-18 stsp path++;
1932 2ab43947 2020-03-18 stsp
1933 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1934 2ab43947 2020-03-18 stsp return NULL;
1935 2ab43947 2020-03-18 stsp }
1936 2ab43947 2020-03-18 stsp
1937 2ab43947 2020-03-18 stsp static const struct got_error *
1938 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1939 2ab43947 2020-03-18 stsp {
1940 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1941 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1942 2ab43947 2020-03-18 stsp return NULL;
1943 2ab43947 2020-03-18 stsp }
1944 2ab43947 2020-03-18 stsp
1945 2ab43947 2020-03-18 stsp static const struct got_error *
1946 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1947 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1948 2ab43947 2020-03-18 stsp struct got_repository *repo)
1949 2ab43947 2020-03-18 stsp {
1950 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1951 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1952 2ab43947 2020-03-18 stsp
1953 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1954 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1955 2ab43947 2020-03-18 stsp if (err)
1956 2ab43947 2020-03-18 stsp return err;
1957 2ab43947 2020-03-18 stsp
1958 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1959 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1960 2ab43947 2020-03-18 stsp
1961 2ab43947 2020-03-18 stsp /*
1962 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1963 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1964 2ab43947 2020-03-18 stsp *
1965 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1966 2ab43947 2020-03-18 stsp *
1967 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1968 2ab43947 2020-03-18 stsp * \ /
1969 2ab43947 2020-03-18 stsp * C E
1970 2ab43947 2020-03-18 stsp * \ /
1971 2ab43947 2020-03-18 stsp * B (yca)
1972 2ab43947 2020-03-18 stsp * |
1973 2ab43947 2020-03-18 stsp * A
1974 2ab43947 2020-03-18 stsp *
1975 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1976 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1977 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1978 2ab43947 2020-03-18 stsp */
1979 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1980 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1981 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1982 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1983 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1984 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1985 2ab43947 2020-03-18 stsp
1986 2ab43947 2020-03-18 stsp free(yca_id);
1987 2ab43947 2020-03-18 stsp return NULL;
1988 2ab43947 2020-03-18 stsp }
1989 2ab43947 2020-03-18 stsp
1990 2ab43947 2020-03-18 stsp static const struct got_error *
1991 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1992 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1993 2ab43947 2020-03-18 stsp struct got_repository *repo)
1994 2ab43947 2020-03-18 stsp {
1995 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1996 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1997 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1998 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1999 2ab43947 2020-03-18 stsp
2000 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2001 2ab43947 2020-03-18 stsp if (err)
2002 2ab43947 2020-03-18 stsp goto done;
2003 2ab43947 2020-03-18 stsp
2004 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2005 2ab43947 2020-03-18 stsp is_same_branch = 1;
2006 2ab43947 2020-03-18 stsp goto done;
2007 2ab43947 2020-03-18 stsp }
2008 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2009 2ab43947 2020-03-18 stsp is_same_branch = 1;
2010 2ab43947 2020-03-18 stsp goto done;
2011 2ab43947 2020-03-18 stsp }
2012 2ab43947 2020-03-18 stsp
2013 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2014 2ab43947 2020-03-18 stsp if (err)
2015 2ab43947 2020-03-18 stsp goto done;
2016 2ab43947 2020-03-18 stsp
2017 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2018 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2019 2ab43947 2020-03-18 stsp if (err)
2020 2ab43947 2020-03-18 stsp goto done;
2021 2ab43947 2020-03-18 stsp
2022 2ab43947 2020-03-18 stsp for (;;) {
2023 2ab43947 2020-03-18 stsp struct got_object_id *id;
2024 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2025 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2026 2ab43947 2020-03-18 stsp if (err) {
2027 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2028 2ab43947 2020-03-18 stsp err = NULL;
2029 2ab43947 2020-03-18 stsp break;
2030 2ab43947 2020-03-18 stsp }
2031 2ab43947 2020-03-18 stsp
2032 2ab43947 2020-03-18 stsp if (id) {
2033 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2034 2ab43947 2020-03-18 stsp break;
2035 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2036 2ab43947 2020-03-18 stsp is_same_branch = 1;
2037 2ab43947 2020-03-18 stsp break;
2038 2ab43947 2020-03-18 stsp }
2039 2ab43947 2020-03-18 stsp }
2040 2ab43947 2020-03-18 stsp }
2041 2ab43947 2020-03-18 stsp done:
2042 2ab43947 2020-03-18 stsp if (graph)
2043 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2044 2ab43947 2020-03-18 stsp free(head_commit_id);
2045 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2046 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2047 2ab43947 2020-03-18 stsp return err;
2048 a367ff0f 2019-05-14 stsp }
2049 8069f636 2019-01-12 stsp
2050 4ed7e80c 2018-05-20 stsp static const struct got_error *
2051 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2052 4b6c9460 2020-03-05 stsp {
2053 4b6c9460 2020-03-05 stsp static char msg[512];
2054 4b6c9460 2020-03-05 stsp const char *branch_name;
2055 4b6c9460 2020-03-05 stsp
2056 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2057 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2058 4b6c9460 2020-03-05 stsp else
2059 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2060 4b6c9460 2020-03-05 stsp
2061 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2062 4b6c9460 2020-03-05 stsp branch_name += 11;
2063 4b6c9460 2020-03-05 stsp
2064 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2065 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2066 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2067 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2068 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2069 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2070 4b6c9460 2020-03-05 stsp
2071 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2072 4b6c9460 2020-03-05 stsp }
2073 4b6c9460 2020-03-05 stsp
2074 4b6c9460 2020-03-05 stsp static const struct got_error *
2075 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2076 c09a553d 2018-03-12 stsp {
2077 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2078 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2079 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2080 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2081 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2082 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2083 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2084 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2085 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2086 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2087 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2088 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2089 f2ea84fa 2019-07-27 stsp
2090 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2091 c09a553d 2018-03-12 stsp
2092 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2093 0bb8a95e 2018-03-12 stsp switch (ch) {
2094 08573d5b 2019-05-14 stsp case 'b':
2095 08573d5b 2019-05-14 stsp branch_name = optarg;
2096 08573d5b 2019-05-14 stsp break;
2097 8069f636 2019-01-12 stsp case 'c':
2098 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2099 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2100 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2101 bb51a5b4 2020-01-13 stsp break;
2102 bb51a5b4 2020-01-13 stsp case 'E':
2103 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2104 8069f636 2019-01-12 stsp break;
2105 0bb8a95e 2018-03-12 stsp case 'p':
2106 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2107 0bb8a95e 2018-03-12 stsp break;
2108 0bb8a95e 2018-03-12 stsp default:
2109 2deda0b9 2019-03-07 stsp usage_checkout();
2110 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2111 0bb8a95e 2018-03-12 stsp }
2112 0bb8a95e 2018-03-12 stsp }
2113 0bb8a95e 2018-03-12 stsp
2114 0bb8a95e 2018-03-12 stsp argc -= optind;
2115 0bb8a95e 2018-03-12 stsp argv += optind;
2116 0bb8a95e 2018-03-12 stsp
2117 6715a751 2018-03-16 stsp #ifndef PROFILE
2118 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2119 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2120 c09a553d 2018-03-12 stsp err(1, "pledge");
2121 6715a751 2018-03-16 stsp #endif
2122 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2123 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2124 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2125 76089277 2018-04-01 stsp if (repo_path == NULL)
2126 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2127 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2128 76089277 2018-04-01 stsp if (cwd == NULL) {
2129 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2130 76089277 2018-04-01 stsp goto done;
2131 76089277 2018-04-01 stsp }
2132 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2133 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2134 230a42bd 2019-05-11 jcs if (base == NULL) {
2135 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2136 230a42bd 2019-05-11 jcs path_prefix);
2137 230a42bd 2019-05-11 jcs goto done;
2138 230a42bd 2019-05-11 jcs }
2139 230a42bd 2019-05-11 jcs } else {
2140 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2141 230a42bd 2019-05-11 jcs if (base == NULL) {
2142 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2143 230a42bd 2019-05-11 jcs repo_path);
2144 230a42bd 2019-05-11 jcs goto done;
2145 230a42bd 2019-05-11 jcs }
2146 76089277 2018-04-01 stsp }
2147 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2148 c09a553d 2018-03-12 stsp if (dotgit)
2149 c09a553d 2018-03-12 stsp *dotgit = '\0';
2150 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2151 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2152 c09a553d 2018-03-12 stsp free(cwd);
2153 76089277 2018-04-01 stsp goto done;
2154 c09a553d 2018-03-12 stsp }
2155 c09a553d 2018-03-12 stsp free(cwd);
2156 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2157 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2158 76089277 2018-04-01 stsp if (repo_path == NULL) {
2159 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2160 76089277 2018-04-01 stsp goto done;
2161 76089277 2018-04-01 stsp }
2162 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2163 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2164 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2165 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2166 b4b3a7dd 2019-07-22 stsp argv[1]);
2167 b4b3a7dd 2019-07-22 stsp goto done;
2168 b4b3a7dd 2019-07-22 stsp }
2169 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2170 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2171 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2172 b4b3a7dd 2019-07-22 stsp goto done;
2173 b4b3a7dd 2019-07-22 stsp }
2174 76089277 2018-04-01 stsp }
2175 c09a553d 2018-03-12 stsp } else
2176 c09a553d 2018-03-12 stsp usage_checkout();
2177 c09a553d 2018-03-12 stsp
2178 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2179 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2180 13bfb272 2019-05-10 jcs
2181 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2182 c09a553d 2018-03-12 stsp if (error != NULL)
2183 c02c541e 2019-03-29 stsp goto done;
2184 c02c541e 2019-03-29 stsp
2185 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2186 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2187 c530dc23 2019-07-23 stsp if (error) {
2188 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2189 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2190 c530dc23 2019-07-23 stsp goto done;
2191 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2192 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2193 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2194 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2195 c530dc23 2019-07-23 stsp goto done;
2196 c530dc23 2019-07-23 stsp }
2197 c530dc23 2019-07-23 stsp }
2198 c530dc23 2019-07-23 stsp
2199 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2200 c02c541e 2019-03-29 stsp if (error)
2201 c09a553d 2018-03-12 stsp goto done;
2202 8069f636 2019-01-12 stsp
2203 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2204 c09a553d 2018-03-12 stsp if (error != NULL)
2205 c09a553d 2018-03-12 stsp goto done;
2206 c09a553d 2018-03-12 stsp
2207 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2208 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2209 c09a553d 2018-03-12 stsp goto done;
2210 c09a553d 2018-03-12 stsp
2211 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2212 c09a553d 2018-03-12 stsp if (error != NULL)
2213 c09a553d 2018-03-12 stsp goto done;
2214 c09a553d 2018-03-12 stsp
2215 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2216 e5dc7198 2018-12-29 stsp path_prefix);
2217 e5dc7198 2018-12-29 stsp if (error != NULL)
2218 e5dc7198 2018-12-29 stsp goto done;
2219 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2220 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2221 49520a32 2018-12-29 stsp goto done;
2222 49520a32 2018-12-29 stsp }
2223 49520a32 2018-12-29 stsp
2224 8069f636 2019-01-12 stsp if (commit_id_str) {
2225 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2226 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2227 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2228 30837e32 2019-07-25 stsp if (error)
2229 8069f636 2019-01-12 stsp goto done;
2230 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2231 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2232 8069f636 2019-01-12 stsp if (error != NULL) {
2233 8069f636 2019-01-12 stsp free(commit_id);
2234 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2235 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2236 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2237 4b6c9460 2020-03-05 stsp }
2238 8069f636 2019-01-12 stsp goto done;
2239 8069f636 2019-01-12 stsp }
2240 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2241 4b6c9460 2020-03-05 stsp if (error) {
2242 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2243 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2244 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2245 4b6c9460 2020-03-05 stsp }
2246 45d344f6 2019-05-14 stsp goto done;
2247 4b6c9460 2020-03-05 stsp }
2248 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2249 8069f636 2019-01-12 stsp commit_id);
2250 8069f636 2019-01-12 stsp free(commit_id);
2251 8069f636 2019-01-12 stsp if (error)
2252 8069f636 2019-01-12 stsp goto done;
2253 8069f636 2019-01-12 stsp }
2254 8069f636 2019-01-12 stsp
2255 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2256 f2ea84fa 2019-07-27 stsp if (error)
2257 f2ea84fa 2019-07-27 stsp goto done;
2258 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2259 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2260 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2261 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2262 c09a553d 2018-03-12 stsp if (error != NULL)
2263 c09a553d 2018-03-12 stsp goto done;
2264 c09a553d 2018-03-12 stsp
2265 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2266 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2267 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2268 c09a553d 2018-03-12 stsp done:
2269 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2270 8069f636 2019-01-12 stsp free(commit_id_str);
2271 76089277 2018-04-01 stsp free(repo_path);
2272 507dc3bb 2018-12-29 stsp free(worktree_path);
2273 507dc3bb 2018-12-29 stsp return error;
2274 507dc3bb 2018-12-29 stsp }
2275 507dc3bb 2018-12-29 stsp
2276 507dc3bb 2018-12-29 stsp __dead static void
2277 507dc3bb 2018-12-29 stsp usage_update(void)
2278 507dc3bb 2018-12-29 stsp {
2279 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2280 507dc3bb 2018-12-29 stsp getprogname());
2281 507dc3bb 2018-12-29 stsp exit(1);
2282 507dc3bb 2018-12-29 stsp }
2283 507dc3bb 2018-12-29 stsp
2284 1ee397ad 2019-07-12 stsp static const struct got_error *
2285 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2286 507dc3bb 2018-12-29 stsp {
2287 784955db 2019-01-12 stsp int *did_something = arg;
2288 784955db 2019-01-12 stsp
2289 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2290 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2291 1ee397ad 2019-07-12 stsp return NULL;
2292 507dc3bb 2018-12-29 stsp
2293 1545c615 2019-02-10 stsp *did_something = 1;
2294 a484d721 2019-06-10 stsp
2295 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2296 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2297 1ee397ad 2019-07-12 stsp return NULL;
2298 a484d721 2019-06-10 stsp
2299 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2300 507dc3bb 2018-12-29 stsp path++;
2301 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2302 1ee397ad 2019-07-12 stsp return NULL;
2303 be7061eb 2018-12-30 stsp }
2304 be7061eb 2018-12-30 stsp
2305 be7061eb 2018-12-30 stsp static const struct got_error *
2306 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2307 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2308 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2309 a1fb16d8 2019-05-24 stsp {
2310 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2311 a1fb16d8 2019-05-24 stsp char *base_id_str;
2312 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2313 a1fb16d8 2019-05-24 stsp
2314 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2315 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2316 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2317 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2318 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2319 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2320 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2321 a1fb16d8 2019-05-24 stsp }
2322 a1fb16d8 2019-05-24 stsp
2323 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2324 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2325 a1fb16d8 2019-05-24 stsp if (err) {
2326 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2327 a1fb16d8 2019-05-24 stsp return err;
2328 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2329 a1fb16d8 2019-05-24 stsp }
2330 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2331 a1fb16d8 2019-05-24 stsp return NULL;
2332 a1fb16d8 2019-05-24 stsp
2333 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2334 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2335 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2336 a1fb16d8 2019-05-24 stsp if (err)
2337 a1fb16d8 2019-05-24 stsp return err;
2338 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2339 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2340 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2341 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2342 0ebf8283 2019-07-24 stsp return NULL;
2343 0ebf8283 2019-07-24 stsp }
2344 0ebf8283 2019-07-24 stsp
2345 0ebf8283 2019-07-24 stsp static const struct got_error *
2346 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2347 0ebf8283 2019-07-24 stsp {
2348 0ebf8283 2019-07-24 stsp const struct got_error *err;
2349 0ebf8283 2019-07-24 stsp int in_progress;
2350 0ebf8283 2019-07-24 stsp
2351 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2352 0ebf8283 2019-07-24 stsp if (err)
2353 0ebf8283 2019-07-24 stsp return err;
2354 0ebf8283 2019-07-24 stsp if (in_progress)
2355 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2356 0ebf8283 2019-07-24 stsp
2357 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2358 0ebf8283 2019-07-24 stsp if (err)
2359 0ebf8283 2019-07-24 stsp return err;
2360 0ebf8283 2019-07-24 stsp if (in_progress)
2361 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2362 0ebf8283 2019-07-24 stsp
2363 a1fb16d8 2019-05-24 stsp return NULL;
2364 a5edda0a 2019-07-27 stsp }
2365 a5edda0a 2019-07-27 stsp
2366 a5edda0a 2019-07-27 stsp static const struct got_error *
2367 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2368 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2369 a5edda0a 2019-07-27 stsp {
2370 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2371 a5edda0a 2019-07-27 stsp char *path;
2372 a5edda0a 2019-07-27 stsp int i;
2373 a5edda0a 2019-07-27 stsp
2374 a5edda0a 2019-07-27 stsp if (argc == 0) {
2375 a5edda0a 2019-07-27 stsp path = strdup("");
2376 a5edda0a 2019-07-27 stsp if (path == NULL)
2377 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2378 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2379 a5edda0a 2019-07-27 stsp }
2380 a5edda0a 2019-07-27 stsp
2381 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2382 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2383 a5edda0a 2019-07-27 stsp if (err)
2384 a5edda0a 2019-07-27 stsp break;
2385 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2386 a5edda0a 2019-07-27 stsp if (err) {
2387 a5edda0a 2019-07-27 stsp free(path);
2388 a5edda0a 2019-07-27 stsp break;
2389 a5edda0a 2019-07-27 stsp }
2390 a5edda0a 2019-07-27 stsp }
2391 a5edda0a 2019-07-27 stsp
2392 a5edda0a 2019-07-27 stsp return err;
2393 a1fb16d8 2019-05-24 stsp }
2394 a1fb16d8 2019-05-24 stsp
2395 a1fb16d8 2019-05-24 stsp static const struct got_error *
2396 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2397 507dc3bb 2018-12-29 stsp {
2398 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2399 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2400 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2401 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2402 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2403 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2404 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2405 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2406 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2407 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2408 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2409 507dc3bb 2018-12-29 stsp
2410 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2411 f2ea84fa 2019-07-27 stsp
2412 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2413 507dc3bb 2018-12-29 stsp switch (ch) {
2414 024e9686 2019-05-14 stsp case 'b':
2415 024e9686 2019-05-14 stsp branch_name = optarg;
2416 024e9686 2019-05-14 stsp break;
2417 507dc3bb 2018-12-29 stsp case 'c':
2418 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2419 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2420 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2421 507dc3bb 2018-12-29 stsp break;
2422 507dc3bb 2018-12-29 stsp default:
2423 2deda0b9 2019-03-07 stsp usage_update();
2424 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2425 507dc3bb 2018-12-29 stsp }
2426 507dc3bb 2018-12-29 stsp }
2427 507dc3bb 2018-12-29 stsp
2428 507dc3bb 2018-12-29 stsp argc -= optind;
2429 507dc3bb 2018-12-29 stsp argv += optind;
2430 507dc3bb 2018-12-29 stsp
2431 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2432 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2433 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2434 507dc3bb 2018-12-29 stsp err(1, "pledge");
2435 507dc3bb 2018-12-29 stsp #endif
2436 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2437 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2438 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2439 c4cdcb68 2019-04-03 stsp goto done;
2440 c4cdcb68 2019-04-03 stsp }
2441 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2442 7d5807f4 2019-07-11 stsp if (error)
2443 7d5807f4 2019-07-11 stsp goto done;
2444 7d5807f4 2019-07-11 stsp
2445 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2446 f2ea84fa 2019-07-27 stsp if (error)
2447 f2ea84fa 2019-07-27 stsp goto done;
2448 507dc3bb 2018-12-29 stsp
2449 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2450 c9956ddf 2019-09-08 stsp NULL);
2451 507dc3bb 2018-12-29 stsp if (error != NULL)
2452 507dc3bb 2018-12-29 stsp goto done;
2453 507dc3bb 2018-12-29 stsp
2454 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2455 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2456 087fb88c 2019-08-04 stsp if (error)
2457 087fb88c 2019-08-04 stsp goto done;
2458 087fb88c 2019-08-04 stsp
2459 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2460 0266afb7 2019-01-04 stsp if (error)
2461 0266afb7 2019-01-04 stsp goto done;
2462 0266afb7 2019-01-04 stsp
2463 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2464 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2465 024e9686 2019-05-14 stsp if (error != NULL)
2466 024e9686 2019-05-14 stsp goto done;
2467 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2468 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2469 9c4b8182 2019-01-02 stsp if (error != NULL)
2470 9c4b8182 2019-01-02 stsp goto done;
2471 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2472 507dc3bb 2018-12-29 stsp if (error != NULL)
2473 507dc3bb 2018-12-29 stsp goto done;
2474 507dc3bb 2018-12-29 stsp } else {
2475 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2476 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2477 04f57cb3 2019-07-25 stsp free(commit_id_str);
2478 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2479 30837e32 2019-07-25 stsp if (error)
2480 a297e751 2019-07-11 stsp goto done;
2481 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2482 a297e751 2019-07-11 stsp if (error)
2483 507dc3bb 2018-12-29 stsp goto done;
2484 507dc3bb 2018-12-29 stsp }
2485 35c965b2 2018-12-29 stsp
2486 a1fb16d8 2019-05-24 stsp if (branch_name) {
2487 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2488 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2489 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2490 f2ea84fa 2019-07-27 stsp continue;
2491 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2492 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2493 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2494 024e9686 2019-05-14 stsp goto done;
2495 024e9686 2019-05-14 stsp }
2496 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2497 024e9686 2019-05-14 stsp if (error)
2498 024e9686 2019-05-14 stsp goto done;
2499 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2500 3aef623b 2019-10-15 stsp repo);
2501 a367ff0f 2019-05-14 stsp free(head_commit_id);
2502 024e9686 2019-05-14 stsp if (error != NULL)
2503 024e9686 2019-05-14 stsp goto done;
2504 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2505 a367ff0f 2019-05-14 stsp if (error)
2506 a367ff0f 2019-05-14 stsp goto done;
2507 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2508 024e9686 2019-05-14 stsp if (error)
2509 024e9686 2019-05-14 stsp goto done;
2510 024e9686 2019-05-14 stsp } else {
2511 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2512 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2513 a367ff0f 2019-05-14 stsp if (error != NULL) {
2514 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2515 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2516 024e9686 2019-05-14 stsp goto done;
2517 a367ff0f 2019-05-14 stsp }
2518 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2519 a367ff0f 2019-05-14 stsp if (error)
2520 a367ff0f 2019-05-14 stsp goto done;
2521 024e9686 2019-05-14 stsp }
2522 507dc3bb 2018-12-29 stsp
2523 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2524 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2525 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2526 507dc3bb 2018-12-29 stsp commit_id);
2527 507dc3bb 2018-12-29 stsp if (error)
2528 507dc3bb 2018-12-29 stsp goto done;
2529 507dc3bb 2018-12-29 stsp }
2530 507dc3bb 2018-12-29 stsp
2531 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2532 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2533 507dc3bb 2018-12-29 stsp if (error != NULL)
2534 507dc3bb 2018-12-29 stsp goto done;
2535 9c4b8182 2019-01-02 stsp
2536 784955db 2019-01-12 stsp if (did_something)
2537 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2538 784955db 2019-01-12 stsp else
2539 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2540 507dc3bb 2018-12-29 stsp done:
2541 c09a553d 2018-03-12 stsp free(worktree_path);
2542 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2543 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2544 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2545 507dc3bb 2018-12-29 stsp free(commit_id);
2546 9c4b8182 2019-01-02 stsp free(commit_id_str);
2547 c09a553d 2018-03-12 stsp return error;
2548 c09a553d 2018-03-12 stsp }
2549 c09a553d 2018-03-12 stsp
2550 f42b1b34 2018-03-12 stsp static const struct got_error *
2551 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2552 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2553 63035f9f 2019-10-06 stsp struct got_repository *repo)
2554 5c860e29 2018-03-12 stsp {
2555 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2556 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2557 79109fed 2018-03-27 stsp
2558 44392932 2019-08-25 stsp if (blob_id1) {
2559 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2560 44392932 2019-08-25 stsp if (err)
2561 44392932 2019-08-25 stsp goto done;
2562 44392932 2019-08-25 stsp }
2563 79109fed 2018-03-27 stsp
2564 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2565 44392932 2019-08-25 stsp if (err)
2566 44392932 2019-08-25 stsp goto done;
2567 79109fed 2018-03-27 stsp
2568 44392932 2019-08-25 stsp while (path[0] == '/')
2569 44392932 2019-08-25 stsp path++;
2570 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2571 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2572 44392932 2019-08-25 stsp done:
2573 44392932 2019-08-25 stsp if (blob1)
2574 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2575 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2576 44392932 2019-08-25 stsp return err;
2577 44392932 2019-08-25 stsp }
2578 44392932 2019-08-25 stsp
2579 44392932 2019-08-25 stsp static const struct got_error *
2580 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2581 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2582 63035f9f 2019-10-06 stsp struct got_repository *repo)
2583 44392932 2019-08-25 stsp {
2584 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2585 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2586 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2587 44392932 2019-08-25 stsp
2588 44392932 2019-08-25 stsp if (tree_id1) {
2589 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2590 79109fed 2018-03-27 stsp if (err)
2591 44392932 2019-08-25 stsp goto done;
2592 79109fed 2018-03-27 stsp }
2593 79109fed 2018-03-27 stsp
2594 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2595 0f2b3dca 2018-12-22 stsp if (err)
2596 0f2b3dca 2018-12-22 stsp goto done;
2597 0f2b3dca 2018-12-22 stsp
2598 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2599 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2600 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2601 44392932 2019-08-25 stsp while (path[0] == '/')
2602 44392932 2019-08-25 stsp path++;
2603 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2604 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2605 0f2b3dca 2018-12-22 stsp done:
2606 79109fed 2018-03-27 stsp if (tree1)
2607 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2608 366e0a5f 2019-10-10 stsp if (tree2)
2609 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2610 44392932 2019-08-25 stsp return err;
2611 44392932 2019-08-25 stsp }
2612 44392932 2019-08-25 stsp
2613 44392932 2019-08-25 stsp static const struct got_error *
2614 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2615 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2616 44392932 2019-08-25 stsp {
2617 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2618 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2619 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2620 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2621 44392932 2019-08-25 stsp struct got_object_qid *qid;
2622 44392932 2019-08-25 stsp
2623 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2624 44392932 2019-08-25 stsp if (qid != NULL) {
2625 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2626 44392932 2019-08-25 stsp qid->id);
2627 44392932 2019-08-25 stsp if (err)
2628 44392932 2019-08-25 stsp return err;
2629 44392932 2019-08-25 stsp }
2630 44392932 2019-08-25 stsp
2631 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2632 44392932 2019-08-25 stsp int obj_type;
2633 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2634 44392932 2019-08-25 stsp if (err)
2635 44392932 2019-08-25 stsp goto done;
2636 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2637 44392932 2019-08-25 stsp if (err) {
2638 44392932 2019-08-25 stsp free(obj_id2);
2639 44392932 2019-08-25 stsp goto done;
2640 44392932 2019-08-25 stsp }
2641 44392932 2019-08-25 stsp if (pcommit) {
2642 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2643 44392932 2019-08-25 stsp qid->id, path);
2644 44392932 2019-08-25 stsp if (err) {
2645 44392932 2019-08-25 stsp free(obj_id2);
2646 44392932 2019-08-25 stsp goto done;
2647 44392932 2019-08-25 stsp }
2648 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2649 44392932 2019-08-25 stsp if (err) {
2650 44392932 2019-08-25 stsp free(obj_id2);
2651 44392932 2019-08-25 stsp goto done;
2652 44392932 2019-08-25 stsp }
2653 44392932 2019-08-25 stsp }
2654 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2655 44392932 2019-08-25 stsp if (err) {
2656 44392932 2019-08-25 stsp free(obj_id2);
2657 44392932 2019-08-25 stsp goto done;
2658 44392932 2019-08-25 stsp }
2659 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2660 44392932 2019-08-25 stsp switch (obj_type) {
2661 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2662 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2663 63035f9f 2019-10-06 stsp 0, repo);
2664 44392932 2019-08-25 stsp break;
2665 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2666 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2667 63035f9f 2019-10-06 stsp 0, repo);
2668 44392932 2019-08-25 stsp break;
2669 44392932 2019-08-25 stsp default:
2670 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2671 44392932 2019-08-25 stsp break;
2672 44392932 2019-08-25 stsp }
2673 44392932 2019-08-25 stsp free(obj_id1);
2674 44392932 2019-08-25 stsp free(obj_id2);
2675 44392932 2019-08-25 stsp } else {
2676 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2677 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2678 44392932 2019-08-25 stsp if (err)
2679 44392932 2019-08-25 stsp goto done;
2680 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2681 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2682 44392932 2019-08-25 stsp if (err)
2683 44392932 2019-08-25 stsp goto done;
2684 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2685 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2686 44392932 2019-08-25 stsp }
2687 44392932 2019-08-25 stsp done:
2688 0f2b3dca 2018-12-22 stsp free(id_str1);
2689 0f2b3dca 2018-12-22 stsp free(id_str2);
2690 44392932 2019-08-25 stsp if (pcommit)
2691 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2692 79109fed 2018-03-27 stsp return err;
2693 79109fed 2018-03-27 stsp }
2694 79109fed 2018-03-27 stsp
2695 4bb494d5 2018-06-16 stsp static char *
2696 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2697 6c281f94 2018-06-11 stsp {
2698 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2699 09867e48 2019-08-13 stsp char *p, *s;
2700 09867e48 2019-08-13 stsp
2701 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2702 09867e48 2019-08-13 stsp if (tm == NULL)
2703 09867e48 2019-08-13 stsp return NULL;
2704 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2705 09867e48 2019-08-13 stsp if (s == NULL)
2706 09867e48 2019-08-13 stsp return NULL;
2707 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2708 6c281f94 2018-06-11 stsp if (p)
2709 6c281f94 2018-06-11 stsp *p = '\0';
2710 6c281f94 2018-06-11 stsp return s;
2711 6c281f94 2018-06-11 stsp }
2712 dc424a06 2019-08-07 stsp
2713 6841bf13 2019-11-29 kn static const struct got_error *
2714 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2715 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2716 6841bf13 2019-11-29 kn {
2717 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2718 6841bf13 2019-11-29 kn regmatch_t regmatch;
2719 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2720 6841bf13 2019-11-29 kn
2721 6841bf13 2019-11-29 kn *have_match = 0;
2722 6841bf13 2019-11-29 kn
2723 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2724 6841bf13 2019-11-29 kn if (err)
2725 6841bf13 2019-11-29 kn return err;
2726 6841bf13 2019-11-29 kn
2727 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2728 6841bf13 2019-11-29 kn if (err)
2729 6841bf13 2019-11-29 kn goto done;
2730 6841bf13 2019-11-29 kn
2731 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2732 6841bf13 2019-11-29 kn *have_match = 1;
2733 6841bf13 2019-11-29 kn done:
2734 6841bf13 2019-11-29 kn free(id_str);
2735 6841bf13 2019-11-29 kn free(logmsg);
2736 6841bf13 2019-11-29 kn return err;
2737 6841bf13 2019-11-29 kn }
2738 6841bf13 2019-11-29 kn
2739 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2740 6c281f94 2018-06-11 stsp
2741 79109fed 2018-03-27 stsp static const struct got_error *
2742 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2743 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2744 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2745 79109fed 2018-03-27 stsp {
2746 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2747 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2748 6c281f94 2018-06-11 stsp char datebuf[26];
2749 45d799e2 2018-12-23 stsp time_t committer_time;
2750 45d799e2 2018-12-23 stsp const char *author, *committer;
2751 199a4027 2019-02-02 stsp char *refs_str = NULL;
2752 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2753 5c860e29 2018-03-12 stsp
2754 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2755 199a4027 2019-02-02 stsp char *s;
2756 199a4027 2019-02-02 stsp const char *name;
2757 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2758 a436ad14 2019-08-13 stsp int cmp;
2759 a436ad14 2019-08-13 stsp
2760 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2761 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2762 d9498b20 2019-02-05 stsp continue;
2763 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2764 199a4027 2019-02-02 stsp name += 5;
2765 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2766 7143d404 2019-03-12 stsp continue;
2767 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2768 e34f9ed6 2019-02-02 stsp name += 6;
2769 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2770 141c2bff 2019-02-04 stsp name += 8;
2771 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2772 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2773 5d844a1e 2019-08-13 stsp if (err) {
2774 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2775 5d844a1e 2019-08-13 stsp return err;
2776 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2777 5d844a1e 2019-08-13 stsp err = NULL;
2778 5d844a1e 2019-08-13 stsp tag = NULL;
2779 5d844a1e 2019-08-13 stsp }
2780 a436ad14 2019-08-13 stsp }
2781 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2782 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2783 a436ad14 2019-08-13 stsp if (tag)
2784 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2785 a436ad14 2019-08-13 stsp if (cmp != 0)
2786 a436ad14 2019-08-13 stsp continue;
2787 199a4027 2019-02-02 stsp s = refs_str;
2788 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2789 199a4027 2019-02-02 stsp name) == -1) {
2790 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2791 199a4027 2019-02-02 stsp free(s);
2792 34ca4898 2019-08-13 stsp return err;
2793 199a4027 2019-02-02 stsp }
2794 199a4027 2019-02-02 stsp free(s);
2795 199a4027 2019-02-02 stsp }
2796 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2797 8bf5b3c9 2018-03-17 stsp if (err)
2798 8bf5b3c9 2018-03-17 stsp return err;
2799 788c352e 2018-06-16 stsp
2800 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2801 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2802 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2803 832c249c 2018-06-10 stsp free(id_str);
2804 d3d493d7 2019-02-21 stsp id_str = NULL;
2805 d3d493d7 2019-02-21 stsp free(refs_str);
2806 d3d493d7 2019-02-21 stsp refs_str = NULL;
2807 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2808 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2809 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2810 09867e48 2019-08-13 stsp if (datestr)
2811 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2812 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2813 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2814 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2815 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2816 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2817 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2818 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2819 3fe1abad 2018-06-10 stsp int n = 1;
2820 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2821 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2822 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2823 a0603db2 2018-06-10 stsp if (err)
2824 a0603db2 2018-06-10 stsp return err;
2825 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2826 a0603db2 2018-06-10 stsp free(id_str);
2827 a0603db2 2018-06-10 stsp }
2828 a0603db2 2018-06-10 stsp }
2829 832c249c 2018-06-10 stsp
2830 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2831 5943eee2 2019-08-13 stsp if (err)
2832 5943eee2 2019-08-13 stsp return err;
2833 8bf5b3c9 2018-03-17 stsp
2834 621015ac 2018-07-23 stsp logmsg = logmsg0;
2835 832c249c 2018-06-10 stsp do {
2836 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2837 832c249c 2018-06-10 stsp if (line)
2838 832c249c 2018-06-10 stsp printf(" %s\n", line);
2839 832c249c 2018-06-10 stsp } while (line);
2840 621015ac 2018-07-23 stsp free(logmsg0);
2841 832c249c 2018-06-10 stsp
2842 971751ac 2018-03-27 stsp if (show_patch) {
2843 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2844 971751ac 2018-03-27 stsp if (err == 0)
2845 971751ac 2018-03-27 stsp printf("\n");
2846 971751ac 2018-03-27 stsp }
2847 07862c20 2018-09-15 stsp
2848 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2849 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2850 07862c20 2018-09-15 stsp return err;
2851 f42b1b34 2018-03-12 stsp }
2852 5c860e29 2018-03-12 stsp
2853 f42b1b34 2018-03-12 stsp static const struct got_error *
2854 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2855 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2856 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2857 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2858 f42b1b34 2018-03-12 stsp {
2859 f42b1b34 2018-03-12 stsp const struct got_error *err;
2860 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2861 6841bf13 2019-11-29 kn regex_t regex;
2862 6841bf13 2019-11-29 kn int have_match;
2863 372ccdbb 2018-06-10 stsp
2864 6841bf13 2019-11-29 kn if (search_pattern &&
2865 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2866 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2867 6841bf13 2019-11-29 kn
2868 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2869 f42b1b34 2018-03-12 stsp if (err)
2870 f42b1b34 2018-03-12 stsp return err;
2871 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2872 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2873 372ccdbb 2018-06-10 stsp if (err)
2874 fcc85cad 2018-07-22 stsp goto done;
2875 656b1f76 2019-05-11 jcs for (;;) {
2876 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2877 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2878 8bf5b3c9 2018-03-17 stsp
2879 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2880 84453469 2018-11-11 stsp break;
2881 84453469 2018-11-11 stsp
2882 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2883 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2884 372ccdbb 2018-06-10 stsp if (err) {
2885 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2886 9ba79e04 2018-06-11 stsp err = NULL;
2887 ee780d5c 2020-01-04 stsp break;
2888 7e665116 2018-04-02 stsp }
2889 b43fbaa0 2018-06-11 stsp if (id == NULL)
2890 7e665116 2018-04-02 stsp break;
2891 b43fbaa0 2018-06-11 stsp
2892 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2893 b43fbaa0 2018-06-11 stsp if (err)
2894 fcc85cad 2018-07-22 stsp break;
2895 6841bf13 2019-11-29 kn
2896 6841bf13 2019-11-29 kn if (search_pattern) {
2897 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2898 6841bf13 2019-11-29 kn if (err) {
2899 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2900 6841bf13 2019-11-29 kn break;
2901 6841bf13 2019-11-29 kn }
2902 6841bf13 2019-11-29 kn if (have_match == 0) {
2903 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2904 6841bf13 2019-11-29 kn continue;
2905 6841bf13 2019-11-29 kn }
2906 6841bf13 2019-11-29 kn }
2907 6841bf13 2019-11-29 kn
2908 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2909 44392932 2019-08-25 stsp diff_context, refs);
2910 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2911 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2912 7e665116 2018-04-02 stsp break;
2913 31cedeaf 2018-09-15 stsp }
2914 fcc85cad 2018-07-22 stsp done:
2915 6841bf13 2019-11-29 kn if (search_pattern)
2916 6841bf13 2019-11-29 kn regfree(&regex);
2917 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2918 f42b1b34 2018-03-12 stsp return err;
2919 f42b1b34 2018-03-12 stsp }
2920 5c860e29 2018-03-12 stsp
2921 4ed7e80c 2018-05-20 stsp __dead static void
2922 6f3d1eb0 2018-03-12 stsp usage_log(void)
2923 6f3d1eb0 2018-03-12 stsp {
2924 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2925 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2926 6f3d1eb0 2018-03-12 stsp exit(1);
2927 b1ebc001 2019-08-13 stsp }
2928 b1ebc001 2019-08-13 stsp
2929 b1ebc001 2019-08-13 stsp static int
2930 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2931 b1ebc001 2019-08-13 stsp {
2932 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2933 b1ebc001 2019-08-13 stsp long long n;
2934 b1ebc001 2019-08-13 stsp const char *errstr;
2935 b1ebc001 2019-08-13 stsp
2936 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2937 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2938 b1ebc001 2019-08-13 stsp return 0;
2939 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2940 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2941 b1ebc001 2019-08-13 stsp return 0;
2942 b1ebc001 2019-08-13 stsp return n;
2943 6f3d1eb0 2018-03-12 stsp }
2944 6f3d1eb0 2018-03-12 stsp
2945 4ed7e80c 2018-05-20 stsp static const struct got_error *
2946 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2947 f42b1b34 2018-03-12 stsp {
2948 f42b1b34 2018-03-12 stsp const struct got_error *error;
2949 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2950 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2951 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2952 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2953 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2954 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2955 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2956 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2957 64a96a6d 2018-04-01 stsp const char *errstr;
2958 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2959 1b3893a2 2019-03-18 stsp
2960 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2961 5c860e29 2018-03-12 stsp
2962 6715a751 2018-03-16 stsp #ifndef PROFILE
2963 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2964 6098196c 2019-01-04 stsp NULL)
2965 ad242220 2018-09-08 stsp == -1)
2966 f42b1b34 2018-03-12 stsp err(1, "pledge");
2967 6715a751 2018-03-16 stsp #endif
2968 79109fed 2018-03-27 stsp
2969 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2970 b1ebc001 2019-08-13 stsp
2971 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2972 79109fed 2018-03-27 stsp switch (ch) {
2973 79109fed 2018-03-27 stsp case 'p':
2974 79109fed 2018-03-27 stsp show_patch = 1;
2975 d142fc45 2018-04-01 stsp break;
2976 d142fc45 2018-04-01 stsp case 'c':
2977 d142fc45 2018-04-01 stsp start_commit = optarg;
2978 64a96a6d 2018-04-01 stsp break;
2979 c0cc5c62 2018-10-18 stsp case 'C':
2980 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2981 4a8520aa 2018-10-18 stsp &errstr);
2982 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2983 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2984 c0cc5c62 2018-10-18 stsp break;
2985 64a96a6d 2018-04-01 stsp case 'l':
2986 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2987 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2988 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2989 cc54c501 2019-07-15 stsp break;
2990 48c8c60d 2020-01-27 stsp case 'b':
2991 48c8c60d 2020-01-27 stsp log_branches = 1;
2992 a0603db2 2018-06-10 stsp break;
2993 04ca23f4 2018-07-16 stsp case 'r':
2994 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2995 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2996 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2997 9ba1d308 2019-10-21 stsp optarg);
2998 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2999 04ca23f4 2018-07-16 stsp break;
3000 6841bf13 2019-11-29 kn case 's':
3001 6841bf13 2019-11-29 kn search_pattern = optarg;
3002 6841bf13 2019-11-29 kn break;
3003 79109fed 2018-03-27 stsp default:
3004 2deda0b9 2019-03-07 stsp usage_log();
3005 79109fed 2018-03-27 stsp /* NOTREACHED */
3006 79109fed 2018-03-27 stsp }
3007 79109fed 2018-03-27 stsp }
3008 79109fed 2018-03-27 stsp
3009 79109fed 2018-03-27 stsp argc -= optind;
3010 79109fed 2018-03-27 stsp argv += optind;
3011 f42b1b34 2018-03-12 stsp
3012 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3013 dc1edbfa 2019-11-29 kn diff_context = 3;
3014 dc1edbfa 2019-11-29 kn else if (!show_patch)
3015 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3016 dc1edbfa 2019-11-29 kn
3017 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3018 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3019 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3020 04ca23f4 2018-07-16 stsp goto done;
3021 04ca23f4 2018-07-16 stsp }
3022 cffc0aa4 2019-02-05 stsp
3023 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3024 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3025 cffc0aa4 2019-02-05 stsp goto done;
3026 cffc0aa4 2019-02-05 stsp error = NULL;
3027 cffc0aa4 2019-02-05 stsp
3028 e7301579 2019-03-18 stsp if (argc == 0) {
3029 cbd1af7a 2019-03-18 stsp path = strdup("");
3030 e7301579 2019-03-18 stsp if (path == NULL) {
3031 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3032 cbd1af7a 2019-03-18 stsp goto done;
3033 e7301579 2019-03-18 stsp }
3034 e7301579 2019-03-18 stsp } else if (argc == 1) {
3035 e7301579 2019-03-18 stsp if (worktree) {
3036 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3037 e7301579 2019-03-18 stsp argv[0]);
3038 e7301579 2019-03-18 stsp if (error)
3039 e7301579 2019-03-18 stsp goto done;
3040 e7301579 2019-03-18 stsp } else {
3041 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3042 e7301579 2019-03-18 stsp if (path == NULL) {
3043 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3044 e7301579 2019-03-18 stsp goto done;
3045 e7301579 2019-03-18 stsp }
3046 e7301579 2019-03-18 stsp }
3047 cbd1af7a 2019-03-18 stsp } else
3048 cbd1af7a 2019-03-18 stsp usage_log();
3049 cbd1af7a 2019-03-18 stsp
3050 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3051 5486daa2 2019-05-11 stsp repo_path = worktree ?
3052 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3053 5486daa2 2019-05-11 stsp }
3054 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3055 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3056 cffc0aa4 2019-02-05 stsp goto done;
3057 04ca23f4 2018-07-16 stsp }
3058 6098196c 2019-01-04 stsp
3059 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3060 d7d4f210 2018-03-12 stsp if (error != NULL)
3061 04ca23f4 2018-07-16 stsp goto done;
3062 f42b1b34 2018-03-12 stsp
3063 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3064 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3065 c02c541e 2019-03-29 stsp if (error)
3066 c02c541e 2019-03-29 stsp goto done;
3067 c02c541e 2019-03-29 stsp
3068 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3069 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3070 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3071 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3072 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3073 3235492e 2018-04-01 stsp if (error != NULL)
3074 3235492e 2018-04-01 stsp return error;
3075 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
3076 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3077 3235492e 2018-04-01 stsp if (error != NULL)
3078 3235492e 2018-04-01 stsp return error;
3079 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3080 3235492e 2018-04-01 stsp } else {
3081 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
3082 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3083 3235492e 2018-04-01 stsp if (error == NULL) {
3084 1caad61b 2019-02-01 stsp int obj_type;
3085 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
3086 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
3087 d1f2edc9 2018-06-13 stsp if (error != NULL)
3088 1caad61b 2019-02-01 stsp goto done;
3089 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
3090 1caad61b 2019-02-01 stsp if (error != NULL)
3091 1caad61b 2019-02-01 stsp goto done;
3092 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3093 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
3094 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
3095 1caad61b 2019-02-01 stsp if (error != NULL)
3096 1caad61b 2019-02-01 stsp goto done;
3097 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
3098 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
3099 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3100 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3101 1caad61b 2019-02-01 stsp goto done;
3102 1caad61b 2019-02-01 stsp }
3103 1caad61b 2019-02-01 stsp free(id);
3104 1caad61b 2019-02-01 stsp id = got_object_id_dup(
3105 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
3106 1caad61b 2019-02-01 stsp if (id == NULL)
3107 638f9024 2019-05-13 stsp error = got_error_from_errno(
3108 230a42bd 2019-05-11 jcs "got_object_id_dup");
3109 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3110 1caad61b 2019-02-01 stsp if (error)
3111 1caad61b 2019-02-01 stsp goto done;
3112 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3113 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3114 1caad61b 2019-02-01 stsp goto done;
3115 1caad61b 2019-02-01 stsp }
3116 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3117 d1f2edc9 2018-06-13 stsp if (error != NULL)
3118 1caad61b 2019-02-01 stsp goto done;
3119 d1f2edc9 2018-06-13 stsp }
3120 15a94983 2018-12-23 stsp if (commit == NULL) {
3121 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
3122 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3123 d1f2edc9 2018-06-13 stsp if (error != NULL)
3124 d1f2edc9 2018-06-13 stsp return error;
3125 3235492e 2018-04-01 stsp }
3126 3235492e 2018-04-01 stsp }
3127 d7d4f210 2018-03-12 stsp if (error != NULL)
3128 04ca23f4 2018-07-16 stsp goto done;
3129 04ca23f4 2018-07-16 stsp
3130 deeabeae 2019-08-27 stsp if (worktree) {
3131 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3132 deeabeae 2019-08-27 stsp char *p;
3133 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3134 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3135 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3136 deeabeae 2019-08-27 stsp goto done;
3137 deeabeae 2019-08-27 stsp }
3138 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3139 deeabeae 2019-08-27 stsp free(p);
3140 deeabeae 2019-08-27 stsp } else
3141 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3142 04ca23f4 2018-07-16 stsp if (error != NULL)
3143 04ca23f4 2018-07-16 stsp goto done;
3144 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3145 04ca23f4 2018-07-16 stsp free(path);
3146 04ca23f4 2018-07-16 stsp path = in_repo_path;
3147 04ca23f4 2018-07-16 stsp }
3148 199a4027 2019-02-02 stsp
3149 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3150 199a4027 2019-02-02 stsp if (error)
3151 199a4027 2019-02-02 stsp goto done;
3152 04ca23f4 2018-07-16 stsp
3153 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3154 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3155 04ca23f4 2018-07-16 stsp done:
3156 04ca23f4 2018-07-16 stsp free(path);
3157 04ca23f4 2018-07-16 stsp free(repo_path);
3158 04ca23f4 2018-07-16 stsp free(cwd);
3159 f42b1b34 2018-03-12 stsp free(id);
3160 cffc0aa4 2019-02-05 stsp if (worktree)
3161 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3162 ad242220 2018-09-08 stsp if (repo) {
3163 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3164 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3165 ad242220 2018-09-08 stsp if (error == NULL)
3166 ad242220 2018-09-08 stsp error = repo_error;
3167 ad242220 2018-09-08 stsp }
3168 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3169 8bf5b3c9 2018-03-17 stsp return error;
3170 5c860e29 2018-03-12 stsp }
3171 5c860e29 2018-03-12 stsp
3172 4ed7e80c 2018-05-20 stsp __dead static void
3173 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3174 3f8b7d6a 2018-04-01 stsp {
3175 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3176 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3177 3f8b7d6a 2018-04-01 stsp exit(1);
3178 b00d56cd 2018-04-01 stsp }
3179 b00d56cd 2018-04-01 stsp
3180 b72f483a 2019-02-05 stsp struct print_diff_arg {
3181 b72f483a 2019-02-05 stsp struct got_repository *repo;
3182 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3183 b72f483a 2019-02-05 stsp int diff_context;
3184 3fc0c068 2019-02-10 stsp const char *id_str;
3185 3fc0c068 2019-02-10 stsp int header_shown;
3186 98eaaa12 2019-08-03 stsp int diff_staged;
3187 63035f9f 2019-10-06 stsp int ignore_whitespace;
3188 b72f483a 2019-02-05 stsp };
3189 b72f483a 2019-02-05 stsp
3190 4ed7e80c 2018-05-20 stsp static const struct got_error *
3191 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3192 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3193 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3194 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3195 b72f483a 2019-02-05 stsp {
3196 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3197 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3198 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3199 12463d8b 2019-12-13 stsp int fd = -1;
3200 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3201 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3202 b72f483a 2019-02-05 stsp struct stat sb;
3203 b72f483a 2019-02-05 stsp
3204 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3205 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3206 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3207 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3208 98eaaa12 2019-08-03 stsp return NULL;
3209 98eaaa12 2019-08-03 stsp } else {
3210 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3211 98eaaa12 2019-08-03 stsp return NULL;
3212 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3213 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3214 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3215 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3216 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3217 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3218 98eaaa12 2019-08-03 stsp return NULL;
3219 98eaaa12 2019-08-03 stsp }
3220 3fc0c068 2019-02-10 stsp
3221 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3222 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3223 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3224 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3225 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3226 3fc0c068 2019-02-10 stsp }
3227 b72f483a 2019-02-05 stsp
3228 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3229 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3230 98eaaa12 2019-08-03 stsp switch (staged_status) {
3231 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3232 98eaaa12 2019-08-03 stsp label1 = path;
3233 98eaaa12 2019-08-03 stsp label2 = path;
3234 98eaaa12 2019-08-03 stsp break;
3235 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3236 98eaaa12 2019-08-03 stsp label2 = path;
3237 98eaaa12 2019-08-03 stsp break;
3238 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3239 98eaaa12 2019-08-03 stsp label1 = path;
3240 98eaaa12 2019-08-03 stsp break;
3241 98eaaa12 2019-08-03 stsp default:
3242 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3243 98eaaa12 2019-08-03 stsp }
3244 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3245 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3246 63035f9f 2019-10-06 stsp a->repo, stdout);
3247 98eaaa12 2019-08-03 stsp }
3248 98eaaa12 2019-08-03 stsp
3249 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3250 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3251 4ce46740 2019-08-08 stsp char *id_str;
3252 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3253 408b4ebc 2019-08-03 stsp 8192);
3254 4ce46740 2019-08-08 stsp if (err)
3255 4ce46740 2019-08-08 stsp goto done;
3256 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3257 4ce46740 2019-08-08 stsp if (err)
3258 4ce46740 2019-08-08 stsp goto done;
3259 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3260 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3261 4ce46740 2019-08-08 stsp free(id_str);
3262 4ce46740 2019-08-08 stsp goto done;
3263 4ce46740 2019-08-08 stsp }
3264 4ce46740 2019-08-08 stsp free(id_str);
3265 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3266 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3267 4ce46740 2019-08-08 stsp if (err)
3268 4ce46740 2019-08-08 stsp goto done;
3269 4ce46740 2019-08-08 stsp }
3270 d00136be 2019-03-26 stsp
3271 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3272 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3273 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3274 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3275 2ec1f75b 2019-03-26 stsp goto done;
3276 2ec1f75b 2019-03-26 stsp }
3277 b72f483a 2019-02-05 stsp
3278 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3279 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3280 12463d8b 2019-12-13 stsp if (fd == -1) {
3281 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3282 12463d8b 2019-12-13 stsp goto done;
3283 12463d8b 2019-12-13 stsp }
3284 12463d8b 2019-12-13 stsp } else {
3285 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3286 12463d8b 2019-12-13 stsp if (fd == -1) {
3287 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3288 12463d8b 2019-12-13 stsp goto done;
3289 12463d8b 2019-12-13 stsp }
3290 12463d8b 2019-12-13 stsp }
3291 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3292 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3293 2ec1f75b 2019-03-26 stsp goto done;
3294 2ec1f75b 2019-03-26 stsp }
3295 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3296 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3297 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3298 2ec1f75b 2019-03-26 stsp goto done;
3299 2ec1f75b 2019-03-26 stsp }
3300 12463d8b 2019-12-13 stsp fd = -1;
3301 2ec1f75b 2019-03-26 stsp } else
3302 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3303 b72f483a 2019-02-05 stsp
3304 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3305 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3306 b72f483a 2019-02-05 stsp done:
3307 b72f483a 2019-02-05 stsp if (blob1)
3308 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3309 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3310 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3311 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3312 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3313 b72f483a 2019-02-05 stsp free(abspath);
3314 927df6b7 2019-02-10 stsp return err;
3315 927df6b7 2019-02-10 stsp }
3316 927df6b7 2019-02-10 stsp
3317 927df6b7 2019-02-10 stsp static const struct got_error *
3318 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3319 b00d56cd 2018-04-01 stsp {
3320 b00d56cd 2018-04-01 stsp const struct got_error *error;
3321 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3322 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3323 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3324 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3325 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3326 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3327 15a94983 2018-12-23 stsp int type1, type2;
3328 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3329 c0cc5c62 2018-10-18 stsp const char *errstr;
3330 927df6b7 2019-02-10 stsp char *path = NULL;
3331 b00d56cd 2018-04-01 stsp
3332 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3333 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3334 25eccc22 2019-01-04 stsp NULL) == -1)
3335 b00d56cd 2018-04-01 stsp err(1, "pledge");
3336 b00d56cd 2018-04-01 stsp #endif
3337 b00d56cd 2018-04-01 stsp
3338 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3339 b00d56cd 2018-04-01 stsp switch (ch) {
3340 c0cc5c62 2018-10-18 stsp case 'C':
3341 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3342 dfcab68b 2019-11-29 kn &errstr);
3343 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3344 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3345 c0cc5c62 2018-10-18 stsp break;
3346 b72f483a 2019-02-05 stsp case 'r':
3347 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3348 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3349 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3350 9ba1d308 2019-10-21 stsp optarg);
3351 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3352 b72f483a 2019-02-05 stsp break;
3353 98eaaa12 2019-08-03 stsp case 's':
3354 98eaaa12 2019-08-03 stsp diff_staged = 1;
3355 63035f9f 2019-10-06 stsp break;
3356 63035f9f 2019-10-06 stsp case 'w':
3357 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3358 98eaaa12 2019-08-03 stsp break;
3359 b00d56cd 2018-04-01 stsp default:
3360 2deda0b9 2019-03-07 stsp usage_diff();
3361 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3362 b00d56cd 2018-04-01 stsp }
3363 b00d56cd 2018-04-01 stsp }
3364 b00d56cd 2018-04-01 stsp
3365 b00d56cd 2018-04-01 stsp argc -= optind;
3366 b00d56cd 2018-04-01 stsp argv += optind;
3367 b00d56cd 2018-04-01 stsp
3368 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3369 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3370 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3371 b72f483a 2019-02-05 stsp goto done;
3372 b72f483a 2019-02-05 stsp }
3373 927df6b7 2019-02-10 stsp if (argc <= 1) {
3374 b72f483a 2019-02-05 stsp if (repo_path)
3375 b72f483a 2019-02-05 stsp errx(1,
3376 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3377 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3378 1f03b8da 2020-03-20 stsp if (error)
3379 1f03b8da 2020-03-20 stsp goto done;
3380 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3381 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3382 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3383 927df6b7 2019-02-10 stsp goto done;
3384 927df6b7 2019-02-10 stsp }
3385 927df6b7 2019-02-10 stsp if (argc == 1) {
3386 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3387 cbd1af7a 2019-03-18 stsp argv[0]);
3388 927df6b7 2019-02-10 stsp if (error)
3389 927df6b7 2019-02-10 stsp goto done;
3390 927df6b7 2019-02-10 stsp } else {
3391 927df6b7 2019-02-10 stsp path = strdup("");
3392 927df6b7 2019-02-10 stsp if (path == NULL) {
3393 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3394 927df6b7 2019-02-10 stsp goto done;
3395 927df6b7 2019-02-10 stsp }
3396 927df6b7 2019-02-10 stsp }
3397 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3398 98eaaa12 2019-08-03 stsp if (diff_staged)
3399 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3400 98eaaa12 2019-08-03 stsp "objects in repository");
3401 15a94983 2018-12-23 stsp id_str1 = argv[0];
3402 15a94983 2018-12-23 stsp id_str2 = argv[1];
3403 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3404 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3405 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3406 30db809c 2019-06-05 stsp goto done;
3407 1f03b8da 2020-03-20 stsp if (worktree) {
3408 1f03b8da 2020-03-20 stsp repo_path = strdup(
3409 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3410 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3411 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3412 1f03b8da 2020-03-20 stsp goto done;
3413 1f03b8da 2020-03-20 stsp }
3414 1f03b8da 2020-03-20 stsp } else {
3415 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3416 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3417 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3418 1f03b8da 2020-03-20 stsp goto done;
3419 1f03b8da 2020-03-20 stsp }
3420 30db809c 2019-06-05 stsp }
3421 30db809c 2019-06-05 stsp }
3422 b00d56cd 2018-04-01 stsp } else
3423 b00d56cd 2018-04-01 stsp usage_diff();
3424 b00d56cd 2018-04-01 stsp
3425 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3426 76089277 2018-04-01 stsp free(repo_path);
3427 b00d56cd 2018-04-01 stsp if (error != NULL)
3428 b00d56cd 2018-04-01 stsp goto done;
3429 b00d56cd 2018-04-01 stsp
3430 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3431 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3432 c02c541e 2019-03-29 stsp if (error)
3433 c02c541e 2019-03-29 stsp goto done;
3434 c02c541e 2019-03-29 stsp
3435 30db809c 2019-06-05 stsp if (argc <= 1) {
3436 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3437 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3438 b72f483a 2019-02-05 stsp char *id_str;
3439 72ea6654 2019-07-27 stsp
3440 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3441 72ea6654 2019-07-27 stsp
3442 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3443 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3444 b72f483a 2019-02-05 stsp if (error)
3445 b72f483a 2019-02-05 stsp goto done;
3446 b72f483a 2019-02-05 stsp arg.repo = repo;
3447 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3448 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3449 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3450 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3451 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3452 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3453 b72f483a 2019-02-05 stsp
3454 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3455 72ea6654 2019-07-27 stsp if (error)
3456 72ea6654 2019-07-27 stsp goto done;
3457 72ea6654 2019-07-27 stsp
3458 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3459 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3460 b72f483a 2019-02-05 stsp free(id_str);
3461 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3462 b72f483a 2019-02-05 stsp goto done;
3463 b72f483a 2019-02-05 stsp }
3464 b72f483a 2019-02-05 stsp
3465 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3466 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3467 0adb7196 2019-08-11 stsp if (error)
3468 0adb7196 2019-08-11 stsp goto done;
3469 b00d56cd 2018-04-01 stsp
3470 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3471 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3472 0adb7196 2019-08-11 stsp if (error)
3473 0adb7196 2019-08-11 stsp goto done;
3474 b00d56cd 2018-04-01 stsp
3475 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3476 15a94983 2018-12-23 stsp if (error)
3477 15a94983 2018-12-23 stsp goto done;
3478 15a94983 2018-12-23 stsp
3479 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3480 15a94983 2018-12-23 stsp if (error)
3481 15a94983 2018-12-23 stsp goto done;
3482 15a94983 2018-12-23 stsp
3483 15a94983 2018-12-23 stsp if (type1 != type2) {
3484 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3485 b00d56cd 2018-04-01 stsp goto done;
3486 b00d56cd 2018-04-01 stsp }
3487 b00d56cd 2018-04-01 stsp
3488 15a94983 2018-12-23 stsp switch (type1) {
3489 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3490 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3491 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3492 b00d56cd 2018-04-01 stsp break;
3493 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3494 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3495 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3496 b00d56cd 2018-04-01 stsp break;
3497 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3498 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3499 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3500 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3501 b00d56cd 2018-04-01 stsp break;
3502 b00d56cd 2018-04-01 stsp default:
3503 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3504 b00d56cd 2018-04-01 stsp }
3505 b00d56cd 2018-04-01 stsp done:
3506 5e70831e 2019-05-28 stsp free(label1);
3507 5e70831e 2019-05-28 stsp free(label2);
3508 15a94983 2018-12-23 stsp free(id1);
3509 15a94983 2018-12-23 stsp free(id2);
3510 927df6b7 2019-02-10 stsp free(path);
3511 b72f483a 2019-02-05 stsp if (worktree)
3512 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3513 ad242220 2018-09-08 stsp if (repo) {
3514 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3515 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3516 ad242220 2018-09-08 stsp if (error == NULL)
3517 ad242220 2018-09-08 stsp error = repo_error;
3518 ad242220 2018-09-08 stsp }
3519 b00d56cd 2018-04-01 stsp return error;
3520 404c43c4 2018-06-21 stsp }
3521 404c43c4 2018-06-21 stsp
3522 404c43c4 2018-06-21 stsp __dead static void
3523 404c43c4 2018-06-21 stsp usage_blame(void)
3524 404c43c4 2018-06-21 stsp {
3525 9270e621 2019-02-05 stsp fprintf(stderr,
3526 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3527 404c43c4 2018-06-21 stsp getprogname());
3528 404c43c4 2018-06-21 stsp exit(1);
3529 b00d56cd 2018-04-01 stsp }
3530 b00d56cd 2018-04-01 stsp
3531 28315671 2019-08-14 stsp struct blame_line {
3532 28315671 2019-08-14 stsp int annotated;
3533 28315671 2019-08-14 stsp char *id_str;
3534 82f6abb8 2019-08-14 stsp char *committer;
3535 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3536 28315671 2019-08-14 stsp };
3537 28315671 2019-08-14 stsp
3538 28315671 2019-08-14 stsp struct blame_cb_args {
3539 28315671 2019-08-14 stsp struct blame_line *lines;
3540 28315671 2019-08-14 stsp int nlines;
3541 7ef28ff8 2019-08-14 stsp int nlines_prec;
3542 28315671 2019-08-14 stsp int lineno_cur;
3543 28315671 2019-08-14 stsp off_t *line_offsets;
3544 28315671 2019-08-14 stsp FILE *f;
3545 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3546 28315671 2019-08-14 stsp };
3547 28315671 2019-08-14 stsp
3548 404c43c4 2018-06-21 stsp static const struct got_error *
3549 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3550 28315671 2019-08-14 stsp {
3551 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3552 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3553 28315671 2019-08-14 stsp struct blame_line *bline;
3554 82f6abb8 2019-08-14 stsp char *line = NULL;
3555 28315671 2019-08-14 stsp size_t linesize = 0;
3556 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3557 28315671 2019-08-14 stsp off_t offset;
3558 bcb49d15 2019-08-14 stsp struct tm tm;
3559 bcb49d15 2019-08-14 stsp time_t committer_time;
3560 28315671 2019-08-14 stsp
3561 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3562 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3563 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3564 28315671 2019-08-14 stsp
3565 28315671 2019-08-14 stsp if (sigint_received)
3566 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3567 28315671 2019-08-14 stsp
3568 2839f8b9 2019-08-18 stsp if (lineno == -1)
3569 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3570 2839f8b9 2019-08-18 stsp
3571 28315671 2019-08-14 stsp /* Annotate this line. */
3572 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3573 28315671 2019-08-14 stsp if (bline->annotated)
3574 28315671 2019-08-14 stsp return NULL;
3575 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3576 28315671 2019-08-14 stsp if (err)
3577 28315671 2019-08-14 stsp return err;
3578 82f6abb8 2019-08-14 stsp
3579 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3580 82f6abb8 2019-08-14 stsp if (err)
3581 82f6abb8 2019-08-14 stsp goto done;
3582 82f6abb8 2019-08-14 stsp
3583 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3584 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3585 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3586 bcb49d15 2019-08-14 stsp goto done;
3587 bcb49d15 2019-08-14 stsp }
3588 bcb49d15 2019-08-14 stsp
3589 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3590 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3591 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3592 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3593 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3594 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3595 82f6abb8 2019-08-14 stsp goto done;
3596 82f6abb8 2019-08-14 stsp }
3597 28315671 2019-08-14 stsp bline->annotated = 1;
3598 28315671 2019-08-14 stsp
3599 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3600 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3601 28315671 2019-08-14 stsp if (!bline->annotated)
3602 82f6abb8 2019-08-14 stsp goto done;
3603 28315671 2019-08-14 stsp
3604 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3605 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3606 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3607 82f6abb8 2019-08-14 stsp goto done;
3608 82f6abb8 2019-08-14 stsp }
3609 28315671 2019-08-14 stsp
3610 28315671 2019-08-14 stsp while (bline->annotated) {
3611 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3612 82f6abb8 2019-08-14 stsp size_t len;
3613 82f6abb8 2019-08-14 stsp
3614 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3615 28315671 2019-08-14 stsp if (ferror(a->f))
3616 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3617 28315671 2019-08-14 stsp break;
3618 28315671 2019-08-14 stsp }
3619 82f6abb8 2019-08-14 stsp
3620 82f6abb8 2019-08-14 stsp committer = bline->committer;
3621 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3622 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3623 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3624 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3625 82f6abb8 2019-08-14 stsp if (at)
3626 82f6abb8 2019-08-14 stsp *at = '\0';
3627 82f6abb8 2019-08-14 stsp len = strlen(committer);
3628 82f6abb8 2019-08-14 stsp if (len >= 9)
3629 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3630 28315671 2019-08-14 stsp
3631 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3632 28315671 2019-08-14 stsp if (nl)
3633 28315671 2019-08-14 stsp *nl = '\0';
3634 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3635 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3636 28315671 2019-08-14 stsp
3637 28315671 2019-08-14 stsp a->lineno_cur++;
3638 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3639 28315671 2019-08-14 stsp }
3640 82f6abb8 2019-08-14 stsp done:
3641 82f6abb8 2019-08-14 stsp if (commit)
3642 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3643 28315671 2019-08-14 stsp free(line);
3644 28315671 2019-08-14 stsp return err;
3645 28315671 2019-08-14 stsp }
3646 28315671 2019-08-14 stsp
3647 28315671 2019-08-14 stsp static const struct got_error *
3648 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3649 404c43c4 2018-06-21 stsp {
3650 404c43c4 2018-06-21 stsp const struct got_error *error;
3651 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3652 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3653 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3654 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3655 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3656 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3657 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3658 28315671 2019-08-14 stsp struct blame_cb_args bca;
3659 28315671 2019-08-14 stsp int ch, obj_type, i;
3660 b02560ec 2019-08-19 stsp size_t filesize;
3661 90f3c347 2019-08-19 stsp
3662 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3663 404c43c4 2018-06-21 stsp
3664 404c43c4 2018-06-21 stsp #ifndef PROFILE
3665 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3666 36e2fb66 2019-01-04 stsp NULL) == -1)
3667 404c43c4 2018-06-21 stsp err(1, "pledge");
3668 404c43c4 2018-06-21 stsp #endif
3669 404c43c4 2018-06-21 stsp
3670 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3671 404c43c4 2018-06-21 stsp switch (ch) {
3672 404c43c4 2018-06-21 stsp case 'c':
3673 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3674 404c43c4 2018-06-21 stsp break;
3675 66bea077 2018-08-02 stsp case 'r':
3676 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3677 66bea077 2018-08-02 stsp if (repo_path == NULL)
3678 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3679 9ba1d308 2019-10-21 stsp optarg);
3680 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3681 66bea077 2018-08-02 stsp break;
3682 404c43c4 2018-06-21 stsp default:
3683 2deda0b9 2019-03-07 stsp usage_blame();
3684 404c43c4 2018-06-21 stsp /* NOTREACHED */
3685 404c43c4 2018-06-21 stsp }
3686 404c43c4 2018-06-21 stsp }
3687 404c43c4 2018-06-21 stsp
3688 404c43c4 2018-06-21 stsp argc -= optind;
3689 404c43c4 2018-06-21 stsp argv += optind;
3690 404c43c4 2018-06-21 stsp
3691 a39318fd 2018-08-02 stsp if (argc == 1)
3692 404c43c4 2018-06-21 stsp path = argv[0];
3693 a39318fd 2018-08-02 stsp else
3694 404c43c4 2018-06-21 stsp usage_blame();
3695 404c43c4 2018-06-21 stsp
3696 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3697 66bea077 2018-08-02 stsp if (cwd == NULL) {
3698 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3699 66bea077 2018-08-02 stsp goto done;
3700 66bea077 2018-08-02 stsp }
3701 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3702 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3703 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3704 66bea077 2018-08-02 stsp goto done;
3705 0c06baac 2019-02-05 stsp else
3706 0c06baac 2019-02-05 stsp error = NULL;
3707 0c06baac 2019-02-05 stsp if (worktree) {
3708 0c06baac 2019-02-05 stsp repo_path =
3709 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3710 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3711 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3712 1f03b8da 2020-03-20 stsp if (error)
3713 1f03b8da 2020-03-20 stsp goto done;
3714 1f03b8da 2020-03-20 stsp }
3715 0c06baac 2019-02-05 stsp } else {
3716 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3717 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3718 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3719 0c06baac 2019-02-05 stsp goto done;
3720 0c06baac 2019-02-05 stsp }
3721 66bea077 2018-08-02 stsp }
3722 66bea077 2018-08-02 stsp }
3723 36e2fb66 2019-01-04 stsp
3724 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3725 c02c541e 2019-03-29 stsp if (error != NULL)
3726 36e2fb66 2019-01-04 stsp goto done;
3727 66bea077 2018-08-02 stsp
3728 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3729 c02c541e 2019-03-29 stsp if (error)
3730 404c43c4 2018-06-21 stsp goto done;
3731 404c43c4 2018-06-21 stsp
3732 0c06baac 2019-02-05 stsp if (worktree) {
3733 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3734 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3735 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3736 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3737 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3738 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3739 6efaaa2d 2019-02-05 stsp path) == -1) {
3740 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3741 0c06baac 2019-02-05 stsp goto done;
3742 0c06baac 2019-02-05 stsp }
3743 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3744 0c06baac 2019-02-05 stsp free(p);
3745 0c06baac 2019-02-05 stsp } else {
3746 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3747 0c06baac 2019-02-05 stsp }
3748 0c06baac 2019-02-05 stsp if (error)
3749 66bea077 2018-08-02 stsp goto done;
3750 66bea077 2018-08-02 stsp
3751 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3752 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3753 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3754 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3755 404c43c4 2018-06-21 stsp if (error != NULL)
3756 66bea077 2018-08-02 stsp goto done;
3757 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3758 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3759 404c43c4 2018-06-21 stsp if (error != NULL)
3760 66bea077 2018-08-02 stsp goto done;
3761 404c43c4 2018-06-21 stsp } else {
3762 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3763 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3764 30837e32 2019-07-25 stsp if (error)
3765 66bea077 2018-08-02 stsp goto done;
3766 404c43c4 2018-06-21 stsp }
3767 404c43c4 2018-06-21 stsp
3768 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3769 28315671 2019-08-14 stsp if (error)
3770 28315671 2019-08-14 stsp goto done;
3771 28315671 2019-08-14 stsp
3772 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3773 28315671 2019-08-14 stsp if (error)
3774 28315671 2019-08-14 stsp goto done;
3775 28315671 2019-08-14 stsp
3776 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3777 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3778 28315671 2019-08-14 stsp goto done;
3779 28315671 2019-08-14 stsp }
3780 28315671 2019-08-14 stsp
3781 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3782 28315671 2019-08-14 stsp if (error)
3783 28315671 2019-08-14 stsp goto done;
3784 28315671 2019-08-14 stsp bca.f = got_opentemp();
3785 28315671 2019-08-14 stsp if (bca.f == NULL) {
3786 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3787 28315671 2019-08-14 stsp goto done;
3788 28315671 2019-08-14 stsp }
3789 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3790 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3791 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3792 28315671 2019-08-14 stsp goto done;
3793 28315671 2019-08-14 stsp
3794 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3795 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3796 b02560ec 2019-08-19 stsp bca.nlines--;
3797 b02560ec 2019-08-19 stsp
3798 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3799 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3800 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3801 28315671 2019-08-14 stsp goto done;
3802 28315671 2019-08-14 stsp }
3803 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3804 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3805 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3806 7ef28ff8 2019-08-14 stsp while (i > 0) {
3807 7ef28ff8 2019-08-14 stsp i /= 10;
3808 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3809 7ef28ff8 2019-08-14 stsp }
3810 82f6abb8 2019-08-14 stsp bca.repo = repo;
3811 7ef28ff8 2019-08-14 stsp
3812 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3813 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3814 404c43c4 2018-06-21 stsp done:
3815 66bea077 2018-08-02 stsp free(in_repo_path);
3816 66bea077 2018-08-02 stsp free(repo_path);
3817 66bea077 2018-08-02 stsp free(cwd);
3818 404c43c4 2018-06-21 stsp free(commit_id);
3819 28315671 2019-08-14 stsp free(obj_id);
3820 28315671 2019-08-14 stsp if (blob)
3821 28315671 2019-08-14 stsp got_object_blob_close(blob);
3822 0c06baac 2019-02-05 stsp if (worktree)
3823 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3824 ad242220 2018-09-08 stsp if (repo) {
3825 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3826 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3827 ad242220 2018-09-08 stsp if (error == NULL)
3828 ad242220 2018-09-08 stsp error = repo_error;
3829 ad242220 2018-09-08 stsp }
3830 3affba96 2019-09-22 stsp if (bca.lines) {
3831 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3832 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3833 3affba96 2019-09-22 stsp free(bline->id_str);
3834 3affba96 2019-09-22 stsp free(bline->committer);
3835 3affba96 2019-09-22 stsp }
3836 3affba96 2019-09-22 stsp free(bca.lines);
3837 28315671 2019-08-14 stsp }
3838 28315671 2019-08-14 stsp free(bca.line_offsets);
3839 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3840 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3841 404c43c4 2018-06-21 stsp return error;
3842 5de5890b 2018-10-18 stsp }
3843 5de5890b 2018-10-18 stsp
3844 5de5890b 2018-10-18 stsp __dead static void
3845 5de5890b 2018-10-18 stsp usage_tree(void)
3846 5de5890b 2018-10-18 stsp {
3847 c1669e2e 2019-01-09 stsp fprintf(stderr,
3848 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3849 5de5890b 2018-10-18 stsp getprogname());
3850 5de5890b 2018-10-18 stsp exit(1);
3851 5de5890b 2018-10-18 stsp }
3852 5de5890b 2018-10-18 stsp
3853 c1669e2e 2019-01-09 stsp static void
3854 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3855 c1669e2e 2019-01-09 stsp const char *root_path)
3856 c1669e2e 2019-01-09 stsp {
3857 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3858 848d6979 2019-08-12 stsp const char *modestr = "";
3859 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3860 5de5890b 2018-10-18 stsp
3861 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3862 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3863 c1669e2e 2019-01-09 stsp path++;
3864 c1669e2e 2019-01-09 stsp
3865 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3866 63c5ca5d 2019-08-24 stsp modestr = "$";
3867 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3868 848d6979 2019-08-12 stsp modestr = "@";
3869 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3870 848d6979 2019-08-12 stsp modestr = "/";
3871 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3872 848d6979 2019-08-12 stsp modestr = "*";
3873 848d6979 2019-08-12 stsp
3874 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3875 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3876 c1669e2e 2019-01-09 stsp }
3877 c1669e2e 2019-01-09 stsp
3878 5de5890b 2018-10-18 stsp static const struct got_error *
3879 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3880 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3881 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3882 5de5890b 2018-10-18 stsp {
3883 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3884 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3885 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3886 56e0773d 2019-11-28 stsp int nentries, i;
3887 5de5890b 2018-10-18 stsp
3888 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3889 5de5890b 2018-10-18 stsp if (err)
3890 5de5890b 2018-10-18 stsp goto done;
3891 5de5890b 2018-10-18 stsp
3892 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3893 5de5890b 2018-10-18 stsp if (err)
3894 5de5890b 2018-10-18 stsp goto done;
3895 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3896 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3897 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3898 5de5890b 2018-10-18 stsp char *id = NULL;
3899 84453469 2018-11-11 stsp
3900 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3901 84453469 2018-11-11 stsp break;
3902 84453469 2018-11-11 stsp
3903 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3904 5de5890b 2018-10-18 stsp if (show_ids) {
3905 5de5890b 2018-10-18 stsp char *id_str;
3906 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3907 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3908 5de5890b 2018-10-18 stsp if (err)
3909 5de5890b 2018-10-18 stsp goto done;
3910 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3911 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3912 5de5890b 2018-10-18 stsp free(id_str);
3913 5de5890b 2018-10-18 stsp goto done;
3914 5de5890b 2018-10-18 stsp }
3915 5de5890b 2018-10-18 stsp free(id_str);
3916 5de5890b 2018-10-18 stsp }
3917 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3918 5de5890b 2018-10-18 stsp free(id);
3919 c1669e2e 2019-01-09 stsp
3920 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3921 c1669e2e 2019-01-09 stsp char *child_path;
3922 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3923 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3924 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3925 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3926 c1669e2e 2019-01-09 stsp goto done;
3927 c1669e2e 2019-01-09 stsp }
3928 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3929 c1669e2e 2019-01-09 stsp root_path, repo);
3930 c1669e2e 2019-01-09 stsp free(child_path);
3931 c1669e2e 2019-01-09 stsp if (err)
3932 c1669e2e 2019-01-09 stsp goto done;
3933 c1669e2e 2019-01-09 stsp }
3934 5de5890b 2018-10-18 stsp }
3935 5de5890b 2018-10-18 stsp done:
3936 5de5890b 2018-10-18 stsp if (tree)
3937 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3938 5de5890b 2018-10-18 stsp free(tree_id);
3939 5de5890b 2018-10-18 stsp return err;
3940 404c43c4 2018-06-21 stsp }
3941 404c43c4 2018-06-21 stsp
3942 5de5890b 2018-10-18 stsp static const struct got_error *
3943 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3944 5de5890b 2018-10-18 stsp {
3945 5de5890b 2018-10-18 stsp const struct got_error *error;
3946 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3947 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3948 7a2c19d6 2019-02-05 stsp const char *path;
3949 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3950 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3951 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3952 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3953 5de5890b 2018-10-18 stsp int ch;
3954 5de5890b 2018-10-18 stsp
3955 5de5890b 2018-10-18 stsp #ifndef PROFILE
3956 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3957 0f8d269b 2019-01-04 stsp NULL) == -1)
3958 5de5890b 2018-10-18 stsp err(1, "pledge");
3959 5de5890b 2018-10-18 stsp #endif
3960 5de5890b 2018-10-18 stsp
3961 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3962 5de5890b 2018-10-18 stsp switch (ch) {
3963 5de5890b 2018-10-18 stsp case 'c':
3964 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3965 5de5890b 2018-10-18 stsp break;
3966 5de5890b 2018-10-18 stsp case 'r':
3967 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3968 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3969 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3970 9ba1d308 2019-10-21 stsp optarg);
3971 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3972 5de5890b 2018-10-18 stsp break;
3973 5de5890b 2018-10-18 stsp case 'i':
3974 5de5890b 2018-10-18 stsp show_ids = 1;
3975 5de5890b 2018-10-18 stsp break;
3976 c1669e2e 2019-01-09 stsp case 'R':
3977 c1669e2e 2019-01-09 stsp recurse = 1;
3978 c1669e2e 2019-01-09 stsp break;
3979 5de5890b 2018-10-18 stsp default:
3980 2deda0b9 2019-03-07 stsp usage_tree();
3981 5de5890b 2018-10-18 stsp /* NOTREACHED */
3982 5de5890b 2018-10-18 stsp }
3983 5de5890b 2018-10-18 stsp }
3984 5de5890b 2018-10-18 stsp
3985 5de5890b 2018-10-18 stsp argc -= optind;
3986 5de5890b 2018-10-18 stsp argv += optind;
3987 5de5890b 2018-10-18 stsp
3988 5de5890b 2018-10-18 stsp if (argc == 1)
3989 5de5890b 2018-10-18 stsp path = argv[0];
3990 5de5890b 2018-10-18 stsp else if (argc > 1)
3991 5de5890b 2018-10-18 stsp usage_tree();
3992 5de5890b 2018-10-18 stsp else
3993 7a2c19d6 2019-02-05 stsp path = NULL;
3994 7a2c19d6 2019-02-05 stsp
3995 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3996 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3997 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3998 9bf7a39b 2019-02-05 stsp goto done;
3999 9bf7a39b 2019-02-05 stsp }
4000 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4001 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4002 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4003 7a2c19d6 2019-02-05 stsp goto done;
4004 7a2c19d6 2019-02-05 stsp else
4005 7a2c19d6 2019-02-05 stsp error = NULL;
4006 7a2c19d6 2019-02-05 stsp if (worktree) {
4007 7a2c19d6 2019-02-05 stsp repo_path =
4008 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4009 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4010 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4011 7a2c19d6 2019-02-05 stsp if (error)
4012 7a2c19d6 2019-02-05 stsp goto done;
4013 7a2c19d6 2019-02-05 stsp } else {
4014 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4015 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4016 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4017 7a2c19d6 2019-02-05 stsp goto done;
4018 7a2c19d6 2019-02-05 stsp }
4019 5de5890b 2018-10-18 stsp }
4020 5de5890b 2018-10-18 stsp }
4021 5de5890b 2018-10-18 stsp
4022 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4023 5de5890b 2018-10-18 stsp if (error != NULL)
4024 c02c541e 2019-03-29 stsp goto done;
4025 c02c541e 2019-03-29 stsp
4026 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4027 c02c541e 2019-03-29 stsp if (error)
4028 5de5890b 2018-10-18 stsp goto done;
4029 5de5890b 2018-10-18 stsp
4030 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4031 9bf7a39b 2019-02-05 stsp if (worktree) {
4032 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4033 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4034 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4035 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4036 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4037 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4038 9bf7a39b 2019-02-05 stsp goto done;
4039 9bf7a39b 2019-02-05 stsp }
4040 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4041 9bf7a39b 2019-02-05 stsp free(p);
4042 9bf7a39b 2019-02-05 stsp if (error)
4043 9bf7a39b 2019-02-05 stsp goto done;
4044 9bf7a39b 2019-02-05 stsp } else
4045 9bf7a39b 2019-02-05 stsp path = "/";
4046 9bf7a39b 2019-02-05 stsp }
4047 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4048 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4049 9bf7a39b 2019-02-05 stsp if (error != NULL)
4050 9bf7a39b 2019-02-05 stsp goto done;
4051 9bf7a39b 2019-02-05 stsp }
4052 5de5890b 2018-10-18 stsp
4053 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4054 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4055 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4056 5de5890b 2018-10-18 stsp if (error != NULL)
4057 5de5890b 2018-10-18 stsp goto done;
4058 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4059 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4060 5de5890b 2018-10-18 stsp if (error != NULL)
4061 5de5890b 2018-10-18 stsp goto done;
4062 5de5890b 2018-10-18 stsp } else {
4063 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4064 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4065 30837e32 2019-07-25 stsp if (error)
4066 5de5890b 2018-10-18 stsp goto done;
4067 5de5890b 2018-10-18 stsp }
4068 5de5890b 2018-10-18 stsp
4069 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4070 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4071 5de5890b 2018-10-18 stsp done:
4072 5de5890b 2018-10-18 stsp free(in_repo_path);
4073 5de5890b 2018-10-18 stsp free(repo_path);
4074 5de5890b 2018-10-18 stsp free(cwd);
4075 5de5890b 2018-10-18 stsp free(commit_id);
4076 7a2c19d6 2019-02-05 stsp if (worktree)
4077 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4078 5de5890b 2018-10-18 stsp if (repo) {
4079 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4080 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4081 5de5890b 2018-10-18 stsp if (error == NULL)
4082 5de5890b 2018-10-18 stsp error = repo_error;
4083 5de5890b 2018-10-18 stsp }
4084 5de5890b 2018-10-18 stsp return error;
4085 5de5890b 2018-10-18 stsp }
4086 5de5890b 2018-10-18 stsp
4087 6bad629b 2019-02-04 stsp __dead static void
4088 6bad629b 2019-02-04 stsp usage_status(void)
4089 6bad629b 2019-02-04 stsp {
4090 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4091 6bad629b 2019-02-04 stsp exit(1);
4092 6bad629b 2019-02-04 stsp }
4093 5c860e29 2018-03-12 stsp
4094 b72f483a 2019-02-05 stsp static const struct got_error *
4095 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4096 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4097 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4098 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4099 6bad629b 2019-02-04 stsp {
4100 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4101 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4102 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4103 b72f483a 2019-02-05 stsp return NULL;
4104 6bad629b 2019-02-04 stsp }
4105 5c860e29 2018-03-12 stsp
4106 6bad629b 2019-02-04 stsp static const struct got_error *
4107 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4108 6bad629b 2019-02-04 stsp {
4109 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4110 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4111 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4112 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4113 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4114 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4115 a5edda0a 2019-07-27 stsp int ch;
4116 5c860e29 2018-03-12 stsp
4117 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4118 72ea6654 2019-07-27 stsp
4119 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4120 6bad629b 2019-02-04 stsp switch (ch) {
4121 5c860e29 2018-03-12 stsp default:
4122 2deda0b9 2019-03-07 stsp usage_status();
4123 6bad629b 2019-02-04 stsp /* NOTREACHED */
4124 5c860e29 2018-03-12 stsp }
4125 5c860e29 2018-03-12 stsp }
4126 5c860e29 2018-03-12 stsp
4127 6bad629b 2019-02-04 stsp argc -= optind;
4128 6bad629b 2019-02-04 stsp argv += optind;
4129 5c860e29 2018-03-12 stsp
4130 6bad629b 2019-02-04 stsp #ifndef PROFILE
4131 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4132 6bad629b 2019-02-04 stsp NULL) == -1)
4133 6bad629b 2019-02-04 stsp err(1, "pledge");
4134 f42b1b34 2018-03-12 stsp #endif
4135 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4136 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4137 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4138 927df6b7 2019-02-10 stsp goto done;
4139 927df6b7 2019-02-10 stsp }
4140 927df6b7 2019-02-10 stsp
4141 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4142 927df6b7 2019-02-10 stsp if (error != NULL)
4143 a5edda0a 2019-07-27 stsp goto done;
4144 6bad629b 2019-02-04 stsp
4145 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4146 c9956ddf 2019-09-08 stsp NULL);
4147 6bad629b 2019-02-04 stsp if (error != NULL)
4148 6bad629b 2019-02-04 stsp goto done;
4149 6bad629b 2019-02-04 stsp
4150 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4151 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4152 087fb88c 2019-08-04 stsp if (error)
4153 087fb88c 2019-08-04 stsp goto done;
4154 087fb88c 2019-08-04 stsp
4155 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4156 6bad629b 2019-02-04 stsp if (error)
4157 6bad629b 2019-02-04 stsp goto done;
4158 6bad629b 2019-02-04 stsp
4159 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4160 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4161 6bad629b 2019-02-04 stsp done:
4162 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4163 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4164 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4165 927df6b7 2019-02-10 stsp free(cwd);
4166 d0eebce4 2019-03-11 stsp return error;
4167 d0eebce4 2019-03-11 stsp }
4168 d0eebce4 2019-03-11 stsp
4169 d0eebce4 2019-03-11 stsp __dead static void
4170 d0eebce4 2019-03-11 stsp usage_ref(void)
4171 d0eebce4 2019-03-11 stsp {
4172 d0eebce4 2019-03-11 stsp fprintf(stderr,
4173 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
4174 d0eebce4 2019-03-11 stsp getprogname());
4175 d0eebce4 2019-03-11 stsp exit(1);
4176 d0eebce4 2019-03-11 stsp }
4177 d0eebce4 2019-03-11 stsp
4178 d0eebce4 2019-03-11 stsp static const struct got_error *
4179 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
4180 d0eebce4 2019-03-11 stsp {
4181 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4182 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4183 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4184 d0eebce4 2019-03-11 stsp
4185 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4186 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4187 d0eebce4 2019-03-11 stsp if (err)
4188 d0eebce4 2019-03-11 stsp return err;
4189 d0eebce4 2019-03-11 stsp
4190 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4191 d0eebce4 2019-03-11 stsp char *refstr;
4192 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4193 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4194 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4195 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4196 d0eebce4 2019-03-11 stsp free(refstr);
4197 d0eebce4 2019-03-11 stsp }
4198 d0eebce4 2019-03-11 stsp
4199 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4200 d0eebce4 2019-03-11 stsp return NULL;
4201 d0eebce4 2019-03-11 stsp }
4202 d0eebce4 2019-03-11 stsp
4203 d0eebce4 2019-03-11 stsp static const struct got_error *
4204 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4205 d0eebce4 2019-03-11 stsp {
4206 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4207 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4208 d0eebce4 2019-03-11 stsp
4209 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4210 d0eebce4 2019-03-11 stsp if (err)
4211 d0eebce4 2019-03-11 stsp return err;
4212 d0eebce4 2019-03-11 stsp
4213 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4214 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4215 d0eebce4 2019-03-11 stsp return err;
4216 d0eebce4 2019-03-11 stsp }
4217 d0eebce4 2019-03-11 stsp
4218 d0eebce4 2019-03-11 stsp static const struct got_error *
4219 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4220 d0eebce4 2019-03-11 stsp {
4221 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4222 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4223 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4224 d1644381 2019-07-14 stsp
4225 d1644381 2019-07-14 stsp /*
4226 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4227 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4228 d1644381 2019-07-14 stsp * an unintended typo.
4229 d1644381 2019-07-14 stsp */
4230 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4231 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4232 d0eebce4 2019-03-11 stsp
4233 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4234 dd88155e 2019-06-29 stsp repo);
4235 d83d9d5c 2019-05-13 stsp if (err) {
4236 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4237 d0eebce4 2019-03-11 stsp
4238 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4239 d83d9d5c 2019-05-13 stsp return err;
4240 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4241 d83d9d5c 2019-05-13 stsp if (err)
4242 d83d9d5c 2019-05-13 stsp return err;
4243 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4244 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4245 d83d9d5c 2019-05-13 stsp if (err)
4246 d83d9d5c 2019-05-13 stsp return err;
4247 d83d9d5c 2019-05-13 stsp }
4248 d83d9d5c 2019-05-13 stsp
4249 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4250 d0eebce4 2019-03-11 stsp if (err)
4251 d0eebce4 2019-03-11 stsp goto done;
4252 d0eebce4 2019-03-11 stsp
4253 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4254 d0eebce4 2019-03-11 stsp done:
4255 d0eebce4 2019-03-11 stsp if (ref)
4256 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4257 d0eebce4 2019-03-11 stsp free(id);
4258 d1c1ae5f 2019-08-12 stsp return err;
4259 d1c1ae5f 2019-08-12 stsp }
4260 d1c1ae5f 2019-08-12 stsp
4261 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4262 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4263 d1c1ae5f 2019-08-12 stsp {
4264 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4265 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4266 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4267 d1c1ae5f 2019-08-12 stsp
4268 d1c1ae5f 2019-08-12 stsp /*
4269 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4270 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4271 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4272 d1c1ae5f 2019-08-12 stsp */
4273 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4274 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4275 d1c1ae5f 2019-08-12 stsp
4276 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4277 d1c1ae5f 2019-08-12 stsp if (err)
4278 d1c1ae5f 2019-08-12 stsp return err;
4279 d1c1ae5f 2019-08-12 stsp
4280 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4281 d1c1ae5f 2019-08-12 stsp if (err)
4282 d1c1ae5f 2019-08-12 stsp goto done;
4283 d1c1ae5f 2019-08-12 stsp
4284 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4285 d1c1ae5f 2019-08-12 stsp done:
4286 d1c1ae5f 2019-08-12 stsp if (target_ref)
4287 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4288 d1c1ae5f 2019-08-12 stsp if (ref)
4289 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4290 d0eebce4 2019-03-11 stsp return err;
4291 d0eebce4 2019-03-11 stsp }
4292 d0eebce4 2019-03-11 stsp
4293 d0eebce4 2019-03-11 stsp static const struct got_error *
4294 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4295 d0eebce4 2019-03-11 stsp {
4296 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4297 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4298 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4299 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4300 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
4301 d0eebce4 2019-03-11 stsp const char *delref = NULL;
4302 d0eebce4 2019-03-11 stsp
4303 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
4304 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
4305 d0eebce4 2019-03-11 stsp switch (ch) {
4306 d0eebce4 2019-03-11 stsp case 'd':
4307 d0eebce4 2019-03-11 stsp delref = optarg;
4308 d0eebce4 2019-03-11 stsp break;
4309 d0eebce4 2019-03-11 stsp case 'r':
4310 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4311 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4312 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4313 9ba1d308 2019-10-21 stsp optarg);
4314 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4315 d0eebce4 2019-03-11 stsp break;
4316 d0eebce4 2019-03-11 stsp case 'l':
4317 d0eebce4 2019-03-11 stsp do_list = 1;
4318 d1c1ae5f 2019-08-12 stsp break;
4319 d1c1ae5f 2019-08-12 stsp case 's':
4320 d1c1ae5f 2019-08-12 stsp create_symref = 1;
4321 d0eebce4 2019-03-11 stsp break;
4322 d0eebce4 2019-03-11 stsp default:
4323 d0eebce4 2019-03-11 stsp usage_ref();
4324 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4325 d0eebce4 2019-03-11 stsp }
4326 d0eebce4 2019-03-11 stsp }
4327 d0eebce4 2019-03-11 stsp
4328 d0eebce4 2019-03-11 stsp if (do_list && delref)
4329 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
4330 d0eebce4 2019-03-11 stsp
4331 d0eebce4 2019-03-11 stsp argc -= optind;
4332 d0eebce4 2019-03-11 stsp argv += optind;
4333 d0eebce4 2019-03-11 stsp
4334 d0eebce4 2019-03-11 stsp if (do_list || delref) {
4335 d1c1ae5f 2019-08-12 stsp if (create_symref)
4336 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
4337 d1c1ae5f 2019-08-12 stsp "-l or -d options");
4338 d0eebce4 2019-03-11 stsp if (argc > 0)
4339 d0eebce4 2019-03-11 stsp usage_ref();
4340 d0eebce4 2019-03-11 stsp } else if (argc != 2)
4341 d0eebce4 2019-03-11 stsp usage_ref();
4342 d0eebce4 2019-03-11 stsp
4343 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4344 e0b57350 2019-03-12 stsp if (do_list) {
4345 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4346 e0b57350 2019-03-12 stsp NULL) == -1)
4347 e0b57350 2019-03-12 stsp err(1, "pledge");
4348 e0b57350 2019-03-12 stsp } else {
4349 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4350 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4351 e0b57350 2019-03-12 stsp err(1, "pledge");
4352 e0b57350 2019-03-12 stsp }
4353 d0eebce4 2019-03-11 stsp #endif
4354 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4355 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4356 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4357 d0eebce4 2019-03-11 stsp goto done;
4358 d0eebce4 2019-03-11 stsp }
4359 d0eebce4 2019-03-11 stsp
4360 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4361 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4362 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4363 d0eebce4 2019-03-11 stsp goto done;
4364 d0eebce4 2019-03-11 stsp else
4365 d0eebce4 2019-03-11 stsp error = NULL;
4366 d0eebce4 2019-03-11 stsp if (worktree) {
4367 d0eebce4 2019-03-11 stsp repo_path =
4368 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4369 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4370 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4371 d0eebce4 2019-03-11 stsp if (error)
4372 d0eebce4 2019-03-11 stsp goto done;
4373 d0eebce4 2019-03-11 stsp } else {
4374 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4375 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4376 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4377 d0eebce4 2019-03-11 stsp goto done;
4378 d0eebce4 2019-03-11 stsp }
4379 d0eebce4 2019-03-11 stsp }
4380 d0eebce4 2019-03-11 stsp }
4381 d0eebce4 2019-03-11 stsp
4382 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4383 d0eebce4 2019-03-11 stsp if (error != NULL)
4384 d0eebce4 2019-03-11 stsp goto done;
4385 d0eebce4 2019-03-11 stsp
4386 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4387 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4388 c02c541e 2019-03-29 stsp if (error)
4389 c02c541e 2019-03-29 stsp goto done;
4390 c02c541e 2019-03-29 stsp
4391 d0eebce4 2019-03-11 stsp if (do_list)
4392 d0eebce4 2019-03-11 stsp error = list_refs(repo);
4393 d0eebce4 2019-03-11 stsp else if (delref)
4394 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
4395 d1c1ae5f 2019-08-12 stsp else if (create_symref)
4396 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
4397 d0eebce4 2019-03-11 stsp else
4398 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
4399 4e759de4 2019-06-26 stsp done:
4400 4e759de4 2019-06-26 stsp if (repo)
4401 4e759de4 2019-06-26 stsp got_repo_close(repo);
4402 4e759de4 2019-06-26 stsp if (worktree)
4403 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4404 4e759de4 2019-06-26 stsp free(cwd);
4405 4e759de4 2019-06-26 stsp free(repo_path);
4406 4e759de4 2019-06-26 stsp return error;
4407 4e759de4 2019-06-26 stsp }
4408 4e759de4 2019-06-26 stsp
4409 4e759de4 2019-06-26 stsp __dead static void
4410 4e759de4 2019-06-26 stsp usage_branch(void)
4411 4e759de4 2019-06-26 stsp {
4412 4e759de4 2019-06-26 stsp fprintf(stderr,
4413 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4414 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4415 4e759de4 2019-06-26 stsp exit(1);
4416 4e759de4 2019-06-26 stsp }
4417 4e759de4 2019-06-26 stsp
4418 4e759de4 2019-06-26 stsp static const struct got_error *
4419 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4420 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4421 4e99b47e 2019-10-04 stsp {
4422 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4423 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4424 4e99b47e 2019-10-04 stsp char *refstr;
4425 4e99b47e 2019-10-04 stsp
4426 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4427 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4428 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4429 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4430 4e99b47e 2019-10-04 stsp
4431 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4432 4e99b47e 2019-10-04 stsp if (err)
4433 4e99b47e 2019-10-04 stsp return err;
4434 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4435 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4436 4e99b47e 2019-10-04 stsp marker = "* ";
4437 4e99b47e 2019-10-04 stsp else
4438 4e99b47e 2019-10-04 stsp marker = "~ ";
4439 4e99b47e 2019-10-04 stsp free(id);
4440 4e99b47e 2019-10-04 stsp }
4441 4e99b47e 2019-10-04 stsp
4442 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4443 4e99b47e 2019-10-04 stsp refname += 11;
4444 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4445 4e99b47e 2019-10-04 stsp refname += 18;
4446 4e99b47e 2019-10-04 stsp
4447 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4448 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4449 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4450 4e99b47e 2019-10-04 stsp
4451 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4452 4e99b47e 2019-10-04 stsp free(refstr);
4453 4e99b47e 2019-10-04 stsp return NULL;
4454 4e99b47e 2019-10-04 stsp }
4455 4e99b47e 2019-10-04 stsp
4456 4e99b47e 2019-10-04 stsp static const struct got_error *
4457 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4458 ad89fa31 2019-10-04 stsp {
4459 ad89fa31 2019-10-04 stsp const char *refname;
4460 ad89fa31 2019-10-04 stsp
4461 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4462 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4463 ad89fa31 2019-10-04 stsp
4464 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4465 ad89fa31 2019-10-04 stsp
4466 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4467 ad89fa31 2019-10-04 stsp refname += 11;
4468 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4469 ad89fa31 2019-10-04 stsp refname += 18;
4470 ad89fa31 2019-10-04 stsp
4471 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4472 ad89fa31 2019-10-04 stsp
4473 ad89fa31 2019-10-04 stsp return NULL;
4474 ad89fa31 2019-10-04 stsp }
4475 ad89fa31 2019-10-04 stsp
4476 ad89fa31 2019-10-04 stsp static const struct got_error *
4477 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4478 4e759de4 2019-06-26 stsp {
4479 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4480 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4481 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4482 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4483 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4484 4e759de4 2019-06-26 stsp
4485 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4486 ba882ee3 2019-07-11 stsp
4487 4e99b47e 2019-10-04 stsp if (worktree) {
4488 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4489 4e99b47e 2019-10-04 stsp worktree);
4490 4e99b47e 2019-10-04 stsp if (err)
4491 4e99b47e 2019-10-04 stsp return err;
4492 4e99b47e 2019-10-04 stsp
4493 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4494 4e99b47e 2019-10-04 stsp worktree);
4495 4e99b47e 2019-10-04 stsp if (err)
4496 4e99b47e 2019-10-04 stsp return err;
4497 4e99b47e 2019-10-04 stsp
4498 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4499 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4500 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4501 4e99b47e 2019-10-04 stsp if (err)
4502 4e99b47e 2019-10-04 stsp return err;
4503 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4504 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4505 4e99b47e 2019-10-04 stsp }
4506 4e99b47e 2019-10-04 stsp }
4507 4e99b47e 2019-10-04 stsp
4508 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4509 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4510 4e759de4 2019-06-26 stsp if (err)
4511 4e759de4 2019-06-26 stsp return err;
4512 4e759de4 2019-06-26 stsp
4513 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4514 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4515 4e759de4 2019-06-26 stsp
4516 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4517 4e759de4 2019-06-26 stsp return NULL;
4518 4e759de4 2019-06-26 stsp }
4519 4e759de4 2019-06-26 stsp
4520 4e759de4 2019-06-26 stsp static const struct got_error *
4521 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4522 45cd4e47 2019-08-25 stsp const char *branch_name)
4523 4e759de4 2019-06-26 stsp {
4524 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4525 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4526 4e759de4 2019-06-26 stsp char *refname;
4527 4e759de4 2019-06-26 stsp
4528 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4529 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4530 4e759de4 2019-06-26 stsp
4531 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4532 4e759de4 2019-06-26 stsp if (err)
4533 4e759de4 2019-06-26 stsp goto done;
4534 4e759de4 2019-06-26 stsp
4535 45cd4e47 2019-08-25 stsp if (worktree &&
4536 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4537 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4538 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4539 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4540 45cd4e47 2019-08-25 stsp goto done;
4541 45cd4e47 2019-08-25 stsp }
4542 45cd4e47 2019-08-25 stsp
4543 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4544 4e759de4 2019-06-26 stsp done:
4545 45cd4e47 2019-08-25 stsp if (ref)
4546 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4547 4e759de4 2019-06-26 stsp free(refname);
4548 4e759de4 2019-06-26 stsp return err;
4549 4e759de4 2019-06-26 stsp }
4550 4e759de4 2019-06-26 stsp
4551 4e759de4 2019-06-26 stsp static const struct got_error *
4552 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4553 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4554 4e759de4 2019-06-26 stsp {
4555 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4556 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4557 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4558 d3f84d51 2019-07-11 stsp
4559 d3f84d51 2019-07-11 stsp /*
4560 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4561 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4562 d3f84d51 2019-07-11 stsp * an unintended typo.
4563 d3f84d51 2019-07-11 stsp */
4564 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4565 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4566 4e759de4 2019-06-26 stsp
4567 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4568 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4569 4e759de4 2019-06-26 stsp goto done;
4570 4e759de4 2019-06-26 stsp }
4571 4e759de4 2019-06-26 stsp
4572 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4573 4e759de4 2019-06-26 stsp if (err == NULL) {
4574 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4575 4e759de4 2019-06-26 stsp goto done;
4576 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4577 4e759de4 2019-06-26 stsp goto done;
4578 4e759de4 2019-06-26 stsp
4579 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4580 4e759de4 2019-06-26 stsp if (err)
4581 4e759de4 2019-06-26 stsp goto done;
4582 4e759de4 2019-06-26 stsp
4583 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4584 d0eebce4 2019-03-11 stsp done:
4585 4e759de4 2019-06-26 stsp if (ref)
4586 4e759de4 2019-06-26 stsp got_ref_close(ref);
4587 4e759de4 2019-06-26 stsp free(base_refname);
4588 4e759de4 2019-06-26 stsp free(refname);
4589 4e759de4 2019-06-26 stsp return err;
4590 4e759de4 2019-06-26 stsp }
4591 4e759de4 2019-06-26 stsp
4592 4e759de4 2019-06-26 stsp static const struct got_error *
4593 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4594 4e759de4 2019-06-26 stsp {
4595 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4596 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4597 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4598 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4599 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4600 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4601 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4602 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4603 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4604 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4605 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4606 4e759de4 2019-06-26 stsp
4607 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4608 da76fce2 2020-02-24 stsp
4609 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4610 4e759de4 2019-06-26 stsp switch (ch) {
4611 a74f7e83 2019-11-10 stsp case 'c':
4612 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4613 a74f7e83 2019-11-10 stsp break;
4614 4e759de4 2019-06-26 stsp case 'd':
4615 4e759de4 2019-06-26 stsp delref = optarg;
4616 4e759de4 2019-06-26 stsp break;
4617 4e759de4 2019-06-26 stsp case 'r':
4618 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4619 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4620 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4621 9ba1d308 2019-10-21 stsp optarg);
4622 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4623 4e759de4 2019-06-26 stsp break;
4624 4e759de4 2019-06-26 stsp case 'l':
4625 4e759de4 2019-06-26 stsp do_list = 1;
4626 da76fce2 2020-02-24 stsp break;
4627 da76fce2 2020-02-24 stsp case 'n':
4628 da76fce2 2020-02-24 stsp do_update = 0;
4629 4e759de4 2019-06-26 stsp break;
4630 4e759de4 2019-06-26 stsp default:
4631 4e759de4 2019-06-26 stsp usage_branch();
4632 4e759de4 2019-06-26 stsp /* NOTREACHED */
4633 4e759de4 2019-06-26 stsp }
4634 4e759de4 2019-06-26 stsp }
4635 4e759de4 2019-06-26 stsp
4636 4e759de4 2019-06-26 stsp if (do_list && delref)
4637 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4638 4e759de4 2019-06-26 stsp
4639 4e759de4 2019-06-26 stsp argc -= optind;
4640 4e759de4 2019-06-26 stsp argv += optind;
4641 ad89fa31 2019-10-04 stsp
4642 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4643 ad89fa31 2019-10-04 stsp do_show = 1;
4644 4e759de4 2019-06-26 stsp
4645 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4646 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4647 a74f7e83 2019-11-10 stsp
4648 4e759de4 2019-06-26 stsp if (do_list || delref) {
4649 4e759de4 2019-06-26 stsp if (argc > 0)
4650 4e759de4 2019-06-26 stsp usage_branch();
4651 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4652 4e759de4 2019-06-26 stsp usage_branch();
4653 4e759de4 2019-06-26 stsp
4654 4e759de4 2019-06-26 stsp #ifndef PROFILE
4655 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4656 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4657 4e759de4 2019-06-26 stsp NULL) == -1)
4658 4e759de4 2019-06-26 stsp err(1, "pledge");
4659 4e759de4 2019-06-26 stsp } else {
4660 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4661 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4662 4e759de4 2019-06-26 stsp err(1, "pledge");
4663 4e759de4 2019-06-26 stsp }
4664 4e759de4 2019-06-26 stsp #endif
4665 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4666 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4667 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4668 4e759de4 2019-06-26 stsp goto done;
4669 4e759de4 2019-06-26 stsp }
4670 4e759de4 2019-06-26 stsp
4671 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4672 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4673 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4674 4e759de4 2019-06-26 stsp goto done;
4675 4e759de4 2019-06-26 stsp else
4676 4e759de4 2019-06-26 stsp error = NULL;
4677 4e759de4 2019-06-26 stsp if (worktree) {
4678 4e759de4 2019-06-26 stsp repo_path =
4679 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4680 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4681 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4682 4e759de4 2019-06-26 stsp if (error)
4683 4e759de4 2019-06-26 stsp goto done;
4684 4e759de4 2019-06-26 stsp } else {
4685 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4686 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4687 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4688 4e759de4 2019-06-26 stsp goto done;
4689 4e759de4 2019-06-26 stsp }
4690 4e759de4 2019-06-26 stsp }
4691 4e759de4 2019-06-26 stsp }
4692 4e759de4 2019-06-26 stsp
4693 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4694 4e759de4 2019-06-26 stsp if (error != NULL)
4695 4e759de4 2019-06-26 stsp goto done;
4696 4e759de4 2019-06-26 stsp
4697 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4698 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4699 4e759de4 2019-06-26 stsp if (error)
4700 4e759de4 2019-06-26 stsp goto done;
4701 4e759de4 2019-06-26 stsp
4702 ad89fa31 2019-10-04 stsp if (do_show)
4703 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4704 ad89fa31 2019-10-04 stsp else if (do_list)
4705 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4706 4e759de4 2019-06-26 stsp else if (delref)
4707 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4708 4e759de4 2019-06-26 stsp else {
4709 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4710 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4711 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4712 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4713 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4714 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4715 a74f7e83 2019-11-10 stsp if (error)
4716 a74f7e83 2019-11-10 stsp goto done;
4717 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4718 da76fce2 2020-02-24 stsp if (error)
4719 da76fce2 2020-02-24 stsp goto done;
4720 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4721 da76fce2 2020-02-24 stsp int did_something = 0;
4722 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4723 da76fce2 2020-02-24 stsp
4724 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4725 da76fce2 2020-02-24 stsp if (error)
4726 da76fce2 2020-02-24 stsp goto done;
4727 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4728 da76fce2 2020-02-24 stsp worktree);
4729 da76fce2 2020-02-24 stsp if (error)
4730 da76fce2 2020-02-24 stsp goto done;
4731 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4732 da76fce2 2020-02-24 stsp == -1) {
4733 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4734 da76fce2 2020-02-24 stsp goto done;
4735 da76fce2 2020-02-24 stsp }
4736 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4737 da76fce2 2020-02-24 stsp free(branch_refname);
4738 da76fce2 2020-02-24 stsp if (error)
4739 da76fce2 2020-02-24 stsp goto done;
4740 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4741 da76fce2 2020-02-24 stsp repo);
4742 da76fce2 2020-02-24 stsp if (error)
4743 da76fce2 2020-02-24 stsp goto done;
4744 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4745 da76fce2 2020-02-24 stsp commit_id);
4746 da76fce2 2020-02-24 stsp if (error)
4747 da76fce2 2020-02-24 stsp goto done;
4748 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4749 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4750 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4751 da76fce2 2020-02-24 stsp if (error)
4752 da76fce2 2020-02-24 stsp goto done;
4753 da76fce2 2020-02-24 stsp if (did_something)
4754 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4755 da76fce2 2020-02-24 stsp }
4756 4e759de4 2019-06-26 stsp }
4757 4e759de4 2019-06-26 stsp done:
4758 da76fce2 2020-02-24 stsp if (ref)
4759 da76fce2 2020-02-24 stsp got_ref_close(ref);
4760 d0eebce4 2019-03-11 stsp if (repo)
4761 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4762 d0eebce4 2019-03-11 stsp if (worktree)
4763 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4764 d0eebce4 2019-03-11 stsp free(cwd);
4765 d0eebce4 2019-03-11 stsp free(repo_path);
4766 da76fce2 2020-02-24 stsp free(commit_id);
4767 da76fce2 2020-02-24 stsp free(commit_id_str);
4768 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4769 da76fce2 2020-02-24 stsp free((char *)pe->path);
4770 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4771 d00136be 2019-03-26 stsp return error;
4772 d00136be 2019-03-26 stsp }
4773 d00136be 2019-03-26 stsp
4774 8e7bd50a 2019-08-22 stsp
4775 d00136be 2019-03-26 stsp __dead static void
4776 8e7bd50a 2019-08-22 stsp usage_tag(void)
4777 8e7bd50a 2019-08-22 stsp {
4778 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4779 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4780 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4781 8e7bd50a 2019-08-22 stsp exit(1);
4782 b8bad2ba 2019-08-23 stsp }
4783 b8bad2ba 2019-08-23 stsp
4784 b8bad2ba 2019-08-23 stsp #if 0
4785 b8bad2ba 2019-08-23 stsp static const struct got_error *
4786 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4787 b8bad2ba 2019-08-23 stsp {
4788 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4789 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4790 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4791 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4792 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4793 b8bad2ba 2019-08-23 stsp
4794 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4795 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4796 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4797 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4798 b8bad2ba 2019-08-23 stsp if (err)
4799 b8bad2ba 2019-08-23 stsp return err;
4800 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4801 b8bad2ba 2019-08-23 stsp continue;
4802 b8bad2ba 2019-08-23 stsp } else {
4803 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4804 b8bad2ba 2019-08-23 stsp if (err)
4805 b8bad2ba 2019-08-23 stsp break;
4806 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4807 b8bad2ba 2019-08-23 stsp free(re_id);
4808 b8bad2ba 2019-08-23 stsp if (err)
4809 b8bad2ba 2019-08-23 stsp break;
4810 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4811 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4812 b8bad2ba 2019-08-23 stsp }
4813 b8bad2ba 2019-08-23 stsp
4814 b8bad2ba 2019-08-23 stsp while (se) {
4815 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4816 b8bad2ba 2019-08-23 stsp if (err)
4817 b8bad2ba 2019-08-23 stsp break;
4818 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4819 b8bad2ba 2019-08-23 stsp free(se_id);
4820 b8bad2ba 2019-08-23 stsp if (err)
4821 b8bad2ba 2019-08-23 stsp break;
4822 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4823 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4824 b8bad2ba 2019-08-23 stsp
4825 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4826 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4827 b8bad2ba 2019-08-23 stsp if (err)
4828 b8bad2ba 2019-08-23 stsp return err;
4829 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4830 b8bad2ba 2019-08-23 stsp break;
4831 b8bad2ba 2019-08-23 stsp }
4832 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4833 b8bad2ba 2019-08-23 stsp continue;
4834 b8bad2ba 2019-08-23 stsp }
4835 b8bad2ba 2019-08-23 stsp }
4836 b8bad2ba 2019-08-23 stsp done:
4837 b8bad2ba 2019-08-23 stsp return err;
4838 8e7bd50a 2019-08-22 stsp }
4839 b8bad2ba 2019-08-23 stsp #endif
4840 b8bad2ba 2019-08-23 stsp
4841 b8bad2ba 2019-08-23 stsp static const struct got_error *
4842 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4843 8e7bd50a 2019-08-22 stsp {
4844 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4845 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4846 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4847 8e7bd50a 2019-08-22 stsp
4848 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4849 8e7bd50a 2019-08-22 stsp
4850 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4851 8e7bd50a 2019-08-22 stsp if (err)
4852 8e7bd50a 2019-08-22 stsp return err;
4853 8e7bd50a 2019-08-22 stsp
4854 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4855 8e7bd50a 2019-08-22 stsp const char *refname;
4856 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4857 8e7bd50a 2019-08-22 stsp char datebuf[26];
4858 d4efa91b 2020-01-14 stsp const char *tagger;
4859 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4860 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4861 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4862 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4863 8e7bd50a 2019-08-22 stsp
4864 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4865 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4866 8e7bd50a 2019-08-22 stsp continue;
4867 8e7bd50a 2019-08-22 stsp refname += 10;
4868 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4869 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4870 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4871 8e7bd50a 2019-08-22 stsp break;
4872 8e7bd50a 2019-08-22 stsp }
4873 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4874 8e7bd50a 2019-08-22 stsp free(refstr);
4875 8e7bd50a 2019-08-22 stsp
4876 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4877 8e7bd50a 2019-08-22 stsp if (err)
4878 8e7bd50a 2019-08-22 stsp break;
4879 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4880 d4efa91b 2020-01-14 stsp if (err) {
4881 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4882 d4efa91b 2020-01-14 stsp free(id);
4883 d4efa91b 2020-01-14 stsp break;
4884 d4efa91b 2020-01-14 stsp }
4885 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4886 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4887 d4efa91b 2020-01-14 stsp if (err) {
4888 d4efa91b 2020-01-14 stsp free(id);
4889 d4efa91b 2020-01-14 stsp break;
4890 d4efa91b 2020-01-14 stsp }
4891 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4892 d4efa91b 2020-01-14 stsp tagger_time =
4893 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4894 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4895 d4efa91b 2020-01-14 stsp free(id);
4896 d4efa91b 2020-01-14 stsp if (err)
4897 d4efa91b 2020-01-14 stsp break;
4898 d4efa91b 2020-01-14 stsp } else {
4899 d4efa91b 2020-01-14 stsp free(id);
4900 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4901 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4902 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4903 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4904 d4efa91b 2020-01-14 stsp if (err)
4905 d4efa91b 2020-01-14 stsp break;
4906 d4efa91b 2020-01-14 stsp }
4907 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4908 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4909 2417344c 2019-08-23 stsp if (datestr)
4910 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4911 d4efa91b 2020-01-14 stsp if (commit)
4912 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4913 d4efa91b 2020-01-14 stsp else {
4914 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4915 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4916 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4917 d4efa91b 2020-01-14 stsp id_str);
4918 d4efa91b 2020-01-14 stsp break;
4919 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4920 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4921 d4efa91b 2020-01-14 stsp id_str);
4922 d4efa91b 2020-01-14 stsp break;
4923 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4924 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4925 d4efa91b 2020-01-14 stsp id_str);
4926 d4efa91b 2020-01-14 stsp break;
4927 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4928 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4929 d4efa91b 2020-01-14 stsp id_str);
4930 d4efa91b 2020-01-14 stsp break;
4931 d4efa91b 2020-01-14 stsp default:
4932 d4efa91b 2020-01-14 stsp break;
4933 d4efa91b 2020-01-14 stsp }
4934 8e7bd50a 2019-08-22 stsp }
4935 8e7bd50a 2019-08-22 stsp free(id_str);
4936 d4efa91b 2020-01-14 stsp if (commit) {
4937 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4938 d4efa91b 2020-01-14 stsp if (err)
4939 d4efa91b 2020-01-14 stsp break;
4940 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4941 d4efa91b 2020-01-14 stsp } else {
4942 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4943 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4944 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4945 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4946 d4efa91b 2020-01-14 stsp break;
4947 d4efa91b 2020-01-14 stsp }
4948 8e7bd50a 2019-08-22 stsp }
4949 8e7bd50a 2019-08-22 stsp
4950 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4951 8e7bd50a 2019-08-22 stsp do {
4952 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4953 8e7bd50a 2019-08-22 stsp if (line)
4954 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4955 8e7bd50a 2019-08-22 stsp } while (line);
4956 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4957 8e7bd50a 2019-08-22 stsp }
4958 8e7bd50a 2019-08-22 stsp
4959 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4960 8e7bd50a 2019-08-22 stsp return NULL;
4961 8e7bd50a 2019-08-22 stsp }
4962 8e7bd50a 2019-08-22 stsp
4963 8e7bd50a 2019-08-22 stsp static const struct got_error *
4964 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4965 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4966 8e7bd50a 2019-08-22 stsp {
4967 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4968 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4969 f372d5cd 2019-10-21 stsp char *editor = NULL;
4970 8e7bd50a 2019-08-22 stsp int fd = -1;
4971 8e7bd50a 2019-08-22 stsp
4972 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4973 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4974 8e7bd50a 2019-08-22 stsp goto done;
4975 8e7bd50a 2019-08-22 stsp }
4976 8e7bd50a 2019-08-22 stsp
4977 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4978 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4979 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4980 8e7bd50a 2019-08-22 stsp goto done;
4981 8e7bd50a 2019-08-22 stsp }
4982 8e7bd50a 2019-08-22 stsp
4983 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4984 8e7bd50a 2019-08-22 stsp if (err)
4985 8e7bd50a 2019-08-22 stsp goto done;
4986 8e7bd50a 2019-08-22 stsp
4987 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4988 8e7bd50a 2019-08-22 stsp close(fd);
4989 8e7bd50a 2019-08-22 stsp
4990 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4991 8e7bd50a 2019-08-22 stsp if (err)
4992 8e7bd50a 2019-08-22 stsp goto done;
4993 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4994 8e7bd50a 2019-08-22 stsp done:
4995 8e7bd50a 2019-08-22 stsp free(initial_content);
4996 8e7bd50a 2019-08-22 stsp free(template);
4997 8e7bd50a 2019-08-22 stsp free(editor);
4998 8e7bd50a 2019-08-22 stsp
4999 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5000 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5001 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5002 8e7bd50a 2019-08-22 stsp if (err) {
5003 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5004 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5005 8e7bd50a 2019-08-22 stsp }
5006 8e7bd50a 2019-08-22 stsp }
5007 8e7bd50a 2019-08-22 stsp return err;
5008 8e7bd50a 2019-08-22 stsp }
5009 8e7bd50a 2019-08-22 stsp
5010 8e7bd50a 2019-08-22 stsp static const struct got_error *
5011 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5012 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5013 8e7bd50a 2019-08-22 stsp {
5014 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5015 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5016 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5017 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5018 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5019 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5020 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5021 8e7bd50a 2019-08-22 stsp
5022 8e7bd50a 2019-08-22 stsp /*
5023 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5024 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5025 8e7bd50a 2019-08-22 stsp * an unintended typo.
5026 8e7bd50a 2019-08-22 stsp */
5027 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5028 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5029 8e7bd50a 2019-08-22 stsp
5030 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5031 8e7bd50a 2019-08-22 stsp if (err)
5032 8e7bd50a 2019-08-22 stsp return err;
5033 8e7bd50a 2019-08-22 stsp
5034 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5035 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5036 8e7bd50a 2019-08-22 stsp if (err)
5037 8e7bd50a 2019-08-22 stsp goto done;
5038 8e7bd50a 2019-08-22 stsp
5039 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5040 8e7bd50a 2019-08-22 stsp if (err)
5041 8e7bd50a 2019-08-22 stsp goto done;
5042 8e7bd50a 2019-08-22 stsp
5043 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5044 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5045 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5046 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5047 8e7bd50a 2019-08-22 stsp goto done;
5048 8e7bd50a 2019-08-22 stsp }
5049 8e7bd50a 2019-08-22 stsp tag_name += 10;
5050 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5051 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5052 8e7bd50a 2019-08-22 stsp goto done;
5053 8e7bd50a 2019-08-22 stsp }
5054 8e7bd50a 2019-08-22 stsp
5055 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5056 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5057 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5058 8e7bd50a 2019-08-22 stsp goto done;
5059 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5060 8e7bd50a 2019-08-22 stsp goto done;
5061 8e7bd50a 2019-08-22 stsp
5062 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5063 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5064 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5065 f372d5cd 2019-10-21 stsp if (err) {
5066 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5067 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5068 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5069 8e7bd50a 2019-08-22 stsp goto done;
5070 f372d5cd 2019-10-21 stsp }
5071 8e7bd50a 2019-08-22 stsp }
5072 8e7bd50a 2019-08-22 stsp
5073 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5074 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5075 f372d5cd 2019-10-21 stsp if (err) {
5076 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5077 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5078 8e7bd50a 2019-08-22 stsp goto done;
5079 f372d5cd 2019-10-21 stsp }
5080 8e7bd50a 2019-08-22 stsp
5081 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5082 f372d5cd 2019-10-21 stsp if (err) {
5083 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5084 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5085 8e7bd50a 2019-08-22 stsp goto done;
5086 f372d5cd 2019-10-21 stsp }
5087 8e7bd50a 2019-08-22 stsp
5088 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5089 f372d5cd 2019-10-21 stsp if (err) {
5090 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5091 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5092 f372d5cd 2019-10-21 stsp goto done;
5093 f372d5cd 2019-10-21 stsp }
5094 8e7bd50a 2019-08-22 stsp
5095 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5096 f372d5cd 2019-10-21 stsp if (err) {
5097 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5098 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5099 f372d5cd 2019-10-21 stsp goto done;
5100 8e7bd50a 2019-08-22 stsp }
5101 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5102 8e7bd50a 2019-08-22 stsp done:
5103 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5104 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5105 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5106 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5107 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5108 f372d5cd 2019-10-21 stsp free(tag_id_str);
5109 8e7bd50a 2019-08-22 stsp if (ref)
5110 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5111 8e7bd50a 2019-08-22 stsp free(commit_id);
5112 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5113 8e7bd50a 2019-08-22 stsp free(refname);
5114 8e7bd50a 2019-08-22 stsp free(tagmsg);
5115 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5116 aba9c984 2019-09-08 stsp free(tagger);
5117 8e7bd50a 2019-08-22 stsp return err;
5118 8e7bd50a 2019-08-22 stsp }
5119 8e7bd50a 2019-08-22 stsp
5120 8e7bd50a 2019-08-22 stsp static const struct got_error *
5121 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5122 8e7bd50a 2019-08-22 stsp {
5123 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5124 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5125 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5126 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5127 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5128 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5129 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5130 8e7bd50a 2019-08-22 stsp
5131 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5132 8e7bd50a 2019-08-22 stsp switch (ch) {
5133 80106605 2020-02-24 stsp case 'c':
5134 80106605 2020-02-24 stsp commit_id_arg = optarg;
5135 80106605 2020-02-24 stsp break;
5136 8e7bd50a 2019-08-22 stsp case 'm':
5137 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5138 8e7bd50a 2019-08-22 stsp break;
5139 8e7bd50a 2019-08-22 stsp case 'r':
5140 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5141 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5142 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5143 9ba1d308 2019-10-21 stsp optarg);
5144 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5145 8e7bd50a 2019-08-22 stsp break;
5146 8e7bd50a 2019-08-22 stsp case 'l':
5147 8e7bd50a 2019-08-22 stsp do_list = 1;
5148 8e7bd50a 2019-08-22 stsp break;
5149 8e7bd50a 2019-08-22 stsp default:
5150 8e7bd50a 2019-08-22 stsp usage_tag();
5151 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5152 8e7bd50a 2019-08-22 stsp }
5153 8e7bd50a 2019-08-22 stsp }
5154 8e7bd50a 2019-08-22 stsp
5155 8e7bd50a 2019-08-22 stsp argc -= optind;
5156 8e7bd50a 2019-08-22 stsp argv += optind;
5157 8e7bd50a 2019-08-22 stsp
5158 8e7bd50a 2019-08-22 stsp if (do_list) {
5159 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5160 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
5161 8e7bd50a 2019-08-22 stsp if (tagmsg)
5162 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5163 8e7bd50a 2019-08-22 stsp if (argc > 0)
5164 8e7bd50a 2019-08-22 stsp usage_tag();
5165 80106605 2020-02-24 stsp } else if (argc != 1)
5166 8e7bd50a 2019-08-22 stsp usage_tag();
5167 80106605 2020-02-24 stsp
5168 a2887370 2019-08-23 stsp tag_name = argv[0];
5169 8e7bd50a 2019-08-22 stsp
5170 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5171 8e7bd50a 2019-08-22 stsp if (do_list) {
5172 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5173 8e7bd50a 2019-08-22 stsp NULL) == -1)
5174 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5175 8e7bd50a 2019-08-22 stsp } else {
5176 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5177 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5178 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5179 8e7bd50a 2019-08-22 stsp }
5180 8e7bd50a 2019-08-22 stsp #endif
5181 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5182 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5183 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5184 8e7bd50a 2019-08-22 stsp goto done;
5185 8e7bd50a 2019-08-22 stsp }
5186 8e7bd50a 2019-08-22 stsp
5187 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5188 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5189 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5190 8e7bd50a 2019-08-22 stsp goto done;
5191 8e7bd50a 2019-08-22 stsp else
5192 8e7bd50a 2019-08-22 stsp error = NULL;
5193 8e7bd50a 2019-08-22 stsp if (worktree) {
5194 8e7bd50a 2019-08-22 stsp repo_path =
5195 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5196 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5197 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5198 8e7bd50a 2019-08-22 stsp if (error)
5199 8e7bd50a 2019-08-22 stsp goto done;
5200 8e7bd50a 2019-08-22 stsp } else {
5201 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5202 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5203 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5204 8e7bd50a 2019-08-22 stsp goto done;
5205 8e7bd50a 2019-08-22 stsp }
5206 8e7bd50a 2019-08-22 stsp }
5207 8e7bd50a 2019-08-22 stsp }
5208 8e7bd50a 2019-08-22 stsp
5209 8e7bd50a 2019-08-22 stsp if (do_list) {
5210 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5211 c9956ddf 2019-09-08 stsp if (error != NULL)
5212 c9956ddf 2019-09-08 stsp goto done;
5213 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5214 8e7bd50a 2019-08-22 stsp if (error)
5215 8e7bd50a 2019-08-22 stsp goto done;
5216 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5217 8e7bd50a 2019-08-22 stsp } else {
5218 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5219 c9956ddf 2019-09-08 stsp if (error)
5220 c9956ddf 2019-09-08 stsp goto done;
5221 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5222 c9956ddf 2019-09-08 stsp if (error != NULL)
5223 c9956ddf 2019-09-08 stsp goto done;
5224 c9956ddf 2019-09-08 stsp
5225 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5226 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5227 8e7bd50a 2019-08-22 stsp if (error)
5228 8e7bd50a 2019-08-22 stsp goto done;
5229 8e7bd50a 2019-08-22 stsp }
5230 8e7bd50a 2019-08-22 stsp
5231 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5232 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5233 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5234 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5235 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5236 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5237 8e7bd50a 2019-08-22 stsp if (error)
5238 8e7bd50a 2019-08-22 stsp goto done;
5239 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5240 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5241 8e7bd50a 2019-08-22 stsp if (error)
5242 8e7bd50a 2019-08-22 stsp goto done;
5243 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5244 8e7bd50a 2019-08-22 stsp free(commit_id);
5245 8e7bd50a 2019-08-22 stsp if (error)
5246 8e7bd50a 2019-08-22 stsp goto done;
5247 8e7bd50a 2019-08-22 stsp }
5248 8e7bd50a 2019-08-22 stsp
5249 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5250 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5251 8e7bd50a 2019-08-22 stsp }
5252 8e7bd50a 2019-08-22 stsp done:
5253 8e7bd50a 2019-08-22 stsp if (repo)
5254 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5255 8e7bd50a 2019-08-22 stsp if (worktree)
5256 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5257 8e7bd50a 2019-08-22 stsp free(cwd);
5258 8e7bd50a 2019-08-22 stsp free(repo_path);
5259 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5260 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5261 8e7bd50a 2019-08-22 stsp return error;
5262 8e7bd50a 2019-08-22 stsp }
5263 8e7bd50a 2019-08-22 stsp
5264 8e7bd50a 2019-08-22 stsp __dead static void
5265 d00136be 2019-03-26 stsp usage_add(void)
5266 d00136be 2019-03-26 stsp {
5267 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5268 022fae89 2019-12-06 tracey getprogname());
5269 d00136be 2019-03-26 stsp exit(1);
5270 d00136be 2019-03-26 stsp }
5271 d00136be 2019-03-26 stsp
5272 d00136be 2019-03-26 stsp static const struct got_error *
5273 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5274 4e68cba3 2019-11-23 stsp {
5275 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5276 4e68cba3 2019-11-23 stsp path++;
5277 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5278 4e68cba3 2019-11-23 stsp return NULL;
5279 4e68cba3 2019-11-23 stsp }
5280 4e68cba3 2019-11-23 stsp
5281 4e68cba3 2019-11-23 stsp static const struct got_error *
5282 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5283 d00136be 2019-03-26 stsp {
5284 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5285 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5286 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5287 1dd54920 2019-05-11 stsp char *cwd = NULL;
5288 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5289 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5290 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5291 1dd54920 2019-05-11 stsp
5292 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5293 d00136be 2019-03-26 stsp
5294 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5295 d00136be 2019-03-26 stsp switch (ch) {
5296 022fae89 2019-12-06 tracey case 'I':
5297 022fae89 2019-12-06 tracey no_ignores = 1;
5298 022fae89 2019-12-06 tracey break;
5299 4e68cba3 2019-11-23 stsp case 'R':
5300 4e68cba3 2019-11-23 stsp can_recurse = 1;
5301 4e68cba3 2019-11-23 stsp break;
5302 d00136be 2019-03-26 stsp default:
5303 d00136be 2019-03-26 stsp usage_add();
5304 d00136be 2019-03-26 stsp /* NOTREACHED */
5305 d00136be 2019-03-26 stsp }
5306 d00136be 2019-03-26 stsp }
5307 d00136be 2019-03-26 stsp
5308 d00136be 2019-03-26 stsp argc -= optind;
5309 d00136be 2019-03-26 stsp argv += optind;
5310 d00136be 2019-03-26 stsp
5311 43012d58 2019-07-14 stsp #ifndef PROFILE
5312 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5313 43012d58 2019-07-14 stsp NULL) == -1)
5314 43012d58 2019-07-14 stsp err(1, "pledge");
5315 43012d58 2019-07-14 stsp #endif
5316 723c305c 2019-05-11 jcs if (argc < 1)
5317 d00136be 2019-03-26 stsp usage_add();
5318 d00136be 2019-03-26 stsp
5319 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5320 d00136be 2019-03-26 stsp if (cwd == NULL) {
5321 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5322 d00136be 2019-03-26 stsp goto done;
5323 d00136be 2019-03-26 stsp }
5324 723c305c 2019-05-11 jcs
5325 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5326 d00136be 2019-03-26 stsp if (error)
5327 d00136be 2019-03-26 stsp goto done;
5328 d00136be 2019-03-26 stsp
5329 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5330 c9956ddf 2019-09-08 stsp NULL);
5331 031a5338 2019-03-26 stsp if (error != NULL)
5332 031a5338 2019-03-26 stsp goto done;
5333 031a5338 2019-03-26 stsp
5334 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5335 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5336 d00136be 2019-03-26 stsp if (error)
5337 d00136be 2019-03-26 stsp goto done;
5338 d00136be 2019-03-26 stsp
5339 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5340 6d022e97 2019-08-04 stsp if (error)
5341 022fae89 2019-12-06 tracey goto done;
5342 022fae89 2019-12-06 tracey
5343 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5344 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5345 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5346 6d022e97 2019-08-04 stsp goto done;
5347 723c305c 2019-05-11 jcs
5348 022fae89 2019-12-06 tracey }
5349 022fae89 2019-12-06 tracey
5350 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5351 4e68cba3 2019-11-23 stsp char *ondisk_path;
5352 4e68cba3 2019-11-23 stsp struct stat sb;
5353 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5354 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5355 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5356 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5357 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5358 4e68cba3 2019-11-23 stsp goto done;
5359 4e68cba3 2019-11-23 stsp }
5360 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5361 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5362 4e68cba3 2019-11-23 stsp free(ondisk_path);
5363 4e68cba3 2019-11-23 stsp continue;
5364 4e68cba3 2019-11-23 stsp }
5365 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5366 4e68cba3 2019-11-23 stsp ondisk_path);
5367 4e68cba3 2019-11-23 stsp free(ondisk_path);
5368 4e68cba3 2019-11-23 stsp goto done;
5369 4e68cba3 2019-11-23 stsp }
5370 4e68cba3 2019-11-23 stsp free(ondisk_path);
5371 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5372 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5373 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5374 4e68cba3 2019-11-23 stsp goto done;
5375 4e68cba3 2019-11-23 stsp }
5376 4e68cba3 2019-11-23 stsp }
5377 4e68cba3 2019-11-23 stsp }
5378 022fae89 2019-12-06 tracey
5379 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5380 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5381 d00136be 2019-03-26 stsp done:
5382 031a5338 2019-03-26 stsp if (repo)
5383 031a5338 2019-03-26 stsp got_repo_close(repo);
5384 d00136be 2019-03-26 stsp if (worktree)
5385 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5386 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5387 1dd54920 2019-05-11 stsp free((char *)pe->path);
5388 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5389 2ec1f75b 2019-03-26 stsp free(cwd);
5390 2ec1f75b 2019-03-26 stsp return error;
5391 2ec1f75b 2019-03-26 stsp }
5392 2ec1f75b 2019-03-26 stsp
5393 2ec1f75b 2019-03-26 stsp __dead static void
5394 648e4ef7 2019-07-09 stsp usage_remove(void)
5395 2ec1f75b 2019-03-26 stsp {
5396 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5397 f2a9dc41 2019-12-13 tracey getprogname());
5398 2ec1f75b 2019-03-26 stsp exit(1);
5399 2a06fe5f 2019-08-24 stsp }
5400 2a06fe5f 2019-08-24 stsp
5401 2a06fe5f 2019-08-24 stsp static const struct got_error *
5402 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5403 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5404 2a06fe5f 2019-08-24 stsp {
5405 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5406 f2a9dc41 2019-12-13 tracey path++;
5407 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5408 2a06fe5f 2019-08-24 stsp return NULL;
5409 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5410 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5411 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5412 2a06fe5f 2019-08-24 stsp return NULL;
5413 2ec1f75b 2019-03-26 stsp }
5414 2ec1f75b 2019-03-26 stsp
5415 2ec1f75b 2019-03-26 stsp static const struct got_error *
5416 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5417 2ec1f75b 2019-03-26 stsp {
5418 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5419 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5420 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5421 17ed4618 2019-06-02 stsp char *cwd = NULL;
5422 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5423 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5424 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5425 2ec1f75b 2019-03-26 stsp
5426 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5427 17ed4618 2019-06-02 stsp
5428 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5429 2ec1f75b 2019-03-26 stsp switch (ch) {
5430 2ec1f75b 2019-03-26 stsp case 'f':
5431 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5432 2ec1f75b 2019-03-26 stsp break;
5433 70e3e7f5 2019-12-13 tracey case 'k':
5434 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5435 70e3e7f5 2019-12-13 tracey break;
5436 f2a9dc41 2019-12-13 tracey case 'R':
5437 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5438 f2a9dc41 2019-12-13 tracey break;
5439 2ec1f75b 2019-03-26 stsp default:
5440 f2a9dc41 2019-12-13 tracey usage_remove();
5441 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5442 2ec1f75b 2019-03-26 stsp }
5443 2ec1f75b 2019-03-26 stsp }
5444 2ec1f75b 2019-03-26 stsp
5445 2ec1f75b 2019-03-26 stsp argc -= optind;
5446 2ec1f75b 2019-03-26 stsp argv += optind;
5447 2ec1f75b 2019-03-26 stsp
5448 43012d58 2019-07-14 stsp #ifndef PROFILE
5449 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5450 43012d58 2019-07-14 stsp NULL) == -1)
5451 43012d58 2019-07-14 stsp err(1, "pledge");
5452 43012d58 2019-07-14 stsp #endif
5453 17ed4618 2019-06-02 stsp if (argc < 1)
5454 648e4ef7 2019-07-09 stsp usage_remove();
5455 2ec1f75b 2019-03-26 stsp
5456 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5457 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5458 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5459 2ec1f75b 2019-03-26 stsp goto done;
5460 2ec1f75b 2019-03-26 stsp }
5461 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5462 2ec1f75b 2019-03-26 stsp if (error)
5463 2ec1f75b 2019-03-26 stsp goto done;
5464 2ec1f75b 2019-03-26 stsp
5465 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5466 c9956ddf 2019-09-08 stsp NULL);
5467 2af4a041 2019-05-11 jcs if (error)
5468 2ec1f75b 2019-03-26 stsp goto done;
5469 2ec1f75b 2019-03-26 stsp
5470 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5471 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5472 2ec1f75b 2019-03-26 stsp if (error)
5473 2ec1f75b 2019-03-26 stsp goto done;
5474 2ec1f75b 2019-03-26 stsp
5475 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5476 6d022e97 2019-08-04 stsp if (error)
5477 6d022e97 2019-08-04 stsp goto done;
5478 17ed4618 2019-06-02 stsp
5479 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5480 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5481 f2a9dc41 2019-12-13 tracey struct stat sb;
5482 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5483 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5484 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5485 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5486 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5487 f2a9dc41 2019-12-13 tracey goto done;
5488 f2a9dc41 2019-12-13 tracey }
5489 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5490 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5491 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5492 f2a9dc41 2019-12-13 tracey continue;
5493 f2a9dc41 2019-12-13 tracey }
5494 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5495 f2a9dc41 2019-12-13 tracey ondisk_path);
5496 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5497 f2a9dc41 2019-12-13 tracey goto done;
5498 f2a9dc41 2019-12-13 tracey }
5499 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5500 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5501 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5502 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5503 f2a9dc41 2019-12-13 tracey goto done;
5504 f2a9dc41 2019-12-13 tracey }
5505 f2a9dc41 2019-12-13 tracey }
5506 f2a9dc41 2019-12-13 tracey }
5507 f2a9dc41 2019-12-13 tracey
5508 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5509 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5510 a129376b 2019-03-28 stsp done:
5511 a129376b 2019-03-28 stsp if (repo)
5512 a129376b 2019-03-28 stsp got_repo_close(repo);
5513 a129376b 2019-03-28 stsp if (worktree)
5514 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5515 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5516 17ed4618 2019-06-02 stsp free((char *)pe->path);
5517 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5518 a129376b 2019-03-28 stsp free(cwd);
5519 a129376b 2019-03-28 stsp return error;
5520 a129376b 2019-03-28 stsp }
5521 a129376b 2019-03-28 stsp
5522 a129376b 2019-03-28 stsp __dead static void
5523 a129376b 2019-03-28 stsp usage_revert(void)
5524 a129376b 2019-03-28 stsp {
5525 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5526 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5527 a129376b 2019-03-28 stsp exit(1);
5528 a129376b 2019-03-28 stsp }
5529 a129376b 2019-03-28 stsp
5530 1ee397ad 2019-07-12 stsp static const struct got_error *
5531 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5532 a129376b 2019-03-28 stsp {
5533 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5534 fb9704af 2020-01-27 stsp return NULL;
5535 fb9704af 2020-01-27 stsp
5536 a129376b 2019-03-28 stsp while (path[0] == '/')
5537 a129376b 2019-03-28 stsp path++;
5538 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5539 33aa809d 2019-08-08 stsp return NULL;
5540 33aa809d 2019-08-08 stsp }
5541 33aa809d 2019-08-08 stsp
5542 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5543 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5544 33aa809d 2019-08-08 stsp const char *action;
5545 33aa809d 2019-08-08 stsp };
5546 33aa809d 2019-08-08 stsp
5547 33aa809d 2019-08-08 stsp static const struct got_error *
5548 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5549 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5550 33aa809d 2019-08-08 stsp {
5551 33aa809d 2019-08-08 stsp char *line = NULL;
5552 33aa809d 2019-08-08 stsp size_t linesize = 0;
5553 33aa809d 2019-08-08 stsp ssize_t linelen;
5554 33aa809d 2019-08-08 stsp
5555 33aa809d 2019-08-08 stsp switch (status) {
5556 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5557 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5558 33aa809d 2019-08-08 stsp break;
5559 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5560 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5561 33aa809d 2019-08-08 stsp break;
5562 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5563 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5564 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5565 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5566 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5567 33aa809d 2019-08-08 stsp printf("%s", line);
5568 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5569 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5570 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5571 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5572 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5573 33aa809d 2019-08-08 stsp break;
5574 33aa809d 2019-08-08 stsp default:
5575 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5576 33aa809d 2019-08-08 stsp }
5577 33aa809d 2019-08-08 stsp
5578 33aa809d 2019-08-08 stsp return NULL;
5579 33aa809d 2019-08-08 stsp }
5580 33aa809d 2019-08-08 stsp
5581 33aa809d 2019-08-08 stsp static const struct got_error *
5582 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5583 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5584 33aa809d 2019-08-08 stsp {
5585 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5586 33aa809d 2019-08-08 stsp char *line = NULL;
5587 33aa809d 2019-08-08 stsp size_t linesize = 0;
5588 33aa809d 2019-08-08 stsp ssize_t linelen;
5589 33aa809d 2019-08-08 stsp int resp = ' ';
5590 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5591 33aa809d 2019-08-08 stsp
5592 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5593 33aa809d 2019-08-08 stsp
5594 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5595 33aa809d 2019-08-08 stsp char *nl;
5596 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5597 33aa809d 2019-08-08 stsp a->action);
5598 33aa809d 2019-08-08 stsp if (err)
5599 33aa809d 2019-08-08 stsp return err;
5600 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5601 33aa809d 2019-08-08 stsp if (linelen == -1) {
5602 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5603 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5604 33aa809d 2019-08-08 stsp return NULL;
5605 33aa809d 2019-08-08 stsp }
5606 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5607 33aa809d 2019-08-08 stsp if (nl)
5608 33aa809d 2019-08-08 stsp *nl = '\0';
5609 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5610 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5611 33aa809d 2019-08-08 stsp printf("y\n");
5612 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5613 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5614 33aa809d 2019-08-08 stsp printf("n\n");
5615 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5616 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5617 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5618 33aa809d 2019-08-08 stsp printf("q\n");
5619 33aa809d 2019-08-08 stsp } else
5620 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5621 33aa809d 2019-08-08 stsp free(line);
5622 33aa809d 2019-08-08 stsp return NULL;
5623 33aa809d 2019-08-08 stsp }
5624 33aa809d 2019-08-08 stsp
5625 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5626 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5627 33aa809d 2019-08-08 stsp a->action);
5628 33aa809d 2019-08-08 stsp if (err)
5629 33aa809d 2019-08-08 stsp return err;
5630 33aa809d 2019-08-08 stsp resp = getchar();
5631 33aa809d 2019-08-08 stsp if (resp == '\n')
5632 33aa809d 2019-08-08 stsp resp = getchar();
5633 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5634 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5635 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5636 33aa809d 2019-08-08 stsp resp = ' ';
5637 33aa809d 2019-08-08 stsp }
5638 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5639 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5640 33aa809d 2019-08-08 stsp resp = ' ';
5641 33aa809d 2019-08-08 stsp }
5642 33aa809d 2019-08-08 stsp }
5643 33aa809d 2019-08-08 stsp
5644 33aa809d 2019-08-08 stsp if (resp == 'y')
5645 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5646 33aa809d 2019-08-08 stsp else if (resp == 'n')
5647 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5648 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5649 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5650 33aa809d 2019-08-08 stsp
5651 1ee397ad 2019-07-12 stsp return NULL;
5652 a129376b 2019-03-28 stsp }
5653 a129376b 2019-03-28 stsp
5654 33aa809d 2019-08-08 stsp
5655 a129376b 2019-03-28 stsp static const struct got_error *
5656 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5657 a129376b 2019-03-28 stsp {
5658 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5659 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5660 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5661 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5662 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5663 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5664 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5665 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5666 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5667 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5668 a129376b 2019-03-28 stsp
5669 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5670 e20a8b6f 2019-06-04 stsp
5671 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5672 a129376b 2019-03-28 stsp switch (ch) {
5673 33aa809d 2019-08-08 stsp case 'p':
5674 33aa809d 2019-08-08 stsp pflag = 1;
5675 33aa809d 2019-08-08 stsp break;
5676 33aa809d 2019-08-08 stsp case 'F':
5677 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5678 33aa809d 2019-08-08 stsp break;
5679 0f6d7415 2019-08-08 stsp case 'R':
5680 0f6d7415 2019-08-08 stsp can_recurse = 1;
5681 0f6d7415 2019-08-08 stsp break;
5682 a129376b 2019-03-28 stsp default:
5683 a129376b 2019-03-28 stsp usage_revert();
5684 a129376b 2019-03-28 stsp /* NOTREACHED */
5685 a129376b 2019-03-28 stsp }
5686 a129376b 2019-03-28 stsp }
5687 a129376b 2019-03-28 stsp
5688 a129376b 2019-03-28 stsp argc -= optind;
5689 a129376b 2019-03-28 stsp argv += optind;
5690 a129376b 2019-03-28 stsp
5691 43012d58 2019-07-14 stsp #ifndef PROFILE
5692 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5693 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5694 43012d58 2019-07-14 stsp err(1, "pledge");
5695 43012d58 2019-07-14 stsp #endif
5696 e20a8b6f 2019-06-04 stsp if (argc < 1)
5697 a129376b 2019-03-28 stsp usage_revert();
5698 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5699 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5700 a129376b 2019-03-28 stsp
5701 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5702 a129376b 2019-03-28 stsp if (cwd == NULL) {
5703 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5704 a129376b 2019-03-28 stsp goto done;
5705 a129376b 2019-03-28 stsp }
5706 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5707 2ec1f75b 2019-03-26 stsp if (error)
5708 2ec1f75b 2019-03-26 stsp goto done;
5709 a129376b 2019-03-28 stsp
5710 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5711 c9956ddf 2019-09-08 stsp NULL);
5712 a129376b 2019-03-28 stsp if (error != NULL)
5713 a129376b 2019-03-28 stsp goto done;
5714 a129376b 2019-03-28 stsp
5715 33aa809d 2019-08-08 stsp if (patch_script_path) {
5716 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5717 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5718 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5719 33aa809d 2019-08-08 stsp patch_script_path);
5720 33aa809d 2019-08-08 stsp goto done;
5721 33aa809d 2019-08-08 stsp }
5722 33aa809d 2019-08-08 stsp }
5723 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5724 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5725 a129376b 2019-03-28 stsp if (error)
5726 a129376b 2019-03-28 stsp goto done;
5727 a129376b 2019-03-28 stsp
5728 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5729 6d022e97 2019-08-04 stsp if (error)
5730 6d022e97 2019-08-04 stsp goto done;
5731 00db391e 2019-08-03 stsp
5732 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5733 0f6d7415 2019-08-08 stsp char *ondisk_path;
5734 0f6d7415 2019-08-08 stsp struct stat sb;
5735 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5736 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5737 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5738 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5739 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5740 0f6d7415 2019-08-08 stsp goto done;
5741 0f6d7415 2019-08-08 stsp }
5742 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5743 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5744 0f6d7415 2019-08-08 stsp free(ondisk_path);
5745 0f6d7415 2019-08-08 stsp continue;
5746 0f6d7415 2019-08-08 stsp }
5747 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5748 0f6d7415 2019-08-08 stsp ondisk_path);
5749 0f6d7415 2019-08-08 stsp free(ondisk_path);
5750 0f6d7415 2019-08-08 stsp goto done;
5751 0f6d7415 2019-08-08 stsp }
5752 0f6d7415 2019-08-08 stsp free(ondisk_path);
5753 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5754 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5755 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5756 0f6d7415 2019-08-08 stsp goto done;
5757 0f6d7415 2019-08-08 stsp }
5758 0f6d7415 2019-08-08 stsp }
5759 0f6d7415 2019-08-08 stsp }
5760 0f6d7415 2019-08-08 stsp
5761 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5762 33aa809d 2019-08-08 stsp cpa.action = "revert";
5763 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5764 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5765 2ec1f75b 2019-03-26 stsp done:
5766 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5767 33aa809d 2019-08-08 stsp error == NULL)
5768 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5769 2ec1f75b 2019-03-26 stsp if (repo)
5770 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5771 2ec1f75b 2019-03-26 stsp if (worktree)
5772 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5773 2ec1f75b 2019-03-26 stsp free(path);
5774 d00136be 2019-03-26 stsp free(cwd);
5775 6bad629b 2019-02-04 stsp return error;
5776 c4296144 2019-05-09 stsp }
5777 c4296144 2019-05-09 stsp
5778 c4296144 2019-05-09 stsp __dead static void
5779 c4296144 2019-05-09 stsp usage_commit(void)
5780 c4296144 2019-05-09 stsp {
5781 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5782 5c1e53bc 2019-07-28 stsp getprogname());
5783 c4296144 2019-05-09 stsp exit(1);
5784 33ad4cbe 2019-05-12 jcs }
5785 33ad4cbe 2019-05-12 jcs
5786 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5787 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5788 e2ba3d07 2019-05-13 stsp const char *editor;
5789 e0870e44 2019-05-13 stsp const char *worktree_path;
5790 76d98825 2019-06-03 stsp const char *branch_name;
5791 314a6357 2019-05-13 stsp const char *repo_path;
5792 e0870e44 2019-05-13 stsp char *logmsg_path;
5793 e2ba3d07 2019-05-13 stsp
5794 e2ba3d07 2019-05-13 stsp };
5795 e0870e44 2019-05-13 stsp
5796 33ad4cbe 2019-05-12 jcs static const struct got_error *
5797 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5798 33ad4cbe 2019-05-12 jcs void *arg)
5799 33ad4cbe 2019-05-12 jcs {
5800 76d98825 2019-06-03 stsp char *initial_content = NULL;
5801 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5802 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5803 e0870e44 2019-05-13 stsp char *template = NULL;
5804 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5805 3ce1b845 2019-07-15 stsp int fd;
5806 33ad4cbe 2019-05-12 jcs size_t len;
5807 33ad4cbe 2019-05-12 jcs
5808 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5809 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5810 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5811 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5812 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5813 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5814 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5815 33ad4cbe 2019-05-12 jcs return NULL;
5816 33ad4cbe 2019-05-12 jcs }
5817 33ad4cbe 2019-05-12 jcs
5818 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5819 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5820 76d98825 2019-06-03 stsp
5821 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5822 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5823 76d98825 2019-06-03 stsp a->branch_name) == -1)
5824 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5825 e0870e44 2019-05-13 stsp
5826 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5827 793c30b5 2019-05-13 stsp if (err)
5828 e0870e44 2019-05-13 stsp goto done;
5829 33ad4cbe 2019-05-12 jcs
5830 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5831 33ad4cbe 2019-05-12 jcs
5832 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5833 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5834 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5835 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5836 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5837 33ad4cbe 2019-05-12 jcs }
5838 33ad4cbe 2019-05-12 jcs close(fd);
5839 33ad4cbe 2019-05-12 jcs
5840 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5841 33ad4cbe 2019-05-12 jcs done:
5842 76d98825 2019-06-03 stsp free(initial_content);
5843 e0870e44 2019-05-13 stsp free(template);
5844 314a6357 2019-05-13 stsp
5845 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5846 3ce1b845 2019-07-15 stsp if (err == NULL) {
5847 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5848 3ce1b845 2019-07-15 stsp if (err) {
5849 3ce1b845 2019-07-15 stsp free(*logmsg);
5850 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5851 3ce1b845 2019-07-15 stsp }
5852 3ce1b845 2019-07-15 stsp }
5853 33ad4cbe 2019-05-12 jcs return err;
5854 6bad629b 2019-02-04 stsp }
5855 c4296144 2019-05-09 stsp
5856 c4296144 2019-05-09 stsp static const struct got_error *
5857 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5858 c4296144 2019-05-09 stsp {
5859 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5860 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5861 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5862 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5863 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5864 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5865 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5866 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5867 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5868 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5869 5c1e53bc 2019-07-28 stsp
5870 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5871 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5872 c4296144 2019-05-09 stsp
5873 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5874 c4296144 2019-05-09 stsp switch (ch) {
5875 c4296144 2019-05-09 stsp case 'm':
5876 c4296144 2019-05-09 stsp logmsg = optarg;
5877 c4296144 2019-05-09 stsp break;
5878 c4296144 2019-05-09 stsp default:
5879 c4296144 2019-05-09 stsp usage_commit();
5880 c4296144 2019-05-09 stsp /* NOTREACHED */
5881 c4296144 2019-05-09 stsp }
5882 c4296144 2019-05-09 stsp }
5883 c4296144 2019-05-09 stsp
5884 c4296144 2019-05-09 stsp argc -= optind;
5885 c4296144 2019-05-09 stsp argv += optind;
5886 c4296144 2019-05-09 stsp
5887 43012d58 2019-07-14 stsp #ifndef PROFILE
5888 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5889 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5890 43012d58 2019-07-14 stsp err(1, "pledge");
5891 43012d58 2019-07-14 stsp #endif
5892 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5893 c4296144 2019-05-09 stsp if (cwd == NULL) {
5894 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5895 c4296144 2019-05-09 stsp goto done;
5896 c4296144 2019-05-09 stsp }
5897 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5898 7d5807f4 2019-07-11 stsp if (error)
5899 7d5807f4 2019-07-11 stsp goto done;
5900 7d5807f4 2019-07-11 stsp
5901 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5902 c4296144 2019-05-09 stsp if (error)
5903 a698f62e 2019-07-25 stsp goto done;
5904 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5905 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5906 c4296144 2019-05-09 stsp goto done;
5907 a698f62e 2019-07-25 stsp }
5908 5c1e53bc 2019-07-28 stsp
5909 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5910 916f288c 2019-07-30 stsp worktree);
5911 5c1e53bc 2019-07-28 stsp if (error)
5912 5c1e53bc 2019-07-28 stsp goto done;
5913 c4296144 2019-05-09 stsp
5914 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5915 c9956ddf 2019-09-08 stsp if (error)
5916 c9956ddf 2019-09-08 stsp goto done;
5917 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5918 c9956ddf 2019-09-08 stsp gitconfig_path);
5919 c4296144 2019-05-09 stsp if (error != NULL)
5920 c4296144 2019-05-09 stsp goto done;
5921 c4296144 2019-05-09 stsp
5922 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5923 aba9c984 2019-09-08 stsp if (error)
5924 aba9c984 2019-09-08 stsp return error;
5925 aba9c984 2019-09-08 stsp
5926 314a6357 2019-05-13 stsp /*
5927 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5928 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5929 314a6357 2019-05-13 stsp */
5930 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5931 314a6357 2019-05-13 stsp error = get_editor(&editor);
5932 314a6357 2019-05-13 stsp else
5933 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5934 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5935 c4296144 2019-05-09 stsp if (error)
5936 c4296144 2019-05-09 stsp goto done;
5937 c4296144 2019-05-09 stsp
5938 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5939 087fb88c 2019-08-04 stsp if (error)
5940 087fb88c 2019-08-04 stsp goto done;
5941 087fb88c 2019-08-04 stsp
5942 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5943 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5944 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5945 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5946 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5947 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5948 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5949 916f288c 2019-07-30 stsp goto done;
5950 916f288c 2019-07-30 stsp }
5951 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5952 916f288c 2019-07-30 stsp }
5953 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5954 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5955 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5956 e0870e44 2019-05-13 stsp if (error) {
5957 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5958 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5959 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5960 c4296144 2019-05-09 stsp goto done;
5961 e0870e44 2019-05-13 stsp }
5962 c4296144 2019-05-09 stsp
5963 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5964 c4296144 2019-05-09 stsp if (error)
5965 c4296144 2019-05-09 stsp goto done;
5966 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5967 c4296144 2019-05-09 stsp done:
5968 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5969 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5970 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5971 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5972 7266f21f 2019-10-21 stsp error == NULL)
5973 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5974 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5975 c4296144 2019-05-09 stsp if (repo)
5976 c4296144 2019-05-09 stsp got_repo_close(repo);
5977 c4296144 2019-05-09 stsp if (worktree)
5978 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5979 c4296144 2019-05-09 stsp free(cwd);
5980 c4296144 2019-05-09 stsp free(id_str);
5981 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5982 e2ba3d07 2019-05-13 stsp free(editor);
5983 aba9c984 2019-09-08 stsp free(author);
5984 234035bc 2019-06-01 stsp return error;
5985 234035bc 2019-06-01 stsp }
5986 234035bc 2019-06-01 stsp
5987 234035bc 2019-06-01 stsp __dead static void
5988 234035bc 2019-06-01 stsp usage_cherrypick(void)
5989 234035bc 2019-06-01 stsp {
5990 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5991 234035bc 2019-06-01 stsp exit(1);
5992 234035bc 2019-06-01 stsp }
5993 234035bc 2019-06-01 stsp
5994 234035bc 2019-06-01 stsp static const struct got_error *
5995 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5996 234035bc 2019-06-01 stsp {
5997 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5998 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5999 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6000 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6001 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6002 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6003 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6004 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6005 234035bc 2019-06-01 stsp int ch, did_something = 0;
6006 234035bc 2019-06-01 stsp
6007 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6008 234035bc 2019-06-01 stsp switch (ch) {
6009 234035bc 2019-06-01 stsp default:
6010 234035bc 2019-06-01 stsp usage_cherrypick();
6011 234035bc 2019-06-01 stsp /* NOTREACHED */
6012 234035bc 2019-06-01 stsp }
6013 234035bc 2019-06-01 stsp }
6014 234035bc 2019-06-01 stsp
6015 234035bc 2019-06-01 stsp argc -= optind;
6016 234035bc 2019-06-01 stsp argv += optind;
6017 234035bc 2019-06-01 stsp
6018 43012d58 2019-07-14 stsp #ifndef PROFILE
6019 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6020 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6021 43012d58 2019-07-14 stsp err(1, "pledge");
6022 43012d58 2019-07-14 stsp #endif
6023 234035bc 2019-06-01 stsp if (argc != 1)
6024 234035bc 2019-06-01 stsp usage_cherrypick();
6025 234035bc 2019-06-01 stsp
6026 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6027 234035bc 2019-06-01 stsp if (cwd == NULL) {
6028 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6029 234035bc 2019-06-01 stsp goto done;
6030 234035bc 2019-06-01 stsp }
6031 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6032 234035bc 2019-06-01 stsp if (error)
6033 234035bc 2019-06-01 stsp goto done;
6034 234035bc 2019-06-01 stsp
6035 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6036 c9956ddf 2019-09-08 stsp NULL);
6037 234035bc 2019-06-01 stsp if (error != NULL)
6038 234035bc 2019-06-01 stsp goto done;
6039 234035bc 2019-06-01 stsp
6040 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6041 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6042 234035bc 2019-06-01 stsp if (error)
6043 234035bc 2019-06-01 stsp goto done;
6044 234035bc 2019-06-01 stsp
6045 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6046 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6047 234035bc 2019-06-01 stsp if (error != NULL) {
6048 234035bc 2019-06-01 stsp struct got_reference *ref;
6049 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6050 234035bc 2019-06-01 stsp goto done;
6051 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6052 234035bc 2019-06-01 stsp if (error != NULL)
6053 234035bc 2019-06-01 stsp goto done;
6054 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6055 234035bc 2019-06-01 stsp got_ref_close(ref);
6056 234035bc 2019-06-01 stsp if (error != NULL)
6057 234035bc 2019-06-01 stsp goto done;
6058 234035bc 2019-06-01 stsp }
6059 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6060 234035bc 2019-06-01 stsp if (error)
6061 234035bc 2019-06-01 stsp goto done;
6062 234035bc 2019-06-01 stsp
6063 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6064 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6065 234035bc 2019-06-01 stsp if (error != NULL)
6066 234035bc 2019-06-01 stsp goto done;
6067 234035bc 2019-06-01 stsp
6068 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6069 234035bc 2019-06-01 stsp if (error) {
6070 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6071 234035bc 2019-06-01 stsp goto done;
6072 234035bc 2019-06-01 stsp error = NULL;
6073 234035bc 2019-06-01 stsp } else {
6074 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6075 234035bc 2019-06-01 stsp goto done;
6076 234035bc 2019-06-01 stsp }
6077 234035bc 2019-06-01 stsp
6078 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6079 234035bc 2019-06-01 stsp if (error)
6080 234035bc 2019-06-01 stsp goto done;
6081 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6082 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6083 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
6084 03415a1a 2019-06-02 stsp NULL);
6085 234035bc 2019-06-01 stsp if (error != NULL)
6086 234035bc 2019-06-01 stsp goto done;
6087 234035bc 2019-06-01 stsp
6088 234035bc 2019-06-01 stsp if (did_something)
6089 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6090 234035bc 2019-06-01 stsp done:
6091 234035bc 2019-06-01 stsp if (commit)
6092 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6093 234035bc 2019-06-01 stsp free(commit_id_str);
6094 234035bc 2019-06-01 stsp if (head_ref)
6095 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6096 234035bc 2019-06-01 stsp if (worktree)
6097 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6098 234035bc 2019-06-01 stsp if (repo)
6099 234035bc 2019-06-01 stsp got_repo_close(repo);
6100 c4296144 2019-05-09 stsp return error;
6101 c4296144 2019-05-09 stsp }
6102 5ef14e63 2019-06-02 stsp
6103 5ef14e63 2019-06-02 stsp __dead static void
6104 5ef14e63 2019-06-02 stsp usage_backout(void)
6105 5ef14e63 2019-06-02 stsp {
6106 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6107 5ef14e63 2019-06-02 stsp exit(1);
6108 5ef14e63 2019-06-02 stsp }
6109 5ef14e63 2019-06-02 stsp
6110 5ef14e63 2019-06-02 stsp static const struct got_error *
6111 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6112 5ef14e63 2019-06-02 stsp {
6113 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6114 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6115 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6116 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6117 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6118 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6119 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6120 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6121 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
6122 5ef14e63 2019-06-02 stsp
6123 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6124 5ef14e63 2019-06-02 stsp switch (ch) {
6125 5ef14e63 2019-06-02 stsp default:
6126 5ef14e63 2019-06-02 stsp usage_backout();
6127 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6128 5ef14e63 2019-06-02 stsp }
6129 5ef14e63 2019-06-02 stsp }
6130 5ef14e63 2019-06-02 stsp
6131 5ef14e63 2019-06-02 stsp argc -= optind;
6132 5ef14e63 2019-06-02 stsp argv += optind;
6133 5ef14e63 2019-06-02 stsp
6134 43012d58 2019-07-14 stsp #ifndef PROFILE
6135 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6136 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6137 43012d58 2019-07-14 stsp err(1, "pledge");
6138 43012d58 2019-07-14 stsp #endif
6139 5ef14e63 2019-06-02 stsp if (argc != 1)
6140 5ef14e63 2019-06-02 stsp usage_backout();
6141 5ef14e63 2019-06-02 stsp
6142 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6143 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6144 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6145 5ef14e63 2019-06-02 stsp goto done;
6146 5ef14e63 2019-06-02 stsp }
6147 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6148 5ef14e63 2019-06-02 stsp if (error)
6149 5ef14e63 2019-06-02 stsp goto done;
6150 5ef14e63 2019-06-02 stsp
6151 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6152 c9956ddf 2019-09-08 stsp NULL);
6153 5ef14e63 2019-06-02 stsp if (error != NULL)
6154 5ef14e63 2019-06-02 stsp goto done;
6155 5ef14e63 2019-06-02 stsp
6156 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6157 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6158 5ef14e63 2019-06-02 stsp if (error)
6159 5ef14e63 2019-06-02 stsp goto done;
6160 5ef14e63 2019-06-02 stsp
6161 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6162 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6163 5ef14e63 2019-06-02 stsp if (error != NULL) {
6164 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6165 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6166 5ef14e63 2019-06-02 stsp goto done;
6167 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6168 5ef14e63 2019-06-02 stsp if (error != NULL)
6169 5ef14e63 2019-06-02 stsp goto done;
6170 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6171 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6172 5ef14e63 2019-06-02 stsp if (error != NULL)
6173 5ef14e63 2019-06-02 stsp goto done;
6174 5ef14e63 2019-06-02 stsp }
6175 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6176 5ef14e63 2019-06-02 stsp if (error)
6177 5ef14e63 2019-06-02 stsp goto done;
6178 5ef14e63 2019-06-02 stsp
6179 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6180 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6181 5ef14e63 2019-06-02 stsp if (error != NULL)
6182 5ef14e63 2019-06-02 stsp goto done;
6183 5ef14e63 2019-06-02 stsp
6184 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6185 5ef14e63 2019-06-02 stsp if (error)
6186 5ef14e63 2019-06-02 stsp goto done;
6187 5ef14e63 2019-06-02 stsp
6188 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6189 5ef14e63 2019-06-02 stsp if (error)
6190 5ef14e63 2019-06-02 stsp goto done;
6191 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6192 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6193 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6194 5ef14e63 2019-06-02 stsp goto done;
6195 5ef14e63 2019-06-02 stsp }
6196 5ef14e63 2019-06-02 stsp
6197 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6198 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6199 5ef14e63 2019-06-02 stsp if (error != NULL)
6200 5ef14e63 2019-06-02 stsp goto done;
6201 5ef14e63 2019-06-02 stsp
6202 5ef14e63 2019-06-02 stsp if (did_something)
6203 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6204 5ef14e63 2019-06-02 stsp done:
6205 5ef14e63 2019-06-02 stsp if (commit)
6206 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6207 5ef14e63 2019-06-02 stsp free(commit_id_str);
6208 5ef14e63 2019-06-02 stsp if (head_ref)
6209 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6210 818c7501 2019-07-11 stsp if (worktree)
6211 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6212 818c7501 2019-07-11 stsp if (repo)
6213 818c7501 2019-07-11 stsp got_repo_close(repo);
6214 818c7501 2019-07-11 stsp return error;
6215 818c7501 2019-07-11 stsp }
6216 818c7501 2019-07-11 stsp
6217 818c7501 2019-07-11 stsp __dead static void
6218 818c7501 2019-07-11 stsp usage_rebase(void)
6219 818c7501 2019-07-11 stsp {
6220 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6221 af54c8f8 2019-07-11 stsp getprogname());
6222 818c7501 2019-07-11 stsp exit(1);
6223 0ebf8283 2019-07-24 stsp }
6224 0ebf8283 2019-07-24 stsp
6225 0ebf8283 2019-07-24 stsp void
6226 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6227 0ebf8283 2019-07-24 stsp {
6228 0ebf8283 2019-07-24 stsp char *nl;
6229 0ebf8283 2019-07-24 stsp size_t len;
6230 0ebf8283 2019-07-24 stsp
6231 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6232 0ebf8283 2019-07-24 stsp if (len > limit)
6233 0ebf8283 2019-07-24 stsp len = limit;
6234 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6235 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6236 0ebf8283 2019-07-24 stsp if (nl)
6237 0ebf8283 2019-07-24 stsp *nl = '\0';
6238 0ebf8283 2019-07-24 stsp }
6239 0ebf8283 2019-07-24 stsp
6240 0ebf8283 2019-07-24 stsp static const struct got_error *
6241 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6242 0ebf8283 2019-07-24 stsp {
6243 5943eee2 2019-08-13 stsp const struct got_error *err;
6244 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6245 5943eee2 2019-08-13 stsp const char *s;
6246 0ebf8283 2019-07-24 stsp
6247 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6248 5943eee2 2019-08-13 stsp if (err)
6249 5943eee2 2019-08-13 stsp return err;
6250 0ebf8283 2019-07-24 stsp
6251 5943eee2 2019-08-13 stsp s = logmsg0;
6252 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6253 5943eee2 2019-08-13 stsp s++;
6254 5943eee2 2019-08-13 stsp
6255 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6256 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6257 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6258 5943eee2 2019-08-13 stsp goto done;
6259 5943eee2 2019-08-13 stsp }
6260 0ebf8283 2019-07-24 stsp
6261 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6262 5943eee2 2019-08-13 stsp done:
6263 5943eee2 2019-08-13 stsp free(logmsg0);
6264 a0ea4fc0 2020-02-28 stsp return err;
6265 a0ea4fc0 2020-02-28 stsp }
6266 a0ea4fc0 2020-02-28 stsp
6267 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6268 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6269 a0ea4fc0 2020-02-28 stsp {
6270 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6271 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6272 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6273 a0ea4fc0 2020-02-28 stsp
6274 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6275 a0ea4fc0 2020-02-28 stsp if (err)
6276 a0ea4fc0 2020-02-28 stsp return err;
6277 a0ea4fc0 2020-02-28 stsp
6278 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6279 a0ea4fc0 2020-02-28 stsp if (err)
6280 a0ea4fc0 2020-02-28 stsp goto done;
6281 a0ea4fc0 2020-02-28 stsp
6282 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6283 a0ea4fc0 2020-02-28 stsp
6284 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6285 a0ea4fc0 2020-02-28 stsp if (err)
6286 a0ea4fc0 2020-02-28 stsp goto done;
6287 a0ea4fc0 2020-02-28 stsp
6288 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6289 a0ea4fc0 2020-02-28 stsp done:
6290 a0ea4fc0 2020-02-28 stsp free(id_str);
6291 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6292 a0ea4fc0 2020-02-28 stsp free(logmsg);
6293 5943eee2 2019-08-13 stsp return err;
6294 818c7501 2019-07-11 stsp }
6295 818c7501 2019-07-11 stsp
6296 818c7501 2019-07-11 stsp static const struct got_error *
6297 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6298 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6299 818c7501 2019-07-11 stsp {
6300 818c7501 2019-07-11 stsp const struct got_error *err;
6301 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6302 818c7501 2019-07-11 stsp
6303 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6304 818c7501 2019-07-11 stsp if (err)
6305 818c7501 2019-07-11 stsp goto done;
6306 818c7501 2019-07-11 stsp
6307 ff0d2220 2019-07-11 stsp if (new_id) {
6308 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6309 ff0d2220 2019-07-11 stsp if (err)
6310 ff0d2220 2019-07-11 stsp goto done;
6311 ff0d2220 2019-07-11 stsp }
6312 818c7501 2019-07-11 stsp
6313 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6314 ff0d2220 2019-07-11 stsp if (new_id_str)
6315 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6316 0ebf8283 2019-07-24 stsp
6317 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6318 0ebf8283 2019-07-24 stsp if (err)
6319 0ebf8283 2019-07-24 stsp goto done;
6320 0ebf8283 2019-07-24 stsp
6321 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6322 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6323 818c7501 2019-07-11 stsp done:
6324 818c7501 2019-07-11 stsp free(old_id_str);
6325 818c7501 2019-07-11 stsp free(new_id_str);
6326 272a1371 2020-02-28 stsp free(logmsg);
6327 818c7501 2019-07-11 stsp return err;
6328 818c7501 2019-07-11 stsp }
6329 818c7501 2019-07-11 stsp
6330 1ee397ad 2019-07-12 stsp static const struct got_error *
6331 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6332 818c7501 2019-07-11 stsp {
6333 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6334 818c7501 2019-07-11 stsp
6335 818c7501 2019-07-11 stsp while (path[0] == '/')
6336 818c7501 2019-07-11 stsp path++;
6337 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6338 818c7501 2019-07-11 stsp
6339 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6340 1ee397ad 2019-07-12 stsp return NULL;
6341 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6342 818c7501 2019-07-11 stsp *rebase_status = status;
6343 1ee397ad 2019-07-12 stsp return NULL;
6344 818c7501 2019-07-11 stsp }
6345 818c7501 2019-07-11 stsp
6346 818c7501 2019-07-11 stsp static const struct got_error *
6347 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6348 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6349 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6350 818c7501 2019-07-11 stsp {
6351 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6352 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6353 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6354 818c7501 2019-07-11 stsp }
6355 818c7501 2019-07-11 stsp
6356 818c7501 2019-07-11 stsp static const struct got_error *
6357 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6358 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6359 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6360 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6361 ff0d2220 2019-07-11 stsp {
6362 ff0d2220 2019-07-11 stsp const struct got_error *error;
6363 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6364 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6365 ff0d2220 2019-07-11 stsp
6366 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6367 ff0d2220 2019-07-11 stsp if (error)
6368 ff0d2220 2019-07-11 stsp return error;
6369 ff0d2220 2019-07-11 stsp
6370 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6371 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6372 ff0d2220 2019-07-11 stsp if (error) {
6373 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6374 ff0d2220 2019-07-11 stsp goto done;
6375 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6376 ff0d2220 2019-07-11 stsp } else {
6377 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6378 ff0d2220 2019-07-11 stsp free(new_commit_id);
6379 ff0d2220 2019-07-11 stsp }
6380 ff0d2220 2019-07-11 stsp done:
6381 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6382 ff0d2220 2019-07-11 stsp return error;
6383 64c6d990 2019-07-11 stsp }
6384 64c6d990 2019-07-11 stsp
6385 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6386 64c6d990 2019-07-11 stsp const char *path_prefix;
6387 64c6d990 2019-07-11 stsp size_t len;
6388 8ca9bd68 2019-07-25 stsp int errcode;
6389 64c6d990 2019-07-11 stsp };
6390 64c6d990 2019-07-11 stsp
6391 64c6d990 2019-07-11 stsp static const struct got_error *
6392 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6393 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6394 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6395 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6396 64c6d990 2019-07-11 stsp {
6397 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6398 64c6d990 2019-07-11 stsp
6399 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6400 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6401 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6402 64c6d990 2019-07-11 stsp
6403 64c6d990 2019-07-11 stsp return NULL;
6404 64c6d990 2019-07-11 stsp }
6405 64c6d990 2019-07-11 stsp
6406 64c6d990 2019-07-11 stsp static const struct got_error *
6407 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6408 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6409 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6410 64c6d990 2019-07-11 stsp {
6411 64c6d990 2019-07-11 stsp const struct got_error *err;
6412 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6413 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6414 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6415 64c6d990 2019-07-11 stsp
6416 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6417 64c6d990 2019-07-11 stsp return NULL;
6418 64c6d990 2019-07-11 stsp
6419 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6420 64c6d990 2019-07-11 stsp if (err)
6421 64c6d990 2019-07-11 stsp goto done;
6422 64c6d990 2019-07-11 stsp
6423 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6424 64c6d990 2019-07-11 stsp if (err)
6425 64c6d990 2019-07-11 stsp goto done;
6426 64c6d990 2019-07-11 stsp
6427 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6428 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6429 64c6d990 2019-07-11 stsp if (err)
6430 64c6d990 2019-07-11 stsp goto done;
6431 64c6d990 2019-07-11 stsp
6432 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6433 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6434 64c6d990 2019-07-11 stsp if (err)
6435 64c6d990 2019-07-11 stsp goto done;
6436 64c6d990 2019-07-11 stsp
6437 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6438 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6439 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6440 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6441 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6442 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6443 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6444 64c6d990 2019-07-11 stsp done:
6445 64c6d990 2019-07-11 stsp if (tree1)
6446 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6447 64c6d990 2019-07-11 stsp if (tree2)
6448 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6449 64c6d990 2019-07-11 stsp if (commit)
6450 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6451 64c6d990 2019-07-11 stsp if (parent_commit)
6452 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6453 64c6d990 2019-07-11 stsp return err;
6454 ff0d2220 2019-07-11 stsp }
6455 ff0d2220 2019-07-11 stsp
6456 ff0d2220 2019-07-11 stsp static const struct got_error *
6457 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6458 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6459 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6460 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6461 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6462 af61c510 2019-07-19 stsp {
6463 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6464 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6465 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6466 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6467 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6468 af61c510 2019-07-19 stsp
6469 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6470 af61c510 2019-07-19 stsp if (err)
6471 af61c510 2019-07-19 stsp return err;
6472 af61c510 2019-07-19 stsp
6473 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6474 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6475 af61c510 2019-07-19 stsp if (err)
6476 af61c510 2019-07-19 stsp goto done;
6477 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6478 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6479 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6480 af61c510 2019-07-19 stsp if (err) {
6481 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6482 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6483 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6484 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6485 af61c510 2019-07-19 stsp "been reached?!?");
6486 ee780d5c 2020-01-04 stsp }
6487 ee780d5c 2020-01-04 stsp goto done;
6488 af61c510 2019-07-19 stsp } else {
6489 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6490 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6491 af61c510 2019-07-19 stsp if (err)
6492 af61c510 2019-07-19 stsp goto done;
6493 af61c510 2019-07-19 stsp
6494 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6495 af61c510 2019-07-19 stsp if (err)
6496 af61c510 2019-07-19 stsp goto done;
6497 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6498 af61c510 2019-07-19 stsp commit_id = parent_id;
6499 af61c510 2019-07-19 stsp }
6500 af61c510 2019-07-19 stsp }
6501 af61c510 2019-07-19 stsp done:
6502 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6503 af61c510 2019-07-19 stsp return err;
6504 af61c510 2019-07-19 stsp }
6505 af61c510 2019-07-19 stsp
6506 af61c510 2019-07-19 stsp static const struct got_error *
6507 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6508 818c7501 2019-07-11 stsp {
6509 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6510 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6511 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6512 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6513 818c7501 2019-07-11 stsp char *cwd = NULL;
6514 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6515 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6516 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6517 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6518 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6519 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6520 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6521 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6522 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6523 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6524 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6525 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6526 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6527 818c7501 2019-07-11 stsp
6528 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6529 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6530 818c7501 2019-07-11 stsp
6531 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6532 818c7501 2019-07-11 stsp switch (ch) {
6533 818c7501 2019-07-11 stsp case 'a':
6534 818c7501 2019-07-11 stsp abort_rebase = 1;
6535 818c7501 2019-07-11 stsp break;
6536 818c7501 2019-07-11 stsp case 'c':
6537 818c7501 2019-07-11 stsp continue_rebase = 1;
6538 818c7501 2019-07-11 stsp break;
6539 818c7501 2019-07-11 stsp default:
6540 818c7501 2019-07-11 stsp usage_rebase();
6541 818c7501 2019-07-11 stsp /* NOTREACHED */
6542 818c7501 2019-07-11 stsp }
6543 818c7501 2019-07-11 stsp }
6544 818c7501 2019-07-11 stsp
6545 818c7501 2019-07-11 stsp argc -= optind;
6546 818c7501 2019-07-11 stsp argv += optind;
6547 818c7501 2019-07-11 stsp
6548 43012d58 2019-07-14 stsp #ifndef PROFILE
6549 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6550 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6551 43012d58 2019-07-14 stsp err(1, "pledge");
6552 43012d58 2019-07-14 stsp #endif
6553 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6554 818c7501 2019-07-11 stsp usage_rebase();
6555 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6556 818c7501 2019-07-11 stsp if (argc != 0)
6557 818c7501 2019-07-11 stsp usage_rebase();
6558 818c7501 2019-07-11 stsp } else if (argc != 1)
6559 818c7501 2019-07-11 stsp usage_rebase();
6560 818c7501 2019-07-11 stsp
6561 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6562 818c7501 2019-07-11 stsp if (cwd == NULL) {
6563 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6564 818c7501 2019-07-11 stsp goto done;
6565 818c7501 2019-07-11 stsp }
6566 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6567 818c7501 2019-07-11 stsp if (error)
6568 818c7501 2019-07-11 stsp goto done;
6569 818c7501 2019-07-11 stsp
6570 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6571 c9956ddf 2019-09-08 stsp NULL);
6572 818c7501 2019-07-11 stsp if (error != NULL)
6573 818c7501 2019-07-11 stsp goto done;
6574 818c7501 2019-07-11 stsp
6575 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6576 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6577 7ef62c4e 2020-02-24 stsp if (error)
6578 7ef62c4e 2020-02-24 stsp goto done;
6579 7ef62c4e 2020-02-24 stsp
6580 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6581 7ef62c4e 2020-02-24 stsp worktree);
6582 818c7501 2019-07-11 stsp if (error)
6583 7ef62c4e 2020-02-24 stsp goto done;
6584 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6585 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6586 818c7501 2019-07-11 stsp goto done;
6587 7ef62c4e 2020-02-24 stsp }
6588 818c7501 2019-07-11 stsp
6589 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6590 818c7501 2019-07-11 stsp if (error)
6591 818c7501 2019-07-11 stsp goto done;
6592 818c7501 2019-07-11 stsp
6593 f6794adc 2019-07-23 stsp if (abort_rebase) {
6594 818c7501 2019-07-11 stsp int did_something;
6595 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6596 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6597 f6794adc 2019-07-23 stsp goto done;
6598 f6794adc 2019-07-23 stsp }
6599 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6600 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6601 3e3a69f1 2019-07-25 stsp worktree, repo);
6602 818c7501 2019-07-11 stsp if (error)
6603 818c7501 2019-07-11 stsp goto done;
6604 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6605 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6606 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6607 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6608 818c7501 2019-07-11 stsp if (error)
6609 818c7501 2019-07-11 stsp goto done;
6610 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6611 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6612 818c7501 2019-07-11 stsp }
6613 818c7501 2019-07-11 stsp
6614 818c7501 2019-07-11 stsp if (continue_rebase) {
6615 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6616 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6617 f6794adc 2019-07-23 stsp goto done;
6618 f6794adc 2019-07-23 stsp }
6619 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6620 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6621 3e3a69f1 2019-07-25 stsp worktree, repo);
6622 818c7501 2019-07-11 stsp if (error)
6623 818c7501 2019-07-11 stsp goto done;
6624 818c7501 2019-07-11 stsp
6625 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6626 01757395 2019-07-12 stsp resume_commit_id, repo);
6627 818c7501 2019-07-11 stsp if (error)
6628 818c7501 2019-07-11 stsp goto done;
6629 818c7501 2019-07-11 stsp
6630 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6631 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6632 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6633 818c7501 2019-07-11 stsp goto done;
6634 818c7501 2019-07-11 stsp }
6635 818c7501 2019-07-11 stsp } else {
6636 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6637 818c7501 2019-07-11 stsp if (error != NULL)
6638 818c7501 2019-07-11 stsp goto done;
6639 ff0d2220 2019-07-11 stsp }
6640 818c7501 2019-07-11 stsp
6641 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6642 ff0d2220 2019-07-11 stsp if (error)
6643 ff0d2220 2019-07-11 stsp goto done;
6644 ff0d2220 2019-07-11 stsp
6645 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6646 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6647 a51a74b3 2019-07-27 stsp
6648 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6649 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6650 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6651 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6652 818c7501 2019-07-11 stsp if (error)
6653 818c7501 2019-07-11 stsp goto done;
6654 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6655 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6656 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6657 818c7501 2019-07-11 stsp "with work tree's branch");
6658 818c7501 2019-07-11 stsp goto done;
6659 818c7501 2019-07-11 stsp }
6660 818c7501 2019-07-11 stsp
6661 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6662 a51a74b3 2019-07-27 stsp if (error) {
6663 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6664 a51a74b3 2019-07-27 stsp goto done;
6665 a51a74b3 2019-07-27 stsp error = NULL;
6666 a51a74b3 2019-07-27 stsp } else {
6667 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6668 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6669 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6670 a51a74b3 2019-07-27 stsp goto done;
6671 a51a74b3 2019-07-27 stsp }
6672 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6673 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6674 818c7501 2019-07-11 stsp if (error)
6675 818c7501 2019-07-11 stsp goto done;
6676 818c7501 2019-07-11 stsp }
6677 818c7501 2019-07-11 stsp
6678 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6679 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6680 818c7501 2019-07-11 stsp if (error)
6681 818c7501 2019-07-11 stsp goto done;
6682 818c7501 2019-07-11 stsp
6683 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6684 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6685 fc66b545 2019-08-12 stsp if (pid == NULL) {
6686 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6687 fc66b545 2019-08-12 stsp int did_something;
6688 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6689 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6690 fc66b545 2019-08-12 stsp &did_something);
6691 fc66b545 2019-08-12 stsp if (error)
6692 fc66b545 2019-08-12 stsp goto done;
6693 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6694 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6695 fc66b545 2019-08-12 stsp }
6696 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6697 fc66b545 2019-08-12 stsp goto done;
6698 fc66b545 2019-08-12 stsp }
6699 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6700 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6701 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6702 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6703 818c7501 2019-07-11 stsp commit = NULL;
6704 818c7501 2019-07-11 stsp if (error)
6705 818c7501 2019-07-11 stsp goto done;
6706 64c6d990 2019-07-11 stsp
6707 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6708 38b0338b 2019-11-29 stsp if (continue_rebase) {
6709 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6710 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6711 38b0338b 2019-11-29 stsp goto done;
6712 38b0338b 2019-11-29 stsp } else {
6713 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6714 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6715 38b0338b 2019-11-29 stsp char *id_str;
6716 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6717 38b0338b 2019-11-29 stsp new_base_branch);
6718 38b0338b 2019-11-29 stsp if (error)
6719 38b0338b 2019-11-29 stsp goto done;
6720 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6721 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6722 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6723 38b0338b 2019-11-29 stsp free(id_str);
6724 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6725 38b0338b 2019-11-29 stsp new_head_commit_id);
6726 38b0338b 2019-11-29 stsp if (error)
6727 38b0338b 2019-11-29 stsp goto done;
6728 38b0338b 2019-11-29 stsp }
6729 818c7501 2019-07-11 stsp }
6730 818c7501 2019-07-11 stsp
6731 818c7501 2019-07-11 stsp pid = NULL;
6732 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6733 818c7501 2019-07-11 stsp commit_id = qid->id;
6734 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6735 818c7501 2019-07-11 stsp pid = qid;
6736 818c7501 2019-07-11 stsp
6737 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6738 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6739 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6740 818c7501 2019-07-11 stsp if (error)
6741 818c7501 2019-07-11 stsp goto done;
6742 818c7501 2019-07-11 stsp
6743 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6744 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6745 a0ea4fc0 2020-02-28 stsp if (error)
6746 a0ea4fc0 2020-02-28 stsp goto done;
6747 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6748 818c7501 2019-07-11 stsp break;
6749 01757395 2019-07-12 stsp }
6750 818c7501 2019-07-11 stsp
6751 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6752 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6753 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6754 818c7501 2019-07-11 stsp if (error)
6755 818c7501 2019-07-11 stsp goto done;
6756 818c7501 2019-07-11 stsp }
6757 818c7501 2019-07-11 stsp
6758 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6759 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6760 818c7501 2019-07-11 stsp if (error)
6761 818c7501 2019-07-11 stsp goto done;
6762 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6763 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6764 818c7501 2019-07-11 stsp } else
6765 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6766 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6767 818c7501 2019-07-11 stsp done:
6768 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6769 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6770 818c7501 2019-07-11 stsp free(resume_commit_id);
6771 818c7501 2019-07-11 stsp free(yca_id);
6772 818c7501 2019-07-11 stsp if (commit)
6773 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6774 818c7501 2019-07-11 stsp if (branch)
6775 818c7501 2019-07-11 stsp got_ref_close(branch);
6776 818c7501 2019-07-11 stsp if (new_base_branch)
6777 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6778 818c7501 2019-07-11 stsp if (tmp_branch)
6779 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6780 5ef14e63 2019-06-02 stsp if (worktree)
6781 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6782 5ef14e63 2019-06-02 stsp if (repo)
6783 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6784 5ef14e63 2019-06-02 stsp return error;
6785 0ebf8283 2019-07-24 stsp }
6786 0ebf8283 2019-07-24 stsp
6787 0ebf8283 2019-07-24 stsp __dead static void
6788 0ebf8283 2019-07-24 stsp usage_histedit(void)
6789 0ebf8283 2019-07-24 stsp {
6790 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6791 0ebf8283 2019-07-24 stsp getprogname());
6792 0ebf8283 2019-07-24 stsp exit(1);
6793 0ebf8283 2019-07-24 stsp }
6794 0ebf8283 2019-07-24 stsp
6795 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6796 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6797 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6798 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6799 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6800 0ebf8283 2019-07-24 stsp
6801 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6802 0ebf8283 2019-07-24 stsp unsigned char code;
6803 0ebf8283 2019-07-24 stsp const char *name;
6804 0ebf8283 2019-07-24 stsp const char *desc;
6805 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6806 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6807 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6808 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6809 82997472 2020-01-29 stsp "be used" },
6810 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6811 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6812 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6813 0ebf8283 2019-07-24 stsp };
6814 0ebf8283 2019-07-24 stsp
6815 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6816 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6817 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6818 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6819 0ebf8283 2019-07-24 stsp char *logmsg;
6820 0ebf8283 2019-07-24 stsp };
6821 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6822 0ebf8283 2019-07-24 stsp
6823 0ebf8283 2019-07-24 stsp static const struct got_error *
6824 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6825 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6826 0ebf8283 2019-07-24 stsp {
6827 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6828 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6829 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6830 8138f3e1 2019-08-11 stsp int n;
6831 0ebf8283 2019-07-24 stsp
6832 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6833 0ebf8283 2019-07-24 stsp if (err)
6834 0ebf8283 2019-07-24 stsp goto done;
6835 0ebf8283 2019-07-24 stsp
6836 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6837 0ebf8283 2019-07-24 stsp if (err)
6838 0ebf8283 2019-07-24 stsp goto done;
6839 0ebf8283 2019-07-24 stsp
6840 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6841 0ebf8283 2019-07-24 stsp if (err)
6842 0ebf8283 2019-07-24 stsp goto done;
6843 0ebf8283 2019-07-24 stsp
6844 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6845 0ebf8283 2019-07-24 stsp if (n < 0)
6846 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6847 0ebf8283 2019-07-24 stsp done:
6848 0ebf8283 2019-07-24 stsp if (commit)
6849 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6850 0ebf8283 2019-07-24 stsp free(id_str);
6851 0ebf8283 2019-07-24 stsp free(logmsg);
6852 0ebf8283 2019-07-24 stsp return err;
6853 0ebf8283 2019-07-24 stsp }
6854 0ebf8283 2019-07-24 stsp
6855 0ebf8283 2019-07-24 stsp static const struct got_error *
6856 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6857 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6858 0ebf8283 2019-07-24 stsp {
6859 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6860 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6861 0ebf8283 2019-07-24 stsp
6862 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6863 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6864 0ebf8283 2019-07-24 stsp
6865 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6866 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6867 0ebf8283 2019-07-24 stsp f, repo);
6868 0ebf8283 2019-07-24 stsp if (err)
6869 0ebf8283 2019-07-24 stsp break;
6870 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6871 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6872 083957f4 2020-02-24 stsp if (n < 0) {
6873 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6874 083957f4 2020-02-24 stsp break;
6875 083957f4 2020-02-24 stsp }
6876 083957f4 2020-02-24 stsp }
6877 0ebf8283 2019-07-24 stsp }
6878 0ebf8283 2019-07-24 stsp
6879 0ebf8283 2019-07-24 stsp return err;
6880 0ebf8283 2019-07-24 stsp }
6881 0ebf8283 2019-07-24 stsp
6882 0ebf8283 2019-07-24 stsp static const struct got_error *
6883 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6884 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6885 0ebf8283 2019-07-24 stsp {
6886 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6887 0ebf8283 2019-07-24 stsp int n, i;
6888 514f2ffe 2020-01-29 stsp char *id_str;
6889 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6890 514f2ffe 2020-01-29 stsp
6891 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6892 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6893 514f2ffe 2020-01-29 stsp if (err)
6894 514f2ffe 2020-01-29 stsp return err;
6895 514f2ffe 2020-01-29 stsp
6896 514f2ffe 2020-01-29 stsp n = fprintf(f,
6897 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6898 514f2ffe 2020-01-29 stsp "# commit %s\n"
6899 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6900 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6901 514f2ffe 2020-01-29 stsp if (n < 0) {
6902 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6903 514f2ffe 2020-01-29 stsp goto done;
6904 514f2ffe 2020-01-29 stsp }
6905 0ebf8283 2019-07-24 stsp
6906 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6907 514f2ffe 2020-01-29 stsp if (n < 0) {
6908 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6909 514f2ffe 2020-01-29 stsp goto done;
6910 514f2ffe 2020-01-29 stsp }
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6913 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6914 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6915 0ebf8283 2019-07-24 stsp cmd->desc);
6916 0ebf8283 2019-07-24 stsp if (n < 0) {
6917 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6918 0ebf8283 2019-07-24 stsp break;
6919 0ebf8283 2019-07-24 stsp }
6920 0ebf8283 2019-07-24 stsp }
6921 514f2ffe 2020-01-29 stsp done:
6922 514f2ffe 2020-01-29 stsp free(id_str);
6923 0ebf8283 2019-07-24 stsp return err;
6924 0ebf8283 2019-07-24 stsp }
6925 0ebf8283 2019-07-24 stsp
6926 0ebf8283 2019-07-24 stsp static const struct got_error *
6927 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6928 0ebf8283 2019-07-24 stsp {
6929 0ebf8283 2019-07-24 stsp static char msg[42];
6930 0ebf8283 2019-07-24 stsp int ret;
6931 0ebf8283 2019-07-24 stsp
6932 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6933 0ebf8283 2019-07-24 stsp lineno);
6934 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6935 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6936 0ebf8283 2019-07-24 stsp
6937 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6938 0ebf8283 2019-07-24 stsp }
6939 0ebf8283 2019-07-24 stsp
6940 0ebf8283 2019-07-24 stsp static const struct got_error *
6941 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6942 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6943 0ebf8283 2019-07-24 stsp {
6944 0ebf8283 2019-07-24 stsp const struct got_error *err;
6945 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6946 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6949 0ebf8283 2019-07-24 stsp if (err)
6950 0ebf8283 2019-07-24 stsp return err;
6951 0ebf8283 2019-07-24 stsp
6952 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6953 0ebf8283 2019-07-24 stsp if (err)
6954 0ebf8283 2019-07-24 stsp goto done;
6955 0ebf8283 2019-07-24 stsp
6956 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6957 5943eee2 2019-08-13 stsp if (err)
6958 5943eee2 2019-08-13 stsp goto done;
6959 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6960 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6961 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6962 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6963 0ebf8283 2019-07-24 stsp }
6964 0ebf8283 2019-07-24 stsp done:
6965 0ebf8283 2019-07-24 stsp if (folded_commit)
6966 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6967 0ebf8283 2019-07-24 stsp free(id_str);
6968 5943eee2 2019-08-13 stsp free(folded_logmsg);
6969 0ebf8283 2019-07-24 stsp return err;
6970 0ebf8283 2019-07-24 stsp }
6971 0ebf8283 2019-07-24 stsp
6972 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6973 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6974 0ebf8283 2019-07-24 stsp {
6975 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6976 0ebf8283 2019-07-24 stsp
6977 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6978 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6979 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6980 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6981 3f9de99f 2019-07-24 stsp folded = prev;
6982 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6983 0ebf8283 2019-07-24 stsp }
6984 0ebf8283 2019-07-24 stsp
6985 0ebf8283 2019-07-24 stsp return folded;
6986 0ebf8283 2019-07-24 stsp }
6987 0ebf8283 2019-07-24 stsp
6988 0ebf8283 2019-07-24 stsp static const struct got_error *
6989 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6990 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6991 0ebf8283 2019-07-24 stsp {
6992 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6993 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6994 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6995 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6996 0ebf8283 2019-07-24 stsp int fd;
6997 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6998 0ebf8283 2019-07-24 stsp
6999 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7000 0ebf8283 2019-07-24 stsp if (err)
7001 0ebf8283 2019-07-24 stsp return err;
7002 0ebf8283 2019-07-24 stsp
7003 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7004 0ebf8283 2019-07-24 stsp if (folded) {
7005 0ebf8283 2019-07-24 stsp while (folded != hle) {
7006 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7007 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7008 3f9de99f 2019-07-24 stsp continue;
7009 3f9de99f 2019-07-24 stsp }
7010 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7011 0ebf8283 2019-07-24 stsp logmsg, repo);
7012 0ebf8283 2019-07-24 stsp if (err)
7013 0ebf8283 2019-07-24 stsp goto done;
7014 0ebf8283 2019-07-24 stsp free(logmsg);
7015 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7016 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7017 0ebf8283 2019-07-24 stsp }
7018 0ebf8283 2019-07-24 stsp }
7019 0ebf8283 2019-07-24 stsp
7020 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7021 0ebf8283 2019-07-24 stsp if (err)
7022 0ebf8283 2019-07-24 stsp goto done;
7023 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7024 5943eee2 2019-08-13 stsp if (err)
7025 5943eee2 2019-08-13 stsp goto done;
7026 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7027 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7028 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7029 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7030 0ebf8283 2019-07-24 stsp goto done;
7031 0ebf8283 2019-07-24 stsp }
7032 0ebf8283 2019-07-24 stsp free(logmsg);
7033 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7036 0ebf8283 2019-07-24 stsp if (err)
7037 0ebf8283 2019-07-24 stsp goto done;
7038 0ebf8283 2019-07-24 stsp
7039 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7040 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7041 0ebf8283 2019-07-24 stsp if (err)
7042 0ebf8283 2019-07-24 stsp goto done;
7043 0ebf8283 2019-07-24 stsp
7044 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7045 0ebf8283 2019-07-24 stsp close(fd);
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7048 0ebf8283 2019-07-24 stsp if (err)
7049 0ebf8283 2019-07-24 stsp goto done;
7050 0ebf8283 2019-07-24 stsp
7051 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7052 0ebf8283 2019-07-24 stsp if (err) {
7053 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7056 0ebf8283 2019-07-24 stsp }
7057 0ebf8283 2019-07-24 stsp done:
7058 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7059 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7060 0ebf8283 2019-07-24 stsp free(logmsg_path);
7061 0ebf8283 2019-07-24 stsp free(logmsg);
7062 5943eee2 2019-08-13 stsp free(orig_logmsg);
7063 0ebf8283 2019-07-24 stsp free(editor);
7064 0ebf8283 2019-07-24 stsp if (commit)
7065 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7066 0ebf8283 2019-07-24 stsp return err;
7067 5ef14e63 2019-06-02 stsp }
7068 0ebf8283 2019-07-24 stsp
7069 0ebf8283 2019-07-24 stsp static const struct got_error *
7070 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7071 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7072 0ebf8283 2019-07-24 stsp {
7073 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7074 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7075 0ebf8283 2019-07-24 stsp size_t size;
7076 0ebf8283 2019-07-24 stsp ssize_t len;
7077 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7078 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7079 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7080 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7081 0ebf8283 2019-07-24 stsp
7082 0ebf8283 2019-07-24 stsp for (;;) {
7083 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7084 0ebf8283 2019-07-24 stsp if (len == -1) {
7085 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7086 0ebf8283 2019-07-24 stsp if (feof(f))
7087 0ebf8283 2019-07-24 stsp break;
7088 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7089 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7090 0ebf8283 2019-07-24 stsp break;
7091 0ebf8283 2019-07-24 stsp }
7092 0ebf8283 2019-07-24 stsp lineno++;
7093 0ebf8283 2019-07-24 stsp p = line;
7094 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7095 0ebf8283 2019-07-24 stsp p++;
7096 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7097 0ebf8283 2019-07-24 stsp free(line);
7098 0ebf8283 2019-07-24 stsp line = NULL;
7099 0ebf8283 2019-07-24 stsp continue;
7100 0ebf8283 2019-07-24 stsp }
7101 0ebf8283 2019-07-24 stsp cmd = NULL;
7102 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7103 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7104 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7105 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7106 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7107 0ebf8283 2019-07-24 stsp break;
7108 0ebf8283 2019-07-24 stsp }
7109 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7110 0ebf8283 2019-07-24 stsp p++;
7111 0ebf8283 2019-07-24 stsp break;
7112 0ebf8283 2019-07-24 stsp }
7113 0ebf8283 2019-07-24 stsp }
7114 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7115 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7116 0ebf8283 2019-07-24 stsp break;
7117 0ebf8283 2019-07-24 stsp }
7118 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7119 0ebf8283 2019-07-24 stsp p++;
7120 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7121 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7122 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7123 0ebf8283 2019-07-24 stsp break;
7124 0ebf8283 2019-07-24 stsp }
7125 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7126 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7127 0ebf8283 2019-07-24 stsp if (err)
7128 0ebf8283 2019-07-24 stsp break;
7129 0ebf8283 2019-07-24 stsp } else {
7130 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7131 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7132 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7133 0ebf8283 2019-07-24 stsp break;
7134 0ebf8283 2019-07-24 stsp }
7135 0ebf8283 2019-07-24 stsp }
7136 0ebf8283 2019-07-24 stsp free(line);
7137 0ebf8283 2019-07-24 stsp line = NULL;
7138 0ebf8283 2019-07-24 stsp continue;
7139 0ebf8283 2019-07-24 stsp } else {
7140 0ebf8283 2019-07-24 stsp end = p;
7141 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7142 0ebf8283 2019-07-24 stsp end++;
7143 0ebf8283 2019-07-24 stsp *end = '\0';
7144 0ebf8283 2019-07-24 stsp
7145 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7146 0ebf8283 2019-07-24 stsp if (err) {
7147 0ebf8283 2019-07-24 stsp /* override error code */
7148 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7149 0ebf8283 2019-07-24 stsp break;
7150 0ebf8283 2019-07-24 stsp }
7151 0ebf8283 2019-07-24 stsp }
7152 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7153 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7154 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7155 0ebf8283 2019-07-24 stsp break;
7156 0ebf8283 2019-07-24 stsp }
7157 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7158 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7159 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7160 0ebf8283 2019-07-24 stsp commit_id = NULL;
7161 0ebf8283 2019-07-24 stsp free(line);
7162 0ebf8283 2019-07-24 stsp line = NULL;
7163 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7164 0ebf8283 2019-07-24 stsp }
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp free(line);
7167 0ebf8283 2019-07-24 stsp free(commit_id);
7168 0ebf8283 2019-07-24 stsp return err;
7169 0ebf8283 2019-07-24 stsp }
7170 0ebf8283 2019-07-24 stsp
7171 0ebf8283 2019-07-24 stsp static const struct got_error *
7172 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7173 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7174 bfce7f83 2019-07-27 stsp {
7175 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7176 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7177 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7178 5b87815e 2020-03-05 stsp static char msg[92];
7179 bfce7f83 2019-07-27 stsp char *id_str;
7180 bfce7f83 2019-07-27 stsp
7181 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7182 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7183 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7184 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7185 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7186 5b87815e 2020-03-05 stsp
7187 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7188 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7189 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7190 5b87815e 2020-03-05 stsp if (hle == hle2)
7191 5b87815e 2020-03-05 stsp continue;
7192 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7193 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7194 5b87815e 2020-03-05 stsp continue;
7195 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7196 5b87815e 2020-03-05 stsp if (err)
7197 5b87815e 2020-03-05 stsp return err;
7198 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7199 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7200 5b87815e 2020-03-05 stsp free(id_str);
7201 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7202 5b87815e 2020-03-05 stsp }
7203 5b87815e 2020-03-05 stsp }
7204 bfce7f83 2019-07-27 stsp
7205 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7206 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7207 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7208 bfce7f83 2019-07-27 stsp break;
7209 bfce7f83 2019-07-27 stsp }
7210 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7211 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7212 bfce7f83 2019-07-27 stsp if (err)
7213 bfce7f83 2019-07-27 stsp return err;
7214 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7215 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7216 bfce7f83 2019-07-27 stsp free(id_str);
7217 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7218 bfce7f83 2019-07-27 stsp }
7219 bfce7f83 2019-07-27 stsp }
7220 bfce7f83 2019-07-27 stsp
7221 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7222 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7223 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7224 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7225 bfce7f83 2019-07-27 stsp
7226 bfce7f83 2019-07-27 stsp return NULL;
7227 bfce7f83 2019-07-27 stsp }
7228 bfce7f83 2019-07-27 stsp
7229 bfce7f83 2019-07-27 stsp static const struct got_error *
7230 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7231 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7232 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7233 0ebf8283 2019-07-24 stsp {
7234 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7235 0ebf8283 2019-07-24 stsp char *editor;
7236 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7237 0ebf8283 2019-07-24 stsp
7238 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7239 0ebf8283 2019-07-24 stsp if (err)
7240 0ebf8283 2019-07-24 stsp return err;
7241 0ebf8283 2019-07-24 stsp
7242 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7243 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7244 0ebf8283 2019-07-24 stsp goto done;
7245 0ebf8283 2019-07-24 stsp }
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7248 0ebf8283 2019-07-24 stsp if (f == NULL) {
7249 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7250 0ebf8283 2019-07-24 stsp goto done;
7251 0ebf8283 2019-07-24 stsp }
7252 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7253 bfce7f83 2019-07-27 stsp if (err)
7254 bfce7f83 2019-07-27 stsp goto done;
7255 bfce7f83 2019-07-27 stsp
7256 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7257 0ebf8283 2019-07-24 stsp done:
7258 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7259 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7260 0ebf8283 2019-07-24 stsp free(editor);
7261 0ebf8283 2019-07-24 stsp return err;
7262 0ebf8283 2019-07-24 stsp }
7263 0ebf8283 2019-07-24 stsp
7264 0ebf8283 2019-07-24 stsp static const struct got_error *
7265 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7266 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7267 514f2ffe 2020-01-29 stsp struct got_repository *);
7268 0ebf8283 2019-07-24 stsp
7269 0ebf8283 2019-07-24 stsp static const struct got_error *
7270 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7271 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7272 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7273 0ebf8283 2019-07-24 stsp {
7274 0ebf8283 2019-07-24 stsp const struct got_error *err;
7275 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7276 0ebf8283 2019-07-24 stsp char *path = NULL;
7277 0ebf8283 2019-07-24 stsp
7278 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7279 0ebf8283 2019-07-24 stsp if (err)
7280 0ebf8283 2019-07-24 stsp return err;
7281 0ebf8283 2019-07-24 stsp
7282 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7283 0ebf8283 2019-07-24 stsp if (err)
7284 0ebf8283 2019-07-24 stsp goto done;
7285 0ebf8283 2019-07-24 stsp
7286 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7287 0ebf8283 2019-07-24 stsp if (err)
7288 0ebf8283 2019-07-24 stsp goto done;
7289 0ebf8283 2019-07-24 stsp
7290 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7291 083957f4 2020-02-24 stsp rewind(f);
7292 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7293 083957f4 2020-02-24 stsp } else {
7294 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7295 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7296 0ebf8283 2019-07-24 stsp goto done;
7297 083957f4 2020-02-24 stsp }
7298 083957f4 2020-02-24 stsp f = NULL;
7299 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7300 083957f4 2020-02-24 stsp if (err) {
7301 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7302 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7303 083957f4 2020-02-24 stsp goto done;
7304 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7305 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7306 083957f4 2020-02-24 stsp }
7307 0ebf8283 2019-07-24 stsp }
7308 0ebf8283 2019-07-24 stsp done:
7309 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7310 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7311 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7312 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7313 0ebf8283 2019-07-24 stsp free(path);
7314 0ebf8283 2019-07-24 stsp return err;
7315 0ebf8283 2019-07-24 stsp }
7316 0ebf8283 2019-07-24 stsp
7317 0ebf8283 2019-07-24 stsp static const struct got_error *
7318 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7319 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7320 0ebf8283 2019-07-24 stsp {
7321 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7322 0ebf8283 2019-07-24 stsp char *path = NULL;
7323 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7324 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7325 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7326 0ebf8283 2019-07-24 stsp
7327 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7328 0ebf8283 2019-07-24 stsp if (err)
7329 0ebf8283 2019-07-24 stsp return err;
7330 0ebf8283 2019-07-24 stsp
7331 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7332 0ebf8283 2019-07-24 stsp if (f == NULL) {
7333 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7334 0ebf8283 2019-07-24 stsp goto done;
7335 0ebf8283 2019-07-24 stsp }
7336 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7337 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7338 0ebf8283 2019-07-24 stsp repo);
7339 0ebf8283 2019-07-24 stsp if (err)
7340 0ebf8283 2019-07-24 stsp break;
7341 0ebf8283 2019-07-24 stsp
7342 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7343 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7344 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7345 0ebf8283 2019-07-24 stsp if (n < 0) {
7346 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7347 0ebf8283 2019-07-24 stsp break;
7348 0ebf8283 2019-07-24 stsp }
7349 0ebf8283 2019-07-24 stsp }
7350 0ebf8283 2019-07-24 stsp }
7351 0ebf8283 2019-07-24 stsp done:
7352 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7353 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7354 0ebf8283 2019-07-24 stsp free(path);
7355 0ebf8283 2019-07-24 stsp if (commit)
7356 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7357 0ebf8283 2019-07-24 stsp return err;
7358 0ebf8283 2019-07-24 stsp }
7359 0ebf8283 2019-07-24 stsp
7360 bfce7f83 2019-07-27 stsp void
7361 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7362 bfce7f83 2019-07-27 stsp {
7363 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7364 bfce7f83 2019-07-27 stsp
7365 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7366 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7367 bfce7f83 2019-07-27 stsp free(hle);
7368 bfce7f83 2019-07-27 stsp }
7369 bfce7f83 2019-07-27 stsp }
7370 bfce7f83 2019-07-27 stsp
7371 0ebf8283 2019-07-24 stsp static const struct got_error *
7372 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7373 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7374 0ebf8283 2019-07-24 stsp {
7375 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7376 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7377 0ebf8283 2019-07-24 stsp
7378 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7379 0ebf8283 2019-07-24 stsp if (f == NULL) {
7380 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7381 0ebf8283 2019-07-24 stsp goto done;
7382 0ebf8283 2019-07-24 stsp }
7383 0ebf8283 2019-07-24 stsp
7384 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7385 0ebf8283 2019-07-24 stsp done:
7386 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7387 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7388 0ebf8283 2019-07-24 stsp return err;
7389 0ebf8283 2019-07-24 stsp }
7390 0ebf8283 2019-07-24 stsp
7391 0ebf8283 2019-07-24 stsp static const struct got_error *
7392 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7393 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7394 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7395 0ebf8283 2019-07-24 stsp {
7396 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7397 0ebf8283 2019-07-24 stsp int resp = ' ';
7398 0ebf8283 2019-07-24 stsp
7399 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7400 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7401 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7402 0ebf8283 2019-07-24 stsp resp = getchar();
7403 a61a4414 2019-08-07 stsp if (resp == '\n')
7404 a61a4414 2019-08-07 stsp resp = getchar();
7405 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7406 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7407 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7408 bfce7f83 2019-07-27 stsp repo);
7409 426ebf2e 2019-07-25 stsp if (err) {
7410 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7411 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7412 426ebf2e 2019-07-25 stsp break;
7413 bfce7f83 2019-07-27 stsp prev_err = err;
7414 426ebf2e 2019-07-25 stsp resp = ' ';
7415 426ebf2e 2019-07-25 stsp continue;
7416 426ebf2e 2019-07-25 stsp }
7417 bfce7f83 2019-07-27 stsp break;
7418 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7419 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7420 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7421 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7422 426ebf2e 2019-07-25 stsp if (err) {
7423 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7424 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7425 426ebf2e 2019-07-25 stsp break;
7426 bfce7f83 2019-07-27 stsp prev_err = err;
7427 426ebf2e 2019-07-25 stsp resp = ' ';
7428 426ebf2e 2019-07-25 stsp continue;
7429 426ebf2e 2019-07-25 stsp }
7430 bfce7f83 2019-07-27 stsp break;
7431 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7432 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7433 0ebf8283 2019-07-24 stsp break;
7434 426ebf2e 2019-07-25 stsp } else
7435 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7436 0ebf8283 2019-07-24 stsp }
7437 0ebf8283 2019-07-24 stsp
7438 0ebf8283 2019-07-24 stsp return err;
7439 0ebf8283 2019-07-24 stsp }
7440 0ebf8283 2019-07-24 stsp
7441 0ebf8283 2019-07-24 stsp static const struct got_error *
7442 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7443 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7444 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7445 0ebf8283 2019-07-24 stsp {
7446 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7447 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7448 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7449 3e3a69f1 2019-07-25 stsp branch, repo);
7450 0ebf8283 2019-07-24 stsp }
7451 0ebf8283 2019-07-24 stsp
7452 0ebf8283 2019-07-24 stsp static const struct got_error *
7453 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7454 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7455 0ebf8283 2019-07-24 stsp {
7456 0ebf8283 2019-07-24 stsp const struct got_error *err;
7457 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7458 0ebf8283 2019-07-24 stsp
7459 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7460 0ebf8283 2019-07-24 stsp if (err)
7461 0ebf8283 2019-07-24 stsp goto done;
7462 0ebf8283 2019-07-24 stsp
7463 0ebf8283 2019-07-24 stsp if (new_id) {
7464 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7465 0ebf8283 2019-07-24 stsp if (err)
7466 0ebf8283 2019-07-24 stsp goto done;
7467 0ebf8283 2019-07-24 stsp }
7468 0ebf8283 2019-07-24 stsp
7469 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7470 0ebf8283 2019-07-24 stsp if (new_id_str)
7471 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7472 0ebf8283 2019-07-24 stsp
7473 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7474 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7475 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7476 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7477 0ebf8283 2019-07-24 stsp goto done;
7478 0ebf8283 2019-07-24 stsp }
7479 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7480 0ebf8283 2019-07-24 stsp } else {
7481 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7482 0ebf8283 2019-07-24 stsp if (err)
7483 0ebf8283 2019-07-24 stsp goto done;
7484 0ebf8283 2019-07-24 stsp }
7485 0ebf8283 2019-07-24 stsp
7486 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7487 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7488 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7489 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7490 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7491 0ebf8283 2019-07-24 stsp break;
7492 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7493 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7494 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7495 0ebf8283 2019-07-24 stsp logmsg);
7496 0ebf8283 2019-07-24 stsp break;
7497 0ebf8283 2019-07-24 stsp default:
7498 0ebf8283 2019-07-24 stsp break;
7499 0ebf8283 2019-07-24 stsp }
7500 0ebf8283 2019-07-24 stsp done:
7501 0ebf8283 2019-07-24 stsp free(old_id_str);
7502 0ebf8283 2019-07-24 stsp free(new_id_str);
7503 0ebf8283 2019-07-24 stsp return err;
7504 0ebf8283 2019-07-24 stsp }
7505 0ebf8283 2019-07-24 stsp
7506 0ebf8283 2019-07-24 stsp static const struct got_error *
7507 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7508 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7509 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7510 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7511 0ebf8283 2019-07-24 stsp {
7512 0ebf8283 2019-07-24 stsp const struct got_error *err;
7513 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7514 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7515 0ebf8283 2019-07-24 stsp
7516 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7517 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7518 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7519 0ebf8283 2019-07-24 stsp if (err)
7520 0ebf8283 2019-07-24 stsp return err;
7521 0ebf8283 2019-07-24 stsp }
7522 0ebf8283 2019-07-24 stsp
7523 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7524 0ebf8283 2019-07-24 stsp if (err)
7525 0ebf8283 2019-07-24 stsp return err;
7526 0ebf8283 2019-07-24 stsp
7527 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7528 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7529 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7530 0ebf8283 2019-07-24 stsp if (err) {
7531 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7532 0ebf8283 2019-07-24 stsp goto done;
7533 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7534 0ebf8283 2019-07-24 stsp } else {
7535 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7536 0ebf8283 2019-07-24 stsp free(new_commit_id);
7537 0ebf8283 2019-07-24 stsp }
7538 0ebf8283 2019-07-24 stsp done:
7539 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7540 0ebf8283 2019-07-24 stsp return err;
7541 0ebf8283 2019-07-24 stsp }
7542 0ebf8283 2019-07-24 stsp
7543 0ebf8283 2019-07-24 stsp static const struct got_error *
7544 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7545 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7546 0ebf8283 2019-07-24 stsp {
7547 0ebf8283 2019-07-24 stsp const struct got_error *error;
7548 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7549 0ebf8283 2019-07-24 stsp
7550 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7551 0ebf8283 2019-07-24 stsp repo);
7552 0ebf8283 2019-07-24 stsp if (error)
7553 0ebf8283 2019-07-24 stsp return error;
7554 0ebf8283 2019-07-24 stsp
7555 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7556 0ebf8283 2019-07-24 stsp if (error)
7557 0ebf8283 2019-07-24 stsp return error;
7558 0ebf8283 2019-07-24 stsp
7559 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7560 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7561 0ebf8283 2019-07-24 stsp return error;
7562 ab20a43a 2020-01-29 stsp }
7563 ab20a43a 2020-01-29 stsp
7564 ab20a43a 2020-01-29 stsp static const struct got_error *
7565 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7566 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7567 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7568 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7569 ab20a43a 2020-01-29 stsp {
7570 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7571 ab20a43a 2020-01-29 stsp
7572 ab20a43a 2020-01-29 stsp switch (status) {
7573 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7574 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7575 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7576 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7577 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7578 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7579 ab20a43a 2020-01-29 stsp default:
7580 ab20a43a 2020-01-29 stsp break;
7581 ab20a43a 2020-01-29 stsp }
7582 ab20a43a 2020-01-29 stsp
7583 ab20a43a 2020-01-29 stsp switch (staged_status) {
7584 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7585 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7586 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7587 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7588 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7589 ab20a43a 2020-01-29 stsp default:
7590 ab20a43a 2020-01-29 stsp break;
7591 ab20a43a 2020-01-29 stsp }
7592 ab20a43a 2020-01-29 stsp
7593 ab20a43a 2020-01-29 stsp return NULL;
7594 0ebf8283 2019-07-24 stsp }
7595 0ebf8283 2019-07-24 stsp
7596 0ebf8283 2019-07-24 stsp static const struct got_error *
7597 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7598 0ebf8283 2019-07-24 stsp {
7599 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7600 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7601 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7602 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7603 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7604 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7605 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7606 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7607 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7608 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7609 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7610 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7611 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7612 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7613 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7614 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7615 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7616 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7617 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7618 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7619 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7620 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7621 0ebf8283 2019-07-24 stsp
7622 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7623 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7624 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7625 0ebf8283 2019-07-24 stsp
7626 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7627 0ebf8283 2019-07-24 stsp switch (ch) {
7628 0ebf8283 2019-07-24 stsp case 'a':
7629 0ebf8283 2019-07-24 stsp abort_edit = 1;
7630 0ebf8283 2019-07-24 stsp break;
7631 0ebf8283 2019-07-24 stsp case 'c':
7632 0ebf8283 2019-07-24 stsp continue_edit = 1;
7633 0ebf8283 2019-07-24 stsp break;
7634 0ebf8283 2019-07-24 stsp case 'F':
7635 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7636 0ebf8283 2019-07-24 stsp break;
7637 083957f4 2020-02-24 stsp case 'm':
7638 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7639 083957f4 2020-02-24 stsp break;
7640 0ebf8283 2019-07-24 stsp default:
7641 0ebf8283 2019-07-24 stsp usage_histedit();
7642 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7643 0ebf8283 2019-07-24 stsp }
7644 0ebf8283 2019-07-24 stsp }
7645 0ebf8283 2019-07-24 stsp
7646 0ebf8283 2019-07-24 stsp argc -= optind;
7647 0ebf8283 2019-07-24 stsp argv += optind;
7648 0ebf8283 2019-07-24 stsp
7649 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7650 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7651 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7652 0ebf8283 2019-07-24 stsp err(1, "pledge");
7653 0ebf8283 2019-07-24 stsp #endif
7654 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7655 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7656 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7657 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7658 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7659 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7660 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7661 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7662 0ebf8283 2019-07-24 stsp if (argc != 0)
7663 0ebf8283 2019-07-24 stsp usage_histedit();
7664 0ebf8283 2019-07-24 stsp
7665 0ebf8283 2019-07-24 stsp /*
7666 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7667 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7668 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7669 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7670 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7671 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7672 0ebf8283 2019-07-24 stsp */
7673 0ebf8283 2019-07-24 stsp
7674 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7675 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7676 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7677 0ebf8283 2019-07-24 stsp goto done;
7678 0ebf8283 2019-07-24 stsp }
7679 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7680 0ebf8283 2019-07-24 stsp if (error)
7681 0ebf8283 2019-07-24 stsp goto done;
7682 0ebf8283 2019-07-24 stsp
7683 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7684 c9956ddf 2019-09-08 stsp NULL);
7685 0ebf8283 2019-07-24 stsp if (error != NULL)
7686 0ebf8283 2019-07-24 stsp goto done;
7687 0ebf8283 2019-07-24 stsp
7688 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7689 0ebf8283 2019-07-24 stsp if (error)
7690 0ebf8283 2019-07-24 stsp goto done;
7691 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7692 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7693 0ebf8283 2019-07-24 stsp goto done;
7694 0ebf8283 2019-07-24 stsp }
7695 0ebf8283 2019-07-24 stsp
7696 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7697 0ebf8283 2019-07-24 stsp if (error)
7698 0ebf8283 2019-07-24 stsp goto done;
7699 0ebf8283 2019-07-24 stsp
7700 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7701 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7702 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7703 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7704 083957f4 2020-02-24 stsp "before the -m option can be used");
7705 083957f4 2020-02-24 stsp goto done;
7706 083957f4 2020-02-24 stsp }
7707 083957f4 2020-02-24 stsp
7708 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7709 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7710 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7711 3e3a69f1 2019-07-25 stsp worktree, repo);
7712 0ebf8283 2019-07-24 stsp if (error)
7713 0ebf8283 2019-07-24 stsp goto done;
7714 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7715 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7716 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7717 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7718 0ebf8283 2019-07-24 stsp if (error)
7719 0ebf8283 2019-07-24 stsp goto done;
7720 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7721 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7722 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7723 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7724 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7725 0ebf8283 2019-07-24 stsp goto done;
7726 0ebf8283 2019-07-24 stsp }
7727 0ebf8283 2019-07-24 stsp
7728 0ebf8283 2019-07-24 stsp if (continue_edit) {
7729 0ebf8283 2019-07-24 stsp char *path;
7730 0ebf8283 2019-07-24 stsp
7731 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7732 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7733 0ebf8283 2019-07-24 stsp goto done;
7734 0ebf8283 2019-07-24 stsp }
7735 0ebf8283 2019-07-24 stsp
7736 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7737 0ebf8283 2019-07-24 stsp if (error)
7738 0ebf8283 2019-07-24 stsp goto done;
7739 0ebf8283 2019-07-24 stsp
7740 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7741 0ebf8283 2019-07-24 stsp free(path);
7742 0ebf8283 2019-07-24 stsp if (error)
7743 0ebf8283 2019-07-24 stsp goto done;
7744 0ebf8283 2019-07-24 stsp
7745 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7746 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7747 3e3a69f1 2019-07-25 stsp worktree, repo);
7748 0ebf8283 2019-07-24 stsp if (error)
7749 0ebf8283 2019-07-24 stsp goto done;
7750 0ebf8283 2019-07-24 stsp
7751 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7752 0ebf8283 2019-07-24 stsp if (error)
7753 0ebf8283 2019-07-24 stsp goto done;
7754 0ebf8283 2019-07-24 stsp
7755 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7756 0ebf8283 2019-07-24 stsp head_commit_id);
7757 0ebf8283 2019-07-24 stsp if (error)
7758 0ebf8283 2019-07-24 stsp goto done;
7759 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7760 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7761 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7762 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7763 3aac7cf7 2019-07-25 stsp goto done;
7764 3aac7cf7 2019-07-25 stsp }
7765 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7766 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7767 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7768 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7769 0ebf8283 2019-07-24 stsp commit = NULL;
7770 0ebf8283 2019-07-24 stsp if (error)
7771 0ebf8283 2019-07-24 stsp goto done;
7772 0ebf8283 2019-07-24 stsp } else {
7773 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7774 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7775 0ebf8283 2019-07-24 stsp goto done;
7776 0ebf8283 2019-07-24 stsp }
7777 0ebf8283 2019-07-24 stsp
7778 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7779 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7780 0ebf8283 2019-07-24 stsp if (error != NULL)
7781 0ebf8283 2019-07-24 stsp goto done;
7782 0ebf8283 2019-07-24 stsp
7783 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7784 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7785 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7786 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7787 c7d20a3f 2019-07-30 stsp goto done;
7788 c7d20a3f 2019-07-30 stsp }
7789 c7d20a3f 2019-07-30 stsp
7790 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7791 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7792 bfce7f83 2019-07-27 stsp branch = NULL;
7793 0ebf8283 2019-07-24 stsp if (error)
7794 0ebf8283 2019-07-24 stsp goto done;
7795 0ebf8283 2019-07-24 stsp
7796 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7797 0ebf8283 2019-07-24 stsp head_commit_id);
7798 0ebf8283 2019-07-24 stsp if (error)
7799 0ebf8283 2019-07-24 stsp goto done;
7800 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7801 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7802 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7803 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7804 3aac7cf7 2019-07-25 stsp goto done;
7805 3aac7cf7 2019-07-25 stsp }
7806 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7807 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7808 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7809 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7810 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7811 0ebf8283 2019-07-24 stsp commit = NULL;
7812 0ebf8283 2019-07-24 stsp if (error)
7813 0ebf8283 2019-07-24 stsp goto done;
7814 0ebf8283 2019-07-24 stsp
7815 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7816 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7817 c5996fff 2020-01-29 stsp goto done;
7818 c5996fff 2020-01-29 stsp }
7819 c5996fff 2020-01-29 stsp
7820 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7821 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7822 bfce7f83 2019-07-27 stsp if (error)
7823 bfce7f83 2019-07-27 stsp goto done;
7824 bfce7f83 2019-07-27 stsp
7825 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7826 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7827 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7828 6ba2bea2 2019-07-27 stsp if (error) {
7829 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7830 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7831 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7832 0ebf8283 2019-07-24 stsp goto done;
7833 6ba2bea2 2019-07-27 stsp }
7834 0ebf8283 2019-07-24 stsp } else {
7835 514f2ffe 2020-01-29 stsp const char *branch_name;
7836 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7837 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7838 514f2ffe 2020-01-29 stsp branch_name += 11;
7839 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7840 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7841 6ba2bea2 2019-07-27 stsp if (error) {
7842 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7843 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7844 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7845 0ebf8283 2019-07-24 stsp goto done;
7846 6ba2bea2 2019-07-27 stsp }
7847 0ebf8283 2019-07-24 stsp
7848 0ebf8283 2019-07-24 stsp }
7849 0ebf8283 2019-07-24 stsp
7850 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7851 0ebf8283 2019-07-24 stsp repo);
7852 6ba2bea2 2019-07-27 stsp if (error) {
7853 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7854 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7855 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7856 0ebf8283 2019-07-24 stsp goto done;
7857 6ba2bea2 2019-07-27 stsp }
7858 0ebf8283 2019-07-24 stsp
7859 0ebf8283 2019-07-24 stsp }
7860 0ebf8283 2019-07-24 stsp
7861 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7862 4ec14e60 2019-08-28 hiltjo if (error)
7863 0ebf8283 2019-07-24 stsp goto done;
7864 0ebf8283 2019-07-24 stsp
7865 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7866 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7867 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7868 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7869 0ebf8283 2019-07-24 stsp continue;
7870 0ebf8283 2019-07-24 stsp
7871 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7872 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7873 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7874 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7875 0ebf8283 2019-07-24 stsp repo);
7876 ab20a43a 2020-01-29 stsp if (error)
7877 ab20a43a 2020-01-29 stsp goto done;
7878 0ebf8283 2019-07-24 stsp } else {
7879 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7880 ab20a43a 2020-01-29 stsp int have_changes = 0;
7881 ab20a43a 2020-01-29 stsp
7882 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7883 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7884 ab20a43a 2020-01-29 stsp if (error)
7885 ab20a43a 2020-01-29 stsp goto done;
7886 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7887 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7888 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7889 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7890 ab20a43a 2020-01-29 stsp if (error) {
7891 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7892 ab20a43a 2020-01-29 stsp goto done;
7893 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7894 ab20a43a 2020-01-29 stsp goto done;
7895 ab20a43a 2020-01-29 stsp }
7896 ab20a43a 2020-01-29 stsp if (have_changes) {
7897 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7898 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7899 ab20a43a 2020-01-29 stsp if (error)
7900 ab20a43a 2020-01-29 stsp goto done;
7901 ab20a43a 2020-01-29 stsp } else {
7902 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7903 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7904 ab20a43a 2020-01-29 stsp if (error)
7905 ab20a43a 2020-01-29 stsp goto done;
7906 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7907 ab20a43a 2020-01-29 stsp hle, NULL);
7908 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7909 ab20a43a 2020-01-29 stsp commit = NULL;
7910 ab20a43a 2020-01-29 stsp if (error)
7911 ab20a43a 2020-01-29 stsp goto done;
7912 ab20a43a 2020-01-29 stsp }
7913 0ebf8283 2019-07-24 stsp }
7914 0ebf8283 2019-07-24 stsp continue;
7915 0ebf8283 2019-07-24 stsp }
7916 0ebf8283 2019-07-24 stsp
7917 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7918 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7919 0ebf8283 2019-07-24 stsp if (error)
7920 0ebf8283 2019-07-24 stsp goto done;
7921 0ebf8283 2019-07-24 stsp continue;
7922 0ebf8283 2019-07-24 stsp }
7923 0ebf8283 2019-07-24 stsp
7924 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7925 0ebf8283 2019-07-24 stsp hle->commit_id);
7926 0ebf8283 2019-07-24 stsp if (error)
7927 0ebf8283 2019-07-24 stsp goto done;
7928 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7929 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7930 0ebf8283 2019-07-24 stsp
7931 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7932 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7933 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7934 0ebf8283 2019-07-24 stsp if (error)
7935 0ebf8283 2019-07-24 stsp goto done;
7936 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7937 0ebf8283 2019-07-24 stsp commit = NULL;
7938 0ebf8283 2019-07-24 stsp
7939 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7940 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7941 a0ea4fc0 2020-02-28 stsp repo);
7942 a0ea4fc0 2020-02-28 stsp if (error)
7943 a0ea4fc0 2020-02-28 stsp goto done;
7944 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7945 0ebf8283 2019-07-24 stsp break;
7946 0ebf8283 2019-07-24 stsp }
7947 0ebf8283 2019-07-24 stsp
7948 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7949 0ebf8283 2019-07-24 stsp char *id_str;
7950 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7951 0ebf8283 2019-07-24 stsp if (error)
7952 0ebf8283 2019-07-24 stsp goto done;
7953 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7954 0ebf8283 2019-07-24 stsp id_str);
7955 0ebf8283 2019-07-24 stsp free(id_str);
7956 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7957 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7958 3e3a69f1 2019-07-25 stsp fileindex);
7959 0ebf8283 2019-07-24 stsp goto done;
7960 0ebf8283 2019-07-24 stsp }
7961 0ebf8283 2019-07-24 stsp
7962 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7963 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7964 0ebf8283 2019-07-24 stsp if (error)
7965 0ebf8283 2019-07-24 stsp goto done;
7966 0ebf8283 2019-07-24 stsp continue;
7967 0ebf8283 2019-07-24 stsp }
7968 0ebf8283 2019-07-24 stsp
7969 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7970 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7971 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7972 0ebf8283 2019-07-24 stsp if (error)
7973 0ebf8283 2019-07-24 stsp goto done;
7974 0ebf8283 2019-07-24 stsp }
7975 0ebf8283 2019-07-24 stsp
7976 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7977 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7978 0ebf8283 2019-07-24 stsp if (error)
7979 0ebf8283 2019-07-24 stsp goto done;
7980 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7981 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7982 0ebf8283 2019-07-24 stsp } else
7983 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7984 3e3a69f1 2019-07-25 stsp branch, repo);
7985 0ebf8283 2019-07-24 stsp done:
7986 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7987 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7988 0ebf8283 2019-07-24 stsp free(head_commit_id);
7989 0ebf8283 2019-07-24 stsp free(base_commit_id);
7990 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7991 0ebf8283 2019-07-24 stsp if (commit)
7992 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7993 0ebf8283 2019-07-24 stsp if (branch)
7994 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7995 0ebf8283 2019-07-24 stsp if (tmp_branch)
7996 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7997 0ebf8283 2019-07-24 stsp if (worktree)
7998 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7999 2822a352 2019-10-15 stsp if (repo)
8000 2822a352 2019-10-15 stsp got_repo_close(repo);
8001 2822a352 2019-10-15 stsp return error;
8002 2822a352 2019-10-15 stsp }
8003 2822a352 2019-10-15 stsp
8004 2822a352 2019-10-15 stsp __dead static void
8005 2822a352 2019-10-15 stsp usage_integrate(void)
8006 2822a352 2019-10-15 stsp {
8007 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8008 2822a352 2019-10-15 stsp exit(1);
8009 2822a352 2019-10-15 stsp }
8010 2822a352 2019-10-15 stsp
8011 2822a352 2019-10-15 stsp static const struct got_error *
8012 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8013 2822a352 2019-10-15 stsp {
8014 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8015 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8016 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8017 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8018 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8019 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8020 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8021 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8022 2822a352 2019-10-15 stsp int ch, did_something = 0;
8023 2822a352 2019-10-15 stsp
8024 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8025 2822a352 2019-10-15 stsp switch (ch) {
8026 2822a352 2019-10-15 stsp default:
8027 2822a352 2019-10-15 stsp usage_integrate();
8028 2822a352 2019-10-15 stsp /* NOTREACHED */
8029 2822a352 2019-10-15 stsp }
8030 2822a352 2019-10-15 stsp }
8031 2822a352 2019-10-15 stsp
8032 2822a352 2019-10-15 stsp argc -= optind;
8033 2822a352 2019-10-15 stsp argv += optind;
8034 2822a352 2019-10-15 stsp
8035 2822a352 2019-10-15 stsp if (argc != 1)
8036 2822a352 2019-10-15 stsp usage_integrate();
8037 2822a352 2019-10-15 stsp branch_arg = argv[0];
8038 2822a352 2019-10-15 stsp
8039 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8040 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8041 2822a352 2019-10-15 stsp err(1, "pledge");
8042 2822a352 2019-10-15 stsp
8043 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8044 2822a352 2019-10-15 stsp if (cwd == NULL) {
8045 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8046 2822a352 2019-10-15 stsp goto done;
8047 2822a352 2019-10-15 stsp }
8048 2822a352 2019-10-15 stsp
8049 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8050 2822a352 2019-10-15 stsp if (error)
8051 2822a352 2019-10-15 stsp goto done;
8052 2822a352 2019-10-15 stsp
8053 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8054 2822a352 2019-10-15 stsp if (error)
8055 2822a352 2019-10-15 stsp goto done;
8056 2822a352 2019-10-15 stsp
8057 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8058 2822a352 2019-10-15 stsp NULL);
8059 2822a352 2019-10-15 stsp if (error != NULL)
8060 2822a352 2019-10-15 stsp goto done;
8061 2822a352 2019-10-15 stsp
8062 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8063 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8064 2822a352 2019-10-15 stsp if (error)
8065 2822a352 2019-10-15 stsp goto done;
8066 2822a352 2019-10-15 stsp
8067 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8068 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8069 2822a352 2019-10-15 stsp goto done;
8070 2822a352 2019-10-15 stsp }
8071 2822a352 2019-10-15 stsp
8072 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8073 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8074 2822a352 2019-10-15 stsp if (error)
8075 2822a352 2019-10-15 stsp goto done;
8076 2822a352 2019-10-15 stsp
8077 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8078 2822a352 2019-10-15 stsp if (refname == NULL) {
8079 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8080 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8081 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8082 2822a352 2019-10-15 stsp goto done;
8083 2822a352 2019-10-15 stsp }
8084 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8085 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8086 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8087 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8088 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8089 2822a352 2019-10-15 stsp goto done;
8090 2822a352 2019-10-15 stsp }
8091 2822a352 2019-10-15 stsp
8092 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8093 2822a352 2019-10-15 stsp if (error)
8094 2822a352 2019-10-15 stsp goto done;
8095 2822a352 2019-10-15 stsp
8096 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8097 2822a352 2019-10-15 stsp if (error)
8098 2822a352 2019-10-15 stsp goto done;
8099 2822a352 2019-10-15 stsp
8100 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8101 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8102 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8103 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8104 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8105 2822a352 2019-10-15 stsp goto done;
8106 2822a352 2019-10-15 stsp }
8107 2822a352 2019-10-15 stsp
8108 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8109 2822a352 2019-10-15 stsp if (error) {
8110 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8111 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8112 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8113 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8114 2822a352 2019-10-15 stsp goto done;
8115 2822a352 2019-10-15 stsp }
8116 2822a352 2019-10-15 stsp
8117 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8118 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
8119 2822a352 2019-10-15 stsp check_cancelled, NULL);
8120 2822a352 2019-10-15 stsp if (error)
8121 2822a352 2019-10-15 stsp goto done;
8122 2822a352 2019-10-15 stsp
8123 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8124 2822a352 2019-10-15 stsp done:
8125 715dc77e 2019-08-03 stsp if (repo)
8126 715dc77e 2019-08-03 stsp got_repo_close(repo);
8127 2822a352 2019-10-15 stsp if (worktree)
8128 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8129 2822a352 2019-10-15 stsp free(cwd);
8130 2822a352 2019-10-15 stsp free(base_commit_id);
8131 2822a352 2019-10-15 stsp free(commit_id);
8132 2822a352 2019-10-15 stsp free(refname);
8133 2822a352 2019-10-15 stsp free(base_refname);
8134 715dc77e 2019-08-03 stsp return error;
8135 715dc77e 2019-08-03 stsp }
8136 715dc77e 2019-08-03 stsp
8137 715dc77e 2019-08-03 stsp __dead static void
8138 715dc77e 2019-08-03 stsp usage_stage(void)
8139 715dc77e 2019-08-03 stsp {
8140 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8141 f3055044 2019-08-07 stsp "[file-path ...]\n",
8142 715dc77e 2019-08-03 stsp getprogname());
8143 715dc77e 2019-08-03 stsp exit(1);
8144 715dc77e 2019-08-03 stsp }
8145 715dc77e 2019-08-03 stsp
8146 715dc77e 2019-08-03 stsp static const struct got_error *
8147 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8148 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8149 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8150 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8151 408b4ebc 2019-08-03 stsp {
8152 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8153 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8154 408b4ebc 2019-08-03 stsp
8155 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8156 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8157 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8158 408b4ebc 2019-08-03 stsp return NULL;
8159 408b4ebc 2019-08-03 stsp
8160 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8161 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8162 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8163 408b4ebc 2019-08-03 stsp else
8164 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8165 408b4ebc 2019-08-03 stsp if (err)
8166 408b4ebc 2019-08-03 stsp return err;
8167 408b4ebc 2019-08-03 stsp
8168 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8169 408b4ebc 2019-08-03 stsp free(id_str);
8170 dc424a06 2019-08-07 stsp return NULL;
8171 dc424a06 2019-08-07 stsp }
8172 2e1f37b0 2019-08-08 stsp
8173 dc424a06 2019-08-07 stsp static const struct got_error *
8174 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8175 715dc77e 2019-08-03 stsp {
8176 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8177 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8178 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8179 715dc77e 2019-08-03 stsp char *cwd = NULL;
8180 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8181 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8182 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8183 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8184 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8185 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8186 715dc77e 2019-08-03 stsp
8187 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8188 715dc77e 2019-08-03 stsp
8189 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8190 715dc77e 2019-08-03 stsp switch (ch) {
8191 408b4ebc 2019-08-03 stsp case 'l':
8192 408b4ebc 2019-08-03 stsp list_stage = 1;
8193 408b4ebc 2019-08-03 stsp break;
8194 dc424a06 2019-08-07 stsp case 'p':
8195 dc424a06 2019-08-07 stsp pflag = 1;
8196 dc424a06 2019-08-07 stsp break;
8197 dc424a06 2019-08-07 stsp case 'F':
8198 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8199 dc424a06 2019-08-07 stsp break;
8200 715dc77e 2019-08-03 stsp default:
8201 715dc77e 2019-08-03 stsp usage_stage();
8202 715dc77e 2019-08-03 stsp /* NOTREACHED */
8203 715dc77e 2019-08-03 stsp }
8204 715dc77e 2019-08-03 stsp }
8205 715dc77e 2019-08-03 stsp
8206 715dc77e 2019-08-03 stsp argc -= optind;
8207 715dc77e 2019-08-03 stsp argv += optind;
8208 715dc77e 2019-08-03 stsp
8209 715dc77e 2019-08-03 stsp #ifndef PROFILE
8210 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8211 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8212 715dc77e 2019-08-03 stsp err(1, "pledge");
8213 715dc77e 2019-08-03 stsp #endif
8214 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8215 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8216 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8217 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8218 715dc77e 2019-08-03 stsp
8219 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8220 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8221 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8222 715dc77e 2019-08-03 stsp goto done;
8223 715dc77e 2019-08-03 stsp }
8224 715dc77e 2019-08-03 stsp
8225 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8226 715dc77e 2019-08-03 stsp if (error)
8227 715dc77e 2019-08-03 stsp goto done;
8228 715dc77e 2019-08-03 stsp
8229 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8230 c9956ddf 2019-09-08 stsp NULL);
8231 715dc77e 2019-08-03 stsp if (error != NULL)
8232 715dc77e 2019-08-03 stsp goto done;
8233 715dc77e 2019-08-03 stsp
8234 dc424a06 2019-08-07 stsp if (patch_script_path) {
8235 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8236 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8237 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8238 dc424a06 2019-08-07 stsp patch_script_path);
8239 dc424a06 2019-08-07 stsp goto done;
8240 dc424a06 2019-08-07 stsp }
8241 dc424a06 2019-08-07 stsp }
8242 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8243 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8244 715dc77e 2019-08-03 stsp if (error)
8245 715dc77e 2019-08-03 stsp goto done;
8246 715dc77e 2019-08-03 stsp
8247 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8248 6d022e97 2019-08-04 stsp if (error)
8249 6d022e97 2019-08-04 stsp goto done;
8250 715dc77e 2019-08-03 stsp
8251 6d022e97 2019-08-04 stsp if (list_stage)
8252 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8253 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8254 2e1f37b0 2019-08-08 stsp else {
8255 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8256 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8257 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8258 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8259 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8260 2e1f37b0 2019-08-08 stsp }
8261 ad493afc 2019-08-03 stsp done:
8262 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8263 2e1f37b0 2019-08-08 stsp error == NULL)
8264 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8265 ad493afc 2019-08-03 stsp if (repo)
8266 ad493afc 2019-08-03 stsp got_repo_close(repo);
8267 ad493afc 2019-08-03 stsp if (worktree)
8268 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8269 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8270 ad493afc 2019-08-03 stsp free((char *)pe->path);
8271 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8272 ad493afc 2019-08-03 stsp free(cwd);
8273 ad493afc 2019-08-03 stsp return error;
8274 ad493afc 2019-08-03 stsp }
8275 ad493afc 2019-08-03 stsp
8276 ad493afc 2019-08-03 stsp __dead static void
8277 ad493afc 2019-08-03 stsp usage_unstage(void)
8278 ad493afc 2019-08-03 stsp {
8279 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8280 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8281 ad493afc 2019-08-03 stsp getprogname());
8282 ad493afc 2019-08-03 stsp exit(1);
8283 ad493afc 2019-08-03 stsp }
8284 ad493afc 2019-08-03 stsp
8285 ad493afc 2019-08-03 stsp
8286 ad493afc 2019-08-03 stsp static const struct got_error *
8287 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8288 ad493afc 2019-08-03 stsp {
8289 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8290 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8291 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8292 ad493afc 2019-08-03 stsp char *cwd = NULL;
8293 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8294 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8295 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8296 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8297 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8298 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8299 ad493afc 2019-08-03 stsp
8300 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8301 ad493afc 2019-08-03 stsp
8302 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8303 ad493afc 2019-08-03 stsp switch (ch) {
8304 2e1f37b0 2019-08-08 stsp case 'p':
8305 2e1f37b0 2019-08-08 stsp pflag = 1;
8306 2e1f37b0 2019-08-08 stsp break;
8307 2e1f37b0 2019-08-08 stsp case 'F':
8308 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8309 2e1f37b0 2019-08-08 stsp break;
8310 ad493afc 2019-08-03 stsp default:
8311 ad493afc 2019-08-03 stsp usage_unstage();
8312 ad493afc 2019-08-03 stsp /* NOTREACHED */
8313 ad493afc 2019-08-03 stsp }
8314 ad493afc 2019-08-03 stsp }
8315 ad493afc 2019-08-03 stsp
8316 ad493afc 2019-08-03 stsp argc -= optind;
8317 ad493afc 2019-08-03 stsp argv += optind;
8318 ad493afc 2019-08-03 stsp
8319 ad493afc 2019-08-03 stsp #ifndef PROFILE
8320 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8321 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8322 ad493afc 2019-08-03 stsp err(1, "pledge");
8323 ad493afc 2019-08-03 stsp #endif
8324 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8325 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8326 2e1f37b0 2019-08-08 stsp
8327 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8328 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8329 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8330 ad493afc 2019-08-03 stsp goto done;
8331 ad493afc 2019-08-03 stsp }
8332 ad493afc 2019-08-03 stsp
8333 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8334 ad493afc 2019-08-03 stsp if (error)
8335 ad493afc 2019-08-03 stsp goto done;
8336 ad493afc 2019-08-03 stsp
8337 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8338 c9956ddf 2019-09-08 stsp NULL);
8339 ad493afc 2019-08-03 stsp if (error != NULL)
8340 ad493afc 2019-08-03 stsp goto done;
8341 ad493afc 2019-08-03 stsp
8342 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8343 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8344 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8345 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8346 2e1f37b0 2019-08-08 stsp patch_script_path);
8347 2e1f37b0 2019-08-08 stsp goto done;
8348 2e1f37b0 2019-08-08 stsp }
8349 2e1f37b0 2019-08-08 stsp }
8350 2e1f37b0 2019-08-08 stsp
8351 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8352 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8353 ad493afc 2019-08-03 stsp if (error)
8354 ad493afc 2019-08-03 stsp goto done;
8355 ad493afc 2019-08-03 stsp
8356 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8357 ad493afc 2019-08-03 stsp if (error)
8358 ad493afc 2019-08-03 stsp goto done;
8359 ad493afc 2019-08-03 stsp
8360 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8361 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8362 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8363 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8364 715dc77e 2019-08-03 stsp done:
8365 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8366 2e1f37b0 2019-08-08 stsp error == NULL)
8367 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8368 0ebf8283 2019-07-24 stsp if (repo)
8369 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8370 715dc77e 2019-08-03 stsp if (worktree)
8371 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8372 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8373 715dc77e 2019-08-03 stsp free((char *)pe->path);
8374 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8375 715dc77e 2019-08-03 stsp free(cwd);
8376 01073a5d 2019-08-22 stsp return error;
8377 01073a5d 2019-08-22 stsp }
8378 01073a5d 2019-08-22 stsp
8379 01073a5d 2019-08-22 stsp __dead static void
8380 01073a5d 2019-08-22 stsp usage_cat(void)
8381 01073a5d 2019-08-22 stsp {
8382 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8383 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8384 01073a5d 2019-08-22 stsp exit(1);
8385 01073a5d 2019-08-22 stsp }
8386 01073a5d 2019-08-22 stsp
8387 01073a5d 2019-08-22 stsp static const struct got_error *
8388 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8389 01073a5d 2019-08-22 stsp {
8390 01073a5d 2019-08-22 stsp const struct got_error *err;
8391 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8392 01073a5d 2019-08-22 stsp
8393 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8394 01073a5d 2019-08-22 stsp if (err)
8395 01073a5d 2019-08-22 stsp return err;
8396 01073a5d 2019-08-22 stsp
8397 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8398 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8399 01073a5d 2019-08-22 stsp return err;
8400 01073a5d 2019-08-22 stsp }
8401 01073a5d 2019-08-22 stsp
8402 01073a5d 2019-08-22 stsp static const struct got_error *
8403 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8404 01073a5d 2019-08-22 stsp {
8405 01073a5d 2019-08-22 stsp const struct got_error *err;
8406 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8407 56e0773d 2019-11-28 stsp int nentries, i;
8408 01073a5d 2019-08-22 stsp
8409 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8410 01073a5d 2019-08-22 stsp if (err)
8411 01073a5d 2019-08-22 stsp return err;
8412 01073a5d 2019-08-22 stsp
8413 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8414 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8415 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8416 01073a5d 2019-08-22 stsp char *id_str;
8417 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8418 01073a5d 2019-08-22 stsp break;
8419 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8420 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8421 01073a5d 2019-08-22 stsp if (err)
8422 01073a5d 2019-08-22 stsp break;
8423 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8424 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8425 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8426 01073a5d 2019-08-22 stsp free(id_str);
8427 01073a5d 2019-08-22 stsp }
8428 01073a5d 2019-08-22 stsp
8429 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8430 01073a5d 2019-08-22 stsp return err;
8431 01073a5d 2019-08-22 stsp }
8432 01073a5d 2019-08-22 stsp
8433 01073a5d 2019-08-22 stsp static const struct got_error *
8434 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8435 01073a5d 2019-08-22 stsp {
8436 01073a5d 2019-08-22 stsp const struct got_error *err;
8437 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8438 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8439 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8440 01073a5d 2019-08-22 stsp char *id_str = NULL;
8441 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8442 01073a5d 2019-08-22 stsp
8443 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8444 01073a5d 2019-08-22 stsp if (err)
8445 01073a5d 2019-08-22 stsp return err;
8446 01073a5d 2019-08-22 stsp
8447 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8448 01073a5d 2019-08-22 stsp if (err)
8449 01073a5d 2019-08-22 stsp goto done;
8450 01073a5d 2019-08-22 stsp
8451 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8452 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8453 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8454 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8455 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8456 01073a5d 2019-08-22 stsp char *pid_str;
8457 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8458 01073a5d 2019-08-22 stsp if (err)
8459 01073a5d 2019-08-22 stsp goto done;
8460 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8461 01073a5d 2019-08-22 stsp free(pid_str);
8462 01073a5d 2019-08-22 stsp }
8463 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8464 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8465 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8466 01073a5d 2019-08-22 stsp
8467 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8468 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8469 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8470 01073a5d 2019-08-22 stsp
8471 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8472 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8473 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8474 01073a5d 2019-08-22 stsp done:
8475 01073a5d 2019-08-22 stsp free(id_str);
8476 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8477 01073a5d 2019-08-22 stsp return err;
8478 01073a5d 2019-08-22 stsp }
8479 01073a5d 2019-08-22 stsp
8480 01073a5d 2019-08-22 stsp static const struct got_error *
8481 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8482 01073a5d 2019-08-22 stsp {
8483 01073a5d 2019-08-22 stsp const struct got_error *err;
8484 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8485 01073a5d 2019-08-22 stsp char *id_str = NULL;
8486 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8487 01073a5d 2019-08-22 stsp
8488 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8489 01073a5d 2019-08-22 stsp if (err)
8490 01073a5d 2019-08-22 stsp return err;
8491 01073a5d 2019-08-22 stsp
8492 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8493 01073a5d 2019-08-22 stsp if (err)
8494 01073a5d 2019-08-22 stsp goto done;
8495 01073a5d 2019-08-22 stsp
8496 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8497 e15d5241 2019-08-22 stsp
8498 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8499 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8500 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8501 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8502 01073a5d 2019-08-22 stsp break;
8503 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8504 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8505 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8506 01073a5d 2019-08-22 stsp break;
8507 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8508 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8509 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8510 01073a5d 2019-08-22 stsp break;
8511 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8512 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8513 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8514 01073a5d 2019-08-22 stsp break;
8515 01073a5d 2019-08-22 stsp default:
8516 01073a5d 2019-08-22 stsp break;
8517 01073a5d 2019-08-22 stsp }
8518 01073a5d 2019-08-22 stsp
8519 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8520 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8521 e15d5241 2019-08-22 stsp
8522 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8523 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8524 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8525 01073a5d 2019-08-22 stsp
8526 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8527 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8528 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8529 01073a5d 2019-08-22 stsp done:
8530 01073a5d 2019-08-22 stsp free(id_str);
8531 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8532 01073a5d 2019-08-22 stsp return err;
8533 01073a5d 2019-08-22 stsp }
8534 01073a5d 2019-08-22 stsp
8535 01073a5d 2019-08-22 stsp static const struct got_error *
8536 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8537 01073a5d 2019-08-22 stsp {
8538 01073a5d 2019-08-22 stsp const struct got_error *error;
8539 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8540 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8541 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8542 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8543 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8544 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8545 01073a5d 2019-08-22 stsp
8546 01073a5d 2019-08-22 stsp #ifndef PROFILE
8547 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8548 01073a5d 2019-08-22 stsp NULL) == -1)
8549 01073a5d 2019-08-22 stsp err(1, "pledge");
8550 01073a5d 2019-08-22 stsp #endif
8551 01073a5d 2019-08-22 stsp
8552 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8553 01073a5d 2019-08-22 stsp switch (ch) {
8554 896e9b6f 2019-08-26 stsp case 'c':
8555 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8556 896e9b6f 2019-08-26 stsp break;
8557 01073a5d 2019-08-22 stsp case 'r':
8558 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8559 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8560 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8561 9ba1d308 2019-10-21 stsp optarg);
8562 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8563 01073a5d 2019-08-22 stsp break;
8564 896e9b6f 2019-08-26 stsp case 'P':
8565 896e9b6f 2019-08-26 stsp force_path = 1;
8566 896e9b6f 2019-08-26 stsp break;
8567 01073a5d 2019-08-22 stsp default:
8568 01073a5d 2019-08-22 stsp usage_cat();
8569 01073a5d 2019-08-22 stsp /* NOTREACHED */
8570 01073a5d 2019-08-22 stsp }
8571 01073a5d 2019-08-22 stsp }
8572 01073a5d 2019-08-22 stsp
8573 01073a5d 2019-08-22 stsp argc -= optind;
8574 01073a5d 2019-08-22 stsp argv += optind;
8575 01073a5d 2019-08-22 stsp
8576 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8577 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8578 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8579 01073a5d 2019-08-22 stsp goto done;
8580 01073a5d 2019-08-22 stsp }
8581 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8582 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8583 01073a5d 2019-08-22 stsp goto done;
8584 01073a5d 2019-08-22 stsp if (worktree) {
8585 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8586 01073a5d 2019-08-22 stsp repo_path = strdup(
8587 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8588 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8589 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8590 01073a5d 2019-08-22 stsp goto done;
8591 01073a5d 2019-08-22 stsp }
8592 01073a5d 2019-08-22 stsp }
8593 01073a5d 2019-08-22 stsp }
8594 01073a5d 2019-08-22 stsp
8595 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8596 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8597 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8598 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8599 01073a5d 2019-08-22 stsp }
8600 01073a5d 2019-08-22 stsp
8601 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8602 01073a5d 2019-08-22 stsp free(repo_path);
8603 01073a5d 2019-08-22 stsp if (error != NULL)
8604 01073a5d 2019-08-22 stsp goto done;
8605 01073a5d 2019-08-22 stsp
8606 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8607 896e9b6f 2019-08-26 stsp if (error)
8608 896e9b6f 2019-08-26 stsp goto done;
8609 896e9b6f 2019-08-26 stsp
8610 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8611 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8612 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8613 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8614 01073a5d 2019-08-22 stsp if (error)
8615 01073a5d 2019-08-22 stsp goto done;
8616 01073a5d 2019-08-22 stsp
8617 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8618 896e9b6f 2019-08-26 stsp if (force_path) {
8619 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8620 896e9b6f 2019-08-26 stsp argv[i]);
8621 896e9b6f 2019-08-26 stsp if (error)
8622 896e9b6f 2019-08-26 stsp break;
8623 896e9b6f 2019-08-26 stsp } else {
8624 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8625 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8626 896e9b6f 2019-08-26 stsp if (error) {
8627 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8628 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8629 896e9b6f 2019-08-26 stsp break;
8630 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8631 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8632 896e9b6f 2019-08-26 stsp if (error)
8633 896e9b6f 2019-08-26 stsp break;
8634 896e9b6f 2019-08-26 stsp }
8635 896e9b6f 2019-08-26 stsp }
8636 01073a5d 2019-08-22 stsp
8637 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8638 01073a5d 2019-08-22 stsp if (error)
8639 01073a5d 2019-08-22 stsp break;
8640 01073a5d 2019-08-22 stsp
8641 01073a5d 2019-08-22 stsp switch (obj_type) {
8642 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8643 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8644 01073a5d 2019-08-22 stsp break;
8645 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8646 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8647 01073a5d 2019-08-22 stsp break;
8648 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8649 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8650 01073a5d 2019-08-22 stsp break;
8651 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8652 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8653 01073a5d 2019-08-22 stsp break;
8654 01073a5d 2019-08-22 stsp default:
8655 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8656 01073a5d 2019-08-22 stsp break;
8657 01073a5d 2019-08-22 stsp }
8658 01073a5d 2019-08-22 stsp if (error)
8659 01073a5d 2019-08-22 stsp break;
8660 01073a5d 2019-08-22 stsp free(label);
8661 01073a5d 2019-08-22 stsp label = NULL;
8662 01073a5d 2019-08-22 stsp free(id);
8663 01073a5d 2019-08-22 stsp id = NULL;
8664 01073a5d 2019-08-22 stsp }
8665 01073a5d 2019-08-22 stsp done:
8666 01073a5d 2019-08-22 stsp free(label);
8667 01073a5d 2019-08-22 stsp free(id);
8668 896e9b6f 2019-08-26 stsp free(commit_id);
8669 01073a5d 2019-08-22 stsp if (worktree)
8670 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8671 01073a5d 2019-08-22 stsp if (repo) {
8672 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8673 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8674 01073a5d 2019-08-22 stsp if (error == NULL)
8675 01073a5d 2019-08-22 stsp error = repo_error;
8676 01073a5d 2019-08-22 stsp }
8677 0ebf8283 2019-07-24 stsp return error;
8678 0ebf8283 2019-07-24 stsp }