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 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1418 88609724 2020-03-21 stsp if (err)
1419 88609724 2020-03-21 stsp goto done;
1420 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1421 88609724 2020-03-21 stsp goto done;
1422 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1423 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1424 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1425 db6d8ad8 2020-03-21 stsp }
1426 db6d8ad8 2020-03-21 stsp goto done;
1427 db6d8ad8 2020-03-21 stsp }
1428 db6d8ad8 2020-03-21 stsp
1429 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1430 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1431 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1432 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1433 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1434 688f11b3 2020-03-21 stsp }
1435 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1436 7848a0e1 2020-03-19 stsp if (err)
1437 7848a0e1 2020-03-19 stsp goto done;
1438 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1439 e8a967e0 2020-03-21 stsp if (err)
1440 e8a967e0 2020-03-21 stsp goto done;
1441 7848a0e1 2020-03-19 stsp } else {
1442 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1443 7848a0e1 2020-03-19 stsp if (err)
1444 7848a0e1 2020-03-19 stsp goto done;
1445 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1446 6338a6a1 2020-03-21 stsp goto done;
1447 6338a6a1 2020-03-21 stsp
1448 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1449 6338a6a1 2020-03-21 stsp if (err)
1450 6338a6a1 2020-03-21 stsp goto done;
1451 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1452 6338a6a1 2020-03-21 stsp if (err)
1453 6338a6a1 2020-03-21 stsp goto done;
1454 7848a0e1 2020-03-19 stsp }
1455 6338a6a1 2020-03-21 stsp
1456 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1457 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1458 6338a6a1 2020-03-21 stsp new_id_str);
1459 7848a0e1 2020-03-19 stsp done:
1460 7848a0e1 2020-03-19 stsp free(old_id);
1461 7848a0e1 2020-03-19 stsp free(new_id_str);
1462 7848a0e1 2020-03-19 stsp return err;
1463 2ab43947 2020-03-18 stsp }
1464 2ab43947 2020-03-18 stsp
1465 2ab43947 2020-03-18 stsp __dead static void
1466 7848a0e1 2020-03-19 stsp usage_fetch(void)
1467 7848a0e1 2020-03-19 stsp {
1468 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1469 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1470 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1471 13f12b09 2020-03-21 stsp getprogname());
1472 7848a0e1 2020-03-19 stsp exit(1);
1473 7848a0e1 2020-03-19 stsp }
1474 7848a0e1 2020-03-19 stsp
1475 7848a0e1 2020-03-19 stsp static const struct got_error *
1476 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1477 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1478 f21ec2f0 2020-03-21 stsp {
1479 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1480 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1481 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1482 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1483 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1484 f21ec2f0 2020-03-21 stsp char *id_str;
1485 f21ec2f0 2020-03-21 stsp
1486 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1487 f21ec2f0 2020-03-21 stsp
1488 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1489 f21ec2f0 2020-03-21 stsp if (err)
1490 f21ec2f0 2020-03-21 stsp return err;
1491 f21ec2f0 2020-03-21 stsp
1492 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1493 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1494 f21ec2f0 2020-03-21 stsp
1495 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1496 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1497 f21ec2f0 2020-03-21 stsp continue;
1498 f21ec2f0 2020-03-21 stsp
1499 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1500 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1501 f21ec2f0 2020-03-21 stsp break;
1502 f21ec2f0 2020-03-21 stsp }
1503 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1504 f21ec2f0 2020-03-21 stsp continue;
1505 f21ec2f0 2020-03-21 stsp
1506 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1507 f21ec2f0 2020-03-21 stsp if (err)
1508 f21ec2f0 2020-03-21 stsp break;
1509 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1510 f21ec2f0 2020-03-21 stsp free(id);
1511 f21ec2f0 2020-03-21 stsp if (err)
1512 f21ec2f0 2020-03-21 stsp break;
1513 f21ec2f0 2020-03-21 stsp
1514 f21ec2f0 2020-03-21 stsp free(id_str);
1515 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1516 f21ec2f0 2020-03-21 stsp if (err)
1517 f21ec2f0 2020-03-21 stsp break;
1518 6338a6a1 2020-03-21 stsp if (verbosity >= 0) {
1519 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1520 6338a6a1 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1521 6338a6a1 2020-03-21 stsp }
1522 f21ec2f0 2020-03-21 stsp }
1523 0e4002ca 2020-03-21 stsp
1524 0e4002ca 2020-03-21 stsp return err;
1525 0e4002ca 2020-03-21 stsp }
1526 0e4002ca 2020-03-21 stsp
1527 0e4002ca 2020-03-21 stsp static const struct got_error *
1528 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1529 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1530 0e4002ca 2020-03-21 stsp {
1531 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1532 0e4002ca 2020-03-21 stsp char *remote_refname;
1533 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1534 0e4002ca 2020-03-21 stsp
1535 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1536 0e4002ca 2020-03-21 stsp refname += 5;
1537 0e4002ca 2020-03-21 stsp
1538 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1539 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1540 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1541 f21ec2f0 2020-03-21 stsp
1542 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1543 0e4002ca 2020-03-21 stsp if (err) {
1544 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1545 0e4002ca 2020-03-21 stsp goto done;
1546 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1547 0e4002ca 2020-03-21 stsp } else {
1548 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1549 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1550 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1551 9f142382 2020-03-21 stsp err = unlock_err;
1552 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1553 0e4002ca 2020-03-21 stsp }
1554 0e4002ca 2020-03-21 stsp done:
1555 0e4002ca 2020-03-21 stsp free(remote_refname);
1556 f21ec2f0 2020-03-21 stsp return err;
1557 f21ec2f0 2020-03-21 stsp }
1558 f21ec2f0 2020-03-21 stsp
1559 f21ec2f0 2020-03-21 stsp static const struct got_error *
1560 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1561 7848a0e1 2020-03-19 stsp {
1562 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1563 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1564 7848a0e1 2020-03-19 stsp const char *remote_name;
1565 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1566 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1567 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1568 7848a0e1 2020-03-19 stsp int nremotes;
1569 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1570 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1571 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1572 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1573 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1574 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1575 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1576 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1577 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1578 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1579 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1580 7848a0e1 2020-03-19 stsp
1581 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1582 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1583 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1584 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1585 7848a0e1 2020-03-19 stsp
1586 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1587 7848a0e1 2020-03-19 stsp switch (ch) {
1588 659e7fbd 2020-03-20 stsp case 'a':
1589 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1590 4ba14133 2020-03-20 stsp break;
1591 4ba14133 2020-03-20 stsp case 'b':
1592 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1593 4ba14133 2020-03-20 stsp optarg, NULL);
1594 4ba14133 2020-03-20 stsp if (error)
1595 4ba14133 2020-03-20 stsp return error;
1596 41b0de12 2020-03-21 stsp break;
1597 f21ec2f0 2020-03-21 stsp case 'd':
1598 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1599 f21ec2f0 2020-03-21 stsp break;
1600 41b0de12 2020-03-21 stsp case 'l':
1601 41b0de12 2020-03-21 stsp list_refs_only = 1;
1602 659e7fbd 2020-03-20 stsp break;
1603 7848a0e1 2020-03-19 stsp case 'r':
1604 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1605 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1606 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1607 7848a0e1 2020-03-19 stsp optarg);
1608 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1609 7848a0e1 2020-03-19 stsp break;
1610 db6d8ad8 2020-03-21 stsp case 't':
1611 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1612 db6d8ad8 2020-03-21 stsp break;
1613 7848a0e1 2020-03-19 stsp case 'v':
1614 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1615 7848a0e1 2020-03-19 stsp verbosity = 0;
1616 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1617 7848a0e1 2020-03-19 stsp verbosity++;
1618 7848a0e1 2020-03-19 stsp break;
1619 7848a0e1 2020-03-19 stsp case 'q':
1620 7848a0e1 2020-03-19 stsp verbosity = -1;
1621 7848a0e1 2020-03-19 stsp break;
1622 0e4002ca 2020-03-21 stsp case 'R':
1623 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1624 0e4002ca 2020-03-21 stsp optarg, NULL);
1625 0e4002ca 2020-03-21 stsp if (error)
1626 0e4002ca 2020-03-21 stsp return error;
1627 0e4002ca 2020-03-21 stsp break;
1628 7848a0e1 2020-03-19 stsp default:
1629 7848a0e1 2020-03-19 stsp usage_fetch();
1630 7848a0e1 2020-03-19 stsp break;
1631 7848a0e1 2020-03-19 stsp }
1632 7848a0e1 2020-03-19 stsp }
1633 7848a0e1 2020-03-19 stsp argc -= optind;
1634 7848a0e1 2020-03-19 stsp argv += optind;
1635 7848a0e1 2020-03-19 stsp
1636 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1637 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1638 41b0de12 2020-03-21 stsp if (list_refs_only) {
1639 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1640 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1641 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1642 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1643 f21ec2f0 2020-03-21 stsp if (delete_refs)
1644 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1645 41b0de12 2020-03-21 stsp if (verbosity == -1)
1646 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1647 41b0de12 2020-03-21 stsp }
1648 4ba14133 2020-03-20 stsp
1649 7848a0e1 2020-03-19 stsp if (argc == 0)
1650 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1651 7848a0e1 2020-03-19 stsp else if (argc == 1)
1652 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1653 7848a0e1 2020-03-19 stsp else
1654 7848a0e1 2020-03-19 stsp usage_fetch();
1655 7848a0e1 2020-03-19 stsp
1656 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1657 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1658 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1659 7848a0e1 2020-03-19 stsp goto done;
1660 7848a0e1 2020-03-19 stsp }
1661 7848a0e1 2020-03-19 stsp
1662 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1663 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1664 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1665 7848a0e1 2020-03-19 stsp goto done;
1666 7848a0e1 2020-03-19 stsp else
1667 7848a0e1 2020-03-19 stsp error = NULL;
1668 7848a0e1 2020-03-19 stsp if (worktree) {
1669 7848a0e1 2020-03-19 stsp repo_path =
1670 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1671 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1672 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1673 7848a0e1 2020-03-19 stsp if (error)
1674 7848a0e1 2020-03-19 stsp goto done;
1675 7848a0e1 2020-03-19 stsp } else {
1676 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1677 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1678 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1679 7848a0e1 2020-03-19 stsp goto done;
1680 7848a0e1 2020-03-19 stsp }
1681 7848a0e1 2020-03-19 stsp }
1682 7848a0e1 2020-03-19 stsp }
1683 7848a0e1 2020-03-19 stsp
1684 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1685 7848a0e1 2020-03-19 stsp if (error)
1686 7848a0e1 2020-03-19 stsp goto done;
1687 7848a0e1 2020-03-19 stsp
1688 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1689 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1690 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1691 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1692 7848a0e1 2020-03-19 stsp break;
1693 7848a0e1 2020-03-19 stsp }
1694 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1695 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1696 7848a0e1 2020-03-19 stsp goto done;
1697 7848a0e1 2020-03-19 stsp }
1698 7848a0e1 2020-03-19 stsp
1699 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1700 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1701 7848a0e1 2020-03-19 stsp if (error)
1702 7848a0e1 2020-03-19 stsp goto done;
1703 7848a0e1 2020-03-19 stsp
1704 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1705 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1706 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1707 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1708 7848a0e1 2020-03-19 stsp err(1, "pledge");
1709 7848a0e1 2020-03-19 stsp #endif
1710 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1711 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1712 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1713 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1714 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1715 7848a0e1 2020-03-19 stsp err(1, "pledge");
1716 7848a0e1 2020-03-19 stsp #endif
1717 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1718 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1719 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1720 7848a0e1 2020-03-19 stsp goto done;
1721 7848a0e1 2020-03-19 stsp } else {
1722 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1723 7848a0e1 2020-03-19 stsp goto done;
1724 7848a0e1 2020-03-19 stsp }
1725 7848a0e1 2020-03-19 stsp
1726 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1727 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1728 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1729 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1730 7848a0e1 2020-03-19 stsp goto done;
1731 7848a0e1 2020-03-19 stsp }
1732 7848a0e1 2020-03-19 stsp }
1733 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1734 7848a0e1 2020-03-19 stsp if (error)
1735 7848a0e1 2020-03-19 stsp goto done;
1736 7848a0e1 2020-03-19 stsp
1737 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1738 9c52365f 2020-03-21 stsp server_path, verbosity);
1739 7848a0e1 2020-03-19 stsp if (error)
1740 7848a0e1 2020-03-19 stsp goto done;
1741 7848a0e1 2020-03-19 stsp
1742 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1743 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1744 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1745 7848a0e1 2020-03-19 stsp
1746 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1747 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1748 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1749 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1750 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1751 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1752 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1753 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1754 7848a0e1 2020-03-19 stsp if (error)
1755 7848a0e1 2020-03-19 stsp goto done;
1756 7848a0e1 2020-03-19 stsp
1757 41b0de12 2020-03-21 stsp if (list_refs_only) {
1758 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1759 41b0de12 2020-03-21 stsp goto done;
1760 41b0de12 2020-03-21 stsp }
1761 41b0de12 2020-03-21 stsp
1762 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1763 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1764 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1765 f21ec2f0 2020-03-21 stsp if (delete_refs)
1766 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1767 7848a0e1 2020-03-19 stsp goto done;
1768 7848a0e1 2020-03-19 stsp }
1769 7848a0e1 2020-03-19 stsp
1770 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1771 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1772 984065c8 2020-03-19 stsp if (error)
1773 984065c8 2020-03-19 stsp goto done;
1774 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1775 984065c8 2020-03-19 stsp free(id_str);
1776 984065c8 2020-03-19 stsp id_str = NULL;
1777 984065c8 2020-03-19 stsp }
1778 7848a0e1 2020-03-19 stsp
1779 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1780 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1781 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1782 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1783 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1784 7848a0e1 2020-03-19 stsp char *remote_refname;
1785 7848a0e1 2020-03-19 stsp
1786 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1787 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1788 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1789 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1790 0e4002ca 2020-03-21 stsp if (error)
1791 0e4002ca 2020-03-21 stsp goto done;
1792 0e4002ca 2020-03-21 stsp continue;
1793 0e4002ca 2020-03-21 stsp }
1794 0e4002ca 2020-03-21 stsp
1795 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1796 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1797 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1798 7848a0e1 2020-03-19 stsp if (error) {
1799 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1800 7848a0e1 2020-03-19 stsp goto done;
1801 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1802 6338a6a1 2020-03-21 stsp repo);
1803 7848a0e1 2020-03-19 stsp if (error)
1804 7848a0e1 2020-03-19 stsp goto done;
1805 7848a0e1 2020-03-19 stsp } else {
1806 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1807 db6d8ad8 2020-03-21 stsp verbosity, repo);
1808 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1809 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1810 9f142382 2020-03-21 stsp error = unlock_err;
1811 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1812 7848a0e1 2020-03-19 stsp if (error)
1813 7848a0e1 2020-03-19 stsp goto done;
1814 7848a0e1 2020-03-19 stsp }
1815 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1816 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1817 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1818 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1819 7848a0e1 2020-03-19 stsp goto done;
1820 7848a0e1 2020-03-19 stsp }
1821 7848a0e1 2020-03-19 stsp
1822 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1823 7848a0e1 2020-03-19 stsp if (error) {
1824 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1825 7848a0e1 2020-03-19 stsp goto done;
1826 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1827 688f11b3 2020-03-21 stsp verbosity, repo);
1828 7848a0e1 2020-03-19 stsp if (error)
1829 7848a0e1 2020-03-19 stsp goto done;
1830 7848a0e1 2020-03-19 stsp } else {
1831 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1832 db6d8ad8 2020-03-21 stsp verbosity, repo);
1833 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1834 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1835 9f142382 2020-03-21 stsp error = unlock_err;
1836 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1837 7848a0e1 2020-03-19 stsp if (error)
1838 7848a0e1 2020-03-19 stsp goto done;
1839 7848a0e1 2020-03-19 stsp }
1840 2ec30c80 2020-03-20 stsp
1841 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1842 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1843 2ec30c80 2020-03-20 stsp if (error) {
1844 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1845 2ec30c80 2020-03-20 stsp goto done;
1846 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1847 6338a6a1 2020-03-21 stsp repo);
1848 2ec30c80 2020-03-20 stsp if (error)
1849 2ec30c80 2020-03-20 stsp goto done;
1850 9f142382 2020-03-21 stsp } else {
1851 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1852 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1853 9f142382 2020-03-21 stsp error = unlock_err;
1854 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1855 9f142382 2020-03-21 stsp }
1856 7848a0e1 2020-03-19 stsp }
1857 7848a0e1 2020-03-19 stsp }
1858 f21ec2f0 2020-03-21 stsp if (delete_refs)
1859 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1860 7848a0e1 2020-03-19 stsp done:
1861 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1862 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1863 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1864 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1865 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1866 9c52365f 2020-03-21 stsp }
1867 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1868 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1869 7848a0e1 2020-03-19 stsp if (repo)
1870 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1871 7848a0e1 2020-03-19 stsp if (worktree)
1872 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1873 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, 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(&refs);
1878 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1879 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1880 7848a0e1 2020-03-19 stsp free(pe->data);
1881 7848a0e1 2020-03-19 stsp }
1882 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1883 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1884 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1885 7848a0e1 2020-03-19 stsp free(id_str);
1886 7848a0e1 2020-03-19 stsp free(cwd);
1887 7848a0e1 2020-03-19 stsp free(repo_path);
1888 7848a0e1 2020-03-19 stsp free(pack_hash);
1889 7848a0e1 2020-03-19 stsp free(proto);
1890 7848a0e1 2020-03-19 stsp free(host);
1891 7848a0e1 2020-03-19 stsp free(port);
1892 7848a0e1 2020-03-19 stsp free(server_path);
1893 7848a0e1 2020-03-19 stsp free(repo_name);
1894 7848a0e1 2020-03-19 stsp return error;
1895 7848a0e1 2020-03-19 stsp }
1896 7848a0e1 2020-03-19 stsp
1897 7848a0e1 2020-03-19 stsp
1898 7848a0e1 2020-03-19 stsp __dead static void
1899 2ab43947 2020-03-18 stsp usage_checkout(void)
1900 2ab43947 2020-03-18 stsp {
1901 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1902 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1903 2ab43947 2020-03-18 stsp exit(1);
1904 2ab43947 2020-03-18 stsp }
1905 2ab43947 2020-03-18 stsp
1906 2ab43947 2020-03-18 stsp static void
1907 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1908 2ab43947 2020-03-18 stsp {
1909 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1910 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1911 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1912 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1913 2ab43947 2020-03-18 stsp getprogname());
1914 2ab43947 2020-03-18 stsp }
1915 2ab43947 2020-03-18 stsp
1916 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1917 2ab43947 2020-03-18 stsp const char *worktree_path;
1918 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1919 2ab43947 2020-03-18 stsp };
1920 2ab43947 2020-03-18 stsp
1921 2ab43947 2020-03-18 stsp static const struct got_error *
1922 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1923 2ab43947 2020-03-18 stsp {
1924 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1925 2ab43947 2020-03-18 stsp
1926 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1927 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1928 2ab43947 2020-03-18 stsp return NULL;
1929 2ab43947 2020-03-18 stsp
1930 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1931 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1932 2ab43947 2020-03-18 stsp return NULL;
1933 2ab43947 2020-03-18 stsp }
1934 2ab43947 2020-03-18 stsp
1935 2ab43947 2020-03-18 stsp while (path[0] == '/')
1936 2ab43947 2020-03-18 stsp path++;
1937 2ab43947 2020-03-18 stsp
1938 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1939 2ab43947 2020-03-18 stsp return NULL;
1940 2ab43947 2020-03-18 stsp }
1941 2ab43947 2020-03-18 stsp
1942 2ab43947 2020-03-18 stsp static const struct got_error *
1943 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1944 2ab43947 2020-03-18 stsp {
1945 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1946 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1947 2ab43947 2020-03-18 stsp return NULL;
1948 2ab43947 2020-03-18 stsp }
1949 2ab43947 2020-03-18 stsp
1950 2ab43947 2020-03-18 stsp static const struct got_error *
1951 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1952 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1953 2ab43947 2020-03-18 stsp struct got_repository *repo)
1954 2ab43947 2020-03-18 stsp {
1955 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1956 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1957 2ab43947 2020-03-18 stsp
1958 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1959 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1960 2ab43947 2020-03-18 stsp if (err)
1961 2ab43947 2020-03-18 stsp return err;
1962 2ab43947 2020-03-18 stsp
1963 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1964 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1965 2ab43947 2020-03-18 stsp
1966 2ab43947 2020-03-18 stsp /*
1967 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1968 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1969 2ab43947 2020-03-18 stsp *
1970 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1971 2ab43947 2020-03-18 stsp *
1972 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1973 2ab43947 2020-03-18 stsp * \ /
1974 2ab43947 2020-03-18 stsp * C E
1975 2ab43947 2020-03-18 stsp * \ /
1976 2ab43947 2020-03-18 stsp * B (yca)
1977 2ab43947 2020-03-18 stsp * |
1978 2ab43947 2020-03-18 stsp * A
1979 2ab43947 2020-03-18 stsp *
1980 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1981 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1982 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1983 2ab43947 2020-03-18 stsp */
1984 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1985 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1986 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1987 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1988 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1989 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1990 2ab43947 2020-03-18 stsp
1991 2ab43947 2020-03-18 stsp free(yca_id);
1992 2ab43947 2020-03-18 stsp return NULL;
1993 2ab43947 2020-03-18 stsp }
1994 2ab43947 2020-03-18 stsp
1995 2ab43947 2020-03-18 stsp static const struct got_error *
1996 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1997 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1998 2ab43947 2020-03-18 stsp struct got_repository *repo)
1999 2ab43947 2020-03-18 stsp {
2000 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2001 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2002 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2003 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2004 2ab43947 2020-03-18 stsp
2005 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2006 2ab43947 2020-03-18 stsp if (err)
2007 2ab43947 2020-03-18 stsp goto done;
2008 2ab43947 2020-03-18 stsp
2009 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2010 2ab43947 2020-03-18 stsp is_same_branch = 1;
2011 2ab43947 2020-03-18 stsp goto done;
2012 2ab43947 2020-03-18 stsp }
2013 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2014 2ab43947 2020-03-18 stsp is_same_branch = 1;
2015 2ab43947 2020-03-18 stsp goto done;
2016 2ab43947 2020-03-18 stsp }
2017 2ab43947 2020-03-18 stsp
2018 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
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 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2023 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2024 2ab43947 2020-03-18 stsp if (err)
2025 2ab43947 2020-03-18 stsp goto done;
2026 2ab43947 2020-03-18 stsp
2027 2ab43947 2020-03-18 stsp for (;;) {
2028 2ab43947 2020-03-18 stsp struct got_object_id *id;
2029 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2030 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2031 2ab43947 2020-03-18 stsp if (err) {
2032 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2033 2ab43947 2020-03-18 stsp err = NULL;
2034 2ab43947 2020-03-18 stsp break;
2035 2ab43947 2020-03-18 stsp }
2036 2ab43947 2020-03-18 stsp
2037 2ab43947 2020-03-18 stsp if (id) {
2038 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2039 2ab43947 2020-03-18 stsp break;
2040 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2041 2ab43947 2020-03-18 stsp is_same_branch = 1;
2042 2ab43947 2020-03-18 stsp break;
2043 2ab43947 2020-03-18 stsp }
2044 2ab43947 2020-03-18 stsp }
2045 2ab43947 2020-03-18 stsp }
2046 2ab43947 2020-03-18 stsp done:
2047 2ab43947 2020-03-18 stsp if (graph)
2048 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2049 2ab43947 2020-03-18 stsp free(head_commit_id);
2050 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2051 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2052 2ab43947 2020-03-18 stsp return err;
2053 a367ff0f 2019-05-14 stsp }
2054 8069f636 2019-01-12 stsp
2055 4ed7e80c 2018-05-20 stsp static const struct got_error *
2056 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2057 4b6c9460 2020-03-05 stsp {
2058 4b6c9460 2020-03-05 stsp static char msg[512];
2059 4b6c9460 2020-03-05 stsp const char *branch_name;
2060 4b6c9460 2020-03-05 stsp
2061 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2062 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2063 4b6c9460 2020-03-05 stsp else
2064 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2065 4b6c9460 2020-03-05 stsp
2066 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2067 4b6c9460 2020-03-05 stsp branch_name += 11;
2068 4b6c9460 2020-03-05 stsp
2069 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2070 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2071 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2072 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2073 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2074 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2075 4b6c9460 2020-03-05 stsp
2076 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2077 4b6c9460 2020-03-05 stsp }
2078 4b6c9460 2020-03-05 stsp
2079 4b6c9460 2020-03-05 stsp static const struct got_error *
2080 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2081 c09a553d 2018-03-12 stsp {
2082 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2083 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2084 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2085 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2086 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2087 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2088 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2089 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2090 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2091 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2092 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2093 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2094 f2ea84fa 2019-07-27 stsp
2095 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2096 c09a553d 2018-03-12 stsp
2097 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2098 0bb8a95e 2018-03-12 stsp switch (ch) {
2099 08573d5b 2019-05-14 stsp case 'b':
2100 08573d5b 2019-05-14 stsp branch_name = optarg;
2101 08573d5b 2019-05-14 stsp break;
2102 8069f636 2019-01-12 stsp case 'c':
2103 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2104 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2105 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2106 bb51a5b4 2020-01-13 stsp break;
2107 bb51a5b4 2020-01-13 stsp case 'E':
2108 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2109 8069f636 2019-01-12 stsp break;
2110 0bb8a95e 2018-03-12 stsp case 'p':
2111 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2112 0bb8a95e 2018-03-12 stsp break;
2113 0bb8a95e 2018-03-12 stsp default:
2114 2deda0b9 2019-03-07 stsp usage_checkout();
2115 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2116 0bb8a95e 2018-03-12 stsp }
2117 0bb8a95e 2018-03-12 stsp }
2118 0bb8a95e 2018-03-12 stsp
2119 0bb8a95e 2018-03-12 stsp argc -= optind;
2120 0bb8a95e 2018-03-12 stsp argv += optind;
2121 0bb8a95e 2018-03-12 stsp
2122 6715a751 2018-03-16 stsp #ifndef PROFILE
2123 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2124 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2125 c09a553d 2018-03-12 stsp err(1, "pledge");
2126 6715a751 2018-03-16 stsp #endif
2127 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2128 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2129 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2130 76089277 2018-04-01 stsp if (repo_path == NULL)
2131 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2132 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2133 76089277 2018-04-01 stsp if (cwd == NULL) {
2134 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2135 76089277 2018-04-01 stsp goto done;
2136 76089277 2018-04-01 stsp }
2137 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2138 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2139 230a42bd 2019-05-11 jcs if (base == NULL) {
2140 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2141 230a42bd 2019-05-11 jcs path_prefix);
2142 230a42bd 2019-05-11 jcs goto done;
2143 230a42bd 2019-05-11 jcs }
2144 230a42bd 2019-05-11 jcs } else {
2145 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2146 230a42bd 2019-05-11 jcs if (base == NULL) {
2147 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2148 230a42bd 2019-05-11 jcs repo_path);
2149 230a42bd 2019-05-11 jcs goto done;
2150 230a42bd 2019-05-11 jcs }
2151 76089277 2018-04-01 stsp }
2152 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2153 c09a553d 2018-03-12 stsp if (dotgit)
2154 c09a553d 2018-03-12 stsp *dotgit = '\0';
2155 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2156 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2157 c09a553d 2018-03-12 stsp free(cwd);
2158 76089277 2018-04-01 stsp goto done;
2159 c09a553d 2018-03-12 stsp }
2160 c09a553d 2018-03-12 stsp free(cwd);
2161 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2162 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2163 76089277 2018-04-01 stsp if (repo_path == NULL) {
2164 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2165 76089277 2018-04-01 stsp goto done;
2166 76089277 2018-04-01 stsp }
2167 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2168 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2169 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2170 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2171 b4b3a7dd 2019-07-22 stsp argv[1]);
2172 b4b3a7dd 2019-07-22 stsp goto done;
2173 b4b3a7dd 2019-07-22 stsp }
2174 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2175 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2176 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2177 b4b3a7dd 2019-07-22 stsp goto done;
2178 b4b3a7dd 2019-07-22 stsp }
2179 76089277 2018-04-01 stsp }
2180 c09a553d 2018-03-12 stsp } else
2181 c09a553d 2018-03-12 stsp usage_checkout();
2182 c09a553d 2018-03-12 stsp
2183 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2184 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2185 13bfb272 2019-05-10 jcs
2186 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2187 c09a553d 2018-03-12 stsp if (error != NULL)
2188 c02c541e 2019-03-29 stsp goto done;
2189 c02c541e 2019-03-29 stsp
2190 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2191 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2192 c530dc23 2019-07-23 stsp if (error) {
2193 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2194 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2195 c530dc23 2019-07-23 stsp goto done;
2196 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2197 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2198 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2199 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2200 c530dc23 2019-07-23 stsp goto done;
2201 c530dc23 2019-07-23 stsp }
2202 c530dc23 2019-07-23 stsp }
2203 c530dc23 2019-07-23 stsp
2204 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2205 c02c541e 2019-03-29 stsp if (error)
2206 c09a553d 2018-03-12 stsp goto done;
2207 8069f636 2019-01-12 stsp
2208 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2209 c09a553d 2018-03-12 stsp if (error != NULL)
2210 c09a553d 2018-03-12 stsp goto done;
2211 c09a553d 2018-03-12 stsp
2212 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2213 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2214 c09a553d 2018-03-12 stsp goto done;
2215 c09a553d 2018-03-12 stsp
2216 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2217 c09a553d 2018-03-12 stsp if (error != NULL)
2218 c09a553d 2018-03-12 stsp goto done;
2219 c09a553d 2018-03-12 stsp
2220 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2221 e5dc7198 2018-12-29 stsp path_prefix);
2222 e5dc7198 2018-12-29 stsp if (error != NULL)
2223 e5dc7198 2018-12-29 stsp goto done;
2224 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2225 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2226 49520a32 2018-12-29 stsp goto done;
2227 49520a32 2018-12-29 stsp }
2228 49520a32 2018-12-29 stsp
2229 8069f636 2019-01-12 stsp if (commit_id_str) {
2230 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2231 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2232 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2233 30837e32 2019-07-25 stsp if (error)
2234 8069f636 2019-01-12 stsp goto done;
2235 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2236 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2237 8069f636 2019-01-12 stsp if (error != NULL) {
2238 8069f636 2019-01-12 stsp free(commit_id);
2239 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2240 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2241 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2242 4b6c9460 2020-03-05 stsp }
2243 8069f636 2019-01-12 stsp goto done;
2244 8069f636 2019-01-12 stsp }
2245 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2246 4b6c9460 2020-03-05 stsp if (error) {
2247 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2248 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2249 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2250 4b6c9460 2020-03-05 stsp }
2251 45d344f6 2019-05-14 stsp goto done;
2252 4b6c9460 2020-03-05 stsp }
2253 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2254 8069f636 2019-01-12 stsp commit_id);
2255 8069f636 2019-01-12 stsp free(commit_id);
2256 8069f636 2019-01-12 stsp if (error)
2257 8069f636 2019-01-12 stsp goto done;
2258 8069f636 2019-01-12 stsp }
2259 8069f636 2019-01-12 stsp
2260 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2261 f2ea84fa 2019-07-27 stsp if (error)
2262 f2ea84fa 2019-07-27 stsp goto done;
2263 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2264 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2265 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2266 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2267 c09a553d 2018-03-12 stsp if (error != NULL)
2268 c09a553d 2018-03-12 stsp goto done;
2269 c09a553d 2018-03-12 stsp
2270 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2271 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2272 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2273 c09a553d 2018-03-12 stsp done:
2274 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2275 8069f636 2019-01-12 stsp free(commit_id_str);
2276 76089277 2018-04-01 stsp free(repo_path);
2277 507dc3bb 2018-12-29 stsp free(worktree_path);
2278 507dc3bb 2018-12-29 stsp return error;
2279 507dc3bb 2018-12-29 stsp }
2280 507dc3bb 2018-12-29 stsp
2281 507dc3bb 2018-12-29 stsp __dead static void
2282 507dc3bb 2018-12-29 stsp usage_update(void)
2283 507dc3bb 2018-12-29 stsp {
2284 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2285 507dc3bb 2018-12-29 stsp getprogname());
2286 507dc3bb 2018-12-29 stsp exit(1);
2287 507dc3bb 2018-12-29 stsp }
2288 507dc3bb 2018-12-29 stsp
2289 1ee397ad 2019-07-12 stsp static const struct got_error *
2290 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2291 507dc3bb 2018-12-29 stsp {
2292 784955db 2019-01-12 stsp int *did_something = arg;
2293 784955db 2019-01-12 stsp
2294 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2295 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2296 1ee397ad 2019-07-12 stsp return NULL;
2297 507dc3bb 2018-12-29 stsp
2298 1545c615 2019-02-10 stsp *did_something = 1;
2299 a484d721 2019-06-10 stsp
2300 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2301 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2302 1ee397ad 2019-07-12 stsp return NULL;
2303 a484d721 2019-06-10 stsp
2304 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2305 507dc3bb 2018-12-29 stsp path++;
2306 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2307 1ee397ad 2019-07-12 stsp return NULL;
2308 be7061eb 2018-12-30 stsp }
2309 be7061eb 2018-12-30 stsp
2310 be7061eb 2018-12-30 stsp static const struct got_error *
2311 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2312 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2313 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2314 a1fb16d8 2019-05-24 stsp {
2315 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2316 a1fb16d8 2019-05-24 stsp char *base_id_str;
2317 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2318 a1fb16d8 2019-05-24 stsp
2319 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2320 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2321 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2322 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2323 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2324 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2325 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2326 a1fb16d8 2019-05-24 stsp }
2327 a1fb16d8 2019-05-24 stsp
2328 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2329 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2330 a1fb16d8 2019-05-24 stsp if (err) {
2331 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2332 a1fb16d8 2019-05-24 stsp return err;
2333 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2334 a1fb16d8 2019-05-24 stsp }
2335 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2336 a1fb16d8 2019-05-24 stsp return NULL;
2337 a1fb16d8 2019-05-24 stsp
2338 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2339 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2340 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2341 a1fb16d8 2019-05-24 stsp if (err)
2342 a1fb16d8 2019-05-24 stsp return err;
2343 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2344 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2345 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2346 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2347 0ebf8283 2019-07-24 stsp return NULL;
2348 0ebf8283 2019-07-24 stsp }
2349 0ebf8283 2019-07-24 stsp
2350 0ebf8283 2019-07-24 stsp static const struct got_error *
2351 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2352 0ebf8283 2019-07-24 stsp {
2353 0ebf8283 2019-07-24 stsp const struct got_error *err;
2354 0ebf8283 2019-07-24 stsp int in_progress;
2355 0ebf8283 2019-07-24 stsp
2356 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2357 0ebf8283 2019-07-24 stsp if (err)
2358 0ebf8283 2019-07-24 stsp return err;
2359 0ebf8283 2019-07-24 stsp if (in_progress)
2360 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2361 0ebf8283 2019-07-24 stsp
2362 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2363 0ebf8283 2019-07-24 stsp if (err)
2364 0ebf8283 2019-07-24 stsp return err;
2365 0ebf8283 2019-07-24 stsp if (in_progress)
2366 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2367 0ebf8283 2019-07-24 stsp
2368 a1fb16d8 2019-05-24 stsp return NULL;
2369 a5edda0a 2019-07-27 stsp }
2370 a5edda0a 2019-07-27 stsp
2371 a5edda0a 2019-07-27 stsp static const struct got_error *
2372 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2373 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2374 a5edda0a 2019-07-27 stsp {
2375 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2376 a5edda0a 2019-07-27 stsp char *path;
2377 a5edda0a 2019-07-27 stsp int i;
2378 a5edda0a 2019-07-27 stsp
2379 a5edda0a 2019-07-27 stsp if (argc == 0) {
2380 a5edda0a 2019-07-27 stsp path = strdup("");
2381 a5edda0a 2019-07-27 stsp if (path == NULL)
2382 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2383 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2384 a5edda0a 2019-07-27 stsp }
2385 a5edda0a 2019-07-27 stsp
2386 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2387 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2388 a5edda0a 2019-07-27 stsp if (err)
2389 a5edda0a 2019-07-27 stsp break;
2390 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2391 a5edda0a 2019-07-27 stsp if (err) {
2392 a5edda0a 2019-07-27 stsp free(path);
2393 a5edda0a 2019-07-27 stsp break;
2394 a5edda0a 2019-07-27 stsp }
2395 a5edda0a 2019-07-27 stsp }
2396 a5edda0a 2019-07-27 stsp
2397 a5edda0a 2019-07-27 stsp return err;
2398 a1fb16d8 2019-05-24 stsp }
2399 a1fb16d8 2019-05-24 stsp
2400 a1fb16d8 2019-05-24 stsp static const struct got_error *
2401 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2402 507dc3bb 2018-12-29 stsp {
2403 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2404 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2405 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2406 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2407 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2408 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2409 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2410 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2411 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2412 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2413 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2414 507dc3bb 2018-12-29 stsp
2415 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2416 f2ea84fa 2019-07-27 stsp
2417 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2418 507dc3bb 2018-12-29 stsp switch (ch) {
2419 024e9686 2019-05-14 stsp case 'b':
2420 024e9686 2019-05-14 stsp branch_name = optarg;
2421 024e9686 2019-05-14 stsp break;
2422 507dc3bb 2018-12-29 stsp case 'c':
2423 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2424 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2425 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2426 507dc3bb 2018-12-29 stsp break;
2427 507dc3bb 2018-12-29 stsp default:
2428 2deda0b9 2019-03-07 stsp usage_update();
2429 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2430 507dc3bb 2018-12-29 stsp }
2431 507dc3bb 2018-12-29 stsp }
2432 507dc3bb 2018-12-29 stsp
2433 507dc3bb 2018-12-29 stsp argc -= optind;
2434 507dc3bb 2018-12-29 stsp argv += optind;
2435 507dc3bb 2018-12-29 stsp
2436 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2437 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2438 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2439 507dc3bb 2018-12-29 stsp err(1, "pledge");
2440 507dc3bb 2018-12-29 stsp #endif
2441 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2442 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2443 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2444 c4cdcb68 2019-04-03 stsp goto done;
2445 c4cdcb68 2019-04-03 stsp }
2446 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2447 7d5807f4 2019-07-11 stsp if (error)
2448 7d5807f4 2019-07-11 stsp goto done;
2449 7d5807f4 2019-07-11 stsp
2450 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2451 f2ea84fa 2019-07-27 stsp if (error)
2452 f2ea84fa 2019-07-27 stsp goto done;
2453 507dc3bb 2018-12-29 stsp
2454 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2455 c9956ddf 2019-09-08 stsp NULL);
2456 507dc3bb 2018-12-29 stsp if (error != NULL)
2457 507dc3bb 2018-12-29 stsp goto done;
2458 507dc3bb 2018-12-29 stsp
2459 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2460 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2461 087fb88c 2019-08-04 stsp if (error)
2462 087fb88c 2019-08-04 stsp goto done;
2463 087fb88c 2019-08-04 stsp
2464 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2465 0266afb7 2019-01-04 stsp if (error)
2466 0266afb7 2019-01-04 stsp goto done;
2467 0266afb7 2019-01-04 stsp
2468 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2469 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2470 024e9686 2019-05-14 stsp if (error != NULL)
2471 024e9686 2019-05-14 stsp goto done;
2472 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2473 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2474 9c4b8182 2019-01-02 stsp if (error != NULL)
2475 9c4b8182 2019-01-02 stsp goto done;
2476 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2477 507dc3bb 2018-12-29 stsp if (error != NULL)
2478 507dc3bb 2018-12-29 stsp goto done;
2479 507dc3bb 2018-12-29 stsp } else {
2480 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2481 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2482 04f57cb3 2019-07-25 stsp free(commit_id_str);
2483 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2484 30837e32 2019-07-25 stsp if (error)
2485 a297e751 2019-07-11 stsp goto done;
2486 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2487 a297e751 2019-07-11 stsp if (error)
2488 507dc3bb 2018-12-29 stsp goto done;
2489 507dc3bb 2018-12-29 stsp }
2490 35c965b2 2018-12-29 stsp
2491 a1fb16d8 2019-05-24 stsp if (branch_name) {
2492 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2493 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2494 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2495 f2ea84fa 2019-07-27 stsp continue;
2496 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2497 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2498 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2499 024e9686 2019-05-14 stsp goto done;
2500 024e9686 2019-05-14 stsp }
2501 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2502 024e9686 2019-05-14 stsp if (error)
2503 024e9686 2019-05-14 stsp goto done;
2504 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2505 3aef623b 2019-10-15 stsp repo);
2506 a367ff0f 2019-05-14 stsp free(head_commit_id);
2507 024e9686 2019-05-14 stsp if (error != NULL)
2508 024e9686 2019-05-14 stsp goto done;
2509 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2510 a367ff0f 2019-05-14 stsp if (error)
2511 a367ff0f 2019-05-14 stsp goto done;
2512 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2513 024e9686 2019-05-14 stsp if (error)
2514 024e9686 2019-05-14 stsp goto done;
2515 024e9686 2019-05-14 stsp } else {
2516 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2517 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2518 a367ff0f 2019-05-14 stsp if (error != NULL) {
2519 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2520 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2521 024e9686 2019-05-14 stsp goto done;
2522 a367ff0f 2019-05-14 stsp }
2523 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2524 a367ff0f 2019-05-14 stsp if (error)
2525 a367ff0f 2019-05-14 stsp goto done;
2526 024e9686 2019-05-14 stsp }
2527 507dc3bb 2018-12-29 stsp
2528 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2529 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2530 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2531 507dc3bb 2018-12-29 stsp commit_id);
2532 507dc3bb 2018-12-29 stsp if (error)
2533 507dc3bb 2018-12-29 stsp goto done;
2534 507dc3bb 2018-12-29 stsp }
2535 507dc3bb 2018-12-29 stsp
2536 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2537 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2538 507dc3bb 2018-12-29 stsp if (error != NULL)
2539 507dc3bb 2018-12-29 stsp goto done;
2540 9c4b8182 2019-01-02 stsp
2541 784955db 2019-01-12 stsp if (did_something)
2542 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2543 784955db 2019-01-12 stsp else
2544 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2545 507dc3bb 2018-12-29 stsp done:
2546 c09a553d 2018-03-12 stsp free(worktree_path);
2547 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2548 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2549 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2550 507dc3bb 2018-12-29 stsp free(commit_id);
2551 9c4b8182 2019-01-02 stsp free(commit_id_str);
2552 c09a553d 2018-03-12 stsp return error;
2553 c09a553d 2018-03-12 stsp }
2554 c09a553d 2018-03-12 stsp
2555 f42b1b34 2018-03-12 stsp static const struct got_error *
2556 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2557 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2558 63035f9f 2019-10-06 stsp struct got_repository *repo)
2559 5c860e29 2018-03-12 stsp {
2560 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2561 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2562 79109fed 2018-03-27 stsp
2563 44392932 2019-08-25 stsp if (blob_id1) {
2564 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2565 44392932 2019-08-25 stsp if (err)
2566 44392932 2019-08-25 stsp goto done;
2567 44392932 2019-08-25 stsp }
2568 79109fed 2018-03-27 stsp
2569 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2570 44392932 2019-08-25 stsp if (err)
2571 44392932 2019-08-25 stsp goto done;
2572 79109fed 2018-03-27 stsp
2573 44392932 2019-08-25 stsp while (path[0] == '/')
2574 44392932 2019-08-25 stsp path++;
2575 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2576 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2577 44392932 2019-08-25 stsp done:
2578 44392932 2019-08-25 stsp if (blob1)
2579 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2580 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2581 44392932 2019-08-25 stsp return err;
2582 44392932 2019-08-25 stsp }
2583 44392932 2019-08-25 stsp
2584 44392932 2019-08-25 stsp static const struct got_error *
2585 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2586 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2587 63035f9f 2019-10-06 stsp struct got_repository *repo)
2588 44392932 2019-08-25 stsp {
2589 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2590 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2591 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2592 44392932 2019-08-25 stsp
2593 44392932 2019-08-25 stsp if (tree_id1) {
2594 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2595 79109fed 2018-03-27 stsp if (err)
2596 44392932 2019-08-25 stsp goto done;
2597 79109fed 2018-03-27 stsp }
2598 79109fed 2018-03-27 stsp
2599 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2600 0f2b3dca 2018-12-22 stsp if (err)
2601 0f2b3dca 2018-12-22 stsp goto done;
2602 0f2b3dca 2018-12-22 stsp
2603 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2604 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2605 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2606 44392932 2019-08-25 stsp while (path[0] == '/')
2607 44392932 2019-08-25 stsp path++;
2608 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2609 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2610 0f2b3dca 2018-12-22 stsp done:
2611 79109fed 2018-03-27 stsp if (tree1)
2612 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2613 366e0a5f 2019-10-10 stsp if (tree2)
2614 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2615 44392932 2019-08-25 stsp return err;
2616 44392932 2019-08-25 stsp }
2617 44392932 2019-08-25 stsp
2618 44392932 2019-08-25 stsp static const struct got_error *
2619 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2620 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2621 44392932 2019-08-25 stsp {
2622 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2623 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2624 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2625 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2626 44392932 2019-08-25 stsp struct got_object_qid *qid;
2627 44392932 2019-08-25 stsp
2628 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2629 44392932 2019-08-25 stsp if (qid != NULL) {
2630 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2631 44392932 2019-08-25 stsp qid->id);
2632 44392932 2019-08-25 stsp if (err)
2633 44392932 2019-08-25 stsp return err;
2634 44392932 2019-08-25 stsp }
2635 44392932 2019-08-25 stsp
2636 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2637 44392932 2019-08-25 stsp int obj_type;
2638 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2639 44392932 2019-08-25 stsp if (err)
2640 44392932 2019-08-25 stsp goto done;
2641 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2642 44392932 2019-08-25 stsp if (err) {
2643 44392932 2019-08-25 stsp free(obj_id2);
2644 44392932 2019-08-25 stsp goto done;
2645 44392932 2019-08-25 stsp }
2646 44392932 2019-08-25 stsp if (pcommit) {
2647 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2648 44392932 2019-08-25 stsp qid->id, path);
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 err = got_object_id_str(&id_str1, obj_id1);
2654 44392932 2019-08-25 stsp if (err) {
2655 44392932 2019-08-25 stsp free(obj_id2);
2656 44392932 2019-08-25 stsp goto done;
2657 44392932 2019-08-25 stsp }
2658 44392932 2019-08-25 stsp }
2659 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2660 44392932 2019-08-25 stsp if (err) {
2661 44392932 2019-08-25 stsp free(obj_id2);
2662 44392932 2019-08-25 stsp goto done;
2663 44392932 2019-08-25 stsp }
2664 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2665 44392932 2019-08-25 stsp switch (obj_type) {
2666 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2667 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2668 63035f9f 2019-10-06 stsp 0, repo);
2669 44392932 2019-08-25 stsp break;
2670 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2671 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2672 63035f9f 2019-10-06 stsp 0, repo);
2673 44392932 2019-08-25 stsp break;
2674 44392932 2019-08-25 stsp default:
2675 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2676 44392932 2019-08-25 stsp break;
2677 44392932 2019-08-25 stsp }
2678 44392932 2019-08-25 stsp free(obj_id1);
2679 44392932 2019-08-25 stsp free(obj_id2);
2680 44392932 2019-08-25 stsp } else {
2681 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2682 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2683 44392932 2019-08-25 stsp if (err)
2684 44392932 2019-08-25 stsp goto done;
2685 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2686 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2687 44392932 2019-08-25 stsp if (err)
2688 44392932 2019-08-25 stsp goto done;
2689 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2690 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2691 44392932 2019-08-25 stsp }
2692 44392932 2019-08-25 stsp done:
2693 0f2b3dca 2018-12-22 stsp free(id_str1);
2694 0f2b3dca 2018-12-22 stsp free(id_str2);
2695 44392932 2019-08-25 stsp if (pcommit)
2696 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2697 79109fed 2018-03-27 stsp return err;
2698 79109fed 2018-03-27 stsp }
2699 79109fed 2018-03-27 stsp
2700 4bb494d5 2018-06-16 stsp static char *
2701 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2702 6c281f94 2018-06-11 stsp {
2703 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2704 09867e48 2019-08-13 stsp char *p, *s;
2705 09867e48 2019-08-13 stsp
2706 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2707 09867e48 2019-08-13 stsp if (tm == NULL)
2708 09867e48 2019-08-13 stsp return NULL;
2709 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2710 09867e48 2019-08-13 stsp if (s == NULL)
2711 09867e48 2019-08-13 stsp return NULL;
2712 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2713 6c281f94 2018-06-11 stsp if (p)
2714 6c281f94 2018-06-11 stsp *p = '\0';
2715 6c281f94 2018-06-11 stsp return s;
2716 6c281f94 2018-06-11 stsp }
2717 dc424a06 2019-08-07 stsp
2718 6841bf13 2019-11-29 kn static const struct got_error *
2719 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2720 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2721 6841bf13 2019-11-29 kn {
2722 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2723 6841bf13 2019-11-29 kn regmatch_t regmatch;
2724 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2725 6841bf13 2019-11-29 kn
2726 6841bf13 2019-11-29 kn *have_match = 0;
2727 6841bf13 2019-11-29 kn
2728 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2729 6841bf13 2019-11-29 kn if (err)
2730 6841bf13 2019-11-29 kn return err;
2731 6841bf13 2019-11-29 kn
2732 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2733 6841bf13 2019-11-29 kn if (err)
2734 6841bf13 2019-11-29 kn goto done;
2735 6841bf13 2019-11-29 kn
2736 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2737 6841bf13 2019-11-29 kn *have_match = 1;
2738 6841bf13 2019-11-29 kn done:
2739 6841bf13 2019-11-29 kn free(id_str);
2740 6841bf13 2019-11-29 kn free(logmsg);
2741 6841bf13 2019-11-29 kn return err;
2742 6841bf13 2019-11-29 kn }
2743 6841bf13 2019-11-29 kn
2744 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2745 6c281f94 2018-06-11 stsp
2746 79109fed 2018-03-27 stsp static const struct got_error *
2747 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2748 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2749 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2750 79109fed 2018-03-27 stsp {
2751 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2752 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2753 6c281f94 2018-06-11 stsp char datebuf[26];
2754 45d799e2 2018-12-23 stsp time_t committer_time;
2755 45d799e2 2018-12-23 stsp const char *author, *committer;
2756 199a4027 2019-02-02 stsp char *refs_str = NULL;
2757 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2758 5c860e29 2018-03-12 stsp
2759 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2760 199a4027 2019-02-02 stsp char *s;
2761 199a4027 2019-02-02 stsp const char *name;
2762 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2763 a436ad14 2019-08-13 stsp int cmp;
2764 a436ad14 2019-08-13 stsp
2765 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2766 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2767 d9498b20 2019-02-05 stsp continue;
2768 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2769 199a4027 2019-02-02 stsp name += 5;
2770 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2771 7143d404 2019-03-12 stsp continue;
2772 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2773 e34f9ed6 2019-02-02 stsp name += 6;
2774 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2775 141c2bff 2019-02-04 stsp name += 8;
2776 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2777 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2778 5d844a1e 2019-08-13 stsp if (err) {
2779 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2780 5d844a1e 2019-08-13 stsp return err;
2781 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2782 5d844a1e 2019-08-13 stsp err = NULL;
2783 5d844a1e 2019-08-13 stsp tag = NULL;
2784 5d844a1e 2019-08-13 stsp }
2785 a436ad14 2019-08-13 stsp }
2786 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2787 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2788 a436ad14 2019-08-13 stsp if (tag)
2789 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2790 a436ad14 2019-08-13 stsp if (cmp != 0)
2791 a436ad14 2019-08-13 stsp continue;
2792 199a4027 2019-02-02 stsp s = refs_str;
2793 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2794 199a4027 2019-02-02 stsp name) == -1) {
2795 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2796 199a4027 2019-02-02 stsp free(s);
2797 34ca4898 2019-08-13 stsp return err;
2798 199a4027 2019-02-02 stsp }
2799 199a4027 2019-02-02 stsp free(s);
2800 199a4027 2019-02-02 stsp }
2801 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2802 8bf5b3c9 2018-03-17 stsp if (err)
2803 8bf5b3c9 2018-03-17 stsp return err;
2804 788c352e 2018-06-16 stsp
2805 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2806 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2807 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2808 832c249c 2018-06-10 stsp free(id_str);
2809 d3d493d7 2019-02-21 stsp id_str = NULL;
2810 d3d493d7 2019-02-21 stsp free(refs_str);
2811 d3d493d7 2019-02-21 stsp refs_str = NULL;
2812 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2813 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2814 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2815 09867e48 2019-08-13 stsp if (datestr)
2816 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2817 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2818 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2819 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2820 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2821 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2822 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2823 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2824 3fe1abad 2018-06-10 stsp int n = 1;
2825 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2826 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2827 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2828 a0603db2 2018-06-10 stsp if (err)
2829 a0603db2 2018-06-10 stsp return err;
2830 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2831 a0603db2 2018-06-10 stsp free(id_str);
2832 a0603db2 2018-06-10 stsp }
2833 a0603db2 2018-06-10 stsp }
2834 832c249c 2018-06-10 stsp
2835 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2836 5943eee2 2019-08-13 stsp if (err)
2837 5943eee2 2019-08-13 stsp return err;
2838 8bf5b3c9 2018-03-17 stsp
2839 621015ac 2018-07-23 stsp logmsg = logmsg0;
2840 832c249c 2018-06-10 stsp do {
2841 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2842 832c249c 2018-06-10 stsp if (line)
2843 832c249c 2018-06-10 stsp printf(" %s\n", line);
2844 832c249c 2018-06-10 stsp } while (line);
2845 621015ac 2018-07-23 stsp free(logmsg0);
2846 832c249c 2018-06-10 stsp
2847 971751ac 2018-03-27 stsp if (show_patch) {
2848 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2849 971751ac 2018-03-27 stsp if (err == 0)
2850 971751ac 2018-03-27 stsp printf("\n");
2851 971751ac 2018-03-27 stsp }
2852 07862c20 2018-09-15 stsp
2853 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2854 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2855 07862c20 2018-09-15 stsp return err;
2856 f42b1b34 2018-03-12 stsp }
2857 5c860e29 2018-03-12 stsp
2858 f42b1b34 2018-03-12 stsp static const struct got_error *
2859 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2860 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2861 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2862 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2863 f42b1b34 2018-03-12 stsp {
2864 f42b1b34 2018-03-12 stsp const struct got_error *err;
2865 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2866 6841bf13 2019-11-29 kn regex_t regex;
2867 6841bf13 2019-11-29 kn int have_match;
2868 372ccdbb 2018-06-10 stsp
2869 6841bf13 2019-11-29 kn if (search_pattern &&
2870 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2871 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2872 6841bf13 2019-11-29 kn
2873 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2874 f42b1b34 2018-03-12 stsp if (err)
2875 f42b1b34 2018-03-12 stsp return err;
2876 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2877 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2878 372ccdbb 2018-06-10 stsp if (err)
2879 fcc85cad 2018-07-22 stsp goto done;
2880 656b1f76 2019-05-11 jcs for (;;) {
2881 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2882 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2883 8bf5b3c9 2018-03-17 stsp
2884 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2885 84453469 2018-11-11 stsp break;
2886 84453469 2018-11-11 stsp
2887 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2888 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2889 372ccdbb 2018-06-10 stsp if (err) {
2890 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2891 9ba79e04 2018-06-11 stsp err = NULL;
2892 ee780d5c 2020-01-04 stsp break;
2893 7e665116 2018-04-02 stsp }
2894 b43fbaa0 2018-06-11 stsp if (id == NULL)
2895 7e665116 2018-04-02 stsp break;
2896 b43fbaa0 2018-06-11 stsp
2897 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2898 b43fbaa0 2018-06-11 stsp if (err)
2899 fcc85cad 2018-07-22 stsp break;
2900 6841bf13 2019-11-29 kn
2901 6841bf13 2019-11-29 kn if (search_pattern) {
2902 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2903 6841bf13 2019-11-29 kn if (err) {
2904 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2905 6841bf13 2019-11-29 kn break;
2906 6841bf13 2019-11-29 kn }
2907 6841bf13 2019-11-29 kn if (have_match == 0) {
2908 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2909 6841bf13 2019-11-29 kn continue;
2910 6841bf13 2019-11-29 kn }
2911 6841bf13 2019-11-29 kn }
2912 6841bf13 2019-11-29 kn
2913 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2914 44392932 2019-08-25 stsp diff_context, refs);
2915 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2916 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2917 7e665116 2018-04-02 stsp break;
2918 31cedeaf 2018-09-15 stsp }
2919 fcc85cad 2018-07-22 stsp done:
2920 6841bf13 2019-11-29 kn if (search_pattern)
2921 6841bf13 2019-11-29 kn regfree(&regex);
2922 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2923 f42b1b34 2018-03-12 stsp return err;
2924 f42b1b34 2018-03-12 stsp }
2925 5c860e29 2018-03-12 stsp
2926 4ed7e80c 2018-05-20 stsp __dead static void
2927 6f3d1eb0 2018-03-12 stsp usage_log(void)
2928 6f3d1eb0 2018-03-12 stsp {
2929 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2930 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2931 6f3d1eb0 2018-03-12 stsp exit(1);
2932 b1ebc001 2019-08-13 stsp }
2933 b1ebc001 2019-08-13 stsp
2934 b1ebc001 2019-08-13 stsp static int
2935 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2936 b1ebc001 2019-08-13 stsp {
2937 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2938 b1ebc001 2019-08-13 stsp long long n;
2939 b1ebc001 2019-08-13 stsp const char *errstr;
2940 b1ebc001 2019-08-13 stsp
2941 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2942 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2943 b1ebc001 2019-08-13 stsp return 0;
2944 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2945 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2946 b1ebc001 2019-08-13 stsp return 0;
2947 b1ebc001 2019-08-13 stsp return n;
2948 6f3d1eb0 2018-03-12 stsp }
2949 6f3d1eb0 2018-03-12 stsp
2950 4ed7e80c 2018-05-20 stsp static const struct got_error *
2951 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2952 f42b1b34 2018-03-12 stsp {
2953 f42b1b34 2018-03-12 stsp const struct got_error *error;
2954 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2955 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2956 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2957 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2958 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2959 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2960 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2961 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2962 64a96a6d 2018-04-01 stsp const char *errstr;
2963 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2964 1b3893a2 2019-03-18 stsp
2965 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2966 5c860e29 2018-03-12 stsp
2967 6715a751 2018-03-16 stsp #ifndef PROFILE
2968 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2969 6098196c 2019-01-04 stsp NULL)
2970 ad242220 2018-09-08 stsp == -1)
2971 f42b1b34 2018-03-12 stsp err(1, "pledge");
2972 6715a751 2018-03-16 stsp #endif
2973 79109fed 2018-03-27 stsp
2974 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2975 b1ebc001 2019-08-13 stsp
2976 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2977 79109fed 2018-03-27 stsp switch (ch) {
2978 79109fed 2018-03-27 stsp case 'p':
2979 79109fed 2018-03-27 stsp show_patch = 1;
2980 d142fc45 2018-04-01 stsp break;
2981 d142fc45 2018-04-01 stsp case 'c':
2982 d142fc45 2018-04-01 stsp start_commit = optarg;
2983 64a96a6d 2018-04-01 stsp break;
2984 c0cc5c62 2018-10-18 stsp case 'C':
2985 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2986 4a8520aa 2018-10-18 stsp &errstr);
2987 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2988 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2989 c0cc5c62 2018-10-18 stsp break;
2990 64a96a6d 2018-04-01 stsp case 'l':
2991 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2992 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2993 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2994 cc54c501 2019-07-15 stsp break;
2995 48c8c60d 2020-01-27 stsp case 'b':
2996 48c8c60d 2020-01-27 stsp log_branches = 1;
2997 a0603db2 2018-06-10 stsp break;
2998 04ca23f4 2018-07-16 stsp case 'r':
2999 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3000 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3001 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3002 9ba1d308 2019-10-21 stsp optarg);
3003 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3004 04ca23f4 2018-07-16 stsp break;
3005 6841bf13 2019-11-29 kn case 's':
3006 6841bf13 2019-11-29 kn search_pattern = optarg;
3007 6841bf13 2019-11-29 kn break;
3008 79109fed 2018-03-27 stsp default:
3009 2deda0b9 2019-03-07 stsp usage_log();
3010 79109fed 2018-03-27 stsp /* NOTREACHED */
3011 79109fed 2018-03-27 stsp }
3012 79109fed 2018-03-27 stsp }
3013 79109fed 2018-03-27 stsp
3014 79109fed 2018-03-27 stsp argc -= optind;
3015 79109fed 2018-03-27 stsp argv += optind;
3016 f42b1b34 2018-03-12 stsp
3017 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3018 dc1edbfa 2019-11-29 kn diff_context = 3;
3019 dc1edbfa 2019-11-29 kn else if (!show_patch)
3020 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3021 dc1edbfa 2019-11-29 kn
3022 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3023 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3024 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3025 04ca23f4 2018-07-16 stsp goto done;
3026 04ca23f4 2018-07-16 stsp }
3027 cffc0aa4 2019-02-05 stsp
3028 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3029 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3030 cffc0aa4 2019-02-05 stsp goto done;
3031 cffc0aa4 2019-02-05 stsp error = NULL;
3032 cffc0aa4 2019-02-05 stsp
3033 e7301579 2019-03-18 stsp if (argc == 0) {
3034 cbd1af7a 2019-03-18 stsp path = strdup("");
3035 e7301579 2019-03-18 stsp if (path == NULL) {
3036 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3037 cbd1af7a 2019-03-18 stsp goto done;
3038 e7301579 2019-03-18 stsp }
3039 e7301579 2019-03-18 stsp } else if (argc == 1) {
3040 e7301579 2019-03-18 stsp if (worktree) {
3041 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3042 e7301579 2019-03-18 stsp argv[0]);
3043 e7301579 2019-03-18 stsp if (error)
3044 e7301579 2019-03-18 stsp goto done;
3045 e7301579 2019-03-18 stsp } else {
3046 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3047 e7301579 2019-03-18 stsp if (path == NULL) {
3048 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3049 e7301579 2019-03-18 stsp goto done;
3050 e7301579 2019-03-18 stsp }
3051 e7301579 2019-03-18 stsp }
3052 cbd1af7a 2019-03-18 stsp } else
3053 cbd1af7a 2019-03-18 stsp usage_log();
3054 cbd1af7a 2019-03-18 stsp
3055 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3056 5486daa2 2019-05-11 stsp repo_path = worktree ?
3057 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3058 5486daa2 2019-05-11 stsp }
3059 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3060 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3061 cffc0aa4 2019-02-05 stsp goto done;
3062 04ca23f4 2018-07-16 stsp }
3063 6098196c 2019-01-04 stsp
3064 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3065 d7d4f210 2018-03-12 stsp if (error != NULL)
3066 04ca23f4 2018-07-16 stsp goto done;
3067 f42b1b34 2018-03-12 stsp
3068 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3069 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3070 c02c541e 2019-03-29 stsp if (error)
3071 c02c541e 2019-03-29 stsp goto done;
3072 c02c541e 2019-03-29 stsp
3073 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3074 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3075 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3076 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3077 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3078 3235492e 2018-04-01 stsp if (error != NULL)
3079 3235492e 2018-04-01 stsp return error;
3080 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
3081 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3082 3235492e 2018-04-01 stsp if (error != NULL)
3083 3235492e 2018-04-01 stsp return error;
3084 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3085 3235492e 2018-04-01 stsp } else {
3086 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
3087 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3088 3235492e 2018-04-01 stsp if (error == NULL) {
3089 1caad61b 2019-02-01 stsp int obj_type;
3090 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
3091 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
3092 d1f2edc9 2018-06-13 stsp if (error != NULL)
3093 1caad61b 2019-02-01 stsp goto done;
3094 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, 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 (obj_type == GOT_OBJ_TYPE_TAG) {
3098 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
3099 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
3100 1caad61b 2019-02-01 stsp if (error != NULL)
3101 1caad61b 2019-02-01 stsp goto done;
3102 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
3103 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
3104 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3105 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3106 1caad61b 2019-02-01 stsp goto done;
3107 1caad61b 2019-02-01 stsp }
3108 1caad61b 2019-02-01 stsp free(id);
3109 1caad61b 2019-02-01 stsp id = got_object_id_dup(
3110 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
3111 1caad61b 2019-02-01 stsp if (id == NULL)
3112 638f9024 2019-05-13 stsp error = got_error_from_errno(
3113 230a42bd 2019-05-11 jcs "got_object_id_dup");
3114 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3115 1caad61b 2019-02-01 stsp if (error)
3116 1caad61b 2019-02-01 stsp goto done;
3117 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3118 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3119 1caad61b 2019-02-01 stsp goto done;
3120 1caad61b 2019-02-01 stsp }
3121 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3122 d1f2edc9 2018-06-13 stsp if (error != NULL)
3123 1caad61b 2019-02-01 stsp goto done;
3124 d1f2edc9 2018-06-13 stsp }
3125 15a94983 2018-12-23 stsp if (commit == NULL) {
3126 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
3127 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3128 d1f2edc9 2018-06-13 stsp if (error != NULL)
3129 d1f2edc9 2018-06-13 stsp return error;
3130 3235492e 2018-04-01 stsp }
3131 3235492e 2018-04-01 stsp }
3132 d7d4f210 2018-03-12 stsp if (error != NULL)
3133 04ca23f4 2018-07-16 stsp goto done;
3134 04ca23f4 2018-07-16 stsp
3135 deeabeae 2019-08-27 stsp if (worktree) {
3136 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3137 deeabeae 2019-08-27 stsp char *p;
3138 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3139 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3140 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3141 deeabeae 2019-08-27 stsp goto done;
3142 deeabeae 2019-08-27 stsp }
3143 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3144 deeabeae 2019-08-27 stsp free(p);
3145 deeabeae 2019-08-27 stsp } else
3146 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3147 04ca23f4 2018-07-16 stsp if (error != NULL)
3148 04ca23f4 2018-07-16 stsp goto done;
3149 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3150 04ca23f4 2018-07-16 stsp free(path);
3151 04ca23f4 2018-07-16 stsp path = in_repo_path;
3152 04ca23f4 2018-07-16 stsp }
3153 199a4027 2019-02-02 stsp
3154 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3155 199a4027 2019-02-02 stsp if (error)
3156 199a4027 2019-02-02 stsp goto done;
3157 04ca23f4 2018-07-16 stsp
3158 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3159 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3160 04ca23f4 2018-07-16 stsp done:
3161 04ca23f4 2018-07-16 stsp free(path);
3162 04ca23f4 2018-07-16 stsp free(repo_path);
3163 04ca23f4 2018-07-16 stsp free(cwd);
3164 f42b1b34 2018-03-12 stsp free(id);
3165 cffc0aa4 2019-02-05 stsp if (worktree)
3166 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3167 ad242220 2018-09-08 stsp if (repo) {
3168 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3169 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3170 ad242220 2018-09-08 stsp if (error == NULL)
3171 ad242220 2018-09-08 stsp error = repo_error;
3172 ad242220 2018-09-08 stsp }
3173 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3174 8bf5b3c9 2018-03-17 stsp return error;
3175 5c860e29 2018-03-12 stsp }
3176 5c860e29 2018-03-12 stsp
3177 4ed7e80c 2018-05-20 stsp __dead static void
3178 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3179 3f8b7d6a 2018-04-01 stsp {
3180 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3181 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3182 3f8b7d6a 2018-04-01 stsp exit(1);
3183 b00d56cd 2018-04-01 stsp }
3184 b00d56cd 2018-04-01 stsp
3185 b72f483a 2019-02-05 stsp struct print_diff_arg {
3186 b72f483a 2019-02-05 stsp struct got_repository *repo;
3187 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3188 b72f483a 2019-02-05 stsp int diff_context;
3189 3fc0c068 2019-02-10 stsp const char *id_str;
3190 3fc0c068 2019-02-10 stsp int header_shown;
3191 98eaaa12 2019-08-03 stsp int diff_staged;
3192 63035f9f 2019-10-06 stsp int ignore_whitespace;
3193 b72f483a 2019-02-05 stsp };
3194 b72f483a 2019-02-05 stsp
3195 4ed7e80c 2018-05-20 stsp static const struct got_error *
3196 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3197 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3198 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3199 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3200 b72f483a 2019-02-05 stsp {
3201 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3202 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3203 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3204 12463d8b 2019-12-13 stsp int fd = -1;
3205 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3206 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3207 b72f483a 2019-02-05 stsp struct stat sb;
3208 b72f483a 2019-02-05 stsp
3209 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3210 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3211 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3212 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3213 98eaaa12 2019-08-03 stsp return NULL;
3214 98eaaa12 2019-08-03 stsp } else {
3215 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3216 98eaaa12 2019-08-03 stsp return NULL;
3217 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3218 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3219 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3220 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3221 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3222 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3223 98eaaa12 2019-08-03 stsp return NULL;
3224 98eaaa12 2019-08-03 stsp }
3225 3fc0c068 2019-02-10 stsp
3226 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3227 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3228 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3229 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3230 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3231 3fc0c068 2019-02-10 stsp }
3232 b72f483a 2019-02-05 stsp
3233 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3234 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3235 98eaaa12 2019-08-03 stsp switch (staged_status) {
3236 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3237 98eaaa12 2019-08-03 stsp label1 = path;
3238 98eaaa12 2019-08-03 stsp label2 = path;
3239 98eaaa12 2019-08-03 stsp break;
3240 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3241 98eaaa12 2019-08-03 stsp label2 = path;
3242 98eaaa12 2019-08-03 stsp break;
3243 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3244 98eaaa12 2019-08-03 stsp label1 = path;
3245 98eaaa12 2019-08-03 stsp break;
3246 98eaaa12 2019-08-03 stsp default:
3247 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3248 98eaaa12 2019-08-03 stsp }
3249 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3250 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3251 63035f9f 2019-10-06 stsp a->repo, stdout);
3252 98eaaa12 2019-08-03 stsp }
3253 98eaaa12 2019-08-03 stsp
3254 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3255 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3256 4ce46740 2019-08-08 stsp char *id_str;
3257 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3258 408b4ebc 2019-08-03 stsp 8192);
3259 4ce46740 2019-08-08 stsp if (err)
3260 4ce46740 2019-08-08 stsp goto done;
3261 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3262 4ce46740 2019-08-08 stsp if (err)
3263 4ce46740 2019-08-08 stsp goto done;
3264 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3265 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3266 4ce46740 2019-08-08 stsp free(id_str);
3267 4ce46740 2019-08-08 stsp goto done;
3268 4ce46740 2019-08-08 stsp }
3269 4ce46740 2019-08-08 stsp free(id_str);
3270 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3271 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3272 4ce46740 2019-08-08 stsp if (err)
3273 4ce46740 2019-08-08 stsp goto done;
3274 4ce46740 2019-08-08 stsp }
3275 d00136be 2019-03-26 stsp
3276 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3277 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3278 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3279 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3280 2ec1f75b 2019-03-26 stsp goto done;
3281 2ec1f75b 2019-03-26 stsp }
3282 b72f483a 2019-02-05 stsp
3283 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3284 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3285 12463d8b 2019-12-13 stsp if (fd == -1) {
3286 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3287 12463d8b 2019-12-13 stsp goto done;
3288 12463d8b 2019-12-13 stsp }
3289 12463d8b 2019-12-13 stsp } else {
3290 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3291 12463d8b 2019-12-13 stsp if (fd == -1) {
3292 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3293 12463d8b 2019-12-13 stsp goto done;
3294 12463d8b 2019-12-13 stsp }
3295 12463d8b 2019-12-13 stsp }
3296 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3297 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3298 2ec1f75b 2019-03-26 stsp goto done;
3299 2ec1f75b 2019-03-26 stsp }
3300 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3301 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3302 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3303 2ec1f75b 2019-03-26 stsp goto done;
3304 2ec1f75b 2019-03-26 stsp }
3305 12463d8b 2019-12-13 stsp fd = -1;
3306 2ec1f75b 2019-03-26 stsp } else
3307 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3308 b72f483a 2019-02-05 stsp
3309 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3310 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3311 b72f483a 2019-02-05 stsp done:
3312 b72f483a 2019-02-05 stsp if (blob1)
3313 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3314 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3315 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3316 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3317 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3318 b72f483a 2019-02-05 stsp free(abspath);
3319 927df6b7 2019-02-10 stsp return err;
3320 927df6b7 2019-02-10 stsp }
3321 927df6b7 2019-02-10 stsp
3322 927df6b7 2019-02-10 stsp static const struct got_error *
3323 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3324 b00d56cd 2018-04-01 stsp {
3325 b00d56cd 2018-04-01 stsp const struct got_error *error;
3326 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3327 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3328 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3329 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3330 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3331 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3332 15a94983 2018-12-23 stsp int type1, type2;
3333 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3334 c0cc5c62 2018-10-18 stsp const char *errstr;
3335 927df6b7 2019-02-10 stsp char *path = NULL;
3336 b00d56cd 2018-04-01 stsp
3337 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3338 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3339 25eccc22 2019-01-04 stsp NULL) == -1)
3340 b00d56cd 2018-04-01 stsp err(1, "pledge");
3341 b00d56cd 2018-04-01 stsp #endif
3342 b00d56cd 2018-04-01 stsp
3343 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3344 b00d56cd 2018-04-01 stsp switch (ch) {
3345 c0cc5c62 2018-10-18 stsp case 'C':
3346 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3347 dfcab68b 2019-11-29 kn &errstr);
3348 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3349 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3350 c0cc5c62 2018-10-18 stsp break;
3351 b72f483a 2019-02-05 stsp case 'r':
3352 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3353 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3354 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3355 9ba1d308 2019-10-21 stsp optarg);
3356 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3357 b72f483a 2019-02-05 stsp break;
3358 98eaaa12 2019-08-03 stsp case 's':
3359 98eaaa12 2019-08-03 stsp diff_staged = 1;
3360 63035f9f 2019-10-06 stsp break;
3361 63035f9f 2019-10-06 stsp case 'w':
3362 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3363 98eaaa12 2019-08-03 stsp break;
3364 b00d56cd 2018-04-01 stsp default:
3365 2deda0b9 2019-03-07 stsp usage_diff();
3366 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3367 b00d56cd 2018-04-01 stsp }
3368 b00d56cd 2018-04-01 stsp }
3369 b00d56cd 2018-04-01 stsp
3370 b00d56cd 2018-04-01 stsp argc -= optind;
3371 b00d56cd 2018-04-01 stsp argv += optind;
3372 b00d56cd 2018-04-01 stsp
3373 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3374 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3375 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3376 b72f483a 2019-02-05 stsp goto done;
3377 b72f483a 2019-02-05 stsp }
3378 927df6b7 2019-02-10 stsp if (argc <= 1) {
3379 b72f483a 2019-02-05 stsp if (repo_path)
3380 b72f483a 2019-02-05 stsp errx(1,
3381 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3382 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3383 1f03b8da 2020-03-20 stsp if (error)
3384 1f03b8da 2020-03-20 stsp goto done;
3385 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3386 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3387 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3388 927df6b7 2019-02-10 stsp goto done;
3389 927df6b7 2019-02-10 stsp }
3390 927df6b7 2019-02-10 stsp if (argc == 1) {
3391 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3392 cbd1af7a 2019-03-18 stsp argv[0]);
3393 927df6b7 2019-02-10 stsp if (error)
3394 927df6b7 2019-02-10 stsp goto done;
3395 927df6b7 2019-02-10 stsp } else {
3396 927df6b7 2019-02-10 stsp path = strdup("");
3397 927df6b7 2019-02-10 stsp if (path == NULL) {
3398 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3399 927df6b7 2019-02-10 stsp goto done;
3400 927df6b7 2019-02-10 stsp }
3401 927df6b7 2019-02-10 stsp }
3402 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3403 98eaaa12 2019-08-03 stsp if (diff_staged)
3404 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3405 98eaaa12 2019-08-03 stsp "objects in repository");
3406 15a94983 2018-12-23 stsp id_str1 = argv[0];
3407 15a94983 2018-12-23 stsp id_str2 = argv[1];
3408 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3409 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3410 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3411 30db809c 2019-06-05 stsp goto done;
3412 1f03b8da 2020-03-20 stsp if (worktree) {
3413 1f03b8da 2020-03-20 stsp repo_path = strdup(
3414 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3415 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3416 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3417 1f03b8da 2020-03-20 stsp goto done;
3418 1f03b8da 2020-03-20 stsp }
3419 1f03b8da 2020-03-20 stsp } else {
3420 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3421 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3422 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3423 1f03b8da 2020-03-20 stsp goto done;
3424 1f03b8da 2020-03-20 stsp }
3425 30db809c 2019-06-05 stsp }
3426 30db809c 2019-06-05 stsp }
3427 b00d56cd 2018-04-01 stsp } else
3428 b00d56cd 2018-04-01 stsp usage_diff();
3429 b00d56cd 2018-04-01 stsp
3430 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3431 76089277 2018-04-01 stsp free(repo_path);
3432 b00d56cd 2018-04-01 stsp if (error != NULL)
3433 b00d56cd 2018-04-01 stsp goto done;
3434 b00d56cd 2018-04-01 stsp
3435 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3436 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3437 c02c541e 2019-03-29 stsp if (error)
3438 c02c541e 2019-03-29 stsp goto done;
3439 c02c541e 2019-03-29 stsp
3440 30db809c 2019-06-05 stsp if (argc <= 1) {
3441 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3442 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3443 b72f483a 2019-02-05 stsp char *id_str;
3444 72ea6654 2019-07-27 stsp
3445 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3446 72ea6654 2019-07-27 stsp
3447 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3448 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3449 b72f483a 2019-02-05 stsp if (error)
3450 b72f483a 2019-02-05 stsp goto done;
3451 b72f483a 2019-02-05 stsp arg.repo = repo;
3452 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3453 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3454 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3455 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3456 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3457 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3458 b72f483a 2019-02-05 stsp
3459 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3460 72ea6654 2019-07-27 stsp if (error)
3461 72ea6654 2019-07-27 stsp goto done;
3462 72ea6654 2019-07-27 stsp
3463 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3464 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3465 b72f483a 2019-02-05 stsp free(id_str);
3466 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3467 b72f483a 2019-02-05 stsp goto done;
3468 b72f483a 2019-02-05 stsp }
3469 b72f483a 2019-02-05 stsp
3470 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
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 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3476 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3477 0adb7196 2019-08-11 stsp if (error)
3478 0adb7196 2019-08-11 stsp goto done;
3479 b00d56cd 2018-04-01 stsp
3480 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3481 15a94983 2018-12-23 stsp if (error)
3482 15a94983 2018-12-23 stsp goto done;
3483 15a94983 2018-12-23 stsp
3484 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3485 15a94983 2018-12-23 stsp if (error)
3486 15a94983 2018-12-23 stsp goto done;
3487 15a94983 2018-12-23 stsp
3488 15a94983 2018-12-23 stsp if (type1 != type2) {
3489 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3490 b00d56cd 2018-04-01 stsp goto done;
3491 b00d56cd 2018-04-01 stsp }
3492 b00d56cd 2018-04-01 stsp
3493 15a94983 2018-12-23 stsp switch (type1) {
3494 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3495 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3496 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3497 b00d56cd 2018-04-01 stsp break;
3498 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3499 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3500 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3501 b00d56cd 2018-04-01 stsp break;
3502 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3503 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3504 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3505 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3506 b00d56cd 2018-04-01 stsp break;
3507 b00d56cd 2018-04-01 stsp default:
3508 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3509 b00d56cd 2018-04-01 stsp }
3510 b00d56cd 2018-04-01 stsp done:
3511 5e70831e 2019-05-28 stsp free(label1);
3512 5e70831e 2019-05-28 stsp free(label2);
3513 15a94983 2018-12-23 stsp free(id1);
3514 15a94983 2018-12-23 stsp free(id2);
3515 927df6b7 2019-02-10 stsp free(path);
3516 b72f483a 2019-02-05 stsp if (worktree)
3517 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3518 ad242220 2018-09-08 stsp if (repo) {
3519 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3520 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3521 ad242220 2018-09-08 stsp if (error == NULL)
3522 ad242220 2018-09-08 stsp error = repo_error;
3523 ad242220 2018-09-08 stsp }
3524 b00d56cd 2018-04-01 stsp return error;
3525 404c43c4 2018-06-21 stsp }
3526 404c43c4 2018-06-21 stsp
3527 404c43c4 2018-06-21 stsp __dead static void
3528 404c43c4 2018-06-21 stsp usage_blame(void)
3529 404c43c4 2018-06-21 stsp {
3530 9270e621 2019-02-05 stsp fprintf(stderr,
3531 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3532 404c43c4 2018-06-21 stsp getprogname());
3533 404c43c4 2018-06-21 stsp exit(1);
3534 b00d56cd 2018-04-01 stsp }
3535 b00d56cd 2018-04-01 stsp
3536 28315671 2019-08-14 stsp struct blame_line {
3537 28315671 2019-08-14 stsp int annotated;
3538 28315671 2019-08-14 stsp char *id_str;
3539 82f6abb8 2019-08-14 stsp char *committer;
3540 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3541 28315671 2019-08-14 stsp };
3542 28315671 2019-08-14 stsp
3543 28315671 2019-08-14 stsp struct blame_cb_args {
3544 28315671 2019-08-14 stsp struct blame_line *lines;
3545 28315671 2019-08-14 stsp int nlines;
3546 7ef28ff8 2019-08-14 stsp int nlines_prec;
3547 28315671 2019-08-14 stsp int lineno_cur;
3548 28315671 2019-08-14 stsp off_t *line_offsets;
3549 28315671 2019-08-14 stsp FILE *f;
3550 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3551 28315671 2019-08-14 stsp };
3552 28315671 2019-08-14 stsp
3553 404c43c4 2018-06-21 stsp static const struct got_error *
3554 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3555 28315671 2019-08-14 stsp {
3556 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3557 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3558 28315671 2019-08-14 stsp struct blame_line *bline;
3559 82f6abb8 2019-08-14 stsp char *line = NULL;
3560 28315671 2019-08-14 stsp size_t linesize = 0;
3561 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3562 28315671 2019-08-14 stsp off_t offset;
3563 bcb49d15 2019-08-14 stsp struct tm tm;
3564 bcb49d15 2019-08-14 stsp time_t committer_time;
3565 28315671 2019-08-14 stsp
3566 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3567 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3568 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3569 28315671 2019-08-14 stsp
3570 28315671 2019-08-14 stsp if (sigint_received)
3571 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3572 28315671 2019-08-14 stsp
3573 2839f8b9 2019-08-18 stsp if (lineno == -1)
3574 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3575 2839f8b9 2019-08-18 stsp
3576 28315671 2019-08-14 stsp /* Annotate this line. */
3577 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3578 28315671 2019-08-14 stsp if (bline->annotated)
3579 28315671 2019-08-14 stsp return NULL;
3580 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3581 28315671 2019-08-14 stsp if (err)
3582 28315671 2019-08-14 stsp return err;
3583 82f6abb8 2019-08-14 stsp
3584 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3585 82f6abb8 2019-08-14 stsp if (err)
3586 82f6abb8 2019-08-14 stsp goto done;
3587 82f6abb8 2019-08-14 stsp
3588 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3589 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3590 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3591 bcb49d15 2019-08-14 stsp goto done;
3592 bcb49d15 2019-08-14 stsp }
3593 bcb49d15 2019-08-14 stsp
3594 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3595 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3596 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3597 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3598 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3599 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3600 82f6abb8 2019-08-14 stsp goto done;
3601 82f6abb8 2019-08-14 stsp }
3602 28315671 2019-08-14 stsp bline->annotated = 1;
3603 28315671 2019-08-14 stsp
3604 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3605 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3606 28315671 2019-08-14 stsp if (!bline->annotated)
3607 82f6abb8 2019-08-14 stsp goto done;
3608 28315671 2019-08-14 stsp
3609 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3610 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3611 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3612 82f6abb8 2019-08-14 stsp goto done;
3613 82f6abb8 2019-08-14 stsp }
3614 28315671 2019-08-14 stsp
3615 28315671 2019-08-14 stsp while (bline->annotated) {
3616 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3617 82f6abb8 2019-08-14 stsp size_t len;
3618 82f6abb8 2019-08-14 stsp
3619 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3620 28315671 2019-08-14 stsp if (ferror(a->f))
3621 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3622 28315671 2019-08-14 stsp break;
3623 28315671 2019-08-14 stsp }
3624 82f6abb8 2019-08-14 stsp
3625 82f6abb8 2019-08-14 stsp committer = bline->committer;
3626 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3627 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3628 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3629 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3630 82f6abb8 2019-08-14 stsp if (at)
3631 82f6abb8 2019-08-14 stsp *at = '\0';
3632 82f6abb8 2019-08-14 stsp len = strlen(committer);
3633 82f6abb8 2019-08-14 stsp if (len >= 9)
3634 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3635 28315671 2019-08-14 stsp
3636 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3637 28315671 2019-08-14 stsp if (nl)
3638 28315671 2019-08-14 stsp *nl = '\0';
3639 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3640 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3641 28315671 2019-08-14 stsp
3642 28315671 2019-08-14 stsp a->lineno_cur++;
3643 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3644 28315671 2019-08-14 stsp }
3645 82f6abb8 2019-08-14 stsp done:
3646 82f6abb8 2019-08-14 stsp if (commit)
3647 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3648 28315671 2019-08-14 stsp free(line);
3649 28315671 2019-08-14 stsp return err;
3650 28315671 2019-08-14 stsp }
3651 28315671 2019-08-14 stsp
3652 28315671 2019-08-14 stsp static const struct got_error *
3653 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3654 404c43c4 2018-06-21 stsp {
3655 404c43c4 2018-06-21 stsp const struct got_error *error;
3656 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3657 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3658 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3659 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3660 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3661 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3662 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3663 28315671 2019-08-14 stsp struct blame_cb_args bca;
3664 28315671 2019-08-14 stsp int ch, obj_type, i;
3665 b02560ec 2019-08-19 stsp size_t filesize;
3666 90f3c347 2019-08-19 stsp
3667 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3668 404c43c4 2018-06-21 stsp
3669 404c43c4 2018-06-21 stsp #ifndef PROFILE
3670 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3671 36e2fb66 2019-01-04 stsp NULL) == -1)
3672 404c43c4 2018-06-21 stsp err(1, "pledge");
3673 404c43c4 2018-06-21 stsp #endif
3674 404c43c4 2018-06-21 stsp
3675 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3676 404c43c4 2018-06-21 stsp switch (ch) {
3677 404c43c4 2018-06-21 stsp case 'c':
3678 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3679 404c43c4 2018-06-21 stsp break;
3680 66bea077 2018-08-02 stsp case 'r':
3681 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3682 66bea077 2018-08-02 stsp if (repo_path == NULL)
3683 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3684 9ba1d308 2019-10-21 stsp optarg);
3685 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3686 66bea077 2018-08-02 stsp break;
3687 404c43c4 2018-06-21 stsp default:
3688 2deda0b9 2019-03-07 stsp usage_blame();
3689 404c43c4 2018-06-21 stsp /* NOTREACHED */
3690 404c43c4 2018-06-21 stsp }
3691 404c43c4 2018-06-21 stsp }
3692 404c43c4 2018-06-21 stsp
3693 404c43c4 2018-06-21 stsp argc -= optind;
3694 404c43c4 2018-06-21 stsp argv += optind;
3695 404c43c4 2018-06-21 stsp
3696 a39318fd 2018-08-02 stsp if (argc == 1)
3697 404c43c4 2018-06-21 stsp path = argv[0];
3698 a39318fd 2018-08-02 stsp else
3699 404c43c4 2018-06-21 stsp usage_blame();
3700 404c43c4 2018-06-21 stsp
3701 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3702 66bea077 2018-08-02 stsp if (cwd == NULL) {
3703 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3704 66bea077 2018-08-02 stsp goto done;
3705 66bea077 2018-08-02 stsp }
3706 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3707 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3708 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3709 66bea077 2018-08-02 stsp goto done;
3710 0c06baac 2019-02-05 stsp else
3711 0c06baac 2019-02-05 stsp error = NULL;
3712 0c06baac 2019-02-05 stsp if (worktree) {
3713 0c06baac 2019-02-05 stsp repo_path =
3714 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3715 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3716 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3717 1f03b8da 2020-03-20 stsp if (error)
3718 1f03b8da 2020-03-20 stsp goto done;
3719 1f03b8da 2020-03-20 stsp }
3720 0c06baac 2019-02-05 stsp } else {
3721 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3722 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3723 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3724 0c06baac 2019-02-05 stsp goto done;
3725 0c06baac 2019-02-05 stsp }
3726 66bea077 2018-08-02 stsp }
3727 66bea077 2018-08-02 stsp }
3728 36e2fb66 2019-01-04 stsp
3729 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3730 c02c541e 2019-03-29 stsp if (error != NULL)
3731 36e2fb66 2019-01-04 stsp goto done;
3732 66bea077 2018-08-02 stsp
3733 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3734 c02c541e 2019-03-29 stsp if (error)
3735 404c43c4 2018-06-21 stsp goto done;
3736 404c43c4 2018-06-21 stsp
3737 0c06baac 2019-02-05 stsp if (worktree) {
3738 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3739 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3740 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3741 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3742 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3743 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3744 6efaaa2d 2019-02-05 stsp path) == -1) {
3745 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3746 0c06baac 2019-02-05 stsp goto done;
3747 0c06baac 2019-02-05 stsp }
3748 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3749 0c06baac 2019-02-05 stsp free(p);
3750 0c06baac 2019-02-05 stsp } else {
3751 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3752 0c06baac 2019-02-05 stsp }
3753 0c06baac 2019-02-05 stsp if (error)
3754 66bea077 2018-08-02 stsp goto done;
3755 66bea077 2018-08-02 stsp
3756 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3757 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3758 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3759 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3760 404c43c4 2018-06-21 stsp if (error != NULL)
3761 66bea077 2018-08-02 stsp goto done;
3762 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3763 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3764 404c43c4 2018-06-21 stsp if (error != NULL)
3765 66bea077 2018-08-02 stsp goto done;
3766 404c43c4 2018-06-21 stsp } else {
3767 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3768 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3769 30837e32 2019-07-25 stsp if (error)
3770 66bea077 2018-08-02 stsp goto done;
3771 404c43c4 2018-06-21 stsp }
3772 404c43c4 2018-06-21 stsp
3773 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3774 28315671 2019-08-14 stsp if (error)
3775 28315671 2019-08-14 stsp goto done;
3776 28315671 2019-08-14 stsp
3777 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3778 28315671 2019-08-14 stsp if (error)
3779 28315671 2019-08-14 stsp goto done;
3780 28315671 2019-08-14 stsp
3781 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3782 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3783 28315671 2019-08-14 stsp goto done;
3784 28315671 2019-08-14 stsp }
3785 28315671 2019-08-14 stsp
3786 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3787 28315671 2019-08-14 stsp if (error)
3788 28315671 2019-08-14 stsp goto done;
3789 28315671 2019-08-14 stsp bca.f = got_opentemp();
3790 28315671 2019-08-14 stsp if (bca.f == NULL) {
3791 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3792 28315671 2019-08-14 stsp goto done;
3793 28315671 2019-08-14 stsp }
3794 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3795 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3796 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3797 28315671 2019-08-14 stsp goto done;
3798 28315671 2019-08-14 stsp
3799 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3800 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3801 b02560ec 2019-08-19 stsp bca.nlines--;
3802 b02560ec 2019-08-19 stsp
3803 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3804 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3805 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3806 28315671 2019-08-14 stsp goto done;
3807 28315671 2019-08-14 stsp }
3808 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3809 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3810 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3811 7ef28ff8 2019-08-14 stsp while (i > 0) {
3812 7ef28ff8 2019-08-14 stsp i /= 10;
3813 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3814 7ef28ff8 2019-08-14 stsp }
3815 82f6abb8 2019-08-14 stsp bca.repo = repo;
3816 7ef28ff8 2019-08-14 stsp
3817 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3818 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3819 404c43c4 2018-06-21 stsp done:
3820 66bea077 2018-08-02 stsp free(in_repo_path);
3821 66bea077 2018-08-02 stsp free(repo_path);
3822 66bea077 2018-08-02 stsp free(cwd);
3823 404c43c4 2018-06-21 stsp free(commit_id);
3824 28315671 2019-08-14 stsp free(obj_id);
3825 28315671 2019-08-14 stsp if (blob)
3826 28315671 2019-08-14 stsp got_object_blob_close(blob);
3827 0c06baac 2019-02-05 stsp if (worktree)
3828 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3829 ad242220 2018-09-08 stsp if (repo) {
3830 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3831 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3832 ad242220 2018-09-08 stsp if (error == NULL)
3833 ad242220 2018-09-08 stsp error = repo_error;
3834 ad242220 2018-09-08 stsp }
3835 3affba96 2019-09-22 stsp if (bca.lines) {
3836 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3837 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3838 3affba96 2019-09-22 stsp free(bline->id_str);
3839 3affba96 2019-09-22 stsp free(bline->committer);
3840 3affba96 2019-09-22 stsp }
3841 3affba96 2019-09-22 stsp free(bca.lines);
3842 28315671 2019-08-14 stsp }
3843 28315671 2019-08-14 stsp free(bca.line_offsets);
3844 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3845 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3846 404c43c4 2018-06-21 stsp return error;
3847 5de5890b 2018-10-18 stsp }
3848 5de5890b 2018-10-18 stsp
3849 5de5890b 2018-10-18 stsp __dead static void
3850 5de5890b 2018-10-18 stsp usage_tree(void)
3851 5de5890b 2018-10-18 stsp {
3852 c1669e2e 2019-01-09 stsp fprintf(stderr,
3853 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3854 5de5890b 2018-10-18 stsp getprogname());
3855 5de5890b 2018-10-18 stsp exit(1);
3856 5de5890b 2018-10-18 stsp }
3857 5de5890b 2018-10-18 stsp
3858 c1669e2e 2019-01-09 stsp static void
3859 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3860 c1669e2e 2019-01-09 stsp const char *root_path)
3861 c1669e2e 2019-01-09 stsp {
3862 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3863 848d6979 2019-08-12 stsp const char *modestr = "";
3864 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3865 5de5890b 2018-10-18 stsp
3866 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3867 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3868 c1669e2e 2019-01-09 stsp path++;
3869 c1669e2e 2019-01-09 stsp
3870 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3871 63c5ca5d 2019-08-24 stsp modestr = "$";
3872 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3873 848d6979 2019-08-12 stsp modestr = "@";
3874 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3875 848d6979 2019-08-12 stsp modestr = "/";
3876 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3877 848d6979 2019-08-12 stsp modestr = "*";
3878 848d6979 2019-08-12 stsp
3879 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3880 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3881 c1669e2e 2019-01-09 stsp }
3882 c1669e2e 2019-01-09 stsp
3883 5de5890b 2018-10-18 stsp static const struct got_error *
3884 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3885 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3886 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3887 5de5890b 2018-10-18 stsp {
3888 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3889 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3890 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3891 56e0773d 2019-11-28 stsp int nentries, i;
3892 5de5890b 2018-10-18 stsp
3893 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3894 5de5890b 2018-10-18 stsp if (err)
3895 5de5890b 2018-10-18 stsp goto done;
3896 5de5890b 2018-10-18 stsp
3897 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3898 5de5890b 2018-10-18 stsp if (err)
3899 5de5890b 2018-10-18 stsp goto done;
3900 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3901 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3902 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3903 5de5890b 2018-10-18 stsp char *id = NULL;
3904 84453469 2018-11-11 stsp
3905 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3906 84453469 2018-11-11 stsp break;
3907 84453469 2018-11-11 stsp
3908 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3909 5de5890b 2018-10-18 stsp if (show_ids) {
3910 5de5890b 2018-10-18 stsp char *id_str;
3911 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3912 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3913 5de5890b 2018-10-18 stsp if (err)
3914 5de5890b 2018-10-18 stsp goto done;
3915 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3916 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3917 5de5890b 2018-10-18 stsp free(id_str);
3918 5de5890b 2018-10-18 stsp goto done;
3919 5de5890b 2018-10-18 stsp }
3920 5de5890b 2018-10-18 stsp free(id_str);
3921 5de5890b 2018-10-18 stsp }
3922 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3923 5de5890b 2018-10-18 stsp free(id);
3924 c1669e2e 2019-01-09 stsp
3925 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3926 c1669e2e 2019-01-09 stsp char *child_path;
3927 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3928 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3929 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3930 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3931 c1669e2e 2019-01-09 stsp goto done;
3932 c1669e2e 2019-01-09 stsp }
3933 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3934 c1669e2e 2019-01-09 stsp root_path, repo);
3935 c1669e2e 2019-01-09 stsp free(child_path);
3936 c1669e2e 2019-01-09 stsp if (err)
3937 c1669e2e 2019-01-09 stsp goto done;
3938 c1669e2e 2019-01-09 stsp }
3939 5de5890b 2018-10-18 stsp }
3940 5de5890b 2018-10-18 stsp done:
3941 5de5890b 2018-10-18 stsp if (tree)
3942 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3943 5de5890b 2018-10-18 stsp free(tree_id);
3944 5de5890b 2018-10-18 stsp return err;
3945 404c43c4 2018-06-21 stsp }
3946 404c43c4 2018-06-21 stsp
3947 5de5890b 2018-10-18 stsp static const struct got_error *
3948 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3949 5de5890b 2018-10-18 stsp {
3950 5de5890b 2018-10-18 stsp const struct got_error *error;
3951 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3952 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3953 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
3954 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3955 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3956 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3957 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3958 5de5890b 2018-10-18 stsp int ch;
3959 5de5890b 2018-10-18 stsp
3960 5de5890b 2018-10-18 stsp #ifndef PROFILE
3961 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3962 0f8d269b 2019-01-04 stsp NULL) == -1)
3963 5de5890b 2018-10-18 stsp err(1, "pledge");
3964 5de5890b 2018-10-18 stsp #endif
3965 5de5890b 2018-10-18 stsp
3966 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3967 5de5890b 2018-10-18 stsp switch (ch) {
3968 5de5890b 2018-10-18 stsp case 'c':
3969 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3970 5de5890b 2018-10-18 stsp break;
3971 5de5890b 2018-10-18 stsp case 'r':
3972 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3973 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3974 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3975 9ba1d308 2019-10-21 stsp optarg);
3976 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3977 5de5890b 2018-10-18 stsp break;
3978 5de5890b 2018-10-18 stsp case 'i':
3979 5de5890b 2018-10-18 stsp show_ids = 1;
3980 5de5890b 2018-10-18 stsp break;
3981 c1669e2e 2019-01-09 stsp case 'R':
3982 c1669e2e 2019-01-09 stsp recurse = 1;
3983 c1669e2e 2019-01-09 stsp break;
3984 5de5890b 2018-10-18 stsp default:
3985 2deda0b9 2019-03-07 stsp usage_tree();
3986 5de5890b 2018-10-18 stsp /* NOTREACHED */
3987 5de5890b 2018-10-18 stsp }
3988 5de5890b 2018-10-18 stsp }
3989 5de5890b 2018-10-18 stsp
3990 5de5890b 2018-10-18 stsp argc -= optind;
3991 5de5890b 2018-10-18 stsp argv += optind;
3992 5de5890b 2018-10-18 stsp
3993 5de5890b 2018-10-18 stsp if (argc == 1)
3994 5de5890b 2018-10-18 stsp path = argv[0];
3995 5de5890b 2018-10-18 stsp else if (argc > 1)
3996 5de5890b 2018-10-18 stsp usage_tree();
3997 5de5890b 2018-10-18 stsp else
3998 7a2c19d6 2019-02-05 stsp path = NULL;
3999 7a2c19d6 2019-02-05 stsp
4000 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4001 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4002 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4003 9bf7a39b 2019-02-05 stsp goto done;
4004 9bf7a39b 2019-02-05 stsp }
4005 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4006 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4007 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4008 7a2c19d6 2019-02-05 stsp goto done;
4009 7a2c19d6 2019-02-05 stsp else
4010 7a2c19d6 2019-02-05 stsp error = NULL;
4011 7a2c19d6 2019-02-05 stsp if (worktree) {
4012 7a2c19d6 2019-02-05 stsp repo_path =
4013 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4014 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4015 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4016 7a2c19d6 2019-02-05 stsp if (error)
4017 7a2c19d6 2019-02-05 stsp goto done;
4018 7a2c19d6 2019-02-05 stsp } else {
4019 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4020 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4021 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4022 7a2c19d6 2019-02-05 stsp goto done;
4023 7a2c19d6 2019-02-05 stsp }
4024 5de5890b 2018-10-18 stsp }
4025 5de5890b 2018-10-18 stsp }
4026 5de5890b 2018-10-18 stsp
4027 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4028 5de5890b 2018-10-18 stsp if (error != NULL)
4029 c02c541e 2019-03-29 stsp goto done;
4030 c02c541e 2019-03-29 stsp
4031 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4032 c02c541e 2019-03-29 stsp if (error)
4033 5de5890b 2018-10-18 stsp goto done;
4034 5de5890b 2018-10-18 stsp
4035 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4036 9bf7a39b 2019-02-05 stsp if (worktree) {
4037 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4038 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4039 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4040 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4041 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4042 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4043 9bf7a39b 2019-02-05 stsp goto done;
4044 9bf7a39b 2019-02-05 stsp }
4045 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4046 9bf7a39b 2019-02-05 stsp free(p);
4047 9bf7a39b 2019-02-05 stsp if (error)
4048 9bf7a39b 2019-02-05 stsp goto done;
4049 9bf7a39b 2019-02-05 stsp } else
4050 9bf7a39b 2019-02-05 stsp path = "/";
4051 9bf7a39b 2019-02-05 stsp }
4052 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4053 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4054 9bf7a39b 2019-02-05 stsp if (error != NULL)
4055 9bf7a39b 2019-02-05 stsp goto done;
4056 9bf7a39b 2019-02-05 stsp }
4057 5de5890b 2018-10-18 stsp
4058 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4059 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4060 4e0a20a4 2020-03-23 tracey if (worktree)
4061 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4062 4e0a20a4 2020-03-23 tracey else
4063 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4064 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4065 5de5890b 2018-10-18 stsp if (error != NULL)
4066 5de5890b 2018-10-18 stsp goto done;
4067 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4068 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4069 5de5890b 2018-10-18 stsp if (error != NULL)
4070 5de5890b 2018-10-18 stsp goto done;
4071 5de5890b 2018-10-18 stsp } else {
4072 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4073 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4074 30837e32 2019-07-25 stsp if (error)
4075 5de5890b 2018-10-18 stsp goto done;
4076 5de5890b 2018-10-18 stsp }
4077 5de5890b 2018-10-18 stsp
4078 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4079 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4080 5de5890b 2018-10-18 stsp done:
4081 5de5890b 2018-10-18 stsp free(in_repo_path);
4082 5de5890b 2018-10-18 stsp free(repo_path);
4083 5de5890b 2018-10-18 stsp free(cwd);
4084 5de5890b 2018-10-18 stsp free(commit_id);
4085 7a2c19d6 2019-02-05 stsp if (worktree)
4086 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4087 5de5890b 2018-10-18 stsp if (repo) {
4088 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4089 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4090 5de5890b 2018-10-18 stsp if (error == NULL)
4091 5de5890b 2018-10-18 stsp error = repo_error;
4092 5de5890b 2018-10-18 stsp }
4093 5de5890b 2018-10-18 stsp return error;
4094 5de5890b 2018-10-18 stsp }
4095 5de5890b 2018-10-18 stsp
4096 6bad629b 2019-02-04 stsp __dead static void
4097 6bad629b 2019-02-04 stsp usage_status(void)
4098 6bad629b 2019-02-04 stsp {
4099 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4100 6bad629b 2019-02-04 stsp exit(1);
4101 6bad629b 2019-02-04 stsp }
4102 5c860e29 2018-03-12 stsp
4103 b72f483a 2019-02-05 stsp static const struct got_error *
4104 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4105 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4106 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4107 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4108 6bad629b 2019-02-04 stsp {
4109 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4110 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4111 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4112 b72f483a 2019-02-05 stsp return NULL;
4113 6bad629b 2019-02-04 stsp }
4114 5c860e29 2018-03-12 stsp
4115 6bad629b 2019-02-04 stsp static const struct got_error *
4116 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4117 6bad629b 2019-02-04 stsp {
4118 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4119 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4120 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4121 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4122 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4123 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4124 a5edda0a 2019-07-27 stsp int ch;
4125 5c860e29 2018-03-12 stsp
4126 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4127 72ea6654 2019-07-27 stsp
4128 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4129 6bad629b 2019-02-04 stsp switch (ch) {
4130 5c860e29 2018-03-12 stsp default:
4131 2deda0b9 2019-03-07 stsp usage_status();
4132 6bad629b 2019-02-04 stsp /* NOTREACHED */
4133 5c860e29 2018-03-12 stsp }
4134 5c860e29 2018-03-12 stsp }
4135 5c860e29 2018-03-12 stsp
4136 6bad629b 2019-02-04 stsp argc -= optind;
4137 6bad629b 2019-02-04 stsp argv += optind;
4138 5c860e29 2018-03-12 stsp
4139 6bad629b 2019-02-04 stsp #ifndef PROFILE
4140 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4141 6bad629b 2019-02-04 stsp NULL) == -1)
4142 6bad629b 2019-02-04 stsp err(1, "pledge");
4143 f42b1b34 2018-03-12 stsp #endif
4144 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4145 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4146 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4147 927df6b7 2019-02-10 stsp goto done;
4148 927df6b7 2019-02-10 stsp }
4149 927df6b7 2019-02-10 stsp
4150 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4151 927df6b7 2019-02-10 stsp if (error != NULL)
4152 a5edda0a 2019-07-27 stsp goto done;
4153 6bad629b 2019-02-04 stsp
4154 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4155 c9956ddf 2019-09-08 stsp NULL);
4156 6bad629b 2019-02-04 stsp if (error != NULL)
4157 6bad629b 2019-02-04 stsp goto done;
4158 6bad629b 2019-02-04 stsp
4159 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4160 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4161 087fb88c 2019-08-04 stsp if (error)
4162 087fb88c 2019-08-04 stsp goto done;
4163 087fb88c 2019-08-04 stsp
4164 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4165 6bad629b 2019-02-04 stsp if (error)
4166 6bad629b 2019-02-04 stsp goto done;
4167 6bad629b 2019-02-04 stsp
4168 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4169 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4170 6bad629b 2019-02-04 stsp done:
4171 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4172 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4173 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4174 927df6b7 2019-02-10 stsp free(cwd);
4175 d0eebce4 2019-03-11 stsp return error;
4176 d0eebce4 2019-03-11 stsp }
4177 d0eebce4 2019-03-11 stsp
4178 d0eebce4 2019-03-11 stsp __dead static void
4179 d0eebce4 2019-03-11 stsp usage_ref(void)
4180 d0eebce4 2019-03-11 stsp {
4181 d0eebce4 2019-03-11 stsp fprintf(stderr,
4182 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4183 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4184 d0eebce4 2019-03-11 stsp getprogname());
4185 d0eebce4 2019-03-11 stsp exit(1);
4186 d0eebce4 2019-03-11 stsp }
4187 d0eebce4 2019-03-11 stsp
4188 d0eebce4 2019-03-11 stsp static const struct got_error *
4189 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4190 d0eebce4 2019-03-11 stsp {
4191 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4192 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4193 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4194 d0eebce4 2019-03-11 stsp
4195 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4196 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4197 d0eebce4 2019-03-11 stsp if (err)
4198 d0eebce4 2019-03-11 stsp return err;
4199 d0eebce4 2019-03-11 stsp
4200 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4201 d0eebce4 2019-03-11 stsp char *refstr;
4202 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4203 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4204 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4205 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4206 d0eebce4 2019-03-11 stsp free(refstr);
4207 d0eebce4 2019-03-11 stsp }
4208 d0eebce4 2019-03-11 stsp
4209 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4210 d0eebce4 2019-03-11 stsp return NULL;
4211 d0eebce4 2019-03-11 stsp }
4212 d0eebce4 2019-03-11 stsp
4213 d0eebce4 2019-03-11 stsp static const struct got_error *
4214 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4215 d0eebce4 2019-03-11 stsp {
4216 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4217 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4218 d0eebce4 2019-03-11 stsp
4219 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4220 d0eebce4 2019-03-11 stsp if (err)
4221 d0eebce4 2019-03-11 stsp return err;
4222 d0eebce4 2019-03-11 stsp
4223 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4224 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4225 d0eebce4 2019-03-11 stsp return err;
4226 d0eebce4 2019-03-11 stsp }
4227 d0eebce4 2019-03-11 stsp
4228 d0eebce4 2019-03-11 stsp static const struct got_error *
4229 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4230 d0eebce4 2019-03-11 stsp {
4231 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4232 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4233 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4234 d1644381 2019-07-14 stsp
4235 d1644381 2019-07-14 stsp /*
4236 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4237 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4238 d1644381 2019-07-14 stsp * an unintended typo.
4239 d1644381 2019-07-14 stsp */
4240 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4241 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4242 d0eebce4 2019-03-11 stsp
4243 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4244 dd88155e 2019-06-29 stsp repo);
4245 d83d9d5c 2019-05-13 stsp if (err) {
4246 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4247 d0eebce4 2019-03-11 stsp
4248 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4249 d83d9d5c 2019-05-13 stsp return err;
4250 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4251 d83d9d5c 2019-05-13 stsp if (err)
4252 d83d9d5c 2019-05-13 stsp return err;
4253 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4254 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4255 d83d9d5c 2019-05-13 stsp if (err)
4256 d83d9d5c 2019-05-13 stsp return err;
4257 d83d9d5c 2019-05-13 stsp }
4258 d83d9d5c 2019-05-13 stsp
4259 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4260 d0eebce4 2019-03-11 stsp if (err)
4261 d0eebce4 2019-03-11 stsp goto done;
4262 d0eebce4 2019-03-11 stsp
4263 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4264 d0eebce4 2019-03-11 stsp done:
4265 d0eebce4 2019-03-11 stsp if (ref)
4266 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4267 d0eebce4 2019-03-11 stsp free(id);
4268 d1c1ae5f 2019-08-12 stsp return err;
4269 d1c1ae5f 2019-08-12 stsp }
4270 d1c1ae5f 2019-08-12 stsp
4271 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4272 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4273 d1c1ae5f 2019-08-12 stsp {
4274 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4275 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4276 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4277 d1c1ae5f 2019-08-12 stsp
4278 d1c1ae5f 2019-08-12 stsp /*
4279 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4280 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4281 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4282 d1c1ae5f 2019-08-12 stsp */
4283 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4284 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4285 d1c1ae5f 2019-08-12 stsp
4286 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4287 d1c1ae5f 2019-08-12 stsp if (err)
4288 d1c1ae5f 2019-08-12 stsp return err;
4289 d1c1ae5f 2019-08-12 stsp
4290 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4291 d1c1ae5f 2019-08-12 stsp if (err)
4292 d1c1ae5f 2019-08-12 stsp goto done;
4293 d1c1ae5f 2019-08-12 stsp
4294 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4295 d1c1ae5f 2019-08-12 stsp done:
4296 d1c1ae5f 2019-08-12 stsp if (target_ref)
4297 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4298 d1c1ae5f 2019-08-12 stsp if (ref)
4299 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4300 d0eebce4 2019-03-11 stsp return err;
4301 d0eebce4 2019-03-11 stsp }
4302 d0eebce4 2019-03-11 stsp
4303 d0eebce4 2019-03-11 stsp static const struct got_error *
4304 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4305 d0eebce4 2019-03-11 stsp {
4306 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4307 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4308 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4309 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4310 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4311 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4312 b2070a3f 2020-03-22 stsp char *refname = NULL;
4313 d0eebce4 2019-03-11 stsp
4314 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4315 d0eebce4 2019-03-11 stsp switch (ch) {
4316 e31abbf2 2020-03-22 stsp case 'c':
4317 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4318 e31abbf2 2020-03-22 stsp break;
4319 d0eebce4 2019-03-11 stsp case 'd':
4320 e31abbf2 2020-03-22 stsp do_delete = 1;
4321 d0eebce4 2019-03-11 stsp break;
4322 d0eebce4 2019-03-11 stsp case 'r':
4323 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4324 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4325 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4326 9ba1d308 2019-10-21 stsp optarg);
4327 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4328 d0eebce4 2019-03-11 stsp break;
4329 d0eebce4 2019-03-11 stsp case 'l':
4330 d0eebce4 2019-03-11 stsp do_list = 1;
4331 d1c1ae5f 2019-08-12 stsp break;
4332 d1c1ae5f 2019-08-12 stsp case 's':
4333 e31abbf2 2020-03-22 stsp symref_target = optarg;
4334 d0eebce4 2019-03-11 stsp break;
4335 d0eebce4 2019-03-11 stsp default:
4336 d0eebce4 2019-03-11 stsp usage_ref();
4337 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4338 d0eebce4 2019-03-11 stsp }
4339 d0eebce4 2019-03-11 stsp }
4340 d0eebce4 2019-03-11 stsp
4341 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4342 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4343 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4344 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4345 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4346 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4347 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4348 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4349 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4350 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4351 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4352 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4353 d0eebce4 2019-03-11 stsp
4354 d0eebce4 2019-03-11 stsp argc -= optind;
4355 d0eebce4 2019-03-11 stsp argv += optind;
4356 d0eebce4 2019-03-11 stsp
4357 e31abbf2 2020-03-22 stsp if (do_list) {
4358 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4359 d0eebce4 2019-03-11 stsp usage_ref();
4360 b2070a3f 2020-03-22 stsp if (argc == 1) {
4361 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4362 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4363 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4364 b2070a3f 2020-03-22 stsp goto done;
4365 b2070a3f 2020-03-22 stsp }
4366 b2070a3f 2020-03-22 stsp }
4367 e31abbf2 2020-03-22 stsp } else {
4368 e31abbf2 2020-03-22 stsp if (argc != 1)
4369 e31abbf2 2020-03-22 stsp usage_ref();
4370 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4371 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4372 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4373 b2070a3f 2020-03-22 stsp goto done;
4374 b2070a3f 2020-03-22 stsp }
4375 e31abbf2 2020-03-22 stsp }
4376 b2070a3f 2020-03-22 stsp
4377 b2070a3f 2020-03-22 stsp if (refname)
4378 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4379 d0eebce4 2019-03-11 stsp
4380 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4381 e0b57350 2019-03-12 stsp if (do_list) {
4382 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4383 e0b57350 2019-03-12 stsp NULL) == -1)
4384 e0b57350 2019-03-12 stsp err(1, "pledge");
4385 e0b57350 2019-03-12 stsp } else {
4386 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4387 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4388 e0b57350 2019-03-12 stsp err(1, "pledge");
4389 e0b57350 2019-03-12 stsp }
4390 d0eebce4 2019-03-11 stsp #endif
4391 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4392 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4393 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4394 d0eebce4 2019-03-11 stsp goto done;
4395 d0eebce4 2019-03-11 stsp }
4396 d0eebce4 2019-03-11 stsp
4397 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4398 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4399 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4400 d0eebce4 2019-03-11 stsp goto done;
4401 d0eebce4 2019-03-11 stsp else
4402 d0eebce4 2019-03-11 stsp error = NULL;
4403 d0eebce4 2019-03-11 stsp if (worktree) {
4404 d0eebce4 2019-03-11 stsp repo_path =
4405 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4406 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4407 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4408 d0eebce4 2019-03-11 stsp if (error)
4409 d0eebce4 2019-03-11 stsp goto done;
4410 d0eebce4 2019-03-11 stsp } else {
4411 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4412 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4413 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4414 d0eebce4 2019-03-11 stsp goto done;
4415 d0eebce4 2019-03-11 stsp }
4416 d0eebce4 2019-03-11 stsp }
4417 d0eebce4 2019-03-11 stsp }
4418 d0eebce4 2019-03-11 stsp
4419 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4420 d0eebce4 2019-03-11 stsp if (error != NULL)
4421 d0eebce4 2019-03-11 stsp goto done;
4422 d0eebce4 2019-03-11 stsp
4423 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4424 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4425 c02c541e 2019-03-29 stsp if (error)
4426 c02c541e 2019-03-29 stsp goto done;
4427 c02c541e 2019-03-29 stsp
4428 d0eebce4 2019-03-11 stsp if (do_list)
4429 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4430 e31abbf2 2020-03-22 stsp else if (do_delete)
4431 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4432 e31abbf2 2020-03-22 stsp else if (symref_target)
4433 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4434 e31abbf2 2020-03-22 stsp else {
4435 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4436 e31abbf2 2020-03-22 stsp usage_ref();
4437 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4438 e31abbf2 2020-03-22 stsp }
4439 4e759de4 2019-06-26 stsp done:
4440 b2070a3f 2020-03-22 stsp free(refname);
4441 4e759de4 2019-06-26 stsp if (repo)
4442 4e759de4 2019-06-26 stsp got_repo_close(repo);
4443 4e759de4 2019-06-26 stsp if (worktree)
4444 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4445 4e759de4 2019-06-26 stsp free(cwd);
4446 4e759de4 2019-06-26 stsp free(repo_path);
4447 4e759de4 2019-06-26 stsp return error;
4448 4e759de4 2019-06-26 stsp }
4449 4e759de4 2019-06-26 stsp
4450 4e759de4 2019-06-26 stsp __dead static void
4451 4e759de4 2019-06-26 stsp usage_branch(void)
4452 4e759de4 2019-06-26 stsp {
4453 4e759de4 2019-06-26 stsp fprintf(stderr,
4454 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4455 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4456 4e759de4 2019-06-26 stsp exit(1);
4457 4e759de4 2019-06-26 stsp }
4458 4e759de4 2019-06-26 stsp
4459 4e759de4 2019-06-26 stsp static const struct got_error *
4460 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4461 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4462 4e99b47e 2019-10-04 stsp {
4463 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4464 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4465 4e99b47e 2019-10-04 stsp char *refstr;
4466 4e99b47e 2019-10-04 stsp
4467 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4468 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4469 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4470 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4471 4e99b47e 2019-10-04 stsp
4472 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4473 4e99b47e 2019-10-04 stsp if (err)
4474 4e99b47e 2019-10-04 stsp return err;
4475 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4476 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4477 4e99b47e 2019-10-04 stsp marker = "* ";
4478 4e99b47e 2019-10-04 stsp else
4479 4e99b47e 2019-10-04 stsp marker = "~ ";
4480 4e99b47e 2019-10-04 stsp free(id);
4481 4e99b47e 2019-10-04 stsp }
4482 4e99b47e 2019-10-04 stsp
4483 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4484 4e99b47e 2019-10-04 stsp refname += 11;
4485 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4486 4e99b47e 2019-10-04 stsp refname += 18;
4487 4e99b47e 2019-10-04 stsp
4488 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4489 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4490 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4491 4e99b47e 2019-10-04 stsp
4492 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4493 4e99b47e 2019-10-04 stsp free(refstr);
4494 4e99b47e 2019-10-04 stsp return NULL;
4495 4e99b47e 2019-10-04 stsp }
4496 4e99b47e 2019-10-04 stsp
4497 4e99b47e 2019-10-04 stsp static const struct got_error *
4498 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4499 ad89fa31 2019-10-04 stsp {
4500 ad89fa31 2019-10-04 stsp const char *refname;
4501 ad89fa31 2019-10-04 stsp
4502 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4503 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4504 ad89fa31 2019-10-04 stsp
4505 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4506 ad89fa31 2019-10-04 stsp
4507 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4508 ad89fa31 2019-10-04 stsp refname += 11;
4509 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4510 ad89fa31 2019-10-04 stsp refname += 18;
4511 ad89fa31 2019-10-04 stsp
4512 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4513 ad89fa31 2019-10-04 stsp
4514 ad89fa31 2019-10-04 stsp return NULL;
4515 ad89fa31 2019-10-04 stsp }
4516 ad89fa31 2019-10-04 stsp
4517 ad89fa31 2019-10-04 stsp static const struct got_error *
4518 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4519 4e759de4 2019-06-26 stsp {
4520 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4521 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4522 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4523 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4524 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4525 4e759de4 2019-06-26 stsp
4526 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4527 ba882ee3 2019-07-11 stsp
4528 4e99b47e 2019-10-04 stsp if (worktree) {
4529 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4530 4e99b47e 2019-10-04 stsp worktree);
4531 4e99b47e 2019-10-04 stsp if (err)
4532 4e99b47e 2019-10-04 stsp return err;
4533 4e99b47e 2019-10-04 stsp
4534 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4535 4e99b47e 2019-10-04 stsp worktree);
4536 4e99b47e 2019-10-04 stsp if (err)
4537 4e99b47e 2019-10-04 stsp return err;
4538 4e99b47e 2019-10-04 stsp
4539 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4540 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4541 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4542 4e99b47e 2019-10-04 stsp if (err)
4543 4e99b47e 2019-10-04 stsp return err;
4544 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4545 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4546 4e99b47e 2019-10-04 stsp }
4547 4e99b47e 2019-10-04 stsp }
4548 4e99b47e 2019-10-04 stsp
4549 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4550 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4551 4e759de4 2019-06-26 stsp if (err)
4552 4e759de4 2019-06-26 stsp return err;
4553 4e759de4 2019-06-26 stsp
4554 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4555 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4556 4e759de4 2019-06-26 stsp
4557 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4558 4e759de4 2019-06-26 stsp return NULL;
4559 4e759de4 2019-06-26 stsp }
4560 4e759de4 2019-06-26 stsp
4561 4e759de4 2019-06-26 stsp static const struct got_error *
4562 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4563 45cd4e47 2019-08-25 stsp const char *branch_name)
4564 4e759de4 2019-06-26 stsp {
4565 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4566 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4567 4e759de4 2019-06-26 stsp char *refname;
4568 4e759de4 2019-06-26 stsp
4569 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4570 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
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)
4574 4e759de4 2019-06-26 stsp goto done;
4575 4e759de4 2019-06-26 stsp
4576 45cd4e47 2019-08-25 stsp if (worktree &&
4577 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4578 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4579 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4580 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4581 45cd4e47 2019-08-25 stsp goto done;
4582 45cd4e47 2019-08-25 stsp }
4583 45cd4e47 2019-08-25 stsp
4584 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4585 4e759de4 2019-06-26 stsp done:
4586 45cd4e47 2019-08-25 stsp if (ref)
4587 45cd4e47 2019-08-25 stsp got_ref_close(ref);
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 add_branch(struct got_repository *repo, const char *branch_name,
4594 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4595 4e759de4 2019-06-26 stsp {
4596 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4597 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4598 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4599 d3f84d51 2019-07-11 stsp
4600 d3f84d51 2019-07-11 stsp /*
4601 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4602 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4603 d3f84d51 2019-07-11 stsp * an unintended typo.
4604 d3f84d51 2019-07-11 stsp */
4605 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4606 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4607 4e759de4 2019-06-26 stsp
4608 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4609 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4610 4e759de4 2019-06-26 stsp goto done;
4611 4e759de4 2019-06-26 stsp }
4612 4e759de4 2019-06-26 stsp
4613 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4614 4e759de4 2019-06-26 stsp if (err == NULL) {
4615 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4616 4e759de4 2019-06-26 stsp goto done;
4617 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4618 4e759de4 2019-06-26 stsp goto done;
4619 4e759de4 2019-06-26 stsp
4620 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4621 4e759de4 2019-06-26 stsp if (err)
4622 4e759de4 2019-06-26 stsp goto done;
4623 4e759de4 2019-06-26 stsp
4624 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4625 d0eebce4 2019-03-11 stsp done:
4626 4e759de4 2019-06-26 stsp if (ref)
4627 4e759de4 2019-06-26 stsp got_ref_close(ref);
4628 4e759de4 2019-06-26 stsp free(base_refname);
4629 4e759de4 2019-06-26 stsp free(refname);
4630 4e759de4 2019-06-26 stsp return err;
4631 4e759de4 2019-06-26 stsp }
4632 4e759de4 2019-06-26 stsp
4633 4e759de4 2019-06-26 stsp static const struct got_error *
4634 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4635 4e759de4 2019-06-26 stsp {
4636 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4637 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4638 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4639 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4640 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4641 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4642 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4643 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4644 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4645 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4646 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4647 4e759de4 2019-06-26 stsp
4648 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4649 da76fce2 2020-02-24 stsp
4650 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4651 4e759de4 2019-06-26 stsp switch (ch) {
4652 a74f7e83 2019-11-10 stsp case 'c':
4653 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4654 a74f7e83 2019-11-10 stsp break;
4655 4e759de4 2019-06-26 stsp case 'd':
4656 4e759de4 2019-06-26 stsp delref = optarg;
4657 4e759de4 2019-06-26 stsp break;
4658 4e759de4 2019-06-26 stsp case 'r':
4659 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4660 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4661 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4662 9ba1d308 2019-10-21 stsp optarg);
4663 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4664 4e759de4 2019-06-26 stsp break;
4665 4e759de4 2019-06-26 stsp case 'l':
4666 4e759de4 2019-06-26 stsp do_list = 1;
4667 da76fce2 2020-02-24 stsp break;
4668 da76fce2 2020-02-24 stsp case 'n':
4669 da76fce2 2020-02-24 stsp do_update = 0;
4670 4e759de4 2019-06-26 stsp break;
4671 4e759de4 2019-06-26 stsp default:
4672 4e759de4 2019-06-26 stsp usage_branch();
4673 4e759de4 2019-06-26 stsp /* NOTREACHED */
4674 4e759de4 2019-06-26 stsp }
4675 4e759de4 2019-06-26 stsp }
4676 4e759de4 2019-06-26 stsp
4677 4e759de4 2019-06-26 stsp if (do_list && delref)
4678 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
4679 4e759de4 2019-06-26 stsp
4680 4e759de4 2019-06-26 stsp argc -= optind;
4681 4e759de4 2019-06-26 stsp argv += optind;
4682 ad89fa31 2019-10-04 stsp
4683 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4684 ad89fa31 2019-10-04 stsp do_show = 1;
4685 4e759de4 2019-06-26 stsp
4686 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4687 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4688 a74f7e83 2019-11-10 stsp
4689 4e759de4 2019-06-26 stsp if (do_list || delref) {
4690 4e759de4 2019-06-26 stsp if (argc > 0)
4691 4e759de4 2019-06-26 stsp usage_branch();
4692 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4693 4e759de4 2019-06-26 stsp usage_branch();
4694 4e759de4 2019-06-26 stsp
4695 4e759de4 2019-06-26 stsp #ifndef PROFILE
4696 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4697 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4698 4e759de4 2019-06-26 stsp NULL) == -1)
4699 4e759de4 2019-06-26 stsp err(1, "pledge");
4700 4e759de4 2019-06-26 stsp } else {
4701 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4702 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4703 4e759de4 2019-06-26 stsp err(1, "pledge");
4704 4e759de4 2019-06-26 stsp }
4705 4e759de4 2019-06-26 stsp #endif
4706 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4707 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4708 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4709 4e759de4 2019-06-26 stsp goto done;
4710 4e759de4 2019-06-26 stsp }
4711 4e759de4 2019-06-26 stsp
4712 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4713 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4714 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4715 4e759de4 2019-06-26 stsp goto done;
4716 4e759de4 2019-06-26 stsp else
4717 4e759de4 2019-06-26 stsp error = NULL;
4718 4e759de4 2019-06-26 stsp if (worktree) {
4719 4e759de4 2019-06-26 stsp repo_path =
4720 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4721 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4722 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4723 4e759de4 2019-06-26 stsp if (error)
4724 4e759de4 2019-06-26 stsp goto done;
4725 4e759de4 2019-06-26 stsp } else {
4726 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4727 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4728 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4729 4e759de4 2019-06-26 stsp goto done;
4730 4e759de4 2019-06-26 stsp }
4731 4e759de4 2019-06-26 stsp }
4732 4e759de4 2019-06-26 stsp }
4733 4e759de4 2019-06-26 stsp
4734 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4735 4e759de4 2019-06-26 stsp if (error != NULL)
4736 4e759de4 2019-06-26 stsp goto done;
4737 4e759de4 2019-06-26 stsp
4738 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4739 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4740 4e759de4 2019-06-26 stsp if (error)
4741 4e759de4 2019-06-26 stsp goto done;
4742 4e759de4 2019-06-26 stsp
4743 ad89fa31 2019-10-04 stsp if (do_show)
4744 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4745 ad89fa31 2019-10-04 stsp else if (do_list)
4746 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4747 4e759de4 2019-06-26 stsp else if (delref)
4748 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4749 4e759de4 2019-06-26 stsp else {
4750 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4751 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4752 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4753 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4754 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4755 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4756 a74f7e83 2019-11-10 stsp if (error)
4757 a74f7e83 2019-11-10 stsp goto done;
4758 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4759 da76fce2 2020-02-24 stsp if (error)
4760 da76fce2 2020-02-24 stsp goto done;
4761 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4762 da76fce2 2020-02-24 stsp int did_something = 0;
4763 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4764 da76fce2 2020-02-24 stsp
4765 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4766 da76fce2 2020-02-24 stsp if (error)
4767 da76fce2 2020-02-24 stsp goto done;
4768 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4769 da76fce2 2020-02-24 stsp worktree);
4770 da76fce2 2020-02-24 stsp if (error)
4771 da76fce2 2020-02-24 stsp goto done;
4772 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4773 da76fce2 2020-02-24 stsp == -1) {
4774 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4775 da76fce2 2020-02-24 stsp goto done;
4776 da76fce2 2020-02-24 stsp }
4777 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4778 da76fce2 2020-02-24 stsp free(branch_refname);
4779 da76fce2 2020-02-24 stsp if (error)
4780 da76fce2 2020-02-24 stsp goto done;
4781 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4782 da76fce2 2020-02-24 stsp repo);
4783 da76fce2 2020-02-24 stsp if (error)
4784 da76fce2 2020-02-24 stsp goto done;
4785 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4786 da76fce2 2020-02-24 stsp commit_id);
4787 da76fce2 2020-02-24 stsp if (error)
4788 da76fce2 2020-02-24 stsp goto done;
4789 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4790 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4791 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4792 da76fce2 2020-02-24 stsp if (error)
4793 da76fce2 2020-02-24 stsp goto done;
4794 da76fce2 2020-02-24 stsp if (did_something)
4795 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4796 da76fce2 2020-02-24 stsp }
4797 4e759de4 2019-06-26 stsp }
4798 4e759de4 2019-06-26 stsp done:
4799 da76fce2 2020-02-24 stsp if (ref)
4800 da76fce2 2020-02-24 stsp got_ref_close(ref);
4801 d0eebce4 2019-03-11 stsp if (repo)
4802 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4803 d0eebce4 2019-03-11 stsp if (worktree)
4804 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4805 d0eebce4 2019-03-11 stsp free(cwd);
4806 d0eebce4 2019-03-11 stsp free(repo_path);
4807 da76fce2 2020-02-24 stsp free(commit_id);
4808 da76fce2 2020-02-24 stsp free(commit_id_str);
4809 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4810 da76fce2 2020-02-24 stsp free((char *)pe->path);
4811 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4812 d00136be 2019-03-26 stsp return error;
4813 d00136be 2019-03-26 stsp }
4814 d00136be 2019-03-26 stsp
4815 8e7bd50a 2019-08-22 stsp
4816 d00136be 2019-03-26 stsp __dead static void
4817 8e7bd50a 2019-08-22 stsp usage_tag(void)
4818 8e7bd50a 2019-08-22 stsp {
4819 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4820 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4821 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4822 8e7bd50a 2019-08-22 stsp exit(1);
4823 b8bad2ba 2019-08-23 stsp }
4824 b8bad2ba 2019-08-23 stsp
4825 b8bad2ba 2019-08-23 stsp #if 0
4826 b8bad2ba 2019-08-23 stsp static const struct got_error *
4827 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4828 b8bad2ba 2019-08-23 stsp {
4829 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4830 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4831 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4832 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4833 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4834 b8bad2ba 2019-08-23 stsp
4835 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4836 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4837 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4838 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4839 b8bad2ba 2019-08-23 stsp if (err)
4840 b8bad2ba 2019-08-23 stsp return err;
4841 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4842 b8bad2ba 2019-08-23 stsp continue;
4843 b8bad2ba 2019-08-23 stsp } else {
4844 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4845 b8bad2ba 2019-08-23 stsp if (err)
4846 b8bad2ba 2019-08-23 stsp break;
4847 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4848 b8bad2ba 2019-08-23 stsp free(re_id);
4849 b8bad2ba 2019-08-23 stsp if (err)
4850 b8bad2ba 2019-08-23 stsp break;
4851 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4852 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4853 b8bad2ba 2019-08-23 stsp }
4854 b8bad2ba 2019-08-23 stsp
4855 b8bad2ba 2019-08-23 stsp while (se) {
4856 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4857 b8bad2ba 2019-08-23 stsp if (err)
4858 b8bad2ba 2019-08-23 stsp break;
4859 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4860 b8bad2ba 2019-08-23 stsp free(se_id);
4861 b8bad2ba 2019-08-23 stsp if (err)
4862 b8bad2ba 2019-08-23 stsp break;
4863 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4864 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4865 b8bad2ba 2019-08-23 stsp
4866 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4867 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4868 b8bad2ba 2019-08-23 stsp if (err)
4869 b8bad2ba 2019-08-23 stsp return err;
4870 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4871 b8bad2ba 2019-08-23 stsp break;
4872 b8bad2ba 2019-08-23 stsp }
4873 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4874 b8bad2ba 2019-08-23 stsp continue;
4875 b8bad2ba 2019-08-23 stsp }
4876 b8bad2ba 2019-08-23 stsp }
4877 b8bad2ba 2019-08-23 stsp done:
4878 b8bad2ba 2019-08-23 stsp return err;
4879 8e7bd50a 2019-08-22 stsp }
4880 b8bad2ba 2019-08-23 stsp #endif
4881 b8bad2ba 2019-08-23 stsp
4882 b8bad2ba 2019-08-23 stsp static const struct got_error *
4883 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4884 8e7bd50a 2019-08-22 stsp {
4885 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4886 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4887 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4888 8e7bd50a 2019-08-22 stsp
4889 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4890 8e7bd50a 2019-08-22 stsp
4891 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4892 8e7bd50a 2019-08-22 stsp if (err)
4893 8e7bd50a 2019-08-22 stsp return err;
4894 8e7bd50a 2019-08-22 stsp
4895 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4896 8e7bd50a 2019-08-22 stsp const char *refname;
4897 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4898 8e7bd50a 2019-08-22 stsp char datebuf[26];
4899 d4efa91b 2020-01-14 stsp const char *tagger;
4900 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4901 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4902 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4903 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4904 8e7bd50a 2019-08-22 stsp
4905 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4906 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4907 8e7bd50a 2019-08-22 stsp continue;
4908 8e7bd50a 2019-08-22 stsp refname += 10;
4909 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4910 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4911 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4912 8e7bd50a 2019-08-22 stsp break;
4913 8e7bd50a 2019-08-22 stsp }
4914 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4915 8e7bd50a 2019-08-22 stsp free(refstr);
4916 8e7bd50a 2019-08-22 stsp
4917 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4918 8e7bd50a 2019-08-22 stsp if (err)
4919 8e7bd50a 2019-08-22 stsp break;
4920 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4921 d4efa91b 2020-01-14 stsp if (err) {
4922 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4923 d4efa91b 2020-01-14 stsp free(id);
4924 d4efa91b 2020-01-14 stsp break;
4925 d4efa91b 2020-01-14 stsp }
4926 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4927 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4928 d4efa91b 2020-01-14 stsp if (err) {
4929 d4efa91b 2020-01-14 stsp free(id);
4930 d4efa91b 2020-01-14 stsp break;
4931 d4efa91b 2020-01-14 stsp }
4932 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4933 d4efa91b 2020-01-14 stsp tagger_time =
4934 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4935 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4936 d4efa91b 2020-01-14 stsp free(id);
4937 d4efa91b 2020-01-14 stsp if (err)
4938 d4efa91b 2020-01-14 stsp break;
4939 d4efa91b 2020-01-14 stsp } else {
4940 d4efa91b 2020-01-14 stsp free(id);
4941 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4942 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4943 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4944 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4945 d4efa91b 2020-01-14 stsp if (err)
4946 d4efa91b 2020-01-14 stsp break;
4947 d4efa91b 2020-01-14 stsp }
4948 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4949 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4950 2417344c 2019-08-23 stsp if (datestr)
4951 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4952 d4efa91b 2020-01-14 stsp if (commit)
4953 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4954 d4efa91b 2020-01-14 stsp else {
4955 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4956 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4957 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4958 d4efa91b 2020-01-14 stsp id_str);
4959 d4efa91b 2020-01-14 stsp break;
4960 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4961 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4962 d4efa91b 2020-01-14 stsp id_str);
4963 d4efa91b 2020-01-14 stsp break;
4964 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4965 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4966 d4efa91b 2020-01-14 stsp id_str);
4967 d4efa91b 2020-01-14 stsp break;
4968 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4969 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4970 d4efa91b 2020-01-14 stsp id_str);
4971 d4efa91b 2020-01-14 stsp break;
4972 d4efa91b 2020-01-14 stsp default:
4973 d4efa91b 2020-01-14 stsp break;
4974 d4efa91b 2020-01-14 stsp }
4975 8e7bd50a 2019-08-22 stsp }
4976 8e7bd50a 2019-08-22 stsp free(id_str);
4977 d4efa91b 2020-01-14 stsp if (commit) {
4978 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4979 d4efa91b 2020-01-14 stsp if (err)
4980 d4efa91b 2020-01-14 stsp break;
4981 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4982 d4efa91b 2020-01-14 stsp } else {
4983 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4984 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4985 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4986 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4987 d4efa91b 2020-01-14 stsp break;
4988 d4efa91b 2020-01-14 stsp }
4989 8e7bd50a 2019-08-22 stsp }
4990 8e7bd50a 2019-08-22 stsp
4991 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4992 8e7bd50a 2019-08-22 stsp do {
4993 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4994 8e7bd50a 2019-08-22 stsp if (line)
4995 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4996 8e7bd50a 2019-08-22 stsp } while (line);
4997 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4998 8e7bd50a 2019-08-22 stsp }
4999 8e7bd50a 2019-08-22 stsp
5000 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5001 8e7bd50a 2019-08-22 stsp return NULL;
5002 8e7bd50a 2019-08-22 stsp }
5003 8e7bd50a 2019-08-22 stsp
5004 8e7bd50a 2019-08-22 stsp static const struct got_error *
5005 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5006 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5007 8e7bd50a 2019-08-22 stsp {
5008 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5009 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5010 f372d5cd 2019-10-21 stsp char *editor = NULL;
5011 8e7bd50a 2019-08-22 stsp int fd = -1;
5012 8e7bd50a 2019-08-22 stsp
5013 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5014 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5015 8e7bd50a 2019-08-22 stsp goto done;
5016 8e7bd50a 2019-08-22 stsp }
5017 8e7bd50a 2019-08-22 stsp
5018 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5019 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5020 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5021 8e7bd50a 2019-08-22 stsp goto done;
5022 8e7bd50a 2019-08-22 stsp }
5023 8e7bd50a 2019-08-22 stsp
5024 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5025 8e7bd50a 2019-08-22 stsp if (err)
5026 8e7bd50a 2019-08-22 stsp goto done;
5027 8e7bd50a 2019-08-22 stsp
5028 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5029 8e7bd50a 2019-08-22 stsp close(fd);
5030 8e7bd50a 2019-08-22 stsp
5031 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5032 8e7bd50a 2019-08-22 stsp if (err)
5033 8e7bd50a 2019-08-22 stsp goto done;
5034 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5035 8e7bd50a 2019-08-22 stsp done:
5036 8e7bd50a 2019-08-22 stsp free(initial_content);
5037 8e7bd50a 2019-08-22 stsp free(template);
5038 8e7bd50a 2019-08-22 stsp free(editor);
5039 8e7bd50a 2019-08-22 stsp
5040 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5041 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5042 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5043 8e7bd50a 2019-08-22 stsp if (err) {
5044 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5045 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5046 8e7bd50a 2019-08-22 stsp }
5047 8e7bd50a 2019-08-22 stsp }
5048 8e7bd50a 2019-08-22 stsp return err;
5049 8e7bd50a 2019-08-22 stsp }
5050 8e7bd50a 2019-08-22 stsp
5051 8e7bd50a 2019-08-22 stsp static const struct got_error *
5052 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5053 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5054 8e7bd50a 2019-08-22 stsp {
5055 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5056 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5057 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5058 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5059 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5060 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5061 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5062 8e7bd50a 2019-08-22 stsp
5063 8e7bd50a 2019-08-22 stsp /*
5064 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5065 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5066 8e7bd50a 2019-08-22 stsp * an unintended typo.
5067 8e7bd50a 2019-08-22 stsp */
5068 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5069 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5070 8e7bd50a 2019-08-22 stsp
5071 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5072 8e7bd50a 2019-08-22 stsp if (err)
5073 8e7bd50a 2019-08-22 stsp return err;
5074 8e7bd50a 2019-08-22 stsp
5075 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5076 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5077 8e7bd50a 2019-08-22 stsp if (err)
5078 8e7bd50a 2019-08-22 stsp goto done;
5079 8e7bd50a 2019-08-22 stsp
5080 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5081 8e7bd50a 2019-08-22 stsp if (err)
5082 8e7bd50a 2019-08-22 stsp goto done;
5083 8e7bd50a 2019-08-22 stsp
5084 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5085 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5086 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5087 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5088 8e7bd50a 2019-08-22 stsp goto done;
5089 8e7bd50a 2019-08-22 stsp }
5090 8e7bd50a 2019-08-22 stsp tag_name += 10;
5091 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5092 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5093 8e7bd50a 2019-08-22 stsp goto done;
5094 8e7bd50a 2019-08-22 stsp }
5095 8e7bd50a 2019-08-22 stsp
5096 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5097 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5098 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5099 8e7bd50a 2019-08-22 stsp goto done;
5100 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5101 8e7bd50a 2019-08-22 stsp goto done;
5102 8e7bd50a 2019-08-22 stsp
5103 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5104 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5105 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5106 f372d5cd 2019-10-21 stsp if (err) {
5107 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5108 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5109 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5110 8e7bd50a 2019-08-22 stsp goto done;
5111 f372d5cd 2019-10-21 stsp }
5112 8e7bd50a 2019-08-22 stsp }
5113 8e7bd50a 2019-08-22 stsp
5114 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5115 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5116 f372d5cd 2019-10-21 stsp if (err) {
5117 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5118 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5119 8e7bd50a 2019-08-22 stsp goto done;
5120 f372d5cd 2019-10-21 stsp }
5121 8e7bd50a 2019-08-22 stsp
5122 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5123 f372d5cd 2019-10-21 stsp if (err) {
5124 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5125 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5126 8e7bd50a 2019-08-22 stsp goto done;
5127 f372d5cd 2019-10-21 stsp }
5128 8e7bd50a 2019-08-22 stsp
5129 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5130 f372d5cd 2019-10-21 stsp if (err) {
5131 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5132 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5133 f372d5cd 2019-10-21 stsp goto done;
5134 f372d5cd 2019-10-21 stsp }
5135 8e7bd50a 2019-08-22 stsp
5136 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5137 f372d5cd 2019-10-21 stsp if (err) {
5138 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5139 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5140 f372d5cd 2019-10-21 stsp goto done;
5141 8e7bd50a 2019-08-22 stsp }
5142 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5143 8e7bd50a 2019-08-22 stsp done:
5144 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5145 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5146 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5147 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5148 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5149 f372d5cd 2019-10-21 stsp free(tag_id_str);
5150 8e7bd50a 2019-08-22 stsp if (ref)
5151 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5152 8e7bd50a 2019-08-22 stsp free(commit_id);
5153 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5154 8e7bd50a 2019-08-22 stsp free(refname);
5155 8e7bd50a 2019-08-22 stsp free(tagmsg);
5156 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5157 aba9c984 2019-09-08 stsp free(tagger);
5158 8e7bd50a 2019-08-22 stsp return err;
5159 8e7bd50a 2019-08-22 stsp }
5160 8e7bd50a 2019-08-22 stsp
5161 8e7bd50a 2019-08-22 stsp static const struct got_error *
5162 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5163 8e7bd50a 2019-08-22 stsp {
5164 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5165 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5166 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5167 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5168 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5169 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5170 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5171 8e7bd50a 2019-08-22 stsp
5172 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5173 8e7bd50a 2019-08-22 stsp switch (ch) {
5174 80106605 2020-02-24 stsp case 'c':
5175 80106605 2020-02-24 stsp commit_id_arg = optarg;
5176 80106605 2020-02-24 stsp break;
5177 8e7bd50a 2019-08-22 stsp case 'm':
5178 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5179 8e7bd50a 2019-08-22 stsp break;
5180 8e7bd50a 2019-08-22 stsp case 'r':
5181 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5182 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5183 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5184 9ba1d308 2019-10-21 stsp optarg);
5185 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5186 8e7bd50a 2019-08-22 stsp break;
5187 8e7bd50a 2019-08-22 stsp case 'l':
5188 8e7bd50a 2019-08-22 stsp do_list = 1;
5189 8e7bd50a 2019-08-22 stsp break;
5190 8e7bd50a 2019-08-22 stsp default:
5191 8e7bd50a 2019-08-22 stsp usage_tag();
5192 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5193 8e7bd50a 2019-08-22 stsp }
5194 8e7bd50a 2019-08-22 stsp }
5195 8e7bd50a 2019-08-22 stsp
5196 8e7bd50a 2019-08-22 stsp argc -= optind;
5197 8e7bd50a 2019-08-22 stsp argv += optind;
5198 8e7bd50a 2019-08-22 stsp
5199 8e7bd50a 2019-08-22 stsp if (do_list) {
5200 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5201 775ce909 2020-03-22 stsp errx(1,
5202 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5203 8e7bd50a 2019-08-22 stsp if (tagmsg)
5204 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5205 8e7bd50a 2019-08-22 stsp if (argc > 0)
5206 8e7bd50a 2019-08-22 stsp usage_tag();
5207 80106605 2020-02-24 stsp } else if (argc != 1)
5208 8e7bd50a 2019-08-22 stsp usage_tag();
5209 80106605 2020-02-24 stsp
5210 a2887370 2019-08-23 stsp tag_name = argv[0];
5211 8e7bd50a 2019-08-22 stsp
5212 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5213 8e7bd50a 2019-08-22 stsp if (do_list) {
5214 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5215 8e7bd50a 2019-08-22 stsp NULL) == -1)
5216 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5217 8e7bd50a 2019-08-22 stsp } else {
5218 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5219 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5220 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5221 8e7bd50a 2019-08-22 stsp }
5222 8e7bd50a 2019-08-22 stsp #endif
5223 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5224 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5225 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5226 8e7bd50a 2019-08-22 stsp goto done;
5227 8e7bd50a 2019-08-22 stsp }
5228 8e7bd50a 2019-08-22 stsp
5229 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5230 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5231 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5232 8e7bd50a 2019-08-22 stsp goto done;
5233 8e7bd50a 2019-08-22 stsp else
5234 8e7bd50a 2019-08-22 stsp error = NULL;
5235 8e7bd50a 2019-08-22 stsp if (worktree) {
5236 8e7bd50a 2019-08-22 stsp repo_path =
5237 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5238 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5239 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5240 8e7bd50a 2019-08-22 stsp if (error)
5241 8e7bd50a 2019-08-22 stsp goto done;
5242 8e7bd50a 2019-08-22 stsp } else {
5243 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5244 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5245 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
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 }
5250 8e7bd50a 2019-08-22 stsp
5251 8e7bd50a 2019-08-22 stsp if (do_list) {
5252 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5253 c9956ddf 2019-09-08 stsp if (error != NULL)
5254 c9956ddf 2019-09-08 stsp goto done;
5255 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5256 8e7bd50a 2019-08-22 stsp if (error)
5257 8e7bd50a 2019-08-22 stsp goto done;
5258 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5259 8e7bd50a 2019-08-22 stsp } else {
5260 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5261 c9956ddf 2019-09-08 stsp if (error)
5262 c9956ddf 2019-09-08 stsp goto done;
5263 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5264 c9956ddf 2019-09-08 stsp if (error != NULL)
5265 c9956ddf 2019-09-08 stsp goto done;
5266 c9956ddf 2019-09-08 stsp
5267 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5268 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5269 8e7bd50a 2019-08-22 stsp if (error)
5270 8e7bd50a 2019-08-22 stsp goto done;
5271 8e7bd50a 2019-08-22 stsp }
5272 8e7bd50a 2019-08-22 stsp
5273 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5274 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5275 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5276 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5277 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5278 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5279 8e7bd50a 2019-08-22 stsp if (error)
5280 8e7bd50a 2019-08-22 stsp goto done;
5281 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5282 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5283 8e7bd50a 2019-08-22 stsp if (error)
5284 8e7bd50a 2019-08-22 stsp goto done;
5285 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5286 8e7bd50a 2019-08-22 stsp free(commit_id);
5287 8e7bd50a 2019-08-22 stsp if (error)
5288 8e7bd50a 2019-08-22 stsp goto done;
5289 8e7bd50a 2019-08-22 stsp }
5290 8e7bd50a 2019-08-22 stsp
5291 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5292 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5293 8e7bd50a 2019-08-22 stsp }
5294 8e7bd50a 2019-08-22 stsp done:
5295 8e7bd50a 2019-08-22 stsp if (repo)
5296 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5297 8e7bd50a 2019-08-22 stsp if (worktree)
5298 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5299 8e7bd50a 2019-08-22 stsp free(cwd);
5300 8e7bd50a 2019-08-22 stsp free(repo_path);
5301 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5302 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5303 8e7bd50a 2019-08-22 stsp return error;
5304 8e7bd50a 2019-08-22 stsp }
5305 8e7bd50a 2019-08-22 stsp
5306 8e7bd50a 2019-08-22 stsp __dead static void
5307 d00136be 2019-03-26 stsp usage_add(void)
5308 d00136be 2019-03-26 stsp {
5309 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5310 022fae89 2019-12-06 tracey getprogname());
5311 d00136be 2019-03-26 stsp exit(1);
5312 d00136be 2019-03-26 stsp }
5313 d00136be 2019-03-26 stsp
5314 d00136be 2019-03-26 stsp static const struct got_error *
5315 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5316 4e68cba3 2019-11-23 stsp {
5317 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5318 4e68cba3 2019-11-23 stsp path++;
5319 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5320 4e68cba3 2019-11-23 stsp return NULL;
5321 4e68cba3 2019-11-23 stsp }
5322 4e68cba3 2019-11-23 stsp
5323 4e68cba3 2019-11-23 stsp static const struct got_error *
5324 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5325 d00136be 2019-03-26 stsp {
5326 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5327 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5328 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5329 1dd54920 2019-05-11 stsp char *cwd = NULL;
5330 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5331 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5332 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5333 1dd54920 2019-05-11 stsp
5334 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5335 d00136be 2019-03-26 stsp
5336 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5337 d00136be 2019-03-26 stsp switch (ch) {
5338 022fae89 2019-12-06 tracey case 'I':
5339 022fae89 2019-12-06 tracey no_ignores = 1;
5340 022fae89 2019-12-06 tracey break;
5341 4e68cba3 2019-11-23 stsp case 'R':
5342 4e68cba3 2019-11-23 stsp can_recurse = 1;
5343 4e68cba3 2019-11-23 stsp break;
5344 d00136be 2019-03-26 stsp default:
5345 d00136be 2019-03-26 stsp usage_add();
5346 d00136be 2019-03-26 stsp /* NOTREACHED */
5347 d00136be 2019-03-26 stsp }
5348 d00136be 2019-03-26 stsp }
5349 d00136be 2019-03-26 stsp
5350 d00136be 2019-03-26 stsp argc -= optind;
5351 d00136be 2019-03-26 stsp argv += optind;
5352 d00136be 2019-03-26 stsp
5353 43012d58 2019-07-14 stsp #ifndef PROFILE
5354 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5355 43012d58 2019-07-14 stsp NULL) == -1)
5356 43012d58 2019-07-14 stsp err(1, "pledge");
5357 43012d58 2019-07-14 stsp #endif
5358 723c305c 2019-05-11 jcs if (argc < 1)
5359 d00136be 2019-03-26 stsp usage_add();
5360 d00136be 2019-03-26 stsp
5361 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5362 d00136be 2019-03-26 stsp if (cwd == NULL) {
5363 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5364 d00136be 2019-03-26 stsp goto done;
5365 d00136be 2019-03-26 stsp }
5366 723c305c 2019-05-11 jcs
5367 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5368 d00136be 2019-03-26 stsp if (error)
5369 d00136be 2019-03-26 stsp goto done;
5370 d00136be 2019-03-26 stsp
5371 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5372 c9956ddf 2019-09-08 stsp NULL);
5373 031a5338 2019-03-26 stsp if (error != NULL)
5374 031a5338 2019-03-26 stsp goto done;
5375 031a5338 2019-03-26 stsp
5376 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5377 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5378 d00136be 2019-03-26 stsp if (error)
5379 d00136be 2019-03-26 stsp goto done;
5380 d00136be 2019-03-26 stsp
5381 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5382 6d022e97 2019-08-04 stsp if (error)
5383 022fae89 2019-12-06 tracey goto done;
5384 022fae89 2019-12-06 tracey
5385 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5386 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5387 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5388 6d022e97 2019-08-04 stsp goto done;
5389 723c305c 2019-05-11 jcs
5390 022fae89 2019-12-06 tracey }
5391 022fae89 2019-12-06 tracey
5392 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5393 4e68cba3 2019-11-23 stsp char *ondisk_path;
5394 4e68cba3 2019-11-23 stsp struct stat sb;
5395 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5396 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5397 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5398 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5399 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5400 4e68cba3 2019-11-23 stsp goto done;
5401 4e68cba3 2019-11-23 stsp }
5402 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5403 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5404 4e68cba3 2019-11-23 stsp free(ondisk_path);
5405 4e68cba3 2019-11-23 stsp continue;
5406 4e68cba3 2019-11-23 stsp }
5407 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5408 4e68cba3 2019-11-23 stsp ondisk_path);
5409 4e68cba3 2019-11-23 stsp free(ondisk_path);
5410 4e68cba3 2019-11-23 stsp goto done;
5411 4e68cba3 2019-11-23 stsp }
5412 4e68cba3 2019-11-23 stsp free(ondisk_path);
5413 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5414 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5415 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5416 4e68cba3 2019-11-23 stsp goto done;
5417 4e68cba3 2019-11-23 stsp }
5418 4e68cba3 2019-11-23 stsp }
5419 4e68cba3 2019-11-23 stsp }
5420 022fae89 2019-12-06 tracey
5421 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5422 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5423 d00136be 2019-03-26 stsp done:
5424 031a5338 2019-03-26 stsp if (repo)
5425 031a5338 2019-03-26 stsp got_repo_close(repo);
5426 d00136be 2019-03-26 stsp if (worktree)
5427 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5428 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5429 1dd54920 2019-05-11 stsp free((char *)pe->path);
5430 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5431 2ec1f75b 2019-03-26 stsp free(cwd);
5432 2ec1f75b 2019-03-26 stsp return error;
5433 2ec1f75b 2019-03-26 stsp }
5434 2ec1f75b 2019-03-26 stsp
5435 2ec1f75b 2019-03-26 stsp __dead static void
5436 648e4ef7 2019-07-09 stsp usage_remove(void)
5437 2ec1f75b 2019-03-26 stsp {
5438 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5439 f2a9dc41 2019-12-13 tracey getprogname());
5440 2ec1f75b 2019-03-26 stsp exit(1);
5441 2a06fe5f 2019-08-24 stsp }
5442 2a06fe5f 2019-08-24 stsp
5443 2a06fe5f 2019-08-24 stsp static const struct got_error *
5444 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5445 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5446 2a06fe5f 2019-08-24 stsp {
5447 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5448 f2a9dc41 2019-12-13 tracey path++;
5449 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5450 2a06fe5f 2019-08-24 stsp return NULL;
5451 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5452 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5453 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5454 2a06fe5f 2019-08-24 stsp return NULL;
5455 2ec1f75b 2019-03-26 stsp }
5456 2ec1f75b 2019-03-26 stsp
5457 2ec1f75b 2019-03-26 stsp static const struct got_error *
5458 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5459 2ec1f75b 2019-03-26 stsp {
5460 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5461 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5462 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5463 17ed4618 2019-06-02 stsp char *cwd = NULL;
5464 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5465 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5466 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5467 2ec1f75b 2019-03-26 stsp
5468 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5469 17ed4618 2019-06-02 stsp
5470 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5471 2ec1f75b 2019-03-26 stsp switch (ch) {
5472 2ec1f75b 2019-03-26 stsp case 'f':
5473 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5474 2ec1f75b 2019-03-26 stsp break;
5475 70e3e7f5 2019-12-13 tracey case 'k':
5476 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5477 70e3e7f5 2019-12-13 tracey break;
5478 f2a9dc41 2019-12-13 tracey case 'R':
5479 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5480 f2a9dc41 2019-12-13 tracey break;
5481 2ec1f75b 2019-03-26 stsp default:
5482 f2a9dc41 2019-12-13 tracey usage_remove();
5483 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5484 2ec1f75b 2019-03-26 stsp }
5485 2ec1f75b 2019-03-26 stsp }
5486 2ec1f75b 2019-03-26 stsp
5487 2ec1f75b 2019-03-26 stsp argc -= optind;
5488 2ec1f75b 2019-03-26 stsp argv += optind;
5489 2ec1f75b 2019-03-26 stsp
5490 43012d58 2019-07-14 stsp #ifndef PROFILE
5491 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5492 43012d58 2019-07-14 stsp NULL) == -1)
5493 43012d58 2019-07-14 stsp err(1, "pledge");
5494 43012d58 2019-07-14 stsp #endif
5495 17ed4618 2019-06-02 stsp if (argc < 1)
5496 648e4ef7 2019-07-09 stsp usage_remove();
5497 2ec1f75b 2019-03-26 stsp
5498 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5499 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5500 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5501 2ec1f75b 2019-03-26 stsp goto done;
5502 2ec1f75b 2019-03-26 stsp }
5503 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5504 2ec1f75b 2019-03-26 stsp if (error)
5505 2ec1f75b 2019-03-26 stsp goto done;
5506 2ec1f75b 2019-03-26 stsp
5507 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5508 c9956ddf 2019-09-08 stsp NULL);
5509 2af4a041 2019-05-11 jcs if (error)
5510 2ec1f75b 2019-03-26 stsp goto done;
5511 2ec1f75b 2019-03-26 stsp
5512 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5513 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5514 2ec1f75b 2019-03-26 stsp if (error)
5515 2ec1f75b 2019-03-26 stsp goto done;
5516 2ec1f75b 2019-03-26 stsp
5517 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5518 6d022e97 2019-08-04 stsp if (error)
5519 6d022e97 2019-08-04 stsp goto done;
5520 17ed4618 2019-06-02 stsp
5521 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5522 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5523 f2a9dc41 2019-12-13 tracey struct stat sb;
5524 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5525 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5526 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5527 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5528 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5529 f2a9dc41 2019-12-13 tracey goto done;
5530 f2a9dc41 2019-12-13 tracey }
5531 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5532 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5533 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5534 f2a9dc41 2019-12-13 tracey continue;
5535 f2a9dc41 2019-12-13 tracey }
5536 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5537 f2a9dc41 2019-12-13 tracey ondisk_path);
5538 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5539 f2a9dc41 2019-12-13 tracey goto done;
5540 f2a9dc41 2019-12-13 tracey }
5541 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5542 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5543 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5544 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5545 f2a9dc41 2019-12-13 tracey goto done;
5546 f2a9dc41 2019-12-13 tracey }
5547 f2a9dc41 2019-12-13 tracey }
5548 f2a9dc41 2019-12-13 tracey }
5549 f2a9dc41 2019-12-13 tracey
5550 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5551 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5552 a129376b 2019-03-28 stsp done:
5553 a129376b 2019-03-28 stsp if (repo)
5554 a129376b 2019-03-28 stsp got_repo_close(repo);
5555 a129376b 2019-03-28 stsp if (worktree)
5556 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5557 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5558 17ed4618 2019-06-02 stsp free((char *)pe->path);
5559 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5560 a129376b 2019-03-28 stsp free(cwd);
5561 a129376b 2019-03-28 stsp return error;
5562 a129376b 2019-03-28 stsp }
5563 a129376b 2019-03-28 stsp
5564 a129376b 2019-03-28 stsp __dead static void
5565 a129376b 2019-03-28 stsp usage_revert(void)
5566 a129376b 2019-03-28 stsp {
5567 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5568 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5569 a129376b 2019-03-28 stsp exit(1);
5570 a129376b 2019-03-28 stsp }
5571 a129376b 2019-03-28 stsp
5572 1ee397ad 2019-07-12 stsp static const struct got_error *
5573 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5574 a129376b 2019-03-28 stsp {
5575 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5576 fb9704af 2020-01-27 stsp return NULL;
5577 fb9704af 2020-01-27 stsp
5578 a129376b 2019-03-28 stsp while (path[0] == '/')
5579 a129376b 2019-03-28 stsp path++;
5580 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5581 33aa809d 2019-08-08 stsp return NULL;
5582 33aa809d 2019-08-08 stsp }
5583 33aa809d 2019-08-08 stsp
5584 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5585 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5586 33aa809d 2019-08-08 stsp const char *action;
5587 33aa809d 2019-08-08 stsp };
5588 33aa809d 2019-08-08 stsp
5589 33aa809d 2019-08-08 stsp static const struct got_error *
5590 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5591 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5592 33aa809d 2019-08-08 stsp {
5593 33aa809d 2019-08-08 stsp char *line = NULL;
5594 33aa809d 2019-08-08 stsp size_t linesize = 0;
5595 33aa809d 2019-08-08 stsp ssize_t linelen;
5596 33aa809d 2019-08-08 stsp
5597 33aa809d 2019-08-08 stsp switch (status) {
5598 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5599 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5600 33aa809d 2019-08-08 stsp break;
5601 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5602 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5603 33aa809d 2019-08-08 stsp break;
5604 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5605 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5606 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5607 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5608 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5609 33aa809d 2019-08-08 stsp printf("%s", line);
5610 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5611 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5612 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5613 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5614 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5615 33aa809d 2019-08-08 stsp break;
5616 33aa809d 2019-08-08 stsp default:
5617 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5618 33aa809d 2019-08-08 stsp }
5619 33aa809d 2019-08-08 stsp
5620 33aa809d 2019-08-08 stsp return NULL;
5621 33aa809d 2019-08-08 stsp }
5622 33aa809d 2019-08-08 stsp
5623 33aa809d 2019-08-08 stsp static const struct got_error *
5624 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5625 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5626 33aa809d 2019-08-08 stsp {
5627 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5628 33aa809d 2019-08-08 stsp char *line = NULL;
5629 33aa809d 2019-08-08 stsp size_t linesize = 0;
5630 33aa809d 2019-08-08 stsp ssize_t linelen;
5631 33aa809d 2019-08-08 stsp int resp = ' ';
5632 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5633 33aa809d 2019-08-08 stsp
5634 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5635 33aa809d 2019-08-08 stsp
5636 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5637 33aa809d 2019-08-08 stsp char *nl;
5638 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5639 33aa809d 2019-08-08 stsp a->action);
5640 33aa809d 2019-08-08 stsp if (err)
5641 33aa809d 2019-08-08 stsp return err;
5642 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5643 33aa809d 2019-08-08 stsp if (linelen == -1) {
5644 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5645 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5646 33aa809d 2019-08-08 stsp return NULL;
5647 33aa809d 2019-08-08 stsp }
5648 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5649 33aa809d 2019-08-08 stsp if (nl)
5650 33aa809d 2019-08-08 stsp *nl = '\0';
5651 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5652 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5653 33aa809d 2019-08-08 stsp printf("y\n");
5654 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5655 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5656 33aa809d 2019-08-08 stsp printf("n\n");
5657 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5658 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5659 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5660 33aa809d 2019-08-08 stsp printf("q\n");
5661 33aa809d 2019-08-08 stsp } else
5662 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5663 33aa809d 2019-08-08 stsp free(line);
5664 33aa809d 2019-08-08 stsp return NULL;
5665 33aa809d 2019-08-08 stsp }
5666 33aa809d 2019-08-08 stsp
5667 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5668 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5669 33aa809d 2019-08-08 stsp a->action);
5670 33aa809d 2019-08-08 stsp if (err)
5671 33aa809d 2019-08-08 stsp return err;
5672 33aa809d 2019-08-08 stsp resp = getchar();
5673 33aa809d 2019-08-08 stsp if (resp == '\n')
5674 33aa809d 2019-08-08 stsp resp = getchar();
5675 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5676 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5677 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5678 33aa809d 2019-08-08 stsp resp = ' ';
5679 33aa809d 2019-08-08 stsp }
5680 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5681 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5682 33aa809d 2019-08-08 stsp resp = ' ';
5683 33aa809d 2019-08-08 stsp }
5684 33aa809d 2019-08-08 stsp }
5685 33aa809d 2019-08-08 stsp
5686 33aa809d 2019-08-08 stsp if (resp == 'y')
5687 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5688 33aa809d 2019-08-08 stsp else if (resp == 'n')
5689 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5690 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5691 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5692 33aa809d 2019-08-08 stsp
5693 1ee397ad 2019-07-12 stsp return NULL;
5694 a129376b 2019-03-28 stsp }
5695 a129376b 2019-03-28 stsp
5696 33aa809d 2019-08-08 stsp
5697 a129376b 2019-03-28 stsp static const struct got_error *
5698 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5699 a129376b 2019-03-28 stsp {
5700 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5701 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5702 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5703 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5704 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5705 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5706 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5707 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5708 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5709 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5710 a129376b 2019-03-28 stsp
5711 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5712 e20a8b6f 2019-06-04 stsp
5713 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5714 a129376b 2019-03-28 stsp switch (ch) {
5715 33aa809d 2019-08-08 stsp case 'p':
5716 33aa809d 2019-08-08 stsp pflag = 1;
5717 33aa809d 2019-08-08 stsp break;
5718 33aa809d 2019-08-08 stsp case 'F':
5719 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5720 33aa809d 2019-08-08 stsp break;
5721 0f6d7415 2019-08-08 stsp case 'R':
5722 0f6d7415 2019-08-08 stsp can_recurse = 1;
5723 0f6d7415 2019-08-08 stsp break;
5724 a129376b 2019-03-28 stsp default:
5725 a129376b 2019-03-28 stsp usage_revert();
5726 a129376b 2019-03-28 stsp /* NOTREACHED */
5727 a129376b 2019-03-28 stsp }
5728 a129376b 2019-03-28 stsp }
5729 a129376b 2019-03-28 stsp
5730 a129376b 2019-03-28 stsp argc -= optind;
5731 a129376b 2019-03-28 stsp argv += optind;
5732 a129376b 2019-03-28 stsp
5733 43012d58 2019-07-14 stsp #ifndef PROFILE
5734 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5735 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5736 43012d58 2019-07-14 stsp err(1, "pledge");
5737 43012d58 2019-07-14 stsp #endif
5738 e20a8b6f 2019-06-04 stsp if (argc < 1)
5739 a129376b 2019-03-28 stsp usage_revert();
5740 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5741 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5742 a129376b 2019-03-28 stsp
5743 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5744 a129376b 2019-03-28 stsp if (cwd == NULL) {
5745 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5746 a129376b 2019-03-28 stsp goto done;
5747 a129376b 2019-03-28 stsp }
5748 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5749 2ec1f75b 2019-03-26 stsp if (error)
5750 2ec1f75b 2019-03-26 stsp goto done;
5751 a129376b 2019-03-28 stsp
5752 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5753 c9956ddf 2019-09-08 stsp NULL);
5754 a129376b 2019-03-28 stsp if (error != NULL)
5755 a129376b 2019-03-28 stsp goto done;
5756 a129376b 2019-03-28 stsp
5757 33aa809d 2019-08-08 stsp if (patch_script_path) {
5758 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5759 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5760 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5761 33aa809d 2019-08-08 stsp patch_script_path);
5762 33aa809d 2019-08-08 stsp goto done;
5763 33aa809d 2019-08-08 stsp }
5764 33aa809d 2019-08-08 stsp }
5765 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5766 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5767 a129376b 2019-03-28 stsp if (error)
5768 a129376b 2019-03-28 stsp goto done;
5769 a129376b 2019-03-28 stsp
5770 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5771 6d022e97 2019-08-04 stsp if (error)
5772 6d022e97 2019-08-04 stsp goto done;
5773 00db391e 2019-08-03 stsp
5774 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5775 0f6d7415 2019-08-08 stsp char *ondisk_path;
5776 0f6d7415 2019-08-08 stsp struct stat sb;
5777 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5778 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5779 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5780 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5781 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5782 0f6d7415 2019-08-08 stsp goto done;
5783 0f6d7415 2019-08-08 stsp }
5784 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5785 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5786 0f6d7415 2019-08-08 stsp free(ondisk_path);
5787 0f6d7415 2019-08-08 stsp continue;
5788 0f6d7415 2019-08-08 stsp }
5789 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5790 0f6d7415 2019-08-08 stsp ondisk_path);
5791 0f6d7415 2019-08-08 stsp free(ondisk_path);
5792 0f6d7415 2019-08-08 stsp goto done;
5793 0f6d7415 2019-08-08 stsp }
5794 0f6d7415 2019-08-08 stsp free(ondisk_path);
5795 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5796 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5797 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5798 0f6d7415 2019-08-08 stsp goto done;
5799 0f6d7415 2019-08-08 stsp }
5800 0f6d7415 2019-08-08 stsp }
5801 0f6d7415 2019-08-08 stsp }
5802 0f6d7415 2019-08-08 stsp
5803 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5804 33aa809d 2019-08-08 stsp cpa.action = "revert";
5805 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5806 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5807 2ec1f75b 2019-03-26 stsp done:
5808 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5809 33aa809d 2019-08-08 stsp error == NULL)
5810 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5811 2ec1f75b 2019-03-26 stsp if (repo)
5812 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5813 2ec1f75b 2019-03-26 stsp if (worktree)
5814 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5815 2ec1f75b 2019-03-26 stsp free(path);
5816 d00136be 2019-03-26 stsp free(cwd);
5817 6bad629b 2019-02-04 stsp return error;
5818 c4296144 2019-05-09 stsp }
5819 c4296144 2019-05-09 stsp
5820 c4296144 2019-05-09 stsp __dead static void
5821 c4296144 2019-05-09 stsp usage_commit(void)
5822 c4296144 2019-05-09 stsp {
5823 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5824 5c1e53bc 2019-07-28 stsp getprogname());
5825 c4296144 2019-05-09 stsp exit(1);
5826 33ad4cbe 2019-05-12 jcs }
5827 33ad4cbe 2019-05-12 jcs
5828 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5829 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5830 e2ba3d07 2019-05-13 stsp const char *editor;
5831 e0870e44 2019-05-13 stsp const char *worktree_path;
5832 76d98825 2019-06-03 stsp const char *branch_name;
5833 314a6357 2019-05-13 stsp const char *repo_path;
5834 e0870e44 2019-05-13 stsp char *logmsg_path;
5835 e2ba3d07 2019-05-13 stsp
5836 e2ba3d07 2019-05-13 stsp };
5837 e0870e44 2019-05-13 stsp
5838 33ad4cbe 2019-05-12 jcs static const struct got_error *
5839 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5840 33ad4cbe 2019-05-12 jcs void *arg)
5841 33ad4cbe 2019-05-12 jcs {
5842 76d98825 2019-06-03 stsp char *initial_content = NULL;
5843 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5844 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5845 e0870e44 2019-05-13 stsp char *template = NULL;
5846 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5847 3ce1b845 2019-07-15 stsp int fd;
5848 33ad4cbe 2019-05-12 jcs size_t len;
5849 33ad4cbe 2019-05-12 jcs
5850 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5851 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5852 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5853 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5854 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5855 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5856 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5857 33ad4cbe 2019-05-12 jcs return NULL;
5858 33ad4cbe 2019-05-12 jcs }
5859 33ad4cbe 2019-05-12 jcs
5860 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5861 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5862 76d98825 2019-06-03 stsp
5863 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5864 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5865 76d98825 2019-06-03 stsp a->branch_name) == -1)
5866 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5867 e0870e44 2019-05-13 stsp
5868 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5869 793c30b5 2019-05-13 stsp if (err)
5870 e0870e44 2019-05-13 stsp goto done;
5871 33ad4cbe 2019-05-12 jcs
5872 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5873 33ad4cbe 2019-05-12 jcs
5874 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5875 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5876 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5877 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5878 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5879 33ad4cbe 2019-05-12 jcs }
5880 33ad4cbe 2019-05-12 jcs close(fd);
5881 33ad4cbe 2019-05-12 jcs
5882 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5883 33ad4cbe 2019-05-12 jcs done:
5884 76d98825 2019-06-03 stsp free(initial_content);
5885 e0870e44 2019-05-13 stsp free(template);
5886 314a6357 2019-05-13 stsp
5887 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5888 3ce1b845 2019-07-15 stsp if (err == NULL) {
5889 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5890 3ce1b845 2019-07-15 stsp if (err) {
5891 3ce1b845 2019-07-15 stsp free(*logmsg);
5892 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5893 3ce1b845 2019-07-15 stsp }
5894 3ce1b845 2019-07-15 stsp }
5895 33ad4cbe 2019-05-12 jcs return err;
5896 6bad629b 2019-02-04 stsp }
5897 c4296144 2019-05-09 stsp
5898 c4296144 2019-05-09 stsp static const struct got_error *
5899 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5900 c4296144 2019-05-09 stsp {
5901 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5902 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5903 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5904 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5905 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5906 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5907 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5908 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5909 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5910 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5911 5c1e53bc 2019-07-28 stsp
5912 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5913 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5914 c4296144 2019-05-09 stsp
5915 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5916 c4296144 2019-05-09 stsp switch (ch) {
5917 c4296144 2019-05-09 stsp case 'm':
5918 c4296144 2019-05-09 stsp logmsg = optarg;
5919 c4296144 2019-05-09 stsp break;
5920 c4296144 2019-05-09 stsp default:
5921 c4296144 2019-05-09 stsp usage_commit();
5922 c4296144 2019-05-09 stsp /* NOTREACHED */
5923 c4296144 2019-05-09 stsp }
5924 c4296144 2019-05-09 stsp }
5925 c4296144 2019-05-09 stsp
5926 c4296144 2019-05-09 stsp argc -= optind;
5927 c4296144 2019-05-09 stsp argv += optind;
5928 c4296144 2019-05-09 stsp
5929 43012d58 2019-07-14 stsp #ifndef PROFILE
5930 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5931 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5932 43012d58 2019-07-14 stsp err(1, "pledge");
5933 43012d58 2019-07-14 stsp #endif
5934 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5935 c4296144 2019-05-09 stsp if (cwd == NULL) {
5936 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5937 c4296144 2019-05-09 stsp goto done;
5938 c4296144 2019-05-09 stsp }
5939 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5940 7d5807f4 2019-07-11 stsp if (error)
5941 7d5807f4 2019-07-11 stsp goto done;
5942 7d5807f4 2019-07-11 stsp
5943 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5944 c4296144 2019-05-09 stsp if (error)
5945 a698f62e 2019-07-25 stsp goto done;
5946 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5947 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5948 c4296144 2019-05-09 stsp goto done;
5949 a698f62e 2019-07-25 stsp }
5950 5c1e53bc 2019-07-28 stsp
5951 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5952 916f288c 2019-07-30 stsp worktree);
5953 5c1e53bc 2019-07-28 stsp if (error)
5954 5c1e53bc 2019-07-28 stsp goto done;
5955 c4296144 2019-05-09 stsp
5956 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5957 c9956ddf 2019-09-08 stsp if (error)
5958 c9956ddf 2019-09-08 stsp goto done;
5959 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5960 c9956ddf 2019-09-08 stsp gitconfig_path);
5961 c4296144 2019-05-09 stsp if (error != NULL)
5962 c4296144 2019-05-09 stsp goto done;
5963 c4296144 2019-05-09 stsp
5964 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5965 aba9c984 2019-09-08 stsp if (error)
5966 aba9c984 2019-09-08 stsp return error;
5967 aba9c984 2019-09-08 stsp
5968 314a6357 2019-05-13 stsp /*
5969 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5970 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5971 314a6357 2019-05-13 stsp */
5972 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5973 314a6357 2019-05-13 stsp error = get_editor(&editor);
5974 314a6357 2019-05-13 stsp else
5975 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5976 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5977 c4296144 2019-05-09 stsp if (error)
5978 c4296144 2019-05-09 stsp goto done;
5979 c4296144 2019-05-09 stsp
5980 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5981 087fb88c 2019-08-04 stsp if (error)
5982 087fb88c 2019-08-04 stsp goto done;
5983 087fb88c 2019-08-04 stsp
5984 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5985 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5986 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5987 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5988 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5989 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5990 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5991 916f288c 2019-07-30 stsp goto done;
5992 916f288c 2019-07-30 stsp }
5993 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5994 916f288c 2019-07-30 stsp }
5995 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5996 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5997 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5998 e0870e44 2019-05-13 stsp if (error) {
5999 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6000 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6001 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6002 c4296144 2019-05-09 stsp goto done;
6003 e0870e44 2019-05-13 stsp }
6004 c4296144 2019-05-09 stsp
6005 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6006 c4296144 2019-05-09 stsp if (error)
6007 c4296144 2019-05-09 stsp goto done;
6008 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6009 c4296144 2019-05-09 stsp done:
6010 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6011 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6012 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6013 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6014 7266f21f 2019-10-21 stsp error == NULL)
6015 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6016 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6017 c4296144 2019-05-09 stsp if (repo)
6018 c4296144 2019-05-09 stsp got_repo_close(repo);
6019 c4296144 2019-05-09 stsp if (worktree)
6020 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6021 c4296144 2019-05-09 stsp free(cwd);
6022 c4296144 2019-05-09 stsp free(id_str);
6023 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6024 e2ba3d07 2019-05-13 stsp free(editor);
6025 aba9c984 2019-09-08 stsp free(author);
6026 234035bc 2019-06-01 stsp return error;
6027 234035bc 2019-06-01 stsp }
6028 234035bc 2019-06-01 stsp
6029 234035bc 2019-06-01 stsp __dead static void
6030 234035bc 2019-06-01 stsp usage_cherrypick(void)
6031 234035bc 2019-06-01 stsp {
6032 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6033 234035bc 2019-06-01 stsp exit(1);
6034 234035bc 2019-06-01 stsp }
6035 234035bc 2019-06-01 stsp
6036 234035bc 2019-06-01 stsp static const struct got_error *
6037 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6038 234035bc 2019-06-01 stsp {
6039 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6040 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6041 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6042 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6043 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6044 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6045 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6046 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6047 234035bc 2019-06-01 stsp int ch, did_something = 0;
6048 234035bc 2019-06-01 stsp
6049 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6050 234035bc 2019-06-01 stsp switch (ch) {
6051 234035bc 2019-06-01 stsp default:
6052 234035bc 2019-06-01 stsp usage_cherrypick();
6053 234035bc 2019-06-01 stsp /* NOTREACHED */
6054 234035bc 2019-06-01 stsp }
6055 234035bc 2019-06-01 stsp }
6056 234035bc 2019-06-01 stsp
6057 234035bc 2019-06-01 stsp argc -= optind;
6058 234035bc 2019-06-01 stsp argv += optind;
6059 234035bc 2019-06-01 stsp
6060 43012d58 2019-07-14 stsp #ifndef PROFILE
6061 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6062 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6063 43012d58 2019-07-14 stsp err(1, "pledge");
6064 43012d58 2019-07-14 stsp #endif
6065 234035bc 2019-06-01 stsp if (argc != 1)
6066 234035bc 2019-06-01 stsp usage_cherrypick();
6067 234035bc 2019-06-01 stsp
6068 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6069 234035bc 2019-06-01 stsp if (cwd == NULL) {
6070 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6071 234035bc 2019-06-01 stsp goto done;
6072 234035bc 2019-06-01 stsp }
6073 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6074 234035bc 2019-06-01 stsp if (error)
6075 234035bc 2019-06-01 stsp goto done;
6076 234035bc 2019-06-01 stsp
6077 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6078 c9956ddf 2019-09-08 stsp NULL);
6079 234035bc 2019-06-01 stsp if (error != NULL)
6080 234035bc 2019-06-01 stsp goto done;
6081 234035bc 2019-06-01 stsp
6082 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6083 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6084 234035bc 2019-06-01 stsp if (error)
6085 234035bc 2019-06-01 stsp goto done;
6086 234035bc 2019-06-01 stsp
6087 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6088 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6089 234035bc 2019-06-01 stsp if (error != NULL) {
6090 234035bc 2019-06-01 stsp struct got_reference *ref;
6091 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6092 234035bc 2019-06-01 stsp goto done;
6093 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6094 234035bc 2019-06-01 stsp if (error != NULL)
6095 234035bc 2019-06-01 stsp goto done;
6096 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6097 234035bc 2019-06-01 stsp got_ref_close(ref);
6098 234035bc 2019-06-01 stsp if (error != NULL)
6099 234035bc 2019-06-01 stsp goto done;
6100 234035bc 2019-06-01 stsp }
6101 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6102 234035bc 2019-06-01 stsp if (error)
6103 234035bc 2019-06-01 stsp goto done;
6104 234035bc 2019-06-01 stsp
6105 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6106 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6107 234035bc 2019-06-01 stsp if (error != NULL)
6108 234035bc 2019-06-01 stsp goto done;
6109 234035bc 2019-06-01 stsp
6110 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6111 234035bc 2019-06-01 stsp if (error) {
6112 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6113 234035bc 2019-06-01 stsp goto done;
6114 234035bc 2019-06-01 stsp error = NULL;
6115 234035bc 2019-06-01 stsp } else {
6116 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6117 234035bc 2019-06-01 stsp goto done;
6118 234035bc 2019-06-01 stsp }
6119 234035bc 2019-06-01 stsp
6120 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6121 234035bc 2019-06-01 stsp if (error)
6122 234035bc 2019-06-01 stsp goto done;
6123 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6124 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6125 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
6126 03415a1a 2019-06-02 stsp NULL);
6127 234035bc 2019-06-01 stsp if (error != NULL)
6128 234035bc 2019-06-01 stsp goto done;
6129 234035bc 2019-06-01 stsp
6130 234035bc 2019-06-01 stsp if (did_something)
6131 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6132 234035bc 2019-06-01 stsp done:
6133 234035bc 2019-06-01 stsp if (commit)
6134 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6135 234035bc 2019-06-01 stsp free(commit_id_str);
6136 234035bc 2019-06-01 stsp if (head_ref)
6137 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6138 234035bc 2019-06-01 stsp if (worktree)
6139 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6140 234035bc 2019-06-01 stsp if (repo)
6141 234035bc 2019-06-01 stsp got_repo_close(repo);
6142 c4296144 2019-05-09 stsp return error;
6143 c4296144 2019-05-09 stsp }
6144 5ef14e63 2019-06-02 stsp
6145 5ef14e63 2019-06-02 stsp __dead static void
6146 5ef14e63 2019-06-02 stsp usage_backout(void)
6147 5ef14e63 2019-06-02 stsp {
6148 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6149 5ef14e63 2019-06-02 stsp exit(1);
6150 5ef14e63 2019-06-02 stsp }
6151 5ef14e63 2019-06-02 stsp
6152 5ef14e63 2019-06-02 stsp static const struct got_error *
6153 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6154 5ef14e63 2019-06-02 stsp {
6155 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6156 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6157 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6158 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6159 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6160 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6161 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6162 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6163 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
6164 5ef14e63 2019-06-02 stsp
6165 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6166 5ef14e63 2019-06-02 stsp switch (ch) {
6167 5ef14e63 2019-06-02 stsp default:
6168 5ef14e63 2019-06-02 stsp usage_backout();
6169 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6170 5ef14e63 2019-06-02 stsp }
6171 5ef14e63 2019-06-02 stsp }
6172 5ef14e63 2019-06-02 stsp
6173 5ef14e63 2019-06-02 stsp argc -= optind;
6174 5ef14e63 2019-06-02 stsp argv += optind;
6175 5ef14e63 2019-06-02 stsp
6176 43012d58 2019-07-14 stsp #ifndef PROFILE
6177 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6178 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6179 43012d58 2019-07-14 stsp err(1, "pledge");
6180 43012d58 2019-07-14 stsp #endif
6181 5ef14e63 2019-06-02 stsp if (argc != 1)
6182 5ef14e63 2019-06-02 stsp usage_backout();
6183 5ef14e63 2019-06-02 stsp
6184 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6185 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6186 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6187 5ef14e63 2019-06-02 stsp goto done;
6188 5ef14e63 2019-06-02 stsp }
6189 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6190 5ef14e63 2019-06-02 stsp if (error)
6191 5ef14e63 2019-06-02 stsp goto done;
6192 5ef14e63 2019-06-02 stsp
6193 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6194 c9956ddf 2019-09-08 stsp NULL);
6195 5ef14e63 2019-06-02 stsp if (error != NULL)
6196 5ef14e63 2019-06-02 stsp goto done;
6197 5ef14e63 2019-06-02 stsp
6198 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6199 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6200 5ef14e63 2019-06-02 stsp if (error)
6201 5ef14e63 2019-06-02 stsp goto done;
6202 5ef14e63 2019-06-02 stsp
6203 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6204 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6205 5ef14e63 2019-06-02 stsp if (error != NULL) {
6206 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6207 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6208 5ef14e63 2019-06-02 stsp goto done;
6209 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6210 5ef14e63 2019-06-02 stsp if (error != NULL)
6211 5ef14e63 2019-06-02 stsp goto done;
6212 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6213 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6214 5ef14e63 2019-06-02 stsp if (error != NULL)
6215 5ef14e63 2019-06-02 stsp goto done;
6216 5ef14e63 2019-06-02 stsp }
6217 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6218 5ef14e63 2019-06-02 stsp if (error)
6219 5ef14e63 2019-06-02 stsp goto done;
6220 5ef14e63 2019-06-02 stsp
6221 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6222 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6223 5ef14e63 2019-06-02 stsp if (error != NULL)
6224 5ef14e63 2019-06-02 stsp goto done;
6225 5ef14e63 2019-06-02 stsp
6226 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6227 5ef14e63 2019-06-02 stsp if (error)
6228 5ef14e63 2019-06-02 stsp goto done;
6229 5ef14e63 2019-06-02 stsp
6230 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6231 5ef14e63 2019-06-02 stsp if (error)
6232 5ef14e63 2019-06-02 stsp goto done;
6233 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6234 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6235 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6236 5ef14e63 2019-06-02 stsp goto done;
6237 5ef14e63 2019-06-02 stsp }
6238 5ef14e63 2019-06-02 stsp
6239 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6240 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6241 5ef14e63 2019-06-02 stsp if (error != NULL)
6242 5ef14e63 2019-06-02 stsp goto done;
6243 5ef14e63 2019-06-02 stsp
6244 5ef14e63 2019-06-02 stsp if (did_something)
6245 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6246 5ef14e63 2019-06-02 stsp done:
6247 5ef14e63 2019-06-02 stsp if (commit)
6248 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6249 5ef14e63 2019-06-02 stsp free(commit_id_str);
6250 5ef14e63 2019-06-02 stsp if (head_ref)
6251 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6252 818c7501 2019-07-11 stsp if (worktree)
6253 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6254 818c7501 2019-07-11 stsp if (repo)
6255 818c7501 2019-07-11 stsp got_repo_close(repo);
6256 818c7501 2019-07-11 stsp return error;
6257 818c7501 2019-07-11 stsp }
6258 818c7501 2019-07-11 stsp
6259 818c7501 2019-07-11 stsp __dead static void
6260 818c7501 2019-07-11 stsp usage_rebase(void)
6261 818c7501 2019-07-11 stsp {
6262 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6263 af54c8f8 2019-07-11 stsp getprogname());
6264 818c7501 2019-07-11 stsp exit(1);
6265 0ebf8283 2019-07-24 stsp }
6266 0ebf8283 2019-07-24 stsp
6267 0ebf8283 2019-07-24 stsp void
6268 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6269 0ebf8283 2019-07-24 stsp {
6270 0ebf8283 2019-07-24 stsp char *nl;
6271 0ebf8283 2019-07-24 stsp size_t len;
6272 0ebf8283 2019-07-24 stsp
6273 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6274 0ebf8283 2019-07-24 stsp if (len > limit)
6275 0ebf8283 2019-07-24 stsp len = limit;
6276 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6277 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6278 0ebf8283 2019-07-24 stsp if (nl)
6279 0ebf8283 2019-07-24 stsp *nl = '\0';
6280 0ebf8283 2019-07-24 stsp }
6281 0ebf8283 2019-07-24 stsp
6282 0ebf8283 2019-07-24 stsp static const struct got_error *
6283 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6284 0ebf8283 2019-07-24 stsp {
6285 5943eee2 2019-08-13 stsp const struct got_error *err;
6286 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6287 5943eee2 2019-08-13 stsp const char *s;
6288 0ebf8283 2019-07-24 stsp
6289 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6290 5943eee2 2019-08-13 stsp if (err)
6291 5943eee2 2019-08-13 stsp return err;
6292 0ebf8283 2019-07-24 stsp
6293 5943eee2 2019-08-13 stsp s = logmsg0;
6294 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6295 5943eee2 2019-08-13 stsp s++;
6296 5943eee2 2019-08-13 stsp
6297 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6298 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6299 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6300 5943eee2 2019-08-13 stsp goto done;
6301 5943eee2 2019-08-13 stsp }
6302 0ebf8283 2019-07-24 stsp
6303 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6304 5943eee2 2019-08-13 stsp done:
6305 5943eee2 2019-08-13 stsp free(logmsg0);
6306 a0ea4fc0 2020-02-28 stsp return err;
6307 a0ea4fc0 2020-02-28 stsp }
6308 a0ea4fc0 2020-02-28 stsp
6309 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6310 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6311 a0ea4fc0 2020-02-28 stsp {
6312 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6313 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6314 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6315 a0ea4fc0 2020-02-28 stsp
6316 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6317 a0ea4fc0 2020-02-28 stsp if (err)
6318 a0ea4fc0 2020-02-28 stsp return err;
6319 a0ea4fc0 2020-02-28 stsp
6320 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6321 a0ea4fc0 2020-02-28 stsp if (err)
6322 a0ea4fc0 2020-02-28 stsp goto done;
6323 a0ea4fc0 2020-02-28 stsp
6324 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6325 a0ea4fc0 2020-02-28 stsp
6326 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6327 a0ea4fc0 2020-02-28 stsp if (err)
6328 a0ea4fc0 2020-02-28 stsp goto done;
6329 a0ea4fc0 2020-02-28 stsp
6330 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6331 a0ea4fc0 2020-02-28 stsp done:
6332 a0ea4fc0 2020-02-28 stsp free(id_str);
6333 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6334 a0ea4fc0 2020-02-28 stsp free(logmsg);
6335 5943eee2 2019-08-13 stsp return err;
6336 818c7501 2019-07-11 stsp }
6337 818c7501 2019-07-11 stsp
6338 818c7501 2019-07-11 stsp static const struct got_error *
6339 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6340 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6341 818c7501 2019-07-11 stsp {
6342 818c7501 2019-07-11 stsp const struct got_error *err;
6343 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6344 818c7501 2019-07-11 stsp
6345 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6346 818c7501 2019-07-11 stsp if (err)
6347 818c7501 2019-07-11 stsp goto done;
6348 818c7501 2019-07-11 stsp
6349 ff0d2220 2019-07-11 stsp if (new_id) {
6350 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6351 ff0d2220 2019-07-11 stsp if (err)
6352 ff0d2220 2019-07-11 stsp goto done;
6353 ff0d2220 2019-07-11 stsp }
6354 818c7501 2019-07-11 stsp
6355 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6356 ff0d2220 2019-07-11 stsp if (new_id_str)
6357 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6358 0ebf8283 2019-07-24 stsp
6359 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6360 0ebf8283 2019-07-24 stsp if (err)
6361 0ebf8283 2019-07-24 stsp goto done;
6362 0ebf8283 2019-07-24 stsp
6363 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6364 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6365 818c7501 2019-07-11 stsp done:
6366 818c7501 2019-07-11 stsp free(old_id_str);
6367 818c7501 2019-07-11 stsp free(new_id_str);
6368 272a1371 2020-02-28 stsp free(logmsg);
6369 818c7501 2019-07-11 stsp return err;
6370 818c7501 2019-07-11 stsp }
6371 818c7501 2019-07-11 stsp
6372 1ee397ad 2019-07-12 stsp static const struct got_error *
6373 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6374 818c7501 2019-07-11 stsp {
6375 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6376 818c7501 2019-07-11 stsp
6377 818c7501 2019-07-11 stsp while (path[0] == '/')
6378 818c7501 2019-07-11 stsp path++;
6379 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6380 818c7501 2019-07-11 stsp
6381 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6382 1ee397ad 2019-07-12 stsp return NULL;
6383 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6384 818c7501 2019-07-11 stsp *rebase_status = status;
6385 1ee397ad 2019-07-12 stsp return NULL;
6386 818c7501 2019-07-11 stsp }
6387 818c7501 2019-07-11 stsp
6388 818c7501 2019-07-11 stsp static const struct got_error *
6389 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6390 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6391 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6392 818c7501 2019-07-11 stsp {
6393 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6394 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6395 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6396 818c7501 2019-07-11 stsp }
6397 818c7501 2019-07-11 stsp
6398 818c7501 2019-07-11 stsp static const struct got_error *
6399 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6400 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6401 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6402 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6403 ff0d2220 2019-07-11 stsp {
6404 ff0d2220 2019-07-11 stsp const struct got_error *error;
6405 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6406 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6407 ff0d2220 2019-07-11 stsp
6408 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6409 ff0d2220 2019-07-11 stsp if (error)
6410 ff0d2220 2019-07-11 stsp return error;
6411 ff0d2220 2019-07-11 stsp
6412 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6413 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6414 ff0d2220 2019-07-11 stsp if (error) {
6415 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6416 ff0d2220 2019-07-11 stsp goto done;
6417 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6418 ff0d2220 2019-07-11 stsp } else {
6419 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6420 ff0d2220 2019-07-11 stsp free(new_commit_id);
6421 ff0d2220 2019-07-11 stsp }
6422 ff0d2220 2019-07-11 stsp done:
6423 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6424 ff0d2220 2019-07-11 stsp return error;
6425 64c6d990 2019-07-11 stsp }
6426 64c6d990 2019-07-11 stsp
6427 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6428 64c6d990 2019-07-11 stsp const char *path_prefix;
6429 64c6d990 2019-07-11 stsp size_t len;
6430 8ca9bd68 2019-07-25 stsp int errcode;
6431 64c6d990 2019-07-11 stsp };
6432 64c6d990 2019-07-11 stsp
6433 64c6d990 2019-07-11 stsp static const struct got_error *
6434 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6435 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6436 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6437 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6438 64c6d990 2019-07-11 stsp {
6439 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6440 64c6d990 2019-07-11 stsp
6441 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6442 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6443 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6444 64c6d990 2019-07-11 stsp
6445 64c6d990 2019-07-11 stsp return NULL;
6446 64c6d990 2019-07-11 stsp }
6447 64c6d990 2019-07-11 stsp
6448 64c6d990 2019-07-11 stsp static const struct got_error *
6449 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6450 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6451 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6452 64c6d990 2019-07-11 stsp {
6453 64c6d990 2019-07-11 stsp const struct got_error *err;
6454 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6455 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6456 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6457 64c6d990 2019-07-11 stsp
6458 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6459 64c6d990 2019-07-11 stsp return NULL;
6460 64c6d990 2019-07-11 stsp
6461 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6462 64c6d990 2019-07-11 stsp if (err)
6463 64c6d990 2019-07-11 stsp goto done;
6464 64c6d990 2019-07-11 stsp
6465 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6466 64c6d990 2019-07-11 stsp if (err)
6467 64c6d990 2019-07-11 stsp goto done;
6468 64c6d990 2019-07-11 stsp
6469 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6470 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6471 64c6d990 2019-07-11 stsp if (err)
6472 64c6d990 2019-07-11 stsp goto done;
6473 64c6d990 2019-07-11 stsp
6474 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6475 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6476 64c6d990 2019-07-11 stsp if (err)
6477 64c6d990 2019-07-11 stsp goto done;
6478 64c6d990 2019-07-11 stsp
6479 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6480 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6481 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6482 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6483 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6484 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6485 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6486 64c6d990 2019-07-11 stsp done:
6487 64c6d990 2019-07-11 stsp if (tree1)
6488 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6489 64c6d990 2019-07-11 stsp if (tree2)
6490 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6491 64c6d990 2019-07-11 stsp if (commit)
6492 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6493 64c6d990 2019-07-11 stsp if (parent_commit)
6494 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6495 64c6d990 2019-07-11 stsp return err;
6496 ff0d2220 2019-07-11 stsp }
6497 ff0d2220 2019-07-11 stsp
6498 ff0d2220 2019-07-11 stsp static const struct got_error *
6499 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6500 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6501 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6502 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6503 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6504 af61c510 2019-07-19 stsp {
6505 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6506 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6507 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6508 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6509 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6510 af61c510 2019-07-19 stsp
6511 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6512 af61c510 2019-07-19 stsp if (err)
6513 af61c510 2019-07-19 stsp return err;
6514 af61c510 2019-07-19 stsp
6515 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6516 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6517 af61c510 2019-07-19 stsp if (err)
6518 af61c510 2019-07-19 stsp goto done;
6519 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6520 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6521 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6522 af61c510 2019-07-19 stsp if (err) {
6523 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6524 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6525 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6526 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6527 af61c510 2019-07-19 stsp "been reached?!?");
6528 ee780d5c 2020-01-04 stsp }
6529 ee780d5c 2020-01-04 stsp goto done;
6530 af61c510 2019-07-19 stsp } else {
6531 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6532 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6533 af61c510 2019-07-19 stsp if (err)
6534 af61c510 2019-07-19 stsp goto done;
6535 af61c510 2019-07-19 stsp
6536 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6537 af61c510 2019-07-19 stsp if (err)
6538 af61c510 2019-07-19 stsp goto done;
6539 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6540 af61c510 2019-07-19 stsp commit_id = parent_id;
6541 af61c510 2019-07-19 stsp }
6542 af61c510 2019-07-19 stsp }
6543 af61c510 2019-07-19 stsp done:
6544 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6545 af61c510 2019-07-19 stsp return err;
6546 af61c510 2019-07-19 stsp }
6547 af61c510 2019-07-19 stsp
6548 af61c510 2019-07-19 stsp static const struct got_error *
6549 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6550 818c7501 2019-07-11 stsp {
6551 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6552 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6553 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6554 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6555 818c7501 2019-07-11 stsp char *cwd = NULL;
6556 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6557 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6558 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6559 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6560 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6561 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6562 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6563 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6564 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6565 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6566 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6567 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6568 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6569 818c7501 2019-07-11 stsp
6570 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6571 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6572 818c7501 2019-07-11 stsp
6573 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6574 818c7501 2019-07-11 stsp switch (ch) {
6575 818c7501 2019-07-11 stsp case 'a':
6576 818c7501 2019-07-11 stsp abort_rebase = 1;
6577 818c7501 2019-07-11 stsp break;
6578 818c7501 2019-07-11 stsp case 'c':
6579 818c7501 2019-07-11 stsp continue_rebase = 1;
6580 818c7501 2019-07-11 stsp break;
6581 818c7501 2019-07-11 stsp default:
6582 818c7501 2019-07-11 stsp usage_rebase();
6583 818c7501 2019-07-11 stsp /* NOTREACHED */
6584 818c7501 2019-07-11 stsp }
6585 818c7501 2019-07-11 stsp }
6586 818c7501 2019-07-11 stsp
6587 818c7501 2019-07-11 stsp argc -= optind;
6588 818c7501 2019-07-11 stsp argv += optind;
6589 818c7501 2019-07-11 stsp
6590 43012d58 2019-07-14 stsp #ifndef PROFILE
6591 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6592 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6593 43012d58 2019-07-14 stsp err(1, "pledge");
6594 43012d58 2019-07-14 stsp #endif
6595 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6596 818c7501 2019-07-11 stsp usage_rebase();
6597 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6598 818c7501 2019-07-11 stsp if (argc != 0)
6599 818c7501 2019-07-11 stsp usage_rebase();
6600 818c7501 2019-07-11 stsp } else if (argc != 1)
6601 818c7501 2019-07-11 stsp usage_rebase();
6602 818c7501 2019-07-11 stsp
6603 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6604 818c7501 2019-07-11 stsp if (cwd == NULL) {
6605 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6606 818c7501 2019-07-11 stsp goto done;
6607 818c7501 2019-07-11 stsp }
6608 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6609 818c7501 2019-07-11 stsp if (error)
6610 818c7501 2019-07-11 stsp goto done;
6611 818c7501 2019-07-11 stsp
6612 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6613 c9956ddf 2019-09-08 stsp NULL);
6614 818c7501 2019-07-11 stsp if (error != NULL)
6615 818c7501 2019-07-11 stsp goto done;
6616 818c7501 2019-07-11 stsp
6617 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6618 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6619 7ef62c4e 2020-02-24 stsp if (error)
6620 7ef62c4e 2020-02-24 stsp goto done;
6621 7ef62c4e 2020-02-24 stsp
6622 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6623 7ef62c4e 2020-02-24 stsp worktree);
6624 818c7501 2019-07-11 stsp if (error)
6625 7ef62c4e 2020-02-24 stsp goto done;
6626 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6627 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6628 818c7501 2019-07-11 stsp goto done;
6629 7ef62c4e 2020-02-24 stsp }
6630 818c7501 2019-07-11 stsp
6631 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6632 818c7501 2019-07-11 stsp if (error)
6633 818c7501 2019-07-11 stsp goto done;
6634 818c7501 2019-07-11 stsp
6635 f6794adc 2019-07-23 stsp if (abort_rebase) {
6636 818c7501 2019-07-11 stsp int did_something;
6637 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6638 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6639 f6794adc 2019-07-23 stsp goto done;
6640 f6794adc 2019-07-23 stsp }
6641 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6642 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6643 3e3a69f1 2019-07-25 stsp worktree, repo);
6644 818c7501 2019-07-11 stsp if (error)
6645 818c7501 2019-07-11 stsp goto done;
6646 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6647 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6648 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6649 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6650 818c7501 2019-07-11 stsp if (error)
6651 818c7501 2019-07-11 stsp goto done;
6652 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6653 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6654 818c7501 2019-07-11 stsp }
6655 818c7501 2019-07-11 stsp
6656 818c7501 2019-07-11 stsp if (continue_rebase) {
6657 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6658 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6659 f6794adc 2019-07-23 stsp goto done;
6660 f6794adc 2019-07-23 stsp }
6661 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6662 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6663 3e3a69f1 2019-07-25 stsp worktree, repo);
6664 818c7501 2019-07-11 stsp if (error)
6665 818c7501 2019-07-11 stsp goto done;
6666 818c7501 2019-07-11 stsp
6667 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6668 01757395 2019-07-12 stsp resume_commit_id, repo);
6669 818c7501 2019-07-11 stsp if (error)
6670 818c7501 2019-07-11 stsp goto done;
6671 818c7501 2019-07-11 stsp
6672 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6673 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6674 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6675 818c7501 2019-07-11 stsp goto done;
6676 818c7501 2019-07-11 stsp }
6677 818c7501 2019-07-11 stsp } else {
6678 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6679 818c7501 2019-07-11 stsp if (error != NULL)
6680 818c7501 2019-07-11 stsp goto done;
6681 ff0d2220 2019-07-11 stsp }
6682 818c7501 2019-07-11 stsp
6683 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6684 ff0d2220 2019-07-11 stsp if (error)
6685 ff0d2220 2019-07-11 stsp goto done;
6686 ff0d2220 2019-07-11 stsp
6687 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6688 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6689 a51a74b3 2019-07-27 stsp
6690 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6691 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6692 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6693 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6694 818c7501 2019-07-11 stsp if (error)
6695 818c7501 2019-07-11 stsp goto done;
6696 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6697 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6698 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6699 818c7501 2019-07-11 stsp "with work tree's branch");
6700 818c7501 2019-07-11 stsp goto done;
6701 818c7501 2019-07-11 stsp }
6702 818c7501 2019-07-11 stsp
6703 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6704 a51a74b3 2019-07-27 stsp if (error) {
6705 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6706 a51a74b3 2019-07-27 stsp goto done;
6707 a51a74b3 2019-07-27 stsp error = NULL;
6708 a51a74b3 2019-07-27 stsp } else {
6709 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6710 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6711 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6712 a51a74b3 2019-07-27 stsp goto done;
6713 a51a74b3 2019-07-27 stsp }
6714 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6715 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6716 818c7501 2019-07-11 stsp if (error)
6717 818c7501 2019-07-11 stsp goto done;
6718 818c7501 2019-07-11 stsp }
6719 818c7501 2019-07-11 stsp
6720 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6721 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6722 818c7501 2019-07-11 stsp if (error)
6723 818c7501 2019-07-11 stsp goto done;
6724 818c7501 2019-07-11 stsp
6725 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6726 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6727 fc66b545 2019-08-12 stsp if (pid == NULL) {
6728 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6729 fc66b545 2019-08-12 stsp int did_something;
6730 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6731 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6732 fc66b545 2019-08-12 stsp &did_something);
6733 fc66b545 2019-08-12 stsp if (error)
6734 fc66b545 2019-08-12 stsp goto done;
6735 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6736 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6737 fc66b545 2019-08-12 stsp }
6738 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6739 fc66b545 2019-08-12 stsp goto done;
6740 fc66b545 2019-08-12 stsp }
6741 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6742 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6743 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6744 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6745 818c7501 2019-07-11 stsp commit = NULL;
6746 818c7501 2019-07-11 stsp if (error)
6747 818c7501 2019-07-11 stsp goto done;
6748 64c6d990 2019-07-11 stsp
6749 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6750 38b0338b 2019-11-29 stsp if (continue_rebase) {
6751 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6752 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6753 38b0338b 2019-11-29 stsp goto done;
6754 38b0338b 2019-11-29 stsp } else {
6755 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6756 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6757 38b0338b 2019-11-29 stsp char *id_str;
6758 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6759 38b0338b 2019-11-29 stsp new_base_branch);
6760 38b0338b 2019-11-29 stsp if (error)
6761 38b0338b 2019-11-29 stsp goto done;
6762 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6763 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6764 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6765 38b0338b 2019-11-29 stsp free(id_str);
6766 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6767 38b0338b 2019-11-29 stsp new_head_commit_id);
6768 38b0338b 2019-11-29 stsp if (error)
6769 38b0338b 2019-11-29 stsp goto done;
6770 38b0338b 2019-11-29 stsp }
6771 818c7501 2019-07-11 stsp }
6772 818c7501 2019-07-11 stsp
6773 818c7501 2019-07-11 stsp pid = NULL;
6774 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6775 818c7501 2019-07-11 stsp commit_id = qid->id;
6776 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6777 818c7501 2019-07-11 stsp pid = qid;
6778 818c7501 2019-07-11 stsp
6779 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6780 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6781 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6782 818c7501 2019-07-11 stsp if (error)
6783 818c7501 2019-07-11 stsp goto done;
6784 818c7501 2019-07-11 stsp
6785 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6786 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6787 a0ea4fc0 2020-02-28 stsp if (error)
6788 a0ea4fc0 2020-02-28 stsp goto done;
6789 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6790 818c7501 2019-07-11 stsp break;
6791 01757395 2019-07-12 stsp }
6792 818c7501 2019-07-11 stsp
6793 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6794 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6795 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6796 818c7501 2019-07-11 stsp if (error)
6797 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp }
6799 818c7501 2019-07-11 stsp
6800 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6801 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6802 818c7501 2019-07-11 stsp if (error)
6803 818c7501 2019-07-11 stsp goto done;
6804 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6805 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6806 818c7501 2019-07-11 stsp } else
6807 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6808 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6809 818c7501 2019-07-11 stsp done:
6810 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6811 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6812 818c7501 2019-07-11 stsp free(resume_commit_id);
6813 818c7501 2019-07-11 stsp free(yca_id);
6814 818c7501 2019-07-11 stsp if (commit)
6815 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6816 818c7501 2019-07-11 stsp if (branch)
6817 818c7501 2019-07-11 stsp got_ref_close(branch);
6818 818c7501 2019-07-11 stsp if (new_base_branch)
6819 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6820 818c7501 2019-07-11 stsp if (tmp_branch)
6821 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6822 5ef14e63 2019-06-02 stsp if (worktree)
6823 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6824 5ef14e63 2019-06-02 stsp if (repo)
6825 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6826 5ef14e63 2019-06-02 stsp return error;
6827 0ebf8283 2019-07-24 stsp }
6828 0ebf8283 2019-07-24 stsp
6829 0ebf8283 2019-07-24 stsp __dead static void
6830 0ebf8283 2019-07-24 stsp usage_histedit(void)
6831 0ebf8283 2019-07-24 stsp {
6832 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6833 0ebf8283 2019-07-24 stsp getprogname());
6834 0ebf8283 2019-07-24 stsp exit(1);
6835 0ebf8283 2019-07-24 stsp }
6836 0ebf8283 2019-07-24 stsp
6837 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6838 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6839 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6840 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6841 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6842 0ebf8283 2019-07-24 stsp
6843 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6844 0ebf8283 2019-07-24 stsp unsigned char code;
6845 0ebf8283 2019-07-24 stsp const char *name;
6846 0ebf8283 2019-07-24 stsp const char *desc;
6847 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6848 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6849 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6850 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6851 82997472 2020-01-29 stsp "be used" },
6852 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6853 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6854 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6855 0ebf8283 2019-07-24 stsp };
6856 0ebf8283 2019-07-24 stsp
6857 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6858 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6859 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6860 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6861 0ebf8283 2019-07-24 stsp char *logmsg;
6862 0ebf8283 2019-07-24 stsp };
6863 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6864 0ebf8283 2019-07-24 stsp
6865 0ebf8283 2019-07-24 stsp static const struct got_error *
6866 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6867 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6868 0ebf8283 2019-07-24 stsp {
6869 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6870 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6871 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6872 8138f3e1 2019-08-11 stsp int n;
6873 0ebf8283 2019-07-24 stsp
6874 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6875 0ebf8283 2019-07-24 stsp if (err)
6876 0ebf8283 2019-07-24 stsp goto done;
6877 0ebf8283 2019-07-24 stsp
6878 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6879 0ebf8283 2019-07-24 stsp if (err)
6880 0ebf8283 2019-07-24 stsp goto done;
6881 0ebf8283 2019-07-24 stsp
6882 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6883 0ebf8283 2019-07-24 stsp if (err)
6884 0ebf8283 2019-07-24 stsp goto done;
6885 0ebf8283 2019-07-24 stsp
6886 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6887 0ebf8283 2019-07-24 stsp if (n < 0)
6888 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6889 0ebf8283 2019-07-24 stsp done:
6890 0ebf8283 2019-07-24 stsp if (commit)
6891 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6892 0ebf8283 2019-07-24 stsp free(id_str);
6893 0ebf8283 2019-07-24 stsp free(logmsg);
6894 0ebf8283 2019-07-24 stsp return err;
6895 0ebf8283 2019-07-24 stsp }
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp static const struct got_error *
6898 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6899 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6900 0ebf8283 2019-07-24 stsp {
6901 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6902 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6905 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6906 0ebf8283 2019-07-24 stsp
6907 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6908 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6909 0ebf8283 2019-07-24 stsp f, repo);
6910 0ebf8283 2019-07-24 stsp if (err)
6911 0ebf8283 2019-07-24 stsp break;
6912 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6913 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6914 083957f4 2020-02-24 stsp if (n < 0) {
6915 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6916 083957f4 2020-02-24 stsp break;
6917 083957f4 2020-02-24 stsp }
6918 083957f4 2020-02-24 stsp }
6919 0ebf8283 2019-07-24 stsp }
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp return err;
6922 0ebf8283 2019-07-24 stsp }
6923 0ebf8283 2019-07-24 stsp
6924 0ebf8283 2019-07-24 stsp static const struct got_error *
6925 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6926 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6927 0ebf8283 2019-07-24 stsp {
6928 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6929 0ebf8283 2019-07-24 stsp int n, i;
6930 514f2ffe 2020-01-29 stsp char *id_str;
6931 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6932 514f2ffe 2020-01-29 stsp
6933 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6934 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6935 514f2ffe 2020-01-29 stsp if (err)
6936 514f2ffe 2020-01-29 stsp return err;
6937 514f2ffe 2020-01-29 stsp
6938 514f2ffe 2020-01-29 stsp n = fprintf(f,
6939 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6940 514f2ffe 2020-01-29 stsp "# commit %s\n"
6941 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6942 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6943 514f2ffe 2020-01-29 stsp if (n < 0) {
6944 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6945 514f2ffe 2020-01-29 stsp goto done;
6946 514f2ffe 2020-01-29 stsp }
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6949 514f2ffe 2020-01-29 stsp if (n < 0) {
6950 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6951 514f2ffe 2020-01-29 stsp goto done;
6952 514f2ffe 2020-01-29 stsp }
6953 0ebf8283 2019-07-24 stsp
6954 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6955 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6956 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6957 0ebf8283 2019-07-24 stsp cmd->desc);
6958 0ebf8283 2019-07-24 stsp if (n < 0) {
6959 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6960 0ebf8283 2019-07-24 stsp break;
6961 0ebf8283 2019-07-24 stsp }
6962 0ebf8283 2019-07-24 stsp }
6963 514f2ffe 2020-01-29 stsp done:
6964 514f2ffe 2020-01-29 stsp free(id_str);
6965 0ebf8283 2019-07-24 stsp return err;
6966 0ebf8283 2019-07-24 stsp }
6967 0ebf8283 2019-07-24 stsp
6968 0ebf8283 2019-07-24 stsp static const struct got_error *
6969 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6970 0ebf8283 2019-07-24 stsp {
6971 0ebf8283 2019-07-24 stsp static char msg[42];
6972 0ebf8283 2019-07-24 stsp int ret;
6973 0ebf8283 2019-07-24 stsp
6974 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6975 0ebf8283 2019-07-24 stsp lineno);
6976 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6977 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6978 0ebf8283 2019-07-24 stsp
6979 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6980 0ebf8283 2019-07-24 stsp }
6981 0ebf8283 2019-07-24 stsp
6982 0ebf8283 2019-07-24 stsp static const struct got_error *
6983 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6984 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6985 0ebf8283 2019-07-24 stsp {
6986 0ebf8283 2019-07-24 stsp const struct got_error *err;
6987 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6988 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6989 0ebf8283 2019-07-24 stsp
6990 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6991 0ebf8283 2019-07-24 stsp if (err)
6992 0ebf8283 2019-07-24 stsp return err;
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6995 0ebf8283 2019-07-24 stsp if (err)
6996 0ebf8283 2019-07-24 stsp goto done;
6997 0ebf8283 2019-07-24 stsp
6998 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6999 5943eee2 2019-08-13 stsp if (err)
7000 5943eee2 2019-08-13 stsp goto done;
7001 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7002 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7003 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7004 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7005 0ebf8283 2019-07-24 stsp }
7006 0ebf8283 2019-07-24 stsp done:
7007 0ebf8283 2019-07-24 stsp if (folded_commit)
7008 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7009 0ebf8283 2019-07-24 stsp free(id_str);
7010 5943eee2 2019-08-13 stsp free(folded_logmsg);
7011 0ebf8283 2019-07-24 stsp return err;
7012 0ebf8283 2019-07-24 stsp }
7013 0ebf8283 2019-07-24 stsp
7014 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7015 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7016 0ebf8283 2019-07-24 stsp {
7017 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7018 0ebf8283 2019-07-24 stsp
7019 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7020 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7021 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7022 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7023 3f9de99f 2019-07-24 stsp folded = prev;
7024 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7025 0ebf8283 2019-07-24 stsp }
7026 0ebf8283 2019-07-24 stsp
7027 0ebf8283 2019-07-24 stsp return folded;
7028 0ebf8283 2019-07-24 stsp }
7029 0ebf8283 2019-07-24 stsp
7030 0ebf8283 2019-07-24 stsp static const struct got_error *
7031 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7032 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7033 0ebf8283 2019-07-24 stsp {
7034 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7035 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7036 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7037 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7038 0ebf8283 2019-07-24 stsp int fd;
7039 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7040 0ebf8283 2019-07-24 stsp
7041 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7042 0ebf8283 2019-07-24 stsp if (err)
7043 0ebf8283 2019-07-24 stsp return err;
7044 0ebf8283 2019-07-24 stsp
7045 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7046 0ebf8283 2019-07-24 stsp if (folded) {
7047 0ebf8283 2019-07-24 stsp while (folded != hle) {
7048 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7049 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7050 3f9de99f 2019-07-24 stsp continue;
7051 3f9de99f 2019-07-24 stsp }
7052 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7053 0ebf8283 2019-07-24 stsp logmsg, repo);
7054 0ebf8283 2019-07-24 stsp if (err)
7055 0ebf8283 2019-07-24 stsp goto done;
7056 0ebf8283 2019-07-24 stsp free(logmsg);
7057 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7058 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7059 0ebf8283 2019-07-24 stsp }
7060 0ebf8283 2019-07-24 stsp }
7061 0ebf8283 2019-07-24 stsp
7062 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7063 0ebf8283 2019-07-24 stsp if (err)
7064 0ebf8283 2019-07-24 stsp goto done;
7065 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7066 5943eee2 2019-08-13 stsp if (err)
7067 5943eee2 2019-08-13 stsp goto done;
7068 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7069 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7070 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7071 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7072 0ebf8283 2019-07-24 stsp goto done;
7073 0ebf8283 2019-07-24 stsp }
7074 0ebf8283 2019-07-24 stsp free(logmsg);
7075 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7076 0ebf8283 2019-07-24 stsp
7077 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7078 0ebf8283 2019-07-24 stsp if (err)
7079 0ebf8283 2019-07-24 stsp goto done;
7080 0ebf8283 2019-07-24 stsp
7081 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7082 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7083 0ebf8283 2019-07-24 stsp if (err)
7084 0ebf8283 2019-07-24 stsp goto done;
7085 0ebf8283 2019-07-24 stsp
7086 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7087 0ebf8283 2019-07-24 stsp close(fd);
7088 0ebf8283 2019-07-24 stsp
7089 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7090 0ebf8283 2019-07-24 stsp if (err)
7091 0ebf8283 2019-07-24 stsp goto done;
7092 0ebf8283 2019-07-24 stsp
7093 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7094 0ebf8283 2019-07-24 stsp if (err) {
7095 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7096 0ebf8283 2019-07-24 stsp goto done;
7097 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7098 0ebf8283 2019-07-24 stsp }
7099 0ebf8283 2019-07-24 stsp done:
7100 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7101 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7102 0ebf8283 2019-07-24 stsp free(logmsg_path);
7103 0ebf8283 2019-07-24 stsp free(logmsg);
7104 5943eee2 2019-08-13 stsp free(orig_logmsg);
7105 0ebf8283 2019-07-24 stsp free(editor);
7106 0ebf8283 2019-07-24 stsp if (commit)
7107 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7108 0ebf8283 2019-07-24 stsp return err;
7109 5ef14e63 2019-06-02 stsp }
7110 0ebf8283 2019-07-24 stsp
7111 0ebf8283 2019-07-24 stsp static const struct got_error *
7112 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7113 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7114 0ebf8283 2019-07-24 stsp {
7115 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7116 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7117 0ebf8283 2019-07-24 stsp size_t size;
7118 0ebf8283 2019-07-24 stsp ssize_t len;
7119 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7120 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7121 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7122 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7123 0ebf8283 2019-07-24 stsp
7124 0ebf8283 2019-07-24 stsp for (;;) {
7125 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7126 0ebf8283 2019-07-24 stsp if (len == -1) {
7127 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7128 0ebf8283 2019-07-24 stsp if (feof(f))
7129 0ebf8283 2019-07-24 stsp break;
7130 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7131 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7132 0ebf8283 2019-07-24 stsp break;
7133 0ebf8283 2019-07-24 stsp }
7134 0ebf8283 2019-07-24 stsp lineno++;
7135 0ebf8283 2019-07-24 stsp p = line;
7136 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7137 0ebf8283 2019-07-24 stsp p++;
7138 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7139 0ebf8283 2019-07-24 stsp free(line);
7140 0ebf8283 2019-07-24 stsp line = NULL;
7141 0ebf8283 2019-07-24 stsp continue;
7142 0ebf8283 2019-07-24 stsp }
7143 0ebf8283 2019-07-24 stsp cmd = NULL;
7144 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7145 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7146 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7147 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7148 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7149 0ebf8283 2019-07-24 stsp break;
7150 0ebf8283 2019-07-24 stsp }
7151 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7152 0ebf8283 2019-07-24 stsp p++;
7153 0ebf8283 2019-07-24 stsp break;
7154 0ebf8283 2019-07-24 stsp }
7155 0ebf8283 2019-07-24 stsp }
7156 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7157 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7158 0ebf8283 2019-07-24 stsp break;
7159 0ebf8283 2019-07-24 stsp }
7160 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7161 0ebf8283 2019-07-24 stsp p++;
7162 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7163 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7164 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7165 0ebf8283 2019-07-24 stsp break;
7166 0ebf8283 2019-07-24 stsp }
7167 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7168 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7169 0ebf8283 2019-07-24 stsp if (err)
7170 0ebf8283 2019-07-24 stsp break;
7171 0ebf8283 2019-07-24 stsp } else {
7172 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7173 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7174 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7175 0ebf8283 2019-07-24 stsp break;
7176 0ebf8283 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp }
7178 0ebf8283 2019-07-24 stsp free(line);
7179 0ebf8283 2019-07-24 stsp line = NULL;
7180 0ebf8283 2019-07-24 stsp continue;
7181 0ebf8283 2019-07-24 stsp } else {
7182 0ebf8283 2019-07-24 stsp end = p;
7183 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7184 0ebf8283 2019-07-24 stsp end++;
7185 0ebf8283 2019-07-24 stsp *end = '\0';
7186 0ebf8283 2019-07-24 stsp
7187 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7188 0ebf8283 2019-07-24 stsp if (err) {
7189 0ebf8283 2019-07-24 stsp /* override error code */
7190 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7191 0ebf8283 2019-07-24 stsp break;
7192 0ebf8283 2019-07-24 stsp }
7193 0ebf8283 2019-07-24 stsp }
7194 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7195 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7196 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7197 0ebf8283 2019-07-24 stsp break;
7198 0ebf8283 2019-07-24 stsp }
7199 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7200 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7201 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7202 0ebf8283 2019-07-24 stsp commit_id = NULL;
7203 0ebf8283 2019-07-24 stsp free(line);
7204 0ebf8283 2019-07-24 stsp line = NULL;
7205 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7206 0ebf8283 2019-07-24 stsp }
7207 0ebf8283 2019-07-24 stsp
7208 0ebf8283 2019-07-24 stsp free(line);
7209 0ebf8283 2019-07-24 stsp free(commit_id);
7210 0ebf8283 2019-07-24 stsp return err;
7211 0ebf8283 2019-07-24 stsp }
7212 0ebf8283 2019-07-24 stsp
7213 0ebf8283 2019-07-24 stsp static const struct got_error *
7214 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7215 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7216 bfce7f83 2019-07-27 stsp {
7217 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7218 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7219 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7220 5b87815e 2020-03-05 stsp static char msg[92];
7221 bfce7f83 2019-07-27 stsp char *id_str;
7222 bfce7f83 2019-07-27 stsp
7223 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7224 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7225 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7226 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7227 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7228 5b87815e 2020-03-05 stsp
7229 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7230 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7231 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7232 5b87815e 2020-03-05 stsp if (hle == hle2)
7233 5b87815e 2020-03-05 stsp continue;
7234 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7235 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7236 5b87815e 2020-03-05 stsp continue;
7237 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7238 5b87815e 2020-03-05 stsp if (err)
7239 5b87815e 2020-03-05 stsp return err;
7240 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7241 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7242 5b87815e 2020-03-05 stsp free(id_str);
7243 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7244 5b87815e 2020-03-05 stsp }
7245 5b87815e 2020-03-05 stsp }
7246 bfce7f83 2019-07-27 stsp
7247 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7248 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7249 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7250 bfce7f83 2019-07-27 stsp break;
7251 bfce7f83 2019-07-27 stsp }
7252 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7253 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7254 bfce7f83 2019-07-27 stsp if (err)
7255 bfce7f83 2019-07-27 stsp return err;
7256 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7257 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7258 bfce7f83 2019-07-27 stsp free(id_str);
7259 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7260 bfce7f83 2019-07-27 stsp }
7261 bfce7f83 2019-07-27 stsp }
7262 bfce7f83 2019-07-27 stsp
7263 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7264 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7265 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7266 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7267 bfce7f83 2019-07-27 stsp
7268 bfce7f83 2019-07-27 stsp return NULL;
7269 bfce7f83 2019-07-27 stsp }
7270 bfce7f83 2019-07-27 stsp
7271 bfce7f83 2019-07-27 stsp static const struct got_error *
7272 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7273 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7274 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7275 0ebf8283 2019-07-24 stsp {
7276 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7277 0ebf8283 2019-07-24 stsp char *editor;
7278 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7279 0ebf8283 2019-07-24 stsp
7280 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7281 0ebf8283 2019-07-24 stsp if (err)
7282 0ebf8283 2019-07-24 stsp return err;
7283 0ebf8283 2019-07-24 stsp
7284 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7285 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7286 0ebf8283 2019-07-24 stsp goto done;
7287 0ebf8283 2019-07-24 stsp }
7288 0ebf8283 2019-07-24 stsp
7289 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7290 0ebf8283 2019-07-24 stsp if (f == NULL) {
7291 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7292 0ebf8283 2019-07-24 stsp goto done;
7293 0ebf8283 2019-07-24 stsp }
7294 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7295 bfce7f83 2019-07-27 stsp if (err)
7296 bfce7f83 2019-07-27 stsp goto done;
7297 bfce7f83 2019-07-27 stsp
7298 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7299 0ebf8283 2019-07-24 stsp done:
7300 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7301 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7302 0ebf8283 2019-07-24 stsp free(editor);
7303 0ebf8283 2019-07-24 stsp return err;
7304 0ebf8283 2019-07-24 stsp }
7305 0ebf8283 2019-07-24 stsp
7306 0ebf8283 2019-07-24 stsp static const struct got_error *
7307 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7308 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7309 514f2ffe 2020-01-29 stsp struct got_repository *);
7310 0ebf8283 2019-07-24 stsp
7311 0ebf8283 2019-07-24 stsp static const struct got_error *
7312 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7313 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7314 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7315 0ebf8283 2019-07-24 stsp {
7316 0ebf8283 2019-07-24 stsp const struct got_error *err;
7317 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7318 0ebf8283 2019-07-24 stsp char *path = NULL;
7319 0ebf8283 2019-07-24 stsp
7320 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7321 0ebf8283 2019-07-24 stsp if (err)
7322 0ebf8283 2019-07-24 stsp return err;
7323 0ebf8283 2019-07-24 stsp
7324 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7325 0ebf8283 2019-07-24 stsp if (err)
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp
7328 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7329 0ebf8283 2019-07-24 stsp if (err)
7330 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp
7332 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7333 083957f4 2020-02-24 stsp rewind(f);
7334 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7335 083957f4 2020-02-24 stsp } else {
7336 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7337 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7338 0ebf8283 2019-07-24 stsp goto done;
7339 083957f4 2020-02-24 stsp }
7340 083957f4 2020-02-24 stsp f = NULL;
7341 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7342 083957f4 2020-02-24 stsp if (err) {
7343 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7344 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7345 083957f4 2020-02-24 stsp goto done;
7346 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7347 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7348 083957f4 2020-02-24 stsp }
7349 0ebf8283 2019-07-24 stsp }
7350 0ebf8283 2019-07-24 stsp done:
7351 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7352 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7353 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7354 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7355 0ebf8283 2019-07-24 stsp free(path);
7356 0ebf8283 2019-07-24 stsp return err;
7357 0ebf8283 2019-07-24 stsp }
7358 0ebf8283 2019-07-24 stsp
7359 0ebf8283 2019-07-24 stsp static const struct got_error *
7360 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7361 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7362 0ebf8283 2019-07-24 stsp {
7363 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7364 0ebf8283 2019-07-24 stsp char *path = NULL;
7365 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7366 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7367 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7368 0ebf8283 2019-07-24 stsp
7369 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7370 0ebf8283 2019-07-24 stsp if (err)
7371 0ebf8283 2019-07-24 stsp return err;
7372 0ebf8283 2019-07-24 stsp
7373 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7374 0ebf8283 2019-07-24 stsp if (f == NULL) {
7375 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7376 0ebf8283 2019-07-24 stsp goto done;
7377 0ebf8283 2019-07-24 stsp }
7378 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7379 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7380 0ebf8283 2019-07-24 stsp repo);
7381 0ebf8283 2019-07-24 stsp if (err)
7382 0ebf8283 2019-07-24 stsp break;
7383 0ebf8283 2019-07-24 stsp
7384 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7385 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7386 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7387 0ebf8283 2019-07-24 stsp if (n < 0) {
7388 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7389 0ebf8283 2019-07-24 stsp break;
7390 0ebf8283 2019-07-24 stsp }
7391 0ebf8283 2019-07-24 stsp }
7392 0ebf8283 2019-07-24 stsp }
7393 0ebf8283 2019-07-24 stsp done:
7394 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7395 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7396 0ebf8283 2019-07-24 stsp free(path);
7397 0ebf8283 2019-07-24 stsp if (commit)
7398 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7399 0ebf8283 2019-07-24 stsp return err;
7400 0ebf8283 2019-07-24 stsp }
7401 0ebf8283 2019-07-24 stsp
7402 bfce7f83 2019-07-27 stsp void
7403 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7404 bfce7f83 2019-07-27 stsp {
7405 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7406 bfce7f83 2019-07-27 stsp
7407 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7408 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7409 bfce7f83 2019-07-27 stsp free(hle);
7410 bfce7f83 2019-07-27 stsp }
7411 bfce7f83 2019-07-27 stsp }
7412 bfce7f83 2019-07-27 stsp
7413 0ebf8283 2019-07-24 stsp static const struct got_error *
7414 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7415 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7416 0ebf8283 2019-07-24 stsp {
7417 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7418 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7419 0ebf8283 2019-07-24 stsp
7420 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7421 0ebf8283 2019-07-24 stsp if (f == NULL) {
7422 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7423 0ebf8283 2019-07-24 stsp goto done;
7424 0ebf8283 2019-07-24 stsp }
7425 0ebf8283 2019-07-24 stsp
7426 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7427 0ebf8283 2019-07-24 stsp done:
7428 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7429 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7430 0ebf8283 2019-07-24 stsp return err;
7431 0ebf8283 2019-07-24 stsp }
7432 0ebf8283 2019-07-24 stsp
7433 0ebf8283 2019-07-24 stsp static const struct got_error *
7434 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7435 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7436 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7437 0ebf8283 2019-07-24 stsp {
7438 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7439 0ebf8283 2019-07-24 stsp int resp = ' ';
7440 0ebf8283 2019-07-24 stsp
7441 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7442 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7443 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7444 0ebf8283 2019-07-24 stsp resp = getchar();
7445 a61a4414 2019-08-07 stsp if (resp == '\n')
7446 a61a4414 2019-08-07 stsp resp = getchar();
7447 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7448 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7449 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7450 bfce7f83 2019-07-27 stsp repo);
7451 426ebf2e 2019-07-25 stsp if (err) {
7452 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7453 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7454 426ebf2e 2019-07-25 stsp break;
7455 bfce7f83 2019-07-27 stsp prev_err = err;
7456 426ebf2e 2019-07-25 stsp resp = ' ';
7457 426ebf2e 2019-07-25 stsp continue;
7458 426ebf2e 2019-07-25 stsp }
7459 bfce7f83 2019-07-27 stsp break;
7460 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7461 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7462 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7463 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7464 426ebf2e 2019-07-25 stsp if (err) {
7465 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7466 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7467 426ebf2e 2019-07-25 stsp break;
7468 bfce7f83 2019-07-27 stsp prev_err = err;
7469 426ebf2e 2019-07-25 stsp resp = ' ';
7470 426ebf2e 2019-07-25 stsp continue;
7471 426ebf2e 2019-07-25 stsp }
7472 bfce7f83 2019-07-27 stsp break;
7473 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7474 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7475 0ebf8283 2019-07-24 stsp break;
7476 426ebf2e 2019-07-25 stsp } else
7477 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7478 0ebf8283 2019-07-24 stsp }
7479 0ebf8283 2019-07-24 stsp
7480 0ebf8283 2019-07-24 stsp return err;
7481 0ebf8283 2019-07-24 stsp }
7482 0ebf8283 2019-07-24 stsp
7483 0ebf8283 2019-07-24 stsp static const struct got_error *
7484 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7485 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7486 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7487 0ebf8283 2019-07-24 stsp {
7488 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7489 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7490 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7491 3e3a69f1 2019-07-25 stsp branch, repo);
7492 0ebf8283 2019-07-24 stsp }
7493 0ebf8283 2019-07-24 stsp
7494 0ebf8283 2019-07-24 stsp static const struct got_error *
7495 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7496 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7497 0ebf8283 2019-07-24 stsp {
7498 0ebf8283 2019-07-24 stsp const struct got_error *err;
7499 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7500 0ebf8283 2019-07-24 stsp
7501 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7502 0ebf8283 2019-07-24 stsp if (err)
7503 0ebf8283 2019-07-24 stsp goto done;
7504 0ebf8283 2019-07-24 stsp
7505 0ebf8283 2019-07-24 stsp if (new_id) {
7506 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7507 0ebf8283 2019-07-24 stsp if (err)
7508 0ebf8283 2019-07-24 stsp goto done;
7509 0ebf8283 2019-07-24 stsp }
7510 0ebf8283 2019-07-24 stsp
7511 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7512 0ebf8283 2019-07-24 stsp if (new_id_str)
7513 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7514 0ebf8283 2019-07-24 stsp
7515 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7516 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7517 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7518 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7519 0ebf8283 2019-07-24 stsp goto done;
7520 0ebf8283 2019-07-24 stsp }
7521 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7522 0ebf8283 2019-07-24 stsp } else {
7523 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7524 0ebf8283 2019-07-24 stsp if (err)
7525 0ebf8283 2019-07-24 stsp goto done;
7526 0ebf8283 2019-07-24 stsp }
7527 0ebf8283 2019-07-24 stsp
7528 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7529 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7530 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7531 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7532 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7533 0ebf8283 2019-07-24 stsp break;
7534 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7535 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7536 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7537 0ebf8283 2019-07-24 stsp logmsg);
7538 0ebf8283 2019-07-24 stsp break;
7539 0ebf8283 2019-07-24 stsp default:
7540 0ebf8283 2019-07-24 stsp break;
7541 0ebf8283 2019-07-24 stsp }
7542 0ebf8283 2019-07-24 stsp done:
7543 0ebf8283 2019-07-24 stsp free(old_id_str);
7544 0ebf8283 2019-07-24 stsp free(new_id_str);
7545 0ebf8283 2019-07-24 stsp return err;
7546 0ebf8283 2019-07-24 stsp }
7547 0ebf8283 2019-07-24 stsp
7548 0ebf8283 2019-07-24 stsp static const struct got_error *
7549 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7550 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7551 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7552 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7553 0ebf8283 2019-07-24 stsp {
7554 0ebf8283 2019-07-24 stsp const struct got_error *err;
7555 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7556 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7557 0ebf8283 2019-07-24 stsp
7558 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7559 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7560 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7561 0ebf8283 2019-07-24 stsp if (err)
7562 0ebf8283 2019-07-24 stsp return err;
7563 0ebf8283 2019-07-24 stsp }
7564 0ebf8283 2019-07-24 stsp
7565 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7566 0ebf8283 2019-07-24 stsp if (err)
7567 0ebf8283 2019-07-24 stsp return err;
7568 0ebf8283 2019-07-24 stsp
7569 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7570 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7571 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7572 0ebf8283 2019-07-24 stsp if (err) {
7573 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7574 0ebf8283 2019-07-24 stsp goto done;
7575 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7576 0ebf8283 2019-07-24 stsp } else {
7577 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7578 0ebf8283 2019-07-24 stsp free(new_commit_id);
7579 0ebf8283 2019-07-24 stsp }
7580 0ebf8283 2019-07-24 stsp done:
7581 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7582 0ebf8283 2019-07-24 stsp return err;
7583 0ebf8283 2019-07-24 stsp }
7584 0ebf8283 2019-07-24 stsp
7585 0ebf8283 2019-07-24 stsp static const struct got_error *
7586 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7587 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7588 0ebf8283 2019-07-24 stsp {
7589 0ebf8283 2019-07-24 stsp const struct got_error *error;
7590 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7591 0ebf8283 2019-07-24 stsp
7592 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7593 0ebf8283 2019-07-24 stsp repo);
7594 0ebf8283 2019-07-24 stsp if (error)
7595 0ebf8283 2019-07-24 stsp return error;
7596 0ebf8283 2019-07-24 stsp
7597 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7598 0ebf8283 2019-07-24 stsp if (error)
7599 0ebf8283 2019-07-24 stsp return error;
7600 0ebf8283 2019-07-24 stsp
7601 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7602 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7603 0ebf8283 2019-07-24 stsp return error;
7604 ab20a43a 2020-01-29 stsp }
7605 ab20a43a 2020-01-29 stsp
7606 ab20a43a 2020-01-29 stsp static const struct got_error *
7607 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7608 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7609 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7610 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7611 ab20a43a 2020-01-29 stsp {
7612 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7613 ab20a43a 2020-01-29 stsp
7614 ab20a43a 2020-01-29 stsp switch (status) {
7615 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7616 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7617 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7618 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7619 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7620 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7621 ab20a43a 2020-01-29 stsp default:
7622 ab20a43a 2020-01-29 stsp break;
7623 ab20a43a 2020-01-29 stsp }
7624 ab20a43a 2020-01-29 stsp
7625 ab20a43a 2020-01-29 stsp switch (staged_status) {
7626 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7627 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7628 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7629 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7630 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7631 ab20a43a 2020-01-29 stsp default:
7632 ab20a43a 2020-01-29 stsp break;
7633 ab20a43a 2020-01-29 stsp }
7634 ab20a43a 2020-01-29 stsp
7635 ab20a43a 2020-01-29 stsp return NULL;
7636 0ebf8283 2019-07-24 stsp }
7637 0ebf8283 2019-07-24 stsp
7638 0ebf8283 2019-07-24 stsp static const struct got_error *
7639 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7640 0ebf8283 2019-07-24 stsp {
7641 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7642 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7643 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7644 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7645 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7646 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7647 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7648 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7649 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7650 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7651 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7652 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7653 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7654 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7655 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7656 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7657 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7658 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7659 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7660 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7661 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7662 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7663 0ebf8283 2019-07-24 stsp
7664 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7665 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7666 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7667 0ebf8283 2019-07-24 stsp
7668 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7669 0ebf8283 2019-07-24 stsp switch (ch) {
7670 0ebf8283 2019-07-24 stsp case 'a':
7671 0ebf8283 2019-07-24 stsp abort_edit = 1;
7672 0ebf8283 2019-07-24 stsp break;
7673 0ebf8283 2019-07-24 stsp case 'c':
7674 0ebf8283 2019-07-24 stsp continue_edit = 1;
7675 0ebf8283 2019-07-24 stsp break;
7676 0ebf8283 2019-07-24 stsp case 'F':
7677 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7678 0ebf8283 2019-07-24 stsp break;
7679 083957f4 2020-02-24 stsp case 'm':
7680 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7681 083957f4 2020-02-24 stsp break;
7682 0ebf8283 2019-07-24 stsp default:
7683 0ebf8283 2019-07-24 stsp usage_histedit();
7684 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7685 0ebf8283 2019-07-24 stsp }
7686 0ebf8283 2019-07-24 stsp }
7687 0ebf8283 2019-07-24 stsp
7688 0ebf8283 2019-07-24 stsp argc -= optind;
7689 0ebf8283 2019-07-24 stsp argv += optind;
7690 0ebf8283 2019-07-24 stsp
7691 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7692 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7693 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7694 0ebf8283 2019-07-24 stsp err(1, "pledge");
7695 0ebf8283 2019-07-24 stsp #endif
7696 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7697 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7698 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7699 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7700 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7701 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7702 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7703 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7704 0ebf8283 2019-07-24 stsp if (argc != 0)
7705 0ebf8283 2019-07-24 stsp usage_histedit();
7706 0ebf8283 2019-07-24 stsp
7707 0ebf8283 2019-07-24 stsp /*
7708 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7709 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7710 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7711 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7712 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7713 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7714 0ebf8283 2019-07-24 stsp */
7715 0ebf8283 2019-07-24 stsp
7716 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7717 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7718 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7719 0ebf8283 2019-07-24 stsp goto done;
7720 0ebf8283 2019-07-24 stsp }
7721 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7722 0ebf8283 2019-07-24 stsp if (error)
7723 0ebf8283 2019-07-24 stsp goto done;
7724 0ebf8283 2019-07-24 stsp
7725 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7726 c9956ddf 2019-09-08 stsp NULL);
7727 0ebf8283 2019-07-24 stsp if (error != NULL)
7728 0ebf8283 2019-07-24 stsp goto done;
7729 0ebf8283 2019-07-24 stsp
7730 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7731 0ebf8283 2019-07-24 stsp if (error)
7732 0ebf8283 2019-07-24 stsp goto done;
7733 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7734 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7735 0ebf8283 2019-07-24 stsp goto done;
7736 0ebf8283 2019-07-24 stsp }
7737 0ebf8283 2019-07-24 stsp
7738 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7739 0ebf8283 2019-07-24 stsp if (error)
7740 0ebf8283 2019-07-24 stsp goto done;
7741 0ebf8283 2019-07-24 stsp
7742 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7743 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7744 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7745 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7746 083957f4 2020-02-24 stsp "before the -m option can be used");
7747 083957f4 2020-02-24 stsp goto done;
7748 083957f4 2020-02-24 stsp }
7749 083957f4 2020-02-24 stsp
7750 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7751 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7752 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7753 3e3a69f1 2019-07-25 stsp worktree, repo);
7754 0ebf8283 2019-07-24 stsp if (error)
7755 0ebf8283 2019-07-24 stsp goto done;
7756 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7757 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7758 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7759 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7760 0ebf8283 2019-07-24 stsp if (error)
7761 0ebf8283 2019-07-24 stsp goto done;
7762 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7763 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7764 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7765 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7766 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7767 0ebf8283 2019-07-24 stsp goto done;
7768 0ebf8283 2019-07-24 stsp }
7769 0ebf8283 2019-07-24 stsp
7770 0ebf8283 2019-07-24 stsp if (continue_edit) {
7771 0ebf8283 2019-07-24 stsp char *path;
7772 0ebf8283 2019-07-24 stsp
7773 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7774 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7775 0ebf8283 2019-07-24 stsp goto done;
7776 0ebf8283 2019-07-24 stsp }
7777 0ebf8283 2019-07-24 stsp
7778 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7779 0ebf8283 2019-07-24 stsp if (error)
7780 0ebf8283 2019-07-24 stsp goto done;
7781 0ebf8283 2019-07-24 stsp
7782 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7783 0ebf8283 2019-07-24 stsp free(path);
7784 0ebf8283 2019-07-24 stsp if (error)
7785 0ebf8283 2019-07-24 stsp goto done;
7786 0ebf8283 2019-07-24 stsp
7787 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7788 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7789 3e3a69f1 2019-07-25 stsp worktree, repo);
7790 0ebf8283 2019-07-24 stsp if (error)
7791 0ebf8283 2019-07-24 stsp goto done;
7792 0ebf8283 2019-07-24 stsp
7793 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7794 0ebf8283 2019-07-24 stsp if (error)
7795 0ebf8283 2019-07-24 stsp goto done;
7796 0ebf8283 2019-07-24 stsp
7797 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7798 0ebf8283 2019-07-24 stsp head_commit_id);
7799 0ebf8283 2019-07-24 stsp if (error)
7800 0ebf8283 2019-07-24 stsp goto done;
7801 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7802 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7803 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7804 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7805 3aac7cf7 2019-07-25 stsp goto done;
7806 3aac7cf7 2019-07-25 stsp }
7807 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7808 8ca9bd68 2019-07-25 stsp base_commit_id, 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 } else {
7815 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7816 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7817 0ebf8283 2019-07-24 stsp goto done;
7818 0ebf8283 2019-07-24 stsp }
7819 0ebf8283 2019-07-24 stsp
7820 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7821 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7822 0ebf8283 2019-07-24 stsp if (error != NULL)
7823 0ebf8283 2019-07-24 stsp goto done;
7824 0ebf8283 2019-07-24 stsp
7825 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7826 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7827 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7828 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7829 c7d20a3f 2019-07-30 stsp goto done;
7830 c7d20a3f 2019-07-30 stsp }
7831 c7d20a3f 2019-07-30 stsp
7832 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7833 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7834 bfce7f83 2019-07-27 stsp branch = NULL;
7835 0ebf8283 2019-07-24 stsp if (error)
7836 0ebf8283 2019-07-24 stsp goto done;
7837 0ebf8283 2019-07-24 stsp
7838 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7839 0ebf8283 2019-07-24 stsp head_commit_id);
7840 0ebf8283 2019-07-24 stsp if (error)
7841 0ebf8283 2019-07-24 stsp goto done;
7842 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7843 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7844 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7845 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7846 3aac7cf7 2019-07-25 stsp goto done;
7847 3aac7cf7 2019-07-25 stsp }
7848 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7849 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7850 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7851 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7852 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7853 0ebf8283 2019-07-24 stsp commit = NULL;
7854 0ebf8283 2019-07-24 stsp if (error)
7855 0ebf8283 2019-07-24 stsp goto done;
7856 0ebf8283 2019-07-24 stsp
7857 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7858 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7859 c5996fff 2020-01-29 stsp goto done;
7860 c5996fff 2020-01-29 stsp }
7861 c5996fff 2020-01-29 stsp
7862 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7863 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7864 bfce7f83 2019-07-27 stsp if (error)
7865 bfce7f83 2019-07-27 stsp goto done;
7866 bfce7f83 2019-07-27 stsp
7867 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7868 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7869 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7870 6ba2bea2 2019-07-27 stsp if (error) {
7871 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7872 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7873 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7874 0ebf8283 2019-07-24 stsp goto done;
7875 6ba2bea2 2019-07-27 stsp }
7876 0ebf8283 2019-07-24 stsp } else {
7877 514f2ffe 2020-01-29 stsp const char *branch_name;
7878 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7879 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7880 514f2ffe 2020-01-29 stsp branch_name += 11;
7881 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7882 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7883 6ba2bea2 2019-07-27 stsp if (error) {
7884 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7885 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7886 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7887 0ebf8283 2019-07-24 stsp goto done;
7888 6ba2bea2 2019-07-27 stsp }
7889 0ebf8283 2019-07-24 stsp
7890 0ebf8283 2019-07-24 stsp }
7891 0ebf8283 2019-07-24 stsp
7892 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7893 0ebf8283 2019-07-24 stsp repo);
7894 6ba2bea2 2019-07-27 stsp if (error) {
7895 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7896 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7897 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7898 0ebf8283 2019-07-24 stsp goto done;
7899 6ba2bea2 2019-07-27 stsp }
7900 0ebf8283 2019-07-24 stsp
7901 0ebf8283 2019-07-24 stsp }
7902 0ebf8283 2019-07-24 stsp
7903 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7904 4ec14e60 2019-08-28 hiltjo if (error)
7905 0ebf8283 2019-07-24 stsp goto done;
7906 0ebf8283 2019-07-24 stsp
7907 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7908 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7909 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7910 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7911 0ebf8283 2019-07-24 stsp continue;
7912 0ebf8283 2019-07-24 stsp
7913 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7914 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7915 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7916 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7917 0ebf8283 2019-07-24 stsp repo);
7918 ab20a43a 2020-01-29 stsp if (error)
7919 ab20a43a 2020-01-29 stsp goto done;
7920 0ebf8283 2019-07-24 stsp } else {
7921 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7922 ab20a43a 2020-01-29 stsp int have_changes = 0;
7923 ab20a43a 2020-01-29 stsp
7924 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7925 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7926 ab20a43a 2020-01-29 stsp if (error)
7927 ab20a43a 2020-01-29 stsp goto done;
7928 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7929 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7930 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7931 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7932 ab20a43a 2020-01-29 stsp if (error) {
7933 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7934 ab20a43a 2020-01-29 stsp goto done;
7935 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7936 ab20a43a 2020-01-29 stsp goto done;
7937 ab20a43a 2020-01-29 stsp }
7938 ab20a43a 2020-01-29 stsp if (have_changes) {
7939 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7940 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7941 ab20a43a 2020-01-29 stsp if (error)
7942 ab20a43a 2020-01-29 stsp goto done;
7943 ab20a43a 2020-01-29 stsp } else {
7944 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7945 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7946 ab20a43a 2020-01-29 stsp if (error)
7947 ab20a43a 2020-01-29 stsp goto done;
7948 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7949 ab20a43a 2020-01-29 stsp hle, NULL);
7950 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7951 ab20a43a 2020-01-29 stsp commit = NULL;
7952 ab20a43a 2020-01-29 stsp if (error)
7953 ab20a43a 2020-01-29 stsp goto done;
7954 ab20a43a 2020-01-29 stsp }
7955 0ebf8283 2019-07-24 stsp }
7956 0ebf8283 2019-07-24 stsp continue;
7957 0ebf8283 2019-07-24 stsp }
7958 0ebf8283 2019-07-24 stsp
7959 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7960 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7961 0ebf8283 2019-07-24 stsp if (error)
7962 0ebf8283 2019-07-24 stsp goto done;
7963 0ebf8283 2019-07-24 stsp continue;
7964 0ebf8283 2019-07-24 stsp }
7965 0ebf8283 2019-07-24 stsp
7966 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7967 0ebf8283 2019-07-24 stsp hle->commit_id);
7968 0ebf8283 2019-07-24 stsp if (error)
7969 0ebf8283 2019-07-24 stsp goto done;
7970 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7971 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7972 0ebf8283 2019-07-24 stsp
7973 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7974 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7975 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7976 0ebf8283 2019-07-24 stsp if (error)
7977 0ebf8283 2019-07-24 stsp goto done;
7978 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7979 0ebf8283 2019-07-24 stsp commit = NULL;
7980 0ebf8283 2019-07-24 stsp
7981 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7982 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7983 a0ea4fc0 2020-02-28 stsp repo);
7984 a0ea4fc0 2020-02-28 stsp if (error)
7985 a0ea4fc0 2020-02-28 stsp goto done;
7986 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7987 0ebf8283 2019-07-24 stsp break;
7988 0ebf8283 2019-07-24 stsp }
7989 0ebf8283 2019-07-24 stsp
7990 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7991 0ebf8283 2019-07-24 stsp char *id_str;
7992 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7993 0ebf8283 2019-07-24 stsp if (error)
7994 0ebf8283 2019-07-24 stsp goto done;
7995 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7996 0ebf8283 2019-07-24 stsp id_str);
7997 0ebf8283 2019-07-24 stsp free(id_str);
7998 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7999 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8000 3e3a69f1 2019-07-25 stsp fileindex);
8001 0ebf8283 2019-07-24 stsp goto done;
8002 0ebf8283 2019-07-24 stsp }
8003 0ebf8283 2019-07-24 stsp
8004 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8005 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8006 0ebf8283 2019-07-24 stsp if (error)
8007 0ebf8283 2019-07-24 stsp goto done;
8008 0ebf8283 2019-07-24 stsp continue;
8009 0ebf8283 2019-07-24 stsp }
8010 0ebf8283 2019-07-24 stsp
8011 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8012 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8013 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8014 0ebf8283 2019-07-24 stsp if (error)
8015 0ebf8283 2019-07-24 stsp goto done;
8016 0ebf8283 2019-07-24 stsp }
8017 0ebf8283 2019-07-24 stsp
8018 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8019 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8020 0ebf8283 2019-07-24 stsp if (error)
8021 0ebf8283 2019-07-24 stsp goto done;
8022 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8023 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8024 0ebf8283 2019-07-24 stsp } else
8025 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8026 3e3a69f1 2019-07-25 stsp branch, repo);
8027 0ebf8283 2019-07-24 stsp done:
8028 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8029 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8030 0ebf8283 2019-07-24 stsp free(head_commit_id);
8031 0ebf8283 2019-07-24 stsp free(base_commit_id);
8032 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8033 0ebf8283 2019-07-24 stsp if (commit)
8034 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8035 0ebf8283 2019-07-24 stsp if (branch)
8036 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8037 0ebf8283 2019-07-24 stsp if (tmp_branch)
8038 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8039 0ebf8283 2019-07-24 stsp if (worktree)
8040 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8041 2822a352 2019-10-15 stsp if (repo)
8042 2822a352 2019-10-15 stsp got_repo_close(repo);
8043 2822a352 2019-10-15 stsp return error;
8044 2822a352 2019-10-15 stsp }
8045 2822a352 2019-10-15 stsp
8046 2822a352 2019-10-15 stsp __dead static void
8047 2822a352 2019-10-15 stsp usage_integrate(void)
8048 2822a352 2019-10-15 stsp {
8049 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8050 2822a352 2019-10-15 stsp exit(1);
8051 2822a352 2019-10-15 stsp }
8052 2822a352 2019-10-15 stsp
8053 2822a352 2019-10-15 stsp static const struct got_error *
8054 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8055 2822a352 2019-10-15 stsp {
8056 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8057 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8058 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8059 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8060 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8061 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8062 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8063 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8064 2822a352 2019-10-15 stsp int ch, did_something = 0;
8065 2822a352 2019-10-15 stsp
8066 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8067 2822a352 2019-10-15 stsp switch (ch) {
8068 2822a352 2019-10-15 stsp default:
8069 2822a352 2019-10-15 stsp usage_integrate();
8070 2822a352 2019-10-15 stsp /* NOTREACHED */
8071 2822a352 2019-10-15 stsp }
8072 2822a352 2019-10-15 stsp }
8073 2822a352 2019-10-15 stsp
8074 2822a352 2019-10-15 stsp argc -= optind;
8075 2822a352 2019-10-15 stsp argv += optind;
8076 2822a352 2019-10-15 stsp
8077 2822a352 2019-10-15 stsp if (argc != 1)
8078 2822a352 2019-10-15 stsp usage_integrate();
8079 2822a352 2019-10-15 stsp branch_arg = argv[0];
8080 2822a352 2019-10-15 stsp
8081 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8082 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8083 2822a352 2019-10-15 stsp err(1, "pledge");
8084 2822a352 2019-10-15 stsp
8085 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8086 2822a352 2019-10-15 stsp if (cwd == NULL) {
8087 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8088 2822a352 2019-10-15 stsp goto done;
8089 2822a352 2019-10-15 stsp }
8090 2822a352 2019-10-15 stsp
8091 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8092 2822a352 2019-10-15 stsp if (error)
8093 2822a352 2019-10-15 stsp goto done;
8094 2822a352 2019-10-15 stsp
8095 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8096 2822a352 2019-10-15 stsp if (error)
8097 2822a352 2019-10-15 stsp goto done;
8098 2822a352 2019-10-15 stsp
8099 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8100 2822a352 2019-10-15 stsp NULL);
8101 2822a352 2019-10-15 stsp if (error != NULL)
8102 2822a352 2019-10-15 stsp goto done;
8103 2822a352 2019-10-15 stsp
8104 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8105 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8106 2822a352 2019-10-15 stsp if (error)
8107 2822a352 2019-10-15 stsp goto done;
8108 2822a352 2019-10-15 stsp
8109 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8110 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8111 2822a352 2019-10-15 stsp goto done;
8112 2822a352 2019-10-15 stsp }
8113 2822a352 2019-10-15 stsp
8114 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8115 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8116 2822a352 2019-10-15 stsp if (error)
8117 2822a352 2019-10-15 stsp goto done;
8118 2822a352 2019-10-15 stsp
8119 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8120 2822a352 2019-10-15 stsp if (refname == NULL) {
8121 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8122 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8123 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8124 2822a352 2019-10-15 stsp goto done;
8125 2822a352 2019-10-15 stsp }
8126 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8127 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8128 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8129 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8130 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8131 2822a352 2019-10-15 stsp goto done;
8132 2822a352 2019-10-15 stsp }
8133 2822a352 2019-10-15 stsp
8134 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8135 2822a352 2019-10-15 stsp if (error)
8136 2822a352 2019-10-15 stsp goto done;
8137 2822a352 2019-10-15 stsp
8138 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8139 2822a352 2019-10-15 stsp if (error)
8140 2822a352 2019-10-15 stsp goto done;
8141 2822a352 2019-10-15 stsp
8142 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8143 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8144 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8145 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8146 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8147 2822a352 2019-10-15 stsp goto done;
8148 2822a352 2019-10-15 stsp }
8149 2822a352 2019-10-15 stsp
8150 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8151 2822a352 2019-10-15 stsp if (error) {
8152 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8153 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8154 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8155 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8156 2822a352 2019-10-15 stsp goto done;
8157 2822a352 2019-10-15 stsp }
8158 2822a352 2019-10-15 stsp
8159 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8160 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
8161 2822a352 2019-10-15 stsp check_cancelled, NULL);
8162 2822a352 2019-10-15 stsp if (error)
8163 2822a352 2019-10-15 stsp goto done;
8164 2822a352 2019-10-15 stsp
8165 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8166 2822a352 2019-10-15 stsp done:
8167 715dc77e 2019-08-03 stsp if (repo)
8168 715dc77e 2019-08-03 stsp got_repo_close(repo);
8169 2822a352 2019-10-15 stsp if (worktree)
8170 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8171 2822a352 2019-10-15 stsp free(cwd);
8172 2822a352 2019-10-15 stsp free(base_commit_id);
8173 2822a352 2019-10-15 stsp free(commit_id);
8174 2822a352 2019-10-15 stsp free(refname);
8175 2822a352 2019-10-15 stsp free(base_refname);
8176 715dc77e 2019-08-03 stsp return error;
8177 715dc77e 2019-08-03 stsp }
8178 715dc77e 2019-08-03 stsp
8179 715dc77e 2019-08-03 stsp __dead static void
8180 715dc77e 2019-08-03 stsp usage_stage(void)
8181 715dc77e 2019-08-03 stsp {
8182 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8183 f3055044 2019-08-07 stsp "[file-path ...]\n",
8184 715dc77e 2019-08-03 stsp getprogname());
8185 715dc77e 2019-08-03 stsp exit(1);
8186 715dc77e 2019-08-03 stsp }
8187 715dc77e 2019-08-03 stsp
8188 715dc77e 2019-08-03 stsp static const struct got_error *
8189 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8190 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8191 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8192 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8193 408b4ebc 2019-08-03 stsp {
8194 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8195 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8196 408b4ebc 2019-08-03 stsp
8197 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8198 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8199 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8200 408b4ebc 2019-08-03 stsp return NULL;
8201 408b4ebc 2019-08-03 stsp
8202 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8203 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8204 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8205 408b4ebc 2019-08-03 stsp else
8206 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8207 408b4ebc 2019-08-03 stsp if (err)
8208 408b4ebc 2019-08-03 stsp return err;
8209 408b4ebc 2019-08-03 stsp
8210 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8211 408b4ebc 2019-08-03 stsp free(id_str);
8212 dc424a06 2019-08-07 stsp return NULL;
8213 dc424a06 2019-08-07 stsp }
8214 2e1f37b0 2019-08-08 stsp
8215 dc424a06 2019-08-07 stsp static const struct got_error *
8216 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8217 715dc77e 2019-08-03 stsp {
8218 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8219 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8220 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8221 715dc77e 2019-08-03 stsp char *cwd = NULL;
8222 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8223 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8224 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8225 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8226 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8227 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8228 715dc77e 2019-08-03 stsp
8229 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8230 715dc77e 2019-08-03 stsp
8231 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8232 715dc77e 2019-08-03 stsp switch (ch) {
8233 408b4ebc 2019-08-03 stsp case 'l':
8234 408b4ebc 2019-08-03 stsp list_stage = 1;
8235 408b4ebc 2019-08-03 stsp break;
8236 dc424a06 2019-08-07 stsp case 'p':
8237 dc424a06 2019-08-07 stsp pflag = 1;
8238 dc424a06 2019-08-07 stsp break;
8239 dc424a06 2019-08-07 stsp case 'F':
8240 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8241 dc424a06 2019-08-07 stsp break;
8242 715dc77e 2019-08-03 stsp default:
8243 715dc77e 2019-08-03 stsp usage_stage();
8244 715dc77e 2019-08-03 stsp /* NOTREACHED */
8245 715dc77e 2019-08-03 stsp }
8246 715dc77e 2019-08-03 stsp }
8247 715dc77e 2019-08-03 stsp
8248 715dc77e 2019-08-03 stsp argc -= optind;
8249 715dc77e 2019-08-03 stsp argv += optind;
8250 715dc77e 2019-08-03 stsp
8251 715dc77e 2019-08-03 stsp #ifndef PROFILE
8252 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8253 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8254 715dc77e 2019-08-03 stsp err(1, "pledge");
8255 715dc77e 2019-08-03 stsp #endif
8256 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8257 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8258 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8259 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8260 715dc77e 2019-08-03 stsp
8261 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8262 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8263 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8264 715dc77e 2019-08-03 stsp goto done;
8265 715dc77e 2019-08-03 stsp }
8266 715dc77e 2019-08-03 stsp
8267 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8268 715dc77e 2019-08-03 stsp if (error)
8269 715dc77e 2019-08-03 stsp goto done;
8270 715dc77e 2019-08-03 stsp
8271 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8272 c9956ddf 2019-09-08 stsp NULL);
8273 715dc77e 2019-08-03 stsp if (error != NULL)
8274 715dc77e 2019-08-03 stsp goto done;
8275 715dc77e 2019-08-03 stsp
8276 dc424a06 2019-08-07 stsp if (patch_script_path) {
8277 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8278 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8279 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8280 dc424a06 2019-08-07 stsp patch_script_path);
8281 dc424a06 2019-08-07 stsp goto done;
8282 dc424a06 2019-08-07 stsp }
8283 dc424a06 2019-08-07 stsp }
8284 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8285 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8286 715dc77e 2019-08-03 stsp if (error)
8287 715dc77e 2019-08-03 stsp goto done;
8288 715dc77e 2019-08-03 stsp
8289 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8290 6d022e97 2019-08-04 stsp if (error)
8291 6d022e97 2019-08-04 stsp goto done;
8292 715dc77e 2019-08-03 stsp
8293 6d022e97 2019-08-04 stsp if (list_stage)
8294 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8295 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8296 2e1f37b0 2019-08-08 stsp else {
8297 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8298 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8299 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8300 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8301 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8302 2e1f37b0 2019-08-08 stsp }
8303 ad493afc 2019-08-03 stsp done:
8304 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8305 2e1f37b0 2019-08-08 stsp error == NULL)
8306 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8307 ad493afc 2019-08-03 stsp if (repo)
8308 ad493afc 2019-08-03 stsp got_repo_close(repo);
8309 ad493afc 2019-08-03 stsp if (worktree)
8310 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8311 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8312 ad493afc 2019-08-03 stsp free((char *)pe->path);
8313 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8314 ad493afc 2019-08-03 stsp free(cwd);
8315 ad493afc 2019-08-03 stsp return error;
8316 ad493afc 2019-08-03 stsp }
8317 ad493afc 2019-08-03 stsp
8318 ad493afc 2019-08-03 stsp __dead static void
8319 ad493afc 2019-08-03 stsp usage_unstage(void)
8320 ad493afc 2019-08-03 stsp {
8321 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8322 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8323 ad493afc 2019-08-03 stsp getprogname());
8324 ad493afc 2019-08-03 stsp exit(1);
8325 ad493afc 2019-08-03 stsp }
8326 ad493afc 2019-08-03 stsp
8327 ad493afc 2019-08-03 stsp
8328 ad493afc 2019-08-03 stsp static const struct got_error *
8329 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8330 ad493afc 2019-08-03 stsp {
8331 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8332 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8333 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8334 ad493afc 2019-08-03 stsp char *cwd = NULL;
8335 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8336 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8337 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8338 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8339 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8340 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8341 ad493afc 2019-08-03 stsp
8342 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8343 ad493afc 2019-08-03 stsp
8344 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8345 ad493afc 2019-08-03 stsp switch (ch) {
8346 2e1f37b0 2019-08-08 stsp case 'p':
8347 2e1f37b0 2019-08-08 stsp pflag = 1;
8348 2e1f37b0 2019-08-08 stsp break;
8349 2e1f37b0 2019-08-08 stsp case 'F':
8350 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8351 2e1f37b0 2019-08-08 stsp break;
8352 ad493afc 2019-08-03 stsp default:
8353 ad493afc 2019-08-03 stsp usage_unstage();
8354 ad493afc 2019-08-03 stsp /* NOTREACHED */
8355 ad493afc 2019-08-03 stsp }
8356 ad493afc 2019-08-03 stsp }
8357 ad493afc 2019-08-03 stsp
8358 ad493afc 2019-08-03 stsp argc -= optind;
8359 ad493afc 2019-08-03 stsp argv += optind;
8360 ad493afc 2019-08-03 stsp
8361 ad493afc 2019-08-03 stsp #ifndef PROFILE
8362 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8363 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8364 ad493afc 2019-08-03 stsp err(1, "pledge");
8365 ad493afc 2019-08-03 stsp #endif
8366 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8367 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8368 2e1f37b0 2019-08-08 stsp
8369 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8370 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8371 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8372 ad493afc 2019-08-03 stsp goto done;
8373 ad493afc 2019-08-03 stsp }
8374 ad493afc 2019-08-03 stsp
8375 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8376 ad493afc 2019-08-03 stsp if (error)
8377 ad493afc 2019-08-03 stsp goto done;
8378 ad493afc 2019-08-03 stsp
8379 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8380 c9956ddf 2019-09-08 stsp NULL);
8381 ad493afc 2019-08-03 stsp if (error != NULL)
8382 ad493afc 2019-08-03 stsp goto done;
8383 ad493afc 2019-08-03 stsp
8384 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8385 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8386 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8387 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8388 2e1f37b0 2019-08-08 stsp patch_script_path);
8389 2e1f37b0 2019-08-08 stsp goto done;
8390 2e1f37b0 2019-08-08 stsp }
8391 2e1f37b0 2019-08-08 stsp }
8392 2e1f37b0 2019-08-08 stsp
8393 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8394 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8395 ad493afc 2019-08-03 stsp if (error)
8396 ad493afc 2019-08-03 stsp goto done;
8397 ad493afc 2019-08-03 stsp
8398 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8399 ad493afc 2019-08-03 stsp if (error)
8400 ad493afc 2019-08-03 stsp goto done;
8401 ad493afc 2019-08-03 stsp
8402 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8403 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8404 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8405 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8406 715dc77e 2019-08-03 stsp done:
8407 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8408 2e1f37b0 2019-08-08 stsp error == NULL)
8409 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8410 0ebf8283 2019-07-24 stsp if (repo)
8411 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8412 715dc77e 2019-08-03 stsp if (worktree)
8413 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8414 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8415 715dc77e 2019-08-03 stsp free((char *)pe->path);
8416 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8417 715dc77e 2019-08-03 stsp free(cwd);
8418 01073a5d 2019-08-22 stsp return error;
8419 01073a5d 2019-08-22 stsp }
8420 01073a5d 2019-08-22 stsp
8421 01073a5d 2019-08-22 stsp __dead static void
8422 01073a5d 2019-08-22 stsp usage_cat(void)
8423 01073a5d 2019-08-22 stsp {
8424 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8425 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8426 01073a5d 2019-08-22 stsp exit(1);
8427 01073a5d 2019-08-22 stsp }
8428 01073a5d 2019-08-22 stsp
8429 01073a5d 2019-08-22 stsp static const struct got_error *
8430 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8431 01073a5d 2019-08-22 stsp {
8432 01073a5d 2019-08-22 stsp const struct got_error *err;
8433 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8434 01073a5d 2019-08-22 stsp
8435 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8436 01073a5d 2019-08-22 stsp if (err)
8437 01073a5d 2019-08-22 stsp return err;
8438 01073a5d 2019-08-22 stsp
8439 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8440 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8441 01073a5d 2019-08-22 stsp return err;
8442 01073a5d 2019-08-22 stsp }
8443 01073a5d 2019-08-22 stsp
8444 01073a5d 2019-08-22 stsp static const struct got_error *
8445 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8446 01073a5d 2019-08-22 stsp {
8447 01073a5d 2019-08-22 stsp const struct got_error *err;
8448 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8449 56e0773d 2019-11-28 stsp int nentries, i;
8450 01073a5d 2019-08-22 stsp
8451 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8452 01073a5d 2019-08-22 stsp if (err)
8453 01073a5d 2019-08-22 stsp return err;
8454 01073a5d 2019-08-22 stsp
8455 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8456 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8457 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8458 01073a5d 2019-08-22 stsp char *id_str;
8459 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8460 01073a5d 2019-08-22 stsp break;
8461 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8462 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8463 01073a5d 2019-08-22 stsp if (err)
8464 01073a5d 2019-08-22 stsp break;
8465 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8466 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8467 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8468 01073a5d 2019-08-22 stsp free(id_str);
8469 01073a5d 2019-08-22 stsp }
8470 01073a5d 2019-08-22 stsp
8471 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8472 01073a5d 2019-08-22 stsp return err;
8473 01073a5d 2019-08-22 stsp }
8474 01073a5d 2019-08-22 stsp
8475 01073a5d 2019-08-22 stsp static const struct got_error *
8476 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8477 01073a5d 2019-08-22 stsp {
8478 01073a5d 2019-08-22 stsp const struct got_error *err;
8479 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8480 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8481 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8482 01073a5d 2019-08-22 stsp char *id_str = NULL;
8483 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8484 01073a5d 2019-08-22 stsp
8485 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8486 01073a5d 2019-08-22 stsp if (err)
8487 01073a5d 2019-08-22 stsp return err;
8488 01073a5d 2019-08-22 stsp
8489 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8490 01073a5d 2019-08-22 stsp if (err)
8491 01073a5d 2019-08-22 stsp goto done;
8492 01073a5d 2019-08-22 stsp
8493 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8494 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8495 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8496 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8497 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8498 01073a5d 2019-08-22 stsp char *pid_str;
8499 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8500 01073a5d 2019-08-22 stsp if (err)
8501 01073a5d 2019-08-22 stsp goto done;
8502 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8503 01073a5d 2019-08-22 stsp free(pid_str);
8504 01073a5d 2019-08-22 stsp }
8505 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8506 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8507 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8508 01073a5d 2019-08-22 stsp
8509 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8510 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8511 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8512 01073a5d 2019-08-22 stsp
8513 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8514 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8515 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8516 01073a5d 2019-08-22 stsp done:
8517 01073a5d 2019-08-22 stsp free(id_str);
8518 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8519 01073a5d 2019-08-22 stsp return err;
8520 01073a5d 2019-08-22 stsp }
8521 01073a5d 2019-08-22 stsp
8522 01073a5d 2019-08-22 stsp static const struct got_error *
8523 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8524 01073a5d 2019-08-22 stsp {
8525 01073a5d 2019-08-22 stsp const struct got_error *err;
8526 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8527 01073a5d 2019-08-22 stsp char *id_str = NULL;
8528 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8529 01073a5d 2019-08-22 stsp
8530 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8531 01073a5d 2019-08-22 stsp if (err)
8532 01073a5d 2019-08-22 stsp return err;
8533 01073a5d 2019-08-22 stsp
8534 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8535 01073a5d 2019-08-22 stsp if (err)
8536 01073a5d 2019-08-22 stsp goto done;
8537 01073a5d 2019-08-22 stsp
8538 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8539 e15d5241 2019-08-22 stsp
8540 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8541 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8542 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8543 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8544 01073a5d 2019-08-22 stsp break;
8545 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8546 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8547 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8548 01073a5d 2019-08-22 stsp break;
8549 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8550 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8551 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8552 01073a5d 2019-08-22 stsp break;
8553 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8554 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8555 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8556 01073a5d 2019-08-22 stsp break;
8557 01073a5d 2019-08-22 stsp default:
8558 01073a5d 2019-08-22 stsp break;
8559 01073a5d 2019-08-22 stsp }
8560 01073a5d 2019-08-22 stsp
8561 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8562 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8563 e15d5241 2019-08-22 stsp
8564 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8565 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8566 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8567 01073a5d 2019-08-22 stsp
8568 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8569 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8570 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8571 01073a5d 2019-08-22 stsp done:
8572 01073a5d 2019-08-22 stsp free(id_str);
8573 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8574 01073a5d 2019-08-22 stsp return err;
8575 01073a5d 2019-08-22 stsp }
8576 01073a5d 2019-08-22 stsp
8577 01073a5d 2019-08-22 stsp static const struct got_error *
8578 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8579 01073a5d 2019-08-22 stsp {
8580 01073a5d 2019-08-22 stsp const struct got_error *error;
8581 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8582 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8583 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8584 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8585 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8586 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8587 01073a5d 2019-08-22 stsp
8588 01073a5d 2019-08-22 stsp #ifndef PROFILE
8589 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8590 01073a5d 2019-08-22 stsp NULL) == -1)
8591 01073a5d 2019-08-22 stsp err(1, "pledge");
8592 01073a5d 2019-08-22 stsp #endif
8593 01073a5d 2019-08-22 stsp
8594 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8595 01073a5d 2019-08-22 stsp switch (ch) {
8596 896e9b6f 2019-08-26 stsp case 'c':
8597 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8598 896e9b6f 2019-08-26 stsp break;
8599 01073a5d 2019-08-22 stsp case 'r':
8600 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8601 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8602 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8603 9ba1d308 2019-10-21 stsp optarg);
8604 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8605 01073a5d 2019-08-22 stsp break;
8606 896e9b6f 2019-08-26 stsp case 'P':
8607 896e9b6f 2019-08-26 stsp force_path = 1;
8608 896e9b6f 2019-08-26 stsp break;
8609 01073a5d 2019-08-22 stsp default:
8610 01073a5d 2019-08-22 stsp usage_cat();
8611 01073a5d 2019-08-22 stsp /* NOTREACHED */
8612 01073a5d 2019-08-22 stsp }
8613 01073a5d 2019-08-22 stsp }
8614 01073a5d 2019-08-22 stsp
8615 01073a5d 2019-08-22 stsp argc -= optind;
8616 01073a5d 2019-08-22 stsp argv += optind;
8617 01073a5d 2019-08-22 stsp
8618 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8619 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8620 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8621 01073a5d 2019-08-22 stsp goto done;
8622 01073a5d 2019-08-22 stsp }
8623 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8624 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8625 01073a5d 2019-08-22 stsp goto done;
8626 01073a5d 2019-08-22 stsp if (worktree) {
8627 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8628 01073a5d 2019-08-22 stsp repo_path = strdup(
8629 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8630 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8631 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8632 01073a5d 2019-08-22 stsp goto done;
8633 01073a5d 2019-08-22 stsp }
8634 01073a5d 2019-08-22 stsp }
8635 01073a5d 2019-08-22 stsp }
8636 01073a5d 2019-08-22 stsp
8637 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8638 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8639 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8640 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8641 01073a5d 2019-08-22 stsp }
8642 01073a5d 2019-08-22 stsp
8643 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8644 01073a5d 2019-08-22 stsp free(repo_path);
8645 01073a5d 2019-08-22 stsp if (error != NULL)
8646 01073a5d 2019-08-22 stsp goto done;
8647 01073a5d 2019-08-22 stsp
8648 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8649 896e9b6f 2019-08-26 stsp if (error)
8650 896e9b6f 2019-08-26 stsp goto done;
8651 896e9b6f 2019-08-26 stsp
8652 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8653 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8654 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8655 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8656 01073a5d 2019-08-22 stsp if (error)
8657 01073a5d 2019-08-22 stsp goto done;
8658 01073a5d 2019-08-22 stsp
8659 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8660 896e9b6f 2019-08-26 stsp if (force_path) {
8661 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8662 896e9b6f 2019-08-26 stsp argv[i]);
8663 896e9b6f 2019-08-26 stsp if (error)
8664 896e9b6f 2019-08-26 stsp break;
8665 896e9b6f 2019-08-26 stsp } else {
8666 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8667 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8668 896e9b6f 2019-08-26 stsp if (error) {
8669 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8670 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8671 896e9b6f 2019-08-26 stsp break;
8672 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8673 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8674 896e9b6f 2019-08-26 stsp if (error)
8675 896e9b6f 2019-08-26 stsp break;
8676 896e9b6f 2019-08-26 stsp }
8677 896e9b6f 2019-08-26 stsp }
8678 01073a5d 2019-08-22 stsp
8679 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8680 01073a5d 2019-08-22 stsp if (error)
8681 01073a5d 2019-08-22 stsp break;
8682 01073a5d 2019-08-22 stsp
8683 01073a5d 2019-08-22 stsp switch (obj_type) {
8684 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8685 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8686 01073a5d 2019-08-22 stsp break;
8687 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8688 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8689 01073a5d 2019-08-22 stsp break;
8690 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8691 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8692 01073a5d 2019-08-22 stsp break;
8693 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8694 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8695 01073a5d 2019-08-22 stsp break;
8696 01073a5d 2019-08-22 stsp default:
8697 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8698 01073a5d 2019-08-22 stsp break;
8699 01073a5d 2019-08-22 stsp }
8700 01073a5d 2019-08-22 stsp if (error)
8701 01073a5d 2019-08-22 stsp break;
8702 01073a5d 2019-08-22 stsp free(label);
8703 01073a5d 2019-08-22 stsp label = NULL;
8704 01073a5d 2019-08-22 stsp free(id);
8705 01073a5d 2019-08-22 stsp id = NULL;
8706 01073a5d 2019-08-22 stsp }
8707 01073a5d 2019-08-22 stsp done:
8708 01073a5d 2019-08-22 stsp free(label);
8709 01073a5d 2019-08-22 stsp free(id);
8710 896e9b6f 2019-08-26 stsp free(commit_id);
8711 01073a5d 2019-08-22 stsp if (worktree)
8712 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8713 01073a5d 2019-08-22 stsp if (repo) {
8714 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8715 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8716 01073a5d 2019-08-22 stsp if (error == NULL)
8717 01073a5d 2019-08-22 stsp error = repo_error;
8718 01073a5d 2019-08-22 stsp }
8719 0ebf8283 2019-07-24 stsp return error;
8720 0ebf8283 2019-07-24 stsp }