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 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
889 f298ae0f 2020-03-25 stsp int verbosity, 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 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, 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 f298ae0f 2020-03-25 stsp free(remote_refname);
1247 39c64a6a 2020-03-18 stsp if (error)
1248 d9b4d0c0 2020-03-18 stsp goto done;
1249 d9b4d0c0 2020-03-18 stsp }
1250 d9b4d0c0 2020-03-18 stsp
1251 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1253 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1254 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1255 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1256 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1257 d9b4d0c0 2020-03-18 stsp
1258 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1259 d9b4d0c0 2020-03-18 stsp continue;
1260 d9b4d0c0 2020-03-18 stsp
1261 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1262 39c64a6a 2020-03-18 stsp if (error) {
1263 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1264 55330abe 2020-03-20 stsp error = NULL;
1265 d9b4d0c0 2020-03-18 stsp continue;
1266 55330abe 2020-03-20 stsp }
1267 d9b4d0c0 2020-03-18 stsp goto done;
1268 d9b4d0c0 2020-03-18 stsp }
1269 d9b4d0c0 2020-03-18 stsp
1270 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1271 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1272 f298ae0f 2020-03-25 stsp if (error)
1273 f298ae0f 2020-03-25 stsp goto done;
1274 f298ae0f 2020-03-25 stsp
1275 f298ae0f 2020-03-25 stsp if (mirror_references)
1276 f298ae0f 2020-03-25 stsp continue;
1277 f298ae0f 2020-03-25 stsp
1278 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1279 f298ae0f 2020-03-25 stsp continue;
1280 f298ae0f 2020-03-25 stsp
1281 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1282 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1283 f298ae0f 2020-03-25 stsp refname) == -1) {
1284 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1285 f298ae0f 2020-03-25 stsp goto done;
1286 f298ae0f 2020-03-25 stsp }
1287 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1288 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1290 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1291 f298ae0f 2020-03-25 stsp free(remote_refname);
1292 f298ae0f 2020-03-25 stsp goto done;
1293 f298ae0f 2020-03-25 stsp }
1294 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1295 f298ae0f 2020-03-25 stsp if (error) {
1296 f298ae0f 2020-03-25 stsp free(remote_refname);
1297 f298ae0f 2020-03-25 stsp free(remote_target);
1298 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1299 f298ae0f 2020-03-25 stsp error = NULL;
1300 f298ae0f 2020-03-25 stsp continue;
1301 f298ae0f 2020-03-25 stsp }
1302 f298ae0f 2020-03-25 stsp goto done;
1303 f298ae0f 2020-03-25 stsp }
1304 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1305 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1306 f298ae0f 2020-03-25 stsp free(remote_refname);
1307 f298ae0f 2020-03-25 stsp free(remote_target);
1308 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1309 39c64a6a 2020-03-18 stsp if (error)
1310 d9b4d0c0 2020-03-18 stsp goto done;
1311 4ba14133 2020-03-20 stsp }
1312 4ba14133 2020-03-20 stsp if (pe == NULL) {
1313 4ba14133 2020-03-20 stsp /*
1314 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1315 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1316 4ba14133 2020-03-20 stsp * which could be fetched instead.
1317 4ba14133 2020-03-20 stsp */
1318 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 4ba14133 2020-03-20 stsp const char *target = pe->path;
1320 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1321 d9b4d0c0 2020-03-18 stsp
1322 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1323 4ba14133 2020-03-20 stsp if (error) {
1324 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1325 4ba14133 2020-03-20 stsp error = NULL;
1326 4ba14133 2020-03-20 stsp continue;
1327 4ba14133 2020-03-20 stsp }
1328 4ba14133 2020-03-20 stsp goto done;
1329 4ba14133 2020-03-20 stsp }
1330 4ba14133 2020-03-20 stsp
1331 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1332 f298ae0f 2020-03-25 stsp verbosity, repo);
1333 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1334 4ba14133 2020-03-20 stsp if (error)
1335 4ba14133 2020-03-20 stsp goto done;
1336 4ba14133 2020-03-20 stsp break;
1337 4ba14133 2020-03-20 stsp }
1338 659e7fbd 2020-03-20 stsp }
1339 659e7fbd 2020-03-20 stsp
1340 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1341 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1343 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1344 659e7fbd 2020-03-20 stsp goto done;
1345 659e7fbd 2020-03-20 stsp }
1346 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1347 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1348 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1349 659e7fbd 2020-03-20 stsp goto done;
1350 b46f3e71 2020-03-18 stsp }
1351 659e7fbd 2020-03-20 stsp if (mirror_references) {
1352 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1353 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1354 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1355 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1356 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1357 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1359 659e7fbd 2020-03-20 stsp goto done;
1360 659e7fbd 2020-03-20 stsp }
1361 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1362 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1363 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1364 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1365 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1369 659e7fbd 2020-03-20 stsp goto done;
1370 659e7fbd 2020-03-20 stsp }
1371 659e7fbd 2020-03-20 stsp } else {
1372 659e7fbd 2020-03-20 stsp const char *branchname;
1373 d715f13e 2020-03-19 stsp
1374 659e7fbd 2020-03-20 stsp /*
1375 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1376 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1377 659e7fbd 2020-03-20 stsp */
1378 659e7fbd 2020-03-20 stsp if (head_symref) {
1379 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1380 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1381 659e7fbd 2020-03-20 stsp branchname += 11;
1382 659e7fbd 2020-03-20 stsp } else
1383 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1384 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1385 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1386 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1387 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 659e7fbd 2020-03-20 stsp branchname) == -1) {
1391 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1392 659e7fbd 2020-03-20 stsp goto done;
1393 659e7fbd 2020-03-20 stsp }
1394 659e7fbd 2020-03-20 stsp }
1395 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1397 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1398 659e7fbd 2020-03-20 stsp goto done;
1399 659e7fbd 2020-03-20 stsp }
1400 659e7fbd 2020-03-20 stsp
1401 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1402 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1403 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1404 09838ffc 2020-03-18 stsp done:
1405 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1406 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1407 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1408 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1410 9c52365f 2020-03-21 stsp }
1411 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1413 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1415 bb64b798 2020-03-18 stsp if (repo)
1416 bb64b798 2020-03-18 stsp got_repo_close(repo);
1417 659e7fbd 2020-03-20 stsp if (head_symref)
1418 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1419 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1420 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1421 d9b4d0c0 2020-03-18 stsp free(pe->data);
1422 d9b4d0c0 2020-03-18 stsp }
1423 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1424 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1425 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1426 d9b4d0c0 2020-03-18 stsp free(pe->data);
1427 d9b4d0c0 2020-03-18 stsp }
1428 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1429 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1430 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1431 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1432 09838ffc 2020-03-18 stsp free(proto);
1433 09838ffc 2020-03-18 stsp free(host);
1434 09838ffc 2020-03-18 stsp free(port);
1435 09838ffc 2020-03-18 stsp free(server_path);
1436 09838ffc 2020-03-18 stsp free(repo_name);
1437 bb64b798 2020-03-18 stsp free(default_destdir);
1438 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1439 b46f3e71 2020-03-18 stsp free(git_url);
1440 39c64a6a 2020-03-18 stsp return error;
1441 7848a0e1 2020-03-19 stsp }
1442 7848a0e1 2020-03-19 stsp
1443 7848a0e1 2020-03-19 stsp static const struct got_error *
1444 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1446 7848a0e1 2020-03-19 stsp {
1447 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1448 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1449 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1450 7848a0e1 2020-03-19 stsp
1451 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1452 7848a0e1 2020-03-19 stsp if (err)
1453 7848a0e1 2020-03-19 stsp goto done;
1454 7848a0e1 2020-03-19 stsp
1455 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1456 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1458 88609724 2020-03-21 stsp if (err)
1459 88609724 2020-03-21 stsp goto done;
1460 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1461 88609724 2020-03-21 stsp goto done;
1462 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1463 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1464 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1465 db6d8ad8 2020-03-21 stsp }
1466 db6d8ad8 2020-03-21 stsp goto done;
1467 db6d8ad8 2020-03-21 stsp }
1468 db6d8ad8 2020-03-21 stsp
1469 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1470 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1471 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1472 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1473 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1474 688f11b3 2020-03-21 stsp }
1475 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1476 7848a0e1 2020-03-19 stsp if (err)
1477 7848a0e1 2020-03-19 stsp goto done;
1478 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1479 e8a967e0 2020-03-21 stsp if (err)
1480 e8a967e0 2020-03-21 stsp goto done;
1481 7848a0e1 2020-03-19 stsp } else {
1482 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1483 7848a0e1 2020-03-19 stsp if (err)
1484 7848a0e1 2020-03-19 stsp goto done;
1485 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1486 6338a6a1 2020-03-21 stsp goto done;
1487 6338a6a1 2020-03-21 stsp
1488 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1489 6338a6a1 2020-03-21 stsp if (err)
1490 6338a6a1 2020-03-21 stsp goto done;
1491 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1492 6338a6a1 2020-03-21 stsp if (err)
1493 6338a6a1 2020-03-21 stsp goto done;
1494 7848a0e1 2020-03-19 stsp }
1495 6338a6a1 2020-03-21 stsp
1496 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1497 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1498 6338a6a1 2020-03-21 stsp new_id_str);
1499 7848a0e1 2020-03-19 stsp done:
1500 7848a0e1 2020-03-19 stsp free(old_id);
1501 7848a0e1 2020-03-19 stsp free(new_id_str);
1502 7848a0e1 2020-03-19 stsp return err;
1503 2ab43947 2020-03-18 stsp }
1504 f1bcca34 2020-03-25 stsp
1505 f1bcca34 2020-03-25 stsp static const struct got_error *
1506 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1507 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1508 f1bcca34 2020-03-25 stsp {
1509 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1510 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1511 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1512 f1bcca34 2020-03-25 stsp
1513 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1514 bcf34b0e 2020-03-26 stsp if (err) {
1515 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1516 bcf34b0e 2020-03-26 stsp return err;
1517 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1518 bcf34b0e 2020-03-26 stsp if (err)
1519 bcf34b0e 2020-03-26 stsp goto done;
1520 2ab43947 2020-03-18 stsp
1521 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1522 bcf34b0e 2020-03-26 stsp if (err)
1523 bcf34b0e 2020-03-26 stsp goto done;
1524 f1bcca34 2020-03-25 stsp
1525 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1526 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1527 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1528 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1529 bcf34b0e 2020-03-26 stsp } else {
1530 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1531 f1bcca34 2020-03-25 stsp
1532 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1533 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1534 bcf34b0e 2020-03-26 stsp goto done;
1535 bcf34b0e 2020-03-26 stsp
1536 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1537 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1538 bcf34b0e 2020-03-26 stsp if (err)
1539 bcf34b0e 2020-03-26 stsp goto done;
1540 bcf34b0e 2020-03-26 stsp
1541 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1542 bcf34b0e 2020-03-26 stsp if (err)
1543 bcf34b0e 2020-03-26 stsp goto done;
1544 bcf34b0e 2020-03-26 stsp
1545 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1546 bcf34b0e 2020-03-26 stsp printf("Updated reference %s: %s\n",
1547 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1548 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1549 bcf34b0e 2020-03-26 stsp
1550 bcf34b0e 2020-03-26 stsp }
1551 f1bcca34 2020-03-25 stsp done:
1552 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1553 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1554 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1555 bcf34b0e 2020-03-26 stsp err = unlock_err;
1556 bcf34b0e 2020-03-26 stsp }
1557 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1558 f1bcca34 2020-03-25 stsp return err;
1559 f1bcca34 2020-03-25 stsp }
1560 f1bcca34 2020-03-25 stsp
1561 2ab43947 2020-03-18 stsp __dead static void
1562 7848a0e1 2020-03-19 stsp usage_fetch(void)
1563 7848a0e1 2020-03-19 stsp {
1564 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1565 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1566 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1567 13f12b09 2020-03-21 stsp getprogname());
1568 7848a0e1 2020-03-19 stsp exit(1);
1569 7848a0e1 2020-03-19 stsp }
1570 7848a0e1 2020-03-19 stsp
1571 7848a0e1 2020-03-19 stsp static const struct got_error *
1572 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1573 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1574 f21ec2f0 2020-03-21 stsp {
1575 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1576 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1577 3789fd73 2020-03-26 stsp char *id_str = NULL;
1578 3789fd73 2020-03-26 stsp
1579 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1580 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1581 3789fd73 2020-03-26 stsp if (err)
1582 3789fd73 2020-03-26 stsp return err;
1583 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1584 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1585 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1586 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1587 3789fd73 2020-03-26 stsp }
1588 3789fd73 2020-03-26 stsp } else {
1589 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1590 3789fd73 2020-03-26 stsp if (err)
1591 3789fd73 2020-03-26 stsp return err;
1592 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1593 3789fd73 2020-03-26 stsp if (err)
1594 3789fd73 2020-03-26 stsp goto done;
1595 3789fd73 2020-03-26 stsp
1596 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1597 3789fd73 2020-03-26 stsp if (err)
1598 3789fd73 2020-03-26 stsp goto done;
1599 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1600 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1601 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1602 3789fd73 2020-03-26 stsp }
1603 3789fd73 2020-03-26 stsp }
1604 3789fd73 2020-03-26 stsp done:
1605 3789fd73 2020-03-26 stsp free(id);
1606 3789fd73 2020-03-26 stsp free(id_str);
1607 3789fd73 2020-03-26 stsp return NULL;
1608 3789fd73 2020-03-26 stsp }
1609 3789fd73 2020-03-26 stsp
1610 3789fd73 2020-03-26 stsp static const struct got_error *
1611 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1612 3789fd73 2020-03-26 stsp struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1613 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1614 3789fd73 2020-03-26 stsp {
1615 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1616 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1617 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1618 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1619 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1620 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1621 f21ec2f0 2020-03-21 stsp
1622 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1623 f21ec2f0 2020-03-21 stsp
1624 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1625 3789fd73 2020-03-26 stsp == -1)
1626 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1627 3789fd73 2020-03-26 stsp
1628 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1629 f21ec2f0 2020-03-21 stsp if (err)
1630 3789fd73 2020-03-26 stsp goto done;
1631 f21ec2f0 2020-03-21 stsp
1632 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1633 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1634 f21ec2f0 2020-03-21 stsp
1635 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1636 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1637 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1638 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1639 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1640 3789fd73 2020-03-26 stsp continue;
1641 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1642 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1643 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1644 3789fd73 2020-03-26 stsp goto done;
1645 3789fd73 2020-03-26 stsp }
1646 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1647 3789fd73 2020-03-26 stsp continue;
1648 3789fd73 2020-03-26 stsp }
1649 f21ec2f0 2020-03-21 stsp
1650 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1651 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1652 f21ec2f0 2020-03-21 stsp break;
1653 f21ec2f0 2020-03-21 stsp }
1654 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1655 f21ec2f0 2020-03-21 stsp continue;
1656 f21ec2f0 2020-03-21 stsp
1657 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1658 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1659 3789fd73 2020-03-26 stsp break;
1660 3789fd73 2020-03-26 stsp }
1661 3789fd73 2020-03-26 stsp if (pe != NULL)
1662 3789fd73 2020-03-26 stsp continue;
1663 f21ec2f0 2020-03-21 stsp
1664 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1665 f21ec2f0 2020-03-21 stsp if (err)
1666 f21ec2f0 2020-03-21 stsp break;
1667 3789fd73 2020-03-26 stsp
1668 3789fd73 2020-03-26 stsp if (local_refname) {
1669 3789fd73 2020-03-26 stsp struct got_reference *ref;
1670 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1671 3789fd73 2020-03-26 stsp if (err) {
1672 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1673 3789fd73 2020-03-26 stsp break;
1674 3789fd73 2020-03-26 stsp free(local_refname);
1675 3789fd73 2020-03-26 stsp local_refname = NULL;
1676 3789fd73 2020-03-26 stsp continue;
1677 3789fd73 2020-03-26 stsp }
1678 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1679 3789fd73 2020-03-26 stsp if (err)
1680 3789fd73 2020-03-26 stsp break;
1681 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1682 3789fd73 2020-03-26 stsp got_ref_close(ref);
1683 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1684 3789fd73 2020-03-26 stsp err = unlock_err;
1685 3789fd73 2020-03-26 stsp break;
1686 3789fd73 2020-03-26 stsp }
1687 3789fd73 2020-03-26 stsp
1688 3789fd73 2020-03-26 stsp free(local_refname);
1689 3789fd73 2020-03-26 stsp local_refname = NULL;
1690 6338a6a1 2020-03-21 stsp }
1691 f21ec2f0 2020-03-21 stsp }
1692 3789fd73 2020-03-26 stsp done:
1693 3789fd73 2020-03-26 stsp free(remote_namespace);
1694 3789fd73 2020-03-26 stsp free(local_refname);
1695 0e4002ca 2020-03-21 stsp return err;
1696 0e4002ca 2020-03-21 stsp }
1697 0e4002ca 2020-03-21 stsp
1698 0e4002ca 2020-03-21 stsp static const struct got_error *
1699 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1700 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1701 0e4002ca 2020-03-21 stsp {
1702 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1703 0e4002ca 2020-03-21 stsp char *remote_refname;
1704 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1705 0e4002ca 2020-03-21 stsp
1706 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1707 0e4002ca 2020-03-21 stsp refname += 5;
1708 0e4002ca 2020-03-21 stsp
1709 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1710 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1711 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1712 f21ec2f0 2020-03-21 stsp
1713 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1714 0e4002ca 2020-03-21 stsp if (err) {
1715 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1716 0e4002ca 2020-03-21 stsp goto done;
1717 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1718 0e4002ca 2020-03-21 stsp } else {
1719 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1720 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1721 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1722 9f142382 2020-03-21 stsp err = unlock_err;
1723 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1724 0e4002ca 2020-03-21 stsp }
1725 0e4002ca 2020-03-21 stsp done:
1726 0e4002ca 2020-03-21 stsp free(remote_refname);
1727 f21ec2f0 2020-03-21 stsp return err;
1728 f21ec2f0 2020-03-21 stsp }
1729 f21ec2f0 2020-03-21 stsp
1730 f21ec2f0 2020-03-21 stsp static const struct got_error *
1731 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1732 7848a0e1 2020-03-19 stsp {
1733 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1734 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1735 7848a0e1 2020-03-19 stsp const char *remote_name;
1736 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1737 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1738 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1739 7848a0e1 2020-03-19 stsp int nremotes;
1740 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1741 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1742 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1743 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1744 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1745 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1746 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1747 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1748 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1749 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1750 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1751 7848a0e1 2020-03-19 stsp
1752 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1753 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1754 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1755 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1756 7848a0e1 2020-03-19 stsp
1757 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1758 7848a0e1 2020-03-19 stsp switch (ch) {
1759 659e7fbd 2020-03-20 stsp case 'a':
1760 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1761 4ba14133 2020-03-20 stsp break;
1762 4ba14133 2020-03-20 stsp case 'b':
1763 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1764 4ba14133 2020-03-20 stsp optarg, NULL);
1765 4ba14133 2020-03-20 stsp if (error)
1766 4ba14133 2020-03-20 stsp return error;
1767 41b0de12 2020-03-21 stsp break;
1768 f21ec2f0 2020-03-21 stsp case 'd':
1769 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1770 f21ec2f0 2020-03-21 stsp break;
1771 41b0de12 2020-03-21 stsp case 'l':
1772 41b0de12 2020-03-21 stsp list_refs_only = 1;
1773 659e7fbd 2020-03-20 stsp break;
1774 7848a0e1 2020-03-19 stsp case 'r':
1775 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1776 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1777 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1778 7848a0e1 2020-03-19 stsp optarg);
1779 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1780 7848a0e1 2020-03-19 stsp break;
1781 db6d8ad8 2020-03-21 stsp case 't':
1782 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1783 db6d8ad8 2020-03-21 stsp break;
1784 7848a0e1 2020-03-19 stsp case 'v':
1785 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1786 7848a0e1 2020-03-19 stsp verbosity = 0;
1787 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1788 7848a0e1 2020-03-19 stsp verbosity++;
1789 7848a0e1 2020-03-19 stsp break;
1790 7848a0e1 2020-03-19 stsp case 'q':
1791 7848a0e1 2020-03-19 stsp verbosity = -1;
1792 7848a0e1 2020-03-19 stsp break;
1793 0e4002ca 2020-03-21 stsp case 'R':
1794 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1795 0e4002ca 2020-03-21 stsp optarg, NULL);
1796 0e4002ca 2020-03-21 stsp if (error)
1797 0e4002ca 2020-03-21 stsp return error;
1798 0e4002ca 2020-03-21 stsp break;
1799 7848a0e1 2020-03-19 stsp default:
1800 7848a0e1 2020-03-19 stsp usage_fetch();
1801 7848a0e1 2020-03-19 stsp break;
1802 7848a0e1 2020-03-19 stsp }
1803 7848a0e1 2020-03-19 stsp }
1804 7848a0e1 2020-03-19 stsp argc -= optind;
1805 7848a0e1 2020-03-19 stsp argv += optind;
1806 7848a0e1 2020-03-19 stsp
1807 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1808 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1809 41b0de12 2020-03-21 stsp if (list_refs_only) {
1810 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1811 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1812 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1813 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1814 f21ec2f0 2020-03-21 stsp if (delete_refs)
1815 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1816 41b0de12 2020-03-21 stsp if (verbosity == -1)
1817 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1818 41b0de12 2020-03-21 stsp }
1819 4ba14133 2020-03-20 stsp
1820 7848a0e1 2020-03-19 stsp if (argc == 0)
1821 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1822 7848a0e1 2020-03-19 stsp else if (argc == 1)
1823 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1824 7848a0e1 2020-03-19 stsp else
1825 7848a0e1 2020-03-19 stsp usage_fetch();
1826 7848a0e1 2020-03-19 stsp
1827 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1828 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1829 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1830 7848a0e1 2020-03-19 stsp goto done;
1831 7848a0e1 2020-03-19 stsp }
1832 7848a0e1 2020-03-19 stsp
1833 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1834 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1835 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1836 7848a0e1 2020-03-19 stsp goto done;
1837 7848a0e1 2020-03-19 stsp else
1838 7848a0e1 2020-03-19 stsp error = NULL;
1839 7848a0e1 2020-03-19 stsp if (worktree) {
1840 7848a0e1 2020-03-19 stsp repo_path =
1841 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1842 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1843 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1844 7848a0e1 2020-03-19 stsp if (error)
1845 7848a0e1 2020-03-19 stsp goto done;
1846 7848a0e1 2020-03-19 stsp } else {
1847 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1848 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1849 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1850 7848a0e1 2020-03-19 stsp goto done;
1851 7848a0e1 2020-03-19 stsp }
1852 7848a0e1 2020-03-19 stsp }
1853 7848a0e1 2020-03-19 stsp }
1854 7848a0e1 2020-03-19 stsp
1855 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1856 7848a0e1 2020-03-19 stsp if (error)
1857 7848a0e1 2020-03-19 stsp goto done;
1858 7848a0e1 2020-03-19 stsp
1859 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1860 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1861 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1862 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1863 7848a0e1 2020-03-19 stsp break;
1864 7848a0e1 2020-03-19 stsp }
1865 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1866 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1867 7848a0e1 2020-03-19 stsp goto done;
1868 7848a0e1 2020-03-19 stsp }
1869 7848a0e1 2020-03-19 stsp
1870 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1871 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1872 7848a0e1 2020-03-19 stsp if (error)
1873 7848a0e1 2020-03-19 stsp goto done;
1874 7848a0e1 2020-03-19 stsp
1875 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1876 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1877 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1878 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1879 7848a0e1 2020-03-19 stsp err(1, "pledge");
1880 7848a0e1 2020-03-19 stsp #endif
1881 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1882 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1883 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1884 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1885 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1886 7848a0e1 2020-03-19 stsp err(1, "pledge");
1887 7848a0e1 2020-03-19 stsp #endif
1888 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1889 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1890 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1891 7848a0e1 2020-03-19 stsp goto done;
1892 7848a0e1 2020-03-19 stsp } else {
1893 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1894 7848a0e1 2020-03-19 stsp goto done;
1895 7848a0e1 2020-03-19 stsp }
1896 7848a0e1 2020-03-19 stsp
1897 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1898 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1899 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1900 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1901 7848a0e1 2020-03-19 stsp goto done;
1902 7848a0e1 2020-03-19 stsp }
1903 7848a0e1 2020-03-19 stsp }
1904 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1905 7848a0e1 2020-03-19 stsp if (error)
1906 7848a0e1 2020-03-19 stsp goto done;
1907 7848a0e1 2020-03-19 stsp
1908 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1909 9c52365f 2020-03-21 stsp server_path, verbosity);
1910 7848a0e1 2020-03-19 stsp if (error)
1911 7848a0e1 2020-03-19 stsp goto done;
1912 7848a0e1 2020-03-19 stsp
1913 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1914 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1915 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1916 7848a0e1 2020-03-19 stsp
1917 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1918 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1919 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1920 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1921 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1922 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1923 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1924 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1925 7848a0e1 2020-03-19 stsp if (error)
1926 7848a0e1 2020-03-19 stsp goto done;
1927 7848a0e1 2020-03-19 stsp
1928 41b0de12 2020-03-21 stsp if (list_refs_only) {
1929 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1930 41b0de12 2020-03-21 stsp goto done;
1931 41b0de12 2020-03-21 stsp }
1932 41b0de12 2020-03-21 stsp
1933 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1934 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1935 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1936 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
1937 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1938 984065c8 2020-03-19 stsp if (error)
1939 984065c8 2020-03-19 stsp goto done;
1940 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1941 984065c8 2020-03-19 stsp free(id_str);
1942 984065c8 2020-03-19 stsp id_str = NULL;
1943 984065c8 2020-03-19 stsp }
1944 7848a0e1 2020-03-19 stsp
1945 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1946 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1947 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1948 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1949 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1950 7848a0e1 2020-03-19 stsp char *remote_refname;
1951 7848a0e1 2020-03-19 stsp
1952 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1953 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1954 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1955 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1956 0e4002ca 2020-03-21 stsp if (error)
1957 0e4002ca 2020-03-21 stsp goto done;
1958 0e4002ca 2020-03-21 stsp continue;
1959 0e4002ca 2020-03-21 stsp }
1960 0e4002ca 2020-03-21 stsp
1961 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1962 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1963 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1964 7848a0e1 2020-03-19 stsp if (error) {
1965 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1966 7848a0e1 2020-03-19 stsp goto done;
1967 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1968 6338a6a1 2020-03-21 stsp repo);
1969 7848a0e1 2020-03-19 stsp if (error)
1970 7848a0e1 2020-03-19 stsp goto done;
1971 7848a0e1 2020-03-19 stsp } else {
1972 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1973 db6d8ad8 2020-03-21 stsp verbosity, repo);
1974 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1975 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1976 9f142382 2020-03-21 stsp error = unlock_err;
1977 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1978 7848a0e1 2020-03-19 stsp if (error)
1979 7848a0e1 2020-03-19 stsp goto done;
1980 7848a0e1 2020-03-19 stsp }
1981 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1982 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1983 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1984 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1985 7848a0e1 2020-03-19 stsp goto done;
1986 7848a0e1 2020-03-19 stsp }
1987 7848a0e1 2020-03-19 stsp
1988 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1989 7848a0e1 2020-03-19 stsp if (error) {
1990 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1991 7848a0e1 2020-03-19 stsp goto done;
1992 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1993 688f11b3 2020-03-21 stsp verbosity, repo);
1994 7848a0e1 2020-03-19 stsp if (error)
1995 7848a0e1 2020-03-19 stsp goto done;
1996 7848a0e1 2020-03-19 stsp } else {
1997 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1998 db6d8ad8 2020-03-21 stsp verbosity, repo);
1999 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2000 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2001 9f142382 2020-03-21 stsp error = unlock_err;
2002 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2003 7848a0e1 2020-03-19 stsp if (error)
2004 7848a0e1 2020-03-19 stsp goto done;
2005 7848a0e1 2020-03-19 stsp }
2006 2ec30c80 2020-03-20 stsp
2007 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2008 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2009 2ec30c80 2020-03-20 stsp if (error) {
2010 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2011 2ec30c80 2020-03-20 stsp goto done;
2012 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2013 6338a6a1 2020-03-21 stsp repo);
2014 2ec30c80 2020-03-20 stsp if (error)
2015 2ec30c80 2020-03-20 stsp goto done;
2016 9f142382 2020-03-21 stsp } else {
2017 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2018 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2019 9f142382 2020-03-21 stsp error = unlock_err;
2020 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2021 9f142382 2020-03-21 stsp }
2022 7848a0e1 2020-03-19 stsp }
2023 7848a0e1 2020-03-19 stsp }
2024 f1bcca34 2020-03-25 stsp if (delete_refs) {
2025 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2026 3789fd73 2020-03-26 stsp verbosity, repo);
2027 f1bcca34 2020-03-25 stsp if (error)
2028 f1bcca34 2020-03-25 stsp goto done;
2029 f1bcca34 2020-03-25 stsp }
2030 f1bcca34 2020-03-25 stsp
2031 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2032 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2033 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2034 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2035 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2036 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2037 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2038 f1bcca34 2020-03-25 stsp
2039 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2040 f1bcca34 2020-03-25 stsp continue;
2041 f1bcca34 2020-03-25 stsp
2042 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2043 f1bcca34 2020-03-25 stsp continue;
2044 f1bcca34 2020-03-25 stsp
2045 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2046 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2047 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2048 f1bcca34 2020-03-25 stsp goto done;
2049 f1bcca34 2020-03-25 stsp }
2050 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2051 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2052 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2053 f1bcca34 2020-03-25 stsp free(remote_refname);
2054 f1bcca34 2020-03-25 stsp goto done;
2055 f1bcca34 2020-03-25 stsp }
2056 f1bcca34 2020-03-25 stsp
2057 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2058 f1bcca34 2020-03-25 stsp 0);
2059 f1bcca34 2020-03-25 stsp if (error) {
2060 f1bcca34 2020-03-25 stsp free(remote_refname);
2061 f1bcca34 2020-03-25 stsp free(remote_target);
2062 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2063 f1bcca34 2020-03-25 stsp error = NULL;
2064 f1bcca34 2020-03-25 stsp continue;
2065 f1bcca34 2020-03-25 stsp }
2066 f1bcca34 2020-03-25 stsp goto done;
2067 f1bcca34 2020-03-25 stsp }
2068 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2069 f1bcca34 2020-03-25 stsp verbosity, repo);
2070 f1bcca34 2020-03-25 stsp free(remote_refname);
2071 f1bcca34 2020-03-25 stsp free(remote_target);
2072 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2073 f1bcca34 2020-03-25 stsp if (error)
2074 f1bcca34 2020-03-25 stsp goto done;
2075 f1bcca34 2020-03-25 stsp }
2076 f1bcca34 2020-03-25 stsp }
2077 7848a0e1 2020-03-19 stsp done:
2078 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2079 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2080 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2081 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2082 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2083 9c52365f 2020-03-21 stsp }
2084 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2085 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2086 7848a0e1 2020-03-19 stsp if (repo)
2087 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2088 7848a0e1 2020-03-19 stsp if (worktree)
2089 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2090 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2091 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2092 7848a0e1 2020-03-19 stsp free(pe->data);
2093 7848a0e1 2020-03-19 stsp }
2094 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2095 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2096 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2097 7848a0e1 2020-03-19 stsp free(pe->data);
2098 7848a0e1 2020-03-19 stsp }
2099 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2100 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2101 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2102 7848a0e1 2020-03-19 stsp free(id_str);
2103 7848a0e1 2020-03-19 stsp free(cwd);
2104 7848a0e1 2020-03-19 stsp free(repo_path);
2105 7848a0e1 2020-03-19 stsp free(pack_hash);
2106 7848a0e1 2020-03-19 stsp free(proto);
2107 7848a0e1 2020-03-19 stsp free(host);
2108 7848a0e1 2020-03-19 stsp free(port);
2109 7848a0e1 2020-03-19 stsp free(server_path);
2110 7848a0e1 2020-03-19 stsp free(repo_name);
2111 7848a0e1 2020-03-19 stsp return error;
2112 7848a0e1 2020-03-19 stsp }
2113 7848a0e1 2020-03-19 stsp
2114 7848a0e1 2020-03-19 stsp
2115 7848a0e1 2020-03-19 stsp __dead static void
2116 2ab43947 2020-03-18 stsp usage_checkout(void)
2117 2ab43947 2020-03-18 stsp {
2118 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2119 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2120 2ab43947 2020-03-18 stsp exit(1);
2121 2ab43947 2020-03-18 stsp }
2122 2ab43947 2020-03-18 stsp
2123 2ab43947 2020-03-18 stsp static void
2124 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2125 2ab43947 2020-03-18 stsp {
2126 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2127 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2128 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2129 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2130 2ab43947 2020-03-18 stsp getprogname());
2131 2ab43947 2020-03-18 stsp }
2132 2ab43947 2020-03-18 stsp
2133 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2134 2ab43947 2020-03-18 stsp const char *worktree_path;
2135 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2136 2ab43947 2020-03-18 stsp };
2137 2ab43947 2020-03-18 stsp
2138 2ab43947 2020-03-18 stsp static const struct got_error *
2139 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2140 2ab43947 2020-03-18 stsp {
2141 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2142 2ab43947 2020-03-18 stsp
2143 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2144 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2145 2ab43947 2020-03-18 stsp return NULL;
2146 2ab43947 2020-03-18 stsp
2147 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2148 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2149 2ab43947 2020-03-18 stsp return NULL;
2150 2ab43947 2020-03-18 stsp }
2151 2ab43947 2020-03-18 stsp
2152 2ab43947 2020-03-18 stsp while (path[0] == '/')
2153 2ab43947 2020-03-18 stsp path++;
2154 2ab43947 2020-03-18 stsp
2155 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2156 2ab43947 2020-03-18 stsp return NULL;
2157 2ab43947 2020-03-18 stsp }
2158 2ab43947 2020-03-18 stsp
2159 2ab43947 2020-03-18 stsp static const struct got_error *
2160 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2161 2ab43947 2020-03-18 stsp {
2162 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2163 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2164 2ab43947 2020-03-18 stsp return NULL;
2165 2ab43947 2020-03-18 stsp }
2166 2ab43947 2020-03-18 stsp
2167 2ab43947 2020-03-18 stsp static const struct got_error *
2168 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2169 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2170 2ab43947 2020-03-18 stsp struct got_repository *repo)
2171 2ab43947 2020-03-18 stsp {
2172 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2173 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2174 2ab43947 2020-03-18 stsp
2175 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2176 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2177 2ab43947 2020-03-18 stsp if (err)
2178 2ab43947 2020-03-18 stsp return err;
2179 2ab43947 2020-03-18 stsp
2180 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2181 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2182 2ab43947 2020-03-18 stsp
2183 2ab43947 2020-03-18 stsp /*
2184 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2185 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2186 2ab43947 2020-03-18 stsp *
2187 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2188 2ab43947 2020-03-18 stsp *
2189 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2190 2ab43947 2020-03-18 stsp * \ /
2191 2ab43947 2020-03-18 stsp * C E
2192 2ab43947 2020-03-18 stsp * \ /
2193 2ab43947 2020-03-18 stsp * B (yca)
2194 2ab43947 2020-03-18 stsp * |
2195 2ab43947 2020-03-18 stsp * A
2196 2ab43947 2020-03-18 stsp *
2197 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2198 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2199 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2200 2ab43947 2020-03-18 stsp */
2201 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2202 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2203 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2204 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2205 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2206 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2207 2ab43947 2020-03-18 stsp
2208 2ab43947 2020-03-18 stsp free(yca_id);
2209 2ab43947 2020-03-18 stsp return NULL;
2210 2ab43947 2020-03-18 stsp }
2211 2ab43947 2020-03-18 stsp
2212 2ab43947 2020-03-18 stsp static const struct got_error *
2213 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2214 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2215 2ab43947 2020-03-18 stsp struct got_repository *repo)
2216 2ab43947 2020-03-18 stsp {
2217 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2218 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2219 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2220 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2221 2ab43947 2020-03-18 stsp
2222 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2223 2ab43947 2020-03-18 stsp if (err)
2224 2ab43947 2020-03-18 stsp goto done;
2225 2ab43947 2020-03-18 stsp
2226 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2227 2ab43947 2020-03-18 stsp is_same_branch = 1;
2228 2ab43947 2020-03-18 stsp goto done;
2229 2ab43947 2020-03-18 stsp }
2230 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2231 2ab43947 2020-03-18 stsp is_same_branch = 1;
2232 2ab43947 2020-03-18 stsp goto done;
2233 2ab43947 2020-03-18 stsp }
2234 2ab43947 2020-03-18 stsp
2235 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2236 2ab43947 2020-03-18 stsp if (err)
2237 2ab43947 2020-03-18 stsp goto done;
2238 2ab43947 2020-03-18 stsp
2239 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2240 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2241 2ab43947 2020-03-18 stsp if (err)
2242 2ab43947 2020-03-18 stsp goto done;
2243 2ab43947 2020-03-18 stsp
2244 2ab43947 2020-03-18 stsp for (;;) {
2245 2ab43947 2020-03-18 stsp struct got_object_id *id;
2246 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2247 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2248 2ab43947 2020-03-18 stsp if (err) {
2249 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2250 2ab43947 2020-03-18 stsp err = NULL;
2251 2ab43947 2020-03-18 stsp break;
2252 2ab43947 2020-03-18 stsp }
2253 2ab43947 2020-03-18 stsp
2254 2ab43947 2020-03-18 stsp if (id) {
2255 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2256 2ab43947 2020-03-18 stsp break;
2257 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2258 2ab43947 2020-03-18 stsp is_same_branch = 1;
2259 2ab43947 2020-03-18 stsp break;
2260 2ab43947 2020-03-18 stsp }
2261 2ab43947 2020-03-18 stsp }
2262 2ab43947 2020-03-18 stsp }
2263 2ab43947 2020-03-18 stsp done:
2264 2ab43947 2020-03-18 stsp if (graph)
2265 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2266 2ab43947 2020-03-18 stsp free(head_commit_id);
2267 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2268 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2269 2ab43947 2020-03-18 stsp return err;
2270 a367ff0f 2019-05-14 stsp }
2271 8069f636 2019-01-12 stsp
2272 4ed7e80c 2018-05-20 stsp static const struct got_error *
2273 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2274 4b6c9460 2020-03-05 stsp {
2275 4b6c9460 2020-03-05 stsp static char msg[512];
2276 4b6c9460 2020-03-05 stsp const char *branch_name;
2277 4b6c9460 2020-03-05 stsp
2278 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2279 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2280 4b6c9460 2020-03-05 stsp else
2281 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2282 4b6c9460 2020-03-05 stsp
2283 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2284 4b6c9460 2020-03-05 stsp branch_name += 11;
2285 4b6c9460 2020-03-05 stsp
2286 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2287 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2288 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2289 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2290 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2291 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2292 4b6c9460 2020-03-05 stsp
2293 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2294 4b6c9460 2020-03-05 stsp }
2295 4b6c9460 2020-03-05 stsp
2296 4b6c9460 2020-03-05 stsp static const struct got_error *
2297 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2298 c09a553d 2018-03-12 stsp {
2299 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2300 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2301 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2302 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2303 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2304 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2305 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2306 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2307 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2308 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2309 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2310 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2311 f2ea84fa 2019-07-27 stsp
2312 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2313 c09a553d 2018-03-12 stsp
2314 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2315 0bb8a95e 2018-03-12 stsp switch (ch) {
2316 08573d5b 2019-05-14 stsp case 'b':
2317 08573d5b 2019-05-14 stsp branch_name = optarg;
2318 08573d5b 2019-05-14 stsp break;
2319 8069f636 2019-01-12 stsp case 'c':
2320 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2321 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2322 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2323 bb51a5b4 2020-01-13 stsp break;
2324 bb51a5b4 2020-01-13 stsp case 'E':
2325 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2326 8069f636 2019-01-12 stsp break;
2327 0bb8a95e 2018-03-12 stsp case 'p':
2328 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2329 0bb8a95e 2018-03-12 stsp break;
2330 0bb8a95e 2018-03-12 stsp default:
2331 2deda0b9 2019-03-07 stsp usage_checkout();
2332 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2333 0bb8a95e 2018-03-12 stsp }
2334 0bb8a95e 2018-03-12 stsp }
2335 0bb8a95e 2018-03-12 stsp
2336 0bb8a95e 2018-03-12 stsp argc -= optind;
2337 0bb8a95e 2018-03-12 stsp argv += optind;
2338 0bb8a95e 2018-03-12 stsp
2339 6715a751 2018-03-16 stsp #ifndef PROFILE
2340 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2341 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2342 c09a553d 2018-03-12 stsp err(1, "pledge");
2343 6715a751 2018-03-16 stsp #endif
2344 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2345 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2346 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2347 76089277 2018-04-01 stsp if (repo_path == NULL)
2348 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2349 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2350 76089277 2018-04-01 stsp if (cwd == NULL) {
2351 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2352 76089277 2018-04-01 stsp goto done;
2353 76089277 2018-04-01 stsp }
2354 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2355 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2356 230a42bd 2019-05-11 jcs if (base == NULL) {
2357 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2358 230a42bd 2019-05-11 jcs path_prefix);
2359 230a42bd 2019-05-11 jcs goto done;
2360 230a42bd 2019-05-11 jcs }
2361 230a42bd 2019-05-11 jcs } else {
2362 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2363 230a42bd 2019-05-11 jcs if (base == NULL) {
2364 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2365 230a42bd 2019-05-11 jcs repo_path);
2366 230a42bd 2019-05-11 jcs goto done;
2367 230a42bd 2019-05-11 jcs }
2368 76089277 2018-04-01 stsp }
2369 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2370 c09a553d 2018-03-12 stsp if (dotgit)
2371 c09a553d 2018-03-12 stsp *dotgit = '\0';
2372 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2373 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2374 c09a553d 2018-03-12 stsp free(cwd);
2375 76089277 2018-04-01 stsp goto done;
2376 c09a553d 2018-03-12 stsp }
2377 c09a553d 2018-03-12 stsp free(cwd);
2378 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2379 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2380 76089277 2018-04-01 stsp if (repo_path == NULL) {
2381 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2382 76089277 2018-04-01 stsp goto done;
2383 76089277 2018-04-01 stsp }
2384 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2385 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2386 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2387 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2388 b4b3a7dd 2019-07-22 stsp argv[1]);
2389 b4b3a7dd 2019-07-22 stsp goto done;
2390 b4b3a7dd 2019-07-22 stsp }
2391 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2392 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2393 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2394 b4b3a7dd 2019-07-22 stsp goto done;
2395 b4b3a7dd 2019-07-22 stsp }
2396 76089277 2018-04-01 stsp }
2397 c09a553d 2018-03-12 stsp } else
2398 c09a553d 2018-03-12 stsp usage_checkout();
2399 c09a553d 2018-03-12 stsp
2400 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2401 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2402 13bfb272 2019-05-10 jcs
2403 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2404 c09a553d 2018-03-12 stsp if (error != NULL)
2405 c02c541e 2019-03-29 stsp goto done;
2406 c02c541e 2019-03-29 stsp
2407 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2408 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2409 c530dc23 2019-07-23 stsp if (error) {
2410 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2411 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2412 c530dc23 2019-07-23 stsp goto done;
2413 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2414 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2415 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2416 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2417 c530dc23 2019-07-23 stsp goto done;
2418 c530dc23 2019-07-23 stsp }
2419 c530dc23 2019-07-23 stsp }
2420 c530dc23 2019-07-23 stsp
2421 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2422 c02c541e 2019-03-29 stsp if (error)
2423 c09a553d 2018-03-12 stsp goto done;
2424 8069f636 2019-01-12 stsp
2425 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2426 c09a553d 2018-03-12 stsp if (error != NULL)
2427 c09a553d 2018-03-12 stsp goto done;
2428 c09a553d 2018-03-12 stsp
2429 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2430 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2431 c09a553d 2018-03-12 stsp goto done;
2432 c09a553d 2018-03-12 stsp
2433 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2434 c09a553d 2018-03-12 stsp if (error != NULL)
2435 c09a553d 2018-03-12 stsp goto done;
2436 c09a553d 2018-03-12 stsp
2437 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2438 e5dc7198 2018-12-29 stsp path_prefix);
2439 e5dc7198 2018-12-29 stsp if (error != NULL)
2440 e5dc7198 2018-12-29 stsp goto done;
2441 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2442 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2443 49520a32 2018-12-29 stsp goto done;
2444 49520a32 2018-12-29 stsp }
2445 49520a32 2018-12-29 stsp
2446 8069f636 2019-01-12 stsp if (commit_id_str) {
2447 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2448 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2449 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2450 30837e32 2019-07-25 stsp if (error)
2451 8069f636 2019-01-12 stsp goto done;
2452 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2453 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2454 8069f636 2019-01-12 stsp if (error != NULL) {
2455 8069f636 2019-01-12 stsp free(commit_id);
2456 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2457 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2458 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2459 4b6c9460 2020-03-05 stsp }
2460 8069f636 2019-01-12 stsp goto done;
2461 8069f636 2019-01-12 stsp }
2462 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2463 4b6c9460 2020-03-05 stsp if (error) {
2464 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2465 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2466 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2467 4b6c9460 2020-03-05 stsp }
2468 45d344f6 2019-05-14 stsp goto done;
2469 4b6c9460 2020-03-05 stsp }
2470 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2471 8069f636 2019-01-12 stsp commit_id);
2472 8069f636 2019-01-12 stsp free(commit_id);
2473 8069f636 2019-01-12 stsp if (error)
2474 8069f636 2019-01-12 stsp goto done;
2475 8069f636 2019-01-12 stsp }
2476 8069f636 2019-01-12 stsp
2477 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2478 f2ea84fa 2019-07-27 stsp if (error)
2479 f2ea84fa 2019-07-27 stsp goto done;
2480 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2481 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2482 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2483 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2484 c09a553d 2018-03-12 stsp if (error != NULL)
2485 c09a553d 2018-03-12 stsp goto done;
2486 c09a553d 2018-03-12 stsp
2487 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2488 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2489 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2490 c09a553d 2018-03-12 stsp done:
2491 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2492 8069f636 2019-01-12 stsp free(commit_id_str);
2493 76089277 2018-04-01 stsp free(repo_path);
2494 507dc3bb 2018-12-29 stsp free(worktree_path);
2495 507dc3bb 2018-12-29 stsp return error;
2496 507dc3bb 2018-12-29 stsp }
2497 507dc3bb 2018-12-29 stsp
2498 507dc3bb 2018-12-29 stsp __dead static void
2499 507dc3bb 2018-12-29 stsp usage_update(void)
2500 507dc3bb 2018-12-29 stsp {
2501 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2502 507dc3bb 2018-12-29 stsp getprogname());
2503 507dc3bb 2018-12-29 stsp exit(1);
2504 507dc3bb 2018-12-29 stsp }
2505 507dc3bb 2018-12-29 stsp
2506 1ee397ad 2019-07-12 stsp static const struct got_error *
2507 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2508 507dc3bb 2018-12-29 stsp {
2509 784955db 2019-01-12 stsp int *did_something = arg;
2510 784955db 2019-01-12 stsp
2511 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2512 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2513 1ee397ad 2019-07-12 stsp return NULL;
2514 507dc3bb 2018-12-29 stsp
2515 1545c615 2019-02-10 stsp *did_something = 1;
2516 a484d721 2019-06-10 stsp
2517 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2518 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2519 1ee397ad 2019-07-12 stsp return NULL;
2520 a484d721 2019-06-10 stsp
2521 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2522 507dc3bb 2018-12-29 stsp path++;
2523 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2524 1ee397ad 2019-07-12 stsp return NULL;
2525 be7061eb 2018-12-30 stsp }
2526 be7061eb 2018-12-30 stsp
2527 be7061eb 2018-12-30 stsp static const struct got_error *
2528 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2529 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2530 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2531 a1fb16d8 2019-05-24 stsp {
2532 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2533 a1fb16d8 2019-05-24 stsp char *base_id_str;
2534 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2535 a1fb16d8 2019-05-24 stsp
2536 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2537 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2538 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2539 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2540 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2541 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2542 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2543 a1fb16d8 2019-05-24 stsp }
2544 a1fb16d8 2019-05-24 stsp
2545 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2546 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2547 a1fb16d8 2019-05-24 stsp if (err) {
2548 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2549 a1fb16d8 2019-05-24 stsp return err;
2550 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2551 a1fb16d8 2019-05-24 stsp }
2552 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2553 a1fb16d8 2019-05-24 stsp return NULL;
2554 a1fb16d8 2019-05-24 stsp
2555 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2556 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2557 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2558 a1fb16d8 2019-05-24 stsp if (err)
2559 a1fb16d8 2019-05-24 stsp return err;
2560 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2561 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2562 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2563 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2564 0ebf8283 2019-07-24 stsp return NULL;
2565 0ebf8283 2019-07-24 stsp }
2566 0ebf8283 2019-07-24 stsp
2567 0ebf8283 2019-07-24 stsp static const struct got_error *
2568 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2569 0ebf8283 2019-07-24 stsp {
2570 0ebf8283 2019-07-24 stsp const struct got_error *err;
2571 0ebf8283 2019-07-24 stsp int in_progress;
2572 0ebf8283 2019-07-24 stsp
2573 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2574 0ebf8283 2019-07-24 stsp if (err)
2575 0ebf8283 2019-07-24 stsp return err;
2576 0ebf8283 2019-07-24 stsp if (in_progress)
2577 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2578 0ebf8283 2019-07-24 stsp
2579 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2580 0ebf8283 2019-07-24 stsp if (err)
2581 0ebf8283 2019-07-24 stsp return err;
2582 0ebf8283 2019-07-24 stsp if (in_progress)
2583 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2584 0ebf8283 2019-07-24 stsp
2585 a1fb16d8 2019-05-24 stsp return NULL;
2586 a5edda0a 2019-07-27 stsp }
2587 a5edda0a 2019-07-27 stsp
2588 a5edda0a 2019-07-27 stsp static const struct got_error *
2589 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2590 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2591 a5edda0a 2019-07-27 stsp {
2592 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2593 a5edda0a 2019-07-27 stsp char *path;
2594 a5edda0a 2019-07-27 stsp int i;
2595 a5edda0a 2019-07-27 stsp
2596 a5edda0a 2019-07-27 stsp if (argc == 0) {
2597 a5edda0a 2019-07-27 stsp path = strdup("");
2598 a5edda0a 2019-07-27 stsp if (path == NULL)
2599 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2600 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2601 a5edda0a 2019-07-27 stsp }
2602 a5edda0a 2019-07-27 stsp
2603 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2604 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2605 a5edda0a 2019-07-27 stsp if (err)
2606 a5edda0a 2019-07-27 stsp break;
2607 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2608 a5edda0a 2019-07-27 stsp if (err) {
2609 a5edda0a 2019-07-27 stsp free(path);
2610 a5edda0a 2019-07-27 stsp break;
2611 a5edda0a 2019-07-27 stsp }
2612 a5edda0a 2019-07-27 stsp }
2613 a5edda0a 2019-07-27 stsp
2614 a5edda0a 2019-07-27 stsp return err;
2615 a1fb16d8 2019-05-24 stsp }
2616 a1fb16d8 2019-05-24 stsp
2617 a1fb16d8 2019-05-24 stsp static const struct got_error *
2618 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2619 507dc3bb 2018-12-29 stsp {
2620 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2621 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2622 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2623 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2624 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2625 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2626 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2627 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2628 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2629 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2630 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2631 507dc3bb 2018-12-29 stsp
2632 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2633 f2ea84fa 2019-07-27 stsp
2634 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2635 507dc3bb 2018-12-29 stsp switch (ch) {
2636 024e9686 2019-05-14 stsp case 'b':
2637 024e9686 2019-05-14 stsp branch_name = optarg;
2638 024e9686 2019-05-14 stsp break;
2639 507dc3bb 2018-12-29 stsp case 'c':
2640 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2641 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2642 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2643 507dc3bb 2018-12-29 stsp break;
2644 507dc3bb 2018-12-29 stsp default:
2645 2deda0b9 2019-03-07 stsp usage_update();
2646 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2647 507dc3bb 2018-12-29 stsp }
2648 507dc3bb 2018-12-29 stsp }
2649 507dc3bb 2018-12-29 stsp
2650 507dc3bb 2018-12-29 stsp argc -= optind;
2651 507dc3bb 2018-12-29 stsp argv += optind;
2652 507dc3bb 2018-12-29 stsp
2653 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2654 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2655 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2656 507dc3bb 2018-12-29 stsp err(1, "pledge");
2657 507dc3bb 2018-12-29 stsp #endif
2658 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2659 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2660 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2661 c4cdcb68 2019-04-03 stsp goto done;
2662 c4cdcb68 2019-04-03 stsp }
2663 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2664 7d5807f4 2019-07-11 stsp if (error)
2665 7d5807f4 2019-07-11 stsp goto done;
2666 7d5807f4 2019-07-11 stsp
2667 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2668 f2ea84fa 2019-07-27 stsp if (error)
2669 f2ea84fa 2019-07-27 stsp goto done;
2670 507dc3bb 2018-12-29 stsp
2671 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2672 c9956ddf 2019-09-08 stsp NULL);
2673 507dc3bb 2018-12-29 stsp if (error != NULL)
2674 507dc3bb 2018-12-29 stsp goto done;
2675 507dc3bb 2018-12-29 stsp
2676 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2677 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2678 087fb88c 2019-08-04 stsp if (error)
2679 087fb88c 2019-08-04 stsp goto done;
2680 087fb88c 2019-08-04 stsp
2681 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2682 0266afb7 2019-01-04 stsp if (error)
2683 0266afb7 2019-01-04 stsp goto done;
2684 0266afb7 2019-01-04 stsp
2685 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2686 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2687 024e9686 2019-05-14 stsp if (error != NULL)
2688 024e9686 2019-05-14 stsp goto done;
2689 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2690 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2691 9c4b8182 2019-01-02 stsp if (error != NULL)
2692 9c4b8182 2019-01-02 stsp goto done;
2693 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2694 507dc3bb 2018-12-29 stsp if (error != NULL)
2695 507dc3bb 2018-12-29 stsp goto done;
2696 507dc3bb 2018-12-29 stsp } else {
2697 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2698 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2699 04f57cb3 2019-07-25 stsp free(commit_id_str);
2700 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2701 30837e32 2019-07-25 stsp if (error)
2702 a297e751 2019-07-11 stsp goto done;
2703 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2704 a297e751 2019-07-11 stsp if (error)
2705 507dc3bb 2018-12-29 stsp goto done;
2706 507dc3bb 2018-12-29 stsp }
2707 35c965b2 2018-12-29 stsp
2708 a1fb16d8 2019-05-24 stsp if (branch_name) {
2709 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2710 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2711 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2712 f2ea84fa 2019-07-27 stsp continue;
2713 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2714 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2715 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2716 024e9686 2019-05-14 stsp goto done;
2717 024e9686 2019-05-14 stsp }
2718 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2719 024e9686 2019-05-14 stsp if (error)
2720 024e9686 2019-05-14 stsp goto done;
2721 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2722 3aef623b 2019-10-15 stsp repo);
2723 a367ff0f 2019-05-14 stsp free(head_commit_id);
2724 024e9686 2019-05-14 stsp if (error != NULL)
2725 024e9686 2019-05-14 stsp goto done;
2726 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2727 a367ff0f 2019-05-14 stsp if (error)
2728 a367ff0f 2019-05-14 stsp goto done;
2729 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2730 024e9686 2019-05-14 stsp if (error)
2731 024e9686 2019-05-14 stsp goto done;
2732 024e9686 2019-05-14 stsp } else {
2733 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2734 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2735 a367ff0f 2019-05-14 stsp if (error != NULL) {
2736 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2737 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2738 024e9686 2019-05-14 stsp goto done;
2739 a367ff0f 2019-05-14 stsp }
2740 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2741 a367ff0f 2019-05-14 stsp if (error)
2742 a367ff0f 2019-05-14 stsp goto done;
2743 024e9686 2019-05-14 stsp }
2744 507dc3bb 2018-12-29 stsp
2745 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2746 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2747 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2748 507dc3bb 2018-12-29 stsp commit_id);
2749 507dc3bb 2018-12-29 stsp if (error)
2750 507dc3bb 2018-12-29 stsp goto done;
2751 507dc3bb 2018-12-29 stsp }
2752 507dc3bb 2018-12-29 stsp
2753 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2754 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2755 507dc3bb 2018-12-29 stsp if (error != NULL)
2756 507dc3bb 2018-12-29 stsp goto done;
2757 9c4b8182 2019-01-02 stsp
2758 784955db 2019-01-12 stsp if (did_something)
2759 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2760 784955db 2019-01-12 stsp else
2761 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2762 507dc3bb 2018-12-29 stsp done:
2763 c09a553d 2018-03-12 stsp free(worktree_path);
2764 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2765 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2766 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2767 507dc3bb 2018-12-29 stsp free(commit_id);
2768 9c4b8182 2019-01-02 stsp free(commit_id_str);
2769 c09a553d 2018-03-12 stsp return error;
2770 c09a553d 2018-03-12 stsp }
2771 c09a553d 2018-03-12 stsp
2772 f42b1b34 2018-03-12 stsp static const struct got_error *
2773 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2774 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2775 63035f9f 2019-10-06 stsp struct got_repository *repo)
2776 5c860e29 2018-03-12 stsp {
2777 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2778 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2779 79109fed 2018-03-27 stsp
2780 44392932 2019-08-25 stsp if (blob_id1) {
2781 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2782 44392932 2019-08-25 stsp if (err)
2783 44392932 2019-08-25 stsp goto done;
2784 44392932 2019-08-25 stsp }
2785 79109fed 2018-03-27 stsp
2786 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2787 44392932 2019-08-25 stsp if (err)
2788 44392932 2019-08-25 stsp goto done;
2789 79109fed 2018-03-27 stsp
2790 44392932 2019-08-25 stsp while (path[0] == '/')
2791 44392932 2019-08-25 stsp path++;
2792 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2793 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2794 44392932 2019-08-25 stsp done:
2795 44392932 2019-08-25 stsp if (blob1)
2796 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2797 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2798 44392932 2019-08-25 stsp return err;
2799 44392932 2019-08-25 stsp }
2800 44392932 2019-08-25 stsp
2801 44392932 2019-08-25 stsp static const struct got_error *
2802 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2803 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2804 63035f9f 2019-10-06 stsp struct got_repository *repo)
2805 44392932 2019-08-25 stsp {
2806 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2807 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2808 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2809 44392932 2019-08-25 stsp
2810 44392932 2019-08-25 stsp if (tree_id1) {
2811 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2812 79109fed 2018-03-27 stsp if (err)
2813 44392932 2019-08-25 stsp goto done;
2814 79109fed 2018-03-27 stsp }
2815 79109fed 2018-03-27 stsp
2816 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2817 0f2b3dca 2018-12-22 stsp if (err)
2818 0f2b3dca 2018-12-22 stsp goto done;
2819 0f2b3dca 2018-12-22 stsp
2820 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2821 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2822 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2823 44392932 2019-08-25 stsp while (path[0] == '/')
2824 44392932 2019-08-25 stsp path++;
2825 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2826 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2827 0f2b3dca 2018-12-22 stsp done:
2828 79109fed 2018-03-27 stsp if (tree1)
2829 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2830 366e0a5f 2019-10-10 stsp if (tree2)
2831 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2832 44392932 2019-08-25 stsp return err;
2833 44392932 2019-08-25 stsp }
2834 44392932 2019-08-25 stsp
2835 44392932 2019-08-25 stsp static const struct got_error *
2836 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2837 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2838 44392932 2019-08-25 stsp {
2839 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2840 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2841 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2842 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2843 44392932 2019-08-25 stsp struct got_object_qid *qid;
2844 44392932 2019-08-25 stsp
2845 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2846 44392932 2019-08-25 stsp if (qid != NULL) {
2847 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2848 44392932 2019-08-25 stsp qid->id);
2849 44392932 2019-08-25 stsp if (err)
2850 44392932 2019-08-25 stsp return err;
2851 44392932 2019-08-25 stsp }
2852 44392932 2019-08-25 stsp
2853 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2854 44392932 2019-08-25 stsp int obj_type;
2855 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2856 44392932 2019-08-25 stsp if (err)
2857 44392932 2019-08-25 stsp goto done;
2858 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2859 44392932 2019-08-25 stsp if (err) {
2860 44392932 2019-08-25 stsp free(obj_id2);
2861 44392932 2019-08-25 stsp goto done;
2862 44392932 2019-08-25 stsp }
2863 44392932 2019-08-25 stsp if (pcommit) {
2864 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2865 44392932 2019-08-25 stsp qid->id, path);
2866 44392932 2019-08-25 stsp if (err) {
2867 44392932 2019-08-25 stsp free(obj_id2);
2868 44392932 2019-08-25 stsp goto done;
2869 44392932 2019-08-25 stsp }
2870 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2871 44392932 2019-08-25 stsp if (err) {
2872 44392932 2019-08-25 stsp free(obj_id2);
2873 44392932 2019-08-25 stsp goto done;
2874 44392932 2019-08-25 stsp }
2875 44392932 2019-08-25 stsp }
2876 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2877 44392932 2019-08-25 stsp if (err) {
2878 44392932 2019-08-25 stsp free(obj_id2);
2879 44392932 2019-08-25 stsp goto done;
2880 44392932 2019-08-25 stsp }
2881 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2882 44392932 2019-08-25 stsp switch (obj_type) {
2883 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2884 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2885 63035f9f 2019-10-06 stsp 0, repo);
2886 44392932 2019-08-25 stsp break;
2887 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2888 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2889 63035f9f 2019-10-06 stsp 0, repo);
2890 44392932 2019-08-25 stsp break;
2891 44392932 2019-08-25 stsp default:
2892 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2893 44392932 2019-08-25 stsp break;
2894 44392932 2019-08-25 stsp }
2895 44392932 2019-08-25 stsp free(obj_id1);
2896 44392932 2019-08-25 stsp free(obj_id2);
2897 44392932 2019-08-25 stsp } else {
2898 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2899 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2900 44392932 2019-08-25 stsp if (err)
2901 44392932 2019-08-25 stsp goto done;
2902 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2903 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2904 44392932 2019-08-25 stsp if (err)
2905 44392932 2019-08-25 stsp goto done;
2906 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2907 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2908 44392932 2019-08-25 stsp }
2909 44392932 2019-08-25 stsp done:
2910 0f2b3dca 2018-12-22 stsp free(id_str1);
2911 0f2b3dca 2018-12-22 stsp free(id_str2);
2912 44392932 2019-08-25 stsp if (pcommit)
2913 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2914 79109fed 2018-03-27 stsp return err;
2915 79109fed 2018-03-27 stsp }
2916 79109fed 2018-03-27 stsp
2917 4bb494d5 2018-06-16 stsp static char *
2918 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2919 6c281f94 2018-06-11 stsp {
2920 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2921 09867e48 2019-08-13 stsp char *p, *s;
2922 09867e48 2019-08-13 stsp
2923 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2924 09867e48 2019-08-13 stsp if (tm == NULL)
2925 09867e48 2019-08-13 stsp return NULL;
2926 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2927 09867e48 2019-08-13 stsp if (s == NULL)
2928 09867e48 2019-08-13 stsp return NULL;
2929 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2930 6c281f94 2018-06-11 stsp if (p)
2931 6c281f94 2018-06-11 stsp *p = '\0';
2932 6c281f94 2018-06-11 stsp return s;
2933 6c281f94 2018-06-11 stsp }
2934 dc424a06 2019-08-07 stsp
2935 6841bf13 2019-11-29 kn static const struct got_error *
2936 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2937 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2938 6841bf13 2019-11-29 kn {
2939 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2940 6841bf13 2019-11-29 kn regmatch_t regmatch;
2941 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2942 6841bf13 2019-11-29 kn
2943 6841bf13 2019-11-29 kn *have_match = 0;
2944 6841bf13 2019-11-29 kn
2945 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2946 6841bf13 2019-11-29 kn if (err)
2947 6841bf13 2019-11-29 kn return err;
2948 6841bf13 2019-11-29 kn
2949 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2950 6841bf13 2019-11-29 kn if (err)
2951 6841bf13 2019-11-29 kn goto done;
2952 6841bf13 2019-11-29 kn
2953 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2954 6841bf13 2019-11-29 kn *have_match = 1;
2955 6841bf13 2019-11-29 kn done:
2956 6841bf13 2019-11-29 kn free(id_str);
2957 6841bf13 2019-11-29 kn free(logmsg);
2958 6841bf13 2019-11-29 kn return err;
2959 6841bf13 2019-11-29 kn }
2960 6841bf13 2019-11-29 kn
2961 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2962 6c281f94 2018-06-11 stsp
2963 79109fed 2018-03-27 stsp static const struct got_error *
2964 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2965 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2966 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2967 79109fed 2018-03-27 stsp {
2968 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2969 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2970 6c281f94 2018-06-11 stsp char datebuf[26];
2971 45d799e2 2018-12-23 stsp time_t committer_time;
2972 45d799e2 2018-12-23 stsp const char *author, *committer;
2973 199a4027 2019-02-02 stsp char *refs_str = NULL;
2974 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2975 5c860e29 2018-03-12 stsp
2976 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2977 199a4027 2019-02-02 stsp char *s;
2978 199a4027 2019-02-02 stsp const char *name;
2979 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2980 a436ad14 2019-08-13 stsp int cmp;
2981 a436ad14 2019-08-13 stsp
2982 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2983 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2984 d9498b20 2019-02-05 stsp continue;
2985 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2986 199a4027 2019-02-02 stsp name += 5;
2987 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2988 7143d404 2019-03-12 stsp continue;
2989 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2990 e34f9ed6 2019-02-02 stsp name += 6;
2991 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2992 141c2bff 2019-02-04 stsp name += 8;
2993 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2994 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2995 5d844a1e 2019-08-13 stsp if (err) {
2996 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2997 5d844a1e 2019-08-13 stsp return err;
2998 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2999 5d844a1e 2019-08-13 stsp err = NULL;
3000 5d844a1e 2019-08-13 stsp tag = NULL;
3001 5d844a1e 2019-08-13 stsp }
3002 a436ad14 2019-08-13 stsp }
3003 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3004 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3005 a436ad14 2019-08-13 stsp if (tag)
3006 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3007 a436ad14 2019-08-13 stsp if (cmp != 0)
3008 a436ad14 2019-08-13 stsp continue;
3009 199a4027 2019-02-02 stsp s = refs_str;
3010 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3011 199a4027 2019-02-02 stsp name) == -1) {
3012 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3013 199a4027 2019-02-02 stsp free(s);
3014 34ca4898 2019-08-13 stsp return err;
3015 199a4027 2019-02-02 stsp }
3016 199a4027 2019-02-02 stsp free(s);
3017 199a4027 2019-02-02 stsp }
3018 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3019 8bf5b3c9 2018-03-17 stsp if (err)
3020 8bf5b3c9 2018-03-17 stsp return err;
3021 788c352e 2018-06-16 stsp
3022 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3023 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3024 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3025 832c249c 2018-06-10 stsp free(id_str);
3026 d3d493d7 2019-02-21 stsp id_str = NULL;
3027 d3d493d7 2019-02-21 stsp free(refs_str);
3028 d3d493d7 2019-02-21 stsp refs_str = NULL;
3029 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3030 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3031 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3032 09867e48 2019-08-13 stsp if (datestr)
3033 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3034 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3035 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3036 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3037 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3038 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3039 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3040 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3041 3fe1abad 2018-06-10 stsp int n = 1;
3042 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3043 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3044 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3045 a0603db2 2018-06-10 stsp if (err)
3046 a0603db2 2018-06-10 stsp return err;
3047 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3048 a0603db2 2018-06-10 stsp free(id_str);
3049 a0603db2 2018-06-10 stsp }
3050 a0603db2 2018-06-10 stsp }
3051 832c249c 2018-06-10 stsp
3052 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3053 5943eee2 2019-08-13 stsp if (err)
3054 5943eee2 2019-08-13 stsp return err;
3055 8bf5b3c9 2018-03-17 stsp
3056 621015ac 2018-07-23 stsp logmsg = logmsg0;
3057 832c249c 2018-06-10 stsp do {
3058 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3059 832c249c 2018-06-10 stsp if (line)
3060 832c249c 2018-06-10 stsp printf(" %s\n", line);
3061 832c249c 2018-06-10 stsp } while (line);
3062 621015ac 2018-07-23 stsp free(logmsg0);
3063 832c249c 2018-06-10 stsp
3064 971751ac 2018-03-27 stsp if (show_patch) {
3065 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3066 971751ac 2018-03-27 stsp if (err == 0)
3067 971751ac 2018-03-27 stsp printf("\n");
3068 971751ac 2018-03-27 stsp }
3069 07862c20 2018-09-15 stsp
3070 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3071 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3072 07862c20 2018-09-15 stsp return err;
3073 f42b1b34 2018-03-12 stsp }
3074 5c860e29 2018-03-12 stsp
3075 f42b1b34 2018-03-12 stsp static const struct got_error *
3076 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
3077 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
3078 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
3079 463a997a 2019-12-08 kn struct got_reflist_head *refs)
3080 f42b1b34 2018-03-12 stsp {
3081 f42b1b34 2018-03-12 stsp const struct got_error *err;
3082 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3083 6841bf13 2019-11-29 kn regex_t regex;
3084 6841bf13 2019-11-29 kn int have_match;
3085 372ccdbb 2018-06-10 stsp
3086 6841bf13 2019-11-29 kn if (search_pattern &&
3087 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3088 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3089 6841bf13 2019-11-29 kn
3090 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3091 f42b1b34 2018-03-12 stsp if (err)
3092 f42b1b34 2018-03-12 stsp return err;
3093 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3094 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3095 372ccdbb 2018-06-10 stsp if (err)
3096 fcc85cad 2018-07-22 stsp goto done;
3097 656b1f76 2019-05-11 jcs for (;;) {
3098 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
3099 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3100 8bf5b3c9 2018-03-17 stsp
3101 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3102 84453469 2018-11-11 stsp break;
3103 84453469 2018-11-11 stsp
3104 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3105 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3106 372ccdbb 2018-06-10 stsp if (err) {
3107 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3108 9ba79e04 2018-06-11 stsp err = NULL;
3109 ee780d5c 2020-01-04 stsp break;
3110 7e665116 2018-04-02 stsp }
3111 b43fbaa0 2018-06-11 stsp if (id == NULL)
3112 7e665116 2018-04-02 stsp break;
3113 b43fbaa0 2018-06-11 stsp
3114 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3115 b43fbaa0 2018-06-11 stsp if (err)
3116 fcc85cad 2018-07-22 stsp break;
3117 6841bf13 2019-11-29 kn
3118 6841bf13 2019-11-29 kn if (search_pattern) {
3119 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3120 6841bf13 2019-11-29 kn if (err) {
3121 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3122 6841bf13 2019-11-29 kn break;
3123 6841bf13 2019-11-29 kn }
3124 6841bf13 2019-11-29 kn if (have_match == 0) {
3125 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3126 6841bf13 2019-11-29 kn continue;
3127 6841bf13 2019-11-29 kn }
3128 6841bf13 2019-11-29 kn }
3129 6841bf13 2019-11-29 kn
3130 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
3131 44392932 2019-08-25 stsp diff_context, refs);
3132 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
3133 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
3134 7e665116 2018-04-02 stsp break;
3135 31cedeaf 2018-09-15 stsp }
3136 fcc85cad 2018-07-22 stsp done:
3137 6841bf13 2019-11-29 kn if (search_pattern)
3138 6841bf13 2019-11-29 kn regfree(&regex);
3139 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3140 f42b1b34 2018-03-12 stsp return err;
3141 f42b1b34 2018-03-12 stsp }
3142 5c860e29 2018-03-12 stsp
3143 4ed7e80c 2018-05-20 stsp __dead static void
3144 6f3d1eb0 2018-03-12 stsp usage_log(void)
3145 6f3d1eb0 2018-03-12 stsp {
3146 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
3147 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
3148 6f3d1eb0 2018-03-12 stsp exit(1);
3149 b1ebc001 2019-08-13 stsp }
3150 b1ebc001 2019-08-13 stsp
3151 b1ebc001 2019-08-13 stsp static int
3152 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3153 b1ebc001 2019-08-13 stsp {
3154 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3155 b1ebc001 2019-08-13 stsp long long n;
3156 b1ebc001 2019-08-13 stsp const char *errstr;
3157 b1ebc001 2019-08-13 stsp
3158 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3159 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3160 b1ebc001 2019-08-13 stsp return 0;
3161 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3162 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3163 b1ebc001 2019-08-13 stsp return 0;
3164 b1ebc001 2019-08-13 stsp return n;
3165 6f3d1eb0 2018-03-12 stsp }
3166 6f3d1eb0 2018-03-12 stsp
3167 4ed7e80c 2018-05-20 stsp static const struct got_error *
3168 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3169 f42b1b34 2018-03-12 stsp {
3170 f42b1b34 2018-03-12 stsp const struct got_error *error;
3171 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3172 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3173 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
3174 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
3175 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3176 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
3177 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3178 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
3179 64a96a6d 2018-04-01 stsp const char *errstr;
3180 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3181 1b3893a2 2019-03-18 stsp
3182 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3183 5c860e29 2018-03-12 stsp
3184 6715a751 2018-03-16 stsp #ifndef PROFILE
3185 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3186 6098196c 2019-01-04 stsp NULL)
3187 ad242220 2018-09-08 stsp == -1)
3188 f42b1b34 2018-03-12 stsp err(1, "pledge");
3189 6715a751 2018-03-16 stsp #endif
3190 79109fed 2018-03-27 stsp
3191 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3192 b1ebc001 2019-08-13 stsp
3193 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
3194 79109fed 2018-03-27 stsp switch (ch) {
3195 79109fed 2018-03-27 stsp case 'p':
3196 79109fed 2018-03-27 stsp show_patch = 1;
3197 d142fc45 2018-04-01 stsp break;
3198 d142fc45 2018-04-01 stsp case 'c':
3199 d142fc45 2018-04-01 stsp start_commit = optarg;
3200 64a96a6d 2018-04-01 stsp break;
3201 c0cc5c62 2018-10-18 stsp case 'C':
3202 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3203 4a8520aa 2018-10-18 stsp &errstr);
3204 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3205 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3206 c0cc5c62 2018-10-18 stsp break;
3207 64a96a6d 2018-04-01 stsp case 'l':
3208 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3209 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3210 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3211 cc54c501 2019-07-15 stsp break;
3212 48c8c60d 2020-01-27 stsp case 'b':
3213 48c8c60d 2020-01-27 stsp log_branches = 1;
3214 a0603db2 2018-06-10 stsp break;
3215 04ca23f4 2018-07-16 stsp case 'r':
3216 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3217 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3218 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3219 9ba1d308 2019-10-21 stsp optarg);
3220 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3221 04ca23f4 2018-07-16 stsp break;
3222 6841bf13 2019-11-29 kn case 's':
3223 6841bf13 2019-11-29 kn search_pattern = optarg;
3224 6841bf13 2019-11-29 kn break;
3225 79109fed 2018-03-27 stsp default:
3226 2deda0b9 2019-03-07 stsp usage_log();
3227 79109fed 2018-03-27 stsp /* NOTREACHED */
3228 79109fed 2018-03-27 stsp }
3229 79109fed 2018-03-27 stsp }
3230 79109fed 2018-03-27 stsp
3231 79109fed 2018-03-27 stsp argc -= optind;
3232 79109fed 2018-03-27 stsp argv += optind;
3233 f42b1b34 2018-03-12 stsp
3234 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3235 dc1edbfa 2019-11-29 kn diff_context = 3;
3236 dc1edbfa 2019-11-29 kn else if (!show_patch)
3237 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3238 dc1edbfa 2019-11-29 kn
3239 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3240 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3241 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3242 04ca23f4 2018-07-16 stsp goto done;
3243 04ca23f4 2018-07-16 stsp }
3244 cffc0aa4 2019-02-05 stsp
3245 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3246 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3247 cffc0aa4 2019-02-05 stsp goto done;
3248 cffc0aa4 2019-02-05 stsp error = NULL;
3249 cffc0aa4 2019-02-05 stsp
3250 e7301579 2019-03-18 stsp if (argc == 0) {
3251 cbd1af7a 2019-03-18 stsp path = strdup("");
3252 e7301579 2019-03-18 stsp if (path == NULL) {
3253 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3254 cbd1af7a 2019-03-18 stsp goto done;
3255 e7301579 2019-03-18 stsp }
3256 e7301579 2019-03-18 stsp } else if (argc == 1) {
3257 e7301579 2019-03-18 stsp if (worktree) {
3258 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3259 e7301579 2019-03-18 stsp argv[0]);
3260 e7301579 2019-03-18 stsp if (error)
3261 e7301579 2019-03-18 stsp goto done;
3262 e7301579 2019-03-18 stsp } else {
3263 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3264 e7301579 2019-03-18 stsp if (path == NULL) {
3265 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3266 e7301579 2019-03-18 stsp goto done;
3267 e7301579 2019-03-18 stsp }
3268 e7301579 2019-03-18 stsp }
3269 cbd1af7a 2019-03-18 stsp } else
3270 cbd1af7a 2019-03-18 stsp usage_log();
3271 cbd1af7a 2019-03-18 stsp
3272 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3273 5486daa2 2019-05-11 stsp repo_path = worktree ?
3274 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3275 5486daa2 2019-05-11 stsp }
3276 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3277 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3278 cffc0aa4 2019-02-05 stsp goto done;
3279 04ca23f4 2018-07-16 stsp }
3280 6098196c 2019-01-04 stsp
3281 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3282 d7d4f210 2018-03-12 stsp if (error != NULL)
3283 04ca23f4 2018-07-16 stsp goto done;
3284 f42b1b34 2018-03-12 stsp
3285 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3286 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3287 c02c541e 2019-03-29 stsp if (error)
3288 c02c541e 2019-03-29 stsp goto done;
3289 c02c541e 2019-03-29 stsp
3290 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3291 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3292 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3293 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3294 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3295 3235492e 2018-04-01 stsp if (error != NULL)
3296 3235492e 2018-04-01 stsp return error;
3297 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
3298 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3299 3235492e 2018-04-01 stsp if (error != NULL)
3300 3235492e 2018-04-01 stsp return error;
3301 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3302 3235492e 2018-04-01 stsp } else {
3303 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
3304 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3305 3235492e 2018-04-01 stsp if (error == NULL) {
3306 1caad61b 2019-02-01 stsp int obj_type;
3307 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
3308 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
3309 d1f2edc9 2018-06-13 stsp if (error != NULL)
3310 1caad61b 2019-02-01 stsp goto done;
3311 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
3312 1caad61b 2019-02-01 stsp if (error != NULL)
3313 1caad61b 2019-02-01 stsp goto done;
3314 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3315 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
3316 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
3317 1caad61b 2019-02-01 stsp if (error != NULL)
3318 1caad61b 2019-02-01 stsp goto done;
3319 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
3320 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
3321 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3322 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3323 1caad61b 2019-02-01 stsp goto done;
3324 1caad61b 2019-02-01 stsp }
3325 1caad61b 2019-02-01 stsp free(id);
3326 1caad61b 2019-02-01 stsp id = got_object_id_dup(
3327 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
3328 1caad61b 2019-02-01 stsp if (id == NULL)
3329 638f9024 2019-05-13 stsp error = got_error_from_errno(
3330 230a42bd 2019-05-11 jcs "got_object_id_dup");
3331 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3332 1caad61b 2019-02-01 stsp if (error)
3333 1caad61b 2019-02-01 stsp goto done;
3334 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3335 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3336 1caad61b 2019-02-01 stsp goto done;
3337 1caad61b 2019-02-01 stsp }
3338 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3339 d1f2edc9 2018-06-13 stsp if (error != NULL)
3340 1caad61b 2019-02-01 stsp goto done;
3341 d1f2edc9 2018-06-13 stsp }
3342 15a94983 2018-12-23 stsp if (commit == NULL) {
3343 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
3344 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3345 d1f2edc9 2018-06-13 stsp if (error != NULL)
3346 d1f2edc9 2018-06-13 stsp return error;
3347 3235492e 2018-04-01 stsp }
3348 3235492e 2018-04-01 stsp }
3349 d7d4f210 2018-03-12 stsp if (error != NULL)
3350 04ca23f4 2018-07-16 stsp goto done;
3351 04ca23f4 2018-07-16 stsp
3352 deeabeae 2019-08-27 stsp if (worktree) {
3353 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3354 deeabeae 2019-08-27 stsp char *p;
3355 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3356 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3357 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3358 deeabeae 2019-08-27 stsp goto done;
3359 deeabeae 2019-08-27 stsp }
3360 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3361 deeabeae 2019-08-27 stsp free(p);
3362 deeabeae 2019-08-27 stsp } else
3363 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3364 04ca23f4 2018-07-16 stsp if (error != NULL)
3365 04ca23f4 2018-07-16 stsp goto done;
3366 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3367 04ca23f4 2018-07-16 stsp free(path);
3368 04ca23f4 2018-07-16 stsp path = in_repo_path;
3369 04ca23f4 2018-07-16 stsp }
3370 199a4027 2019-02-02 stsp
3371 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3372 199a4027 2019-02-02 stsp if (error)
3373 199a4027 2019-02-02 stsp goto done;
3374 04ca23f4 2018-07-16 stsp
3375 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3376 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3377 04ca23f4 2018-07-16 stsp done:
3378 04ca23f4 2018-07-16 stsp free(path);
3379 04ca23f4 2018-07-16 stsp free(repo_path);
3380 04ca23f4 2018-07-16 stsp free(cwd);
3381 f42b1b34 2018-03-12 stsp free(id);
3382 cffc0aa4 2019-02-05 stsp if (worktree)
3383 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3384 ad242220 2018-09-08 stsp if (repo) {
3385 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3386 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3387 ad242220 2018-09-08 stsp if (error == NULL)
3388 ad242220 2018-09-08 stsp error = repo_error;
3389 ad242220 2018-09-08 stsp }
3390 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3391 8bf5b3c9 2018-03-17 stsp return error;
3392 5c860e29 2018-03-12 stsp }
3393 5c860e29 2018-03-12 stsp
3394 4ed7e80c 2018-05-20 stsp __dead static void
3395 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3396 3f8b7d6a 2018-04-01 stsp {
3397 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3398 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3399 3f8b7d6a 2018-04-01 stsp exit(1);
3400 b00d56cd 2018-04-01 stsp }
3401 b00d56cd 2018-04-01 stsp
3402 b72f483a 2019-02-05 stsp struct print_diff_arg {
3403 b72f483a 2019-02-05 stsp struct got_repository *repo;
3404 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3405 b72f483a 2019-02-05 stsp int diff_context;
3406 3fc0c068 2019-02-10 stsp const char *id_str;
3407 3fc0c068 2019-02-10 stsp int header_shown;
3408 98eaaa12 2019-08-03 stsp int diff_staged;
3409 63035f9f 2019-10-06 stsp int ignore_whitespace;
3410 b72f483a 2019-02-05 stsp };
3411 b72f483a 2019-02-05 stsp
3412 4ed7e80c 2018-05-20 stsp static const struct got_error *
3413 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3414 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3415 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3416 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3417 b72f483a 2019-02-05 stsp {
3418 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3419 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3420 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3421 12463d8b 2019-12-13 stsp int fd = -1;
3422 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3423 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3424 b72f483a 2019-02-05 stsp struct stat sb;
3425 b72f483a 2019-02-05 stsp
3426 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3427 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3428 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3429 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3430 98eaaa12 2019-08-03 stsp return NULL;
3431 98eaaa12 2019-08-03 stsp } else {
3432 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3433 98eaaa12 2019-08-03 stsp return NULL;
3434 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3435 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3436 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3437 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3438 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3439 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3440 98eaaa12 2019-08-03 stsp return NULL;
3441 98eaaa12 2019-08-03 stsp }
3442 3fc0c068 2019-02-10 stsp
3443 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3444 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3445 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3446 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3447 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3448 3fc0c068 2019-02-10 stsp }
3449 b72f483a 2019-02-05 stsp
3450 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3451 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3452 98eaaa12 2019-08-03 stsp switch (staged_status) {
3453 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3454 98eaaa12 2019-08-03 stsp label1 = path;
3455 98eaaa12 2019-08-03 stsp label2 = path;
3456 98eaaa12 2019-08-03 stsp break;
3457 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3458 98eaaa12 2019-08-03 stsp label2 = path;
3459 98eaaa12 2019-08-03 stsp break;
3460 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3461 98eaaa12 2019-08-03 stsp label1 = path;
3462 98eaaa12 2019-08-03 stsp break;
3463 98eaaa12 2019-08-03 stsp default:
3464 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3465 98eaaa12 2019-08-03 stsp }
3466 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3467 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3468 63035f9f 2019-10-06 stsp a->repo, stdout);
3469 98eaaa12 2019-08-03 stsp }
3470 98eaaa12 2019-08-03 stsp
3471 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3472 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3473 4ce46740 2019-08-08 stsp char *id_str;
3474 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3475 408b4ebc 2019-08-03 stsp 8192);
3476 4ce46740 2019-08-08 stsp if (err)
3477 4ce46740 2019-08-08 stsp goto done;
3478 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3479 4ce46740 2019-08-08 stsp if (err)
3480 4ce46740 2019-08-08 stsp goto done;
3481 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3482 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3483 4ce46740 2019-08-08 stsp free(id_str);
3484 4ce46740 2019-08-08 stsp goto done;
3485 4ce46740 2019-08-08 stsp }
3486 4ce46740 2019-08-08 stsp free(id_str);
3487 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3488 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3489 4ce46740 2019-08-08 stsp if (err)
3490 4ce46740 2019-08-08 stsp goto done;
3491 4ce46740 2019-08-08 stsp }
3492 d00136be 2019-03-26 stsp
3493 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3494 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3495 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3496 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3497 2ec1f75b 2019-03-26 stsp goto done;
3498 2ec1f75b 2019-03-26 stsp }
3499 b72f483a 2019-02-05 stsp
3500 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3501 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3502 12463d8b 2019-12-13 stsp if (fd == -1) {
3503 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3504 12463d8b 2019-12-13 stsp goto done;
3505 12463d8b 2019-12-13 stsp }
3506 12463d8b 2019-12-13 stsp } else {
3507 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3508 12463d8b 2019-12-13 stsp if (fd == -1) {
3509 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3510 12463d8b 2019-12-13 stsp goto done;
3511 12463d8b 2019-12-13 stsp }
3512 12463d8b 2019-12-13 stsp }
3513 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3514 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3515 2ec1f75b 2019-03-26 stsp goto done;
3516 2ec1f75b 2019-03-26 stsp }
3517 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3518 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3519 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3520 2ec1f75b 2019-03-26 stsp goto done;
3521 2ec1f75b 2019-03-26 stsp }
3522 12463d8b 2019-12-13 stsp fd = -1;
3523 2ec1f75b 2019-03-26 stsp } else
3524 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3525 b72f483a 2019-02-05 stsp
3526 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3527 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3528 b72f483a 2019-02-05 stsp done:
3529 b72f483a 2019-02-05 stsp if (blob1)
3530 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3531 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3532 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3533 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3534 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3535 b72f483a 2019-02-05 stsp free(abspath);
3536 927df6b7 2019-02-10 stsp return err;
3537 927df6b7 2019-02-10 stsp }
3538 927df6b7 2019-02-10 stsp
3539 927df6b7 2019-02-10 stsp static const struct got_error *
3540 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3541 b00d56cd 2018-04-01 stsp {
3542 b00d56cd 2018-04-01 stsp const struct got_error *error;
3543 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3544 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3545 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3546 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3547 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3548 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3549 15a94983 2018-12-23 stsp int type1, type2;
3550 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3551 c0cc5c62 2018-10-18 stsp const char *errstr;
3552 927df6b7 2019-02-10 stsp char *path = NULL;
3553 b00d56cd 2018-04-01 stsp
3554 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3555 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3556 25eccc22 2019-01-04 stsp NULL) == -1)
3557 b00d56cd 2018-04-01 stsp err(1, "pledge");
3558 b00d56cd 2018-04-01 stsp #endif
3559 b00d56cd 2018-04-01 stsp
3560 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3561 b00d56cd 2018-04-01 stsp switch (ch) {
3562 c0cc5c62 2018-10-18 stsp case 'C':
3563 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3564 dfcab68b 2019-11-29 kn &errstr);
3565 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3566 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3567 c0cc5c62 2018-10-18 stsp break;
3568 b72f483a 2019-02-05 stsp case 'r':
3569 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3570 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3571 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3572 9ba1d308 2019-10-21 stsp optarg);
3573 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3574 b72f483a 2019-02-05 stsp break;
3575 98eaaa12 2019-08-03 stsp case 's':
3576 98eaaa12 2019-08-03 stsp diff_staged = 1;
3577 63035f9f 2019-10-06 stsp break;
3578 63035f9f 2019-10-06 stsp case 'w':
3579 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3580 98eaaa12 2019-08-03 stsp break;
3581 b00d56cd 2018-04-01 stsp default:
3582 2deda0b9 2019-03-07 stsp usage_diff();
3583 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3584 b00d56cd 2018-04-01 stsp }
3585 b00d56cd 2018-04-01 stsp }
3586 b00d56cd 2018-04-01 stsp
3587 b00d56cd 2018-04-01 stsp argc -= optind;
3588 b00d56cd 2018-04-01 stsp argv += optind;
3589 b00d56cd 2018-04-01 stsp
3590 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3591 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3592 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3593 b72f483a 2019-02-05 stsp goto done;
3594 b72f483a 2019-02-05 stsp }
3595 927df6b7 2019-02-10 stsp if (argc <= 1) {
3596 b72f483a 2019-02-05 stsp if (repo_path)
3597 b72f483a 2019-02-05 stsp errx(1,
3598 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3599 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3600 1f03b8da 2020-03-20 stsp if (error)
3601 1f03b8da 2020-03-20 stsp goto done;
3602 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3603 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3604 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3605 927df6b7 2019-02-10 stsp goto done;
3606 927df6b7 2019-02-10 stsp }
3607 927df6b7 2019-02-10 stsp if (argc == 1) {
3608 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3609 cbd1af7a 2019-03-18 stsp argv[0]);
3610 927df6b7 2019-02-10 stsp if (error)
3611 927df6b7 2019-02-10 stsp goto done;
3612 927df6b7 2019-02-10 stsp } else {
3613 927df6b7 2019-02-10 stsp path = strdup("");
3614 927df6b7 2019-02-10 stsp if (path == NULL) {
3615 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3616 927df6b7 2019-02-10 stsp goto done;
3617 927df6b7 2019-02-10 stsp }
3618 927df6b7 2019-02-10 stsp }
3619 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3620 98eaaa12 2019-08-03 stsp if (diff_staged)
3621 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3622 98eaaa12 2019-08-03 stsp "objects in repository");
3623 15a94983 2018-12-23 stsp id_str1 = argv[0];
3624 15a94983 2018-12-23 stsp id_str2 = argv[1];
3625 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3626 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3627 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3628 30db809c 2019-06-05 stsp goto done;
3629 1f03b8da 2020-03-20 stsp if (worktree) {
3630 1f03b8da 2020-03-20 stsp repo_path = strdup(
3631 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3632 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3633 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3634 1f03b8da 2020-03-20 stsp goto done;
3635 1f03b8da 2020-03-20 stsp }
3636 1f03b8da 2020-03-20 stsp } else {
3637 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3638 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3639 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3640 1f03b8da 2020-03-20 stsp goto done;
3641 1f03b8da 2020-03-20 stsp }
3642 30db809c 2019-06-05 stsp }
3643 30db809c 2019-06-05 stsp }
3644 b00d56cd 2018-04-01 stsp } else
3645 b00d56cd 2018-04-01 stsp usage_diff();
3646 b00d56cd 2018-04-01 stsp
3647 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3648 76089277 2018-04-01 stsp free(repo_path);
3649 b00d56cd 2018-04-01 stsp if (error != NULL)
3650 b00d56cd 2018-04-01 stsp goto done;
3651 b00d56cd 2018-04-01 stsp
3652 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3653 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3654 c02c541e 2019-03-29 stsp if (error)
3655 c02c541e 2019-03-29 stsp goto done;
3656 c02c541e 2019-03-29 stsp
3657 30db809c 2019-06-05 stsp if (argc <= 1) {
3658 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3659 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3660 b72f483a 2019-02-05 stsp char *id_str;
3661 72ea6654 2019-07-27 stsp
3662 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3663 72ea6654 2019-07-27 stsp
3664 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3665 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3666 b72f483a 2019-02-05 stsp if (error)
3667 b72f483a 2019-02-05 stsp goto done;
3668 b72f483a 2019-02-05 stsp arg.repo = repo;
3669 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3670 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3671 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3672 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3673 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3674 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3675 b72f483a 2019-02-05 stsp
3676 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3677 72ea6654 2019-07-27 stsp if (error)
3678 72ea6654 2019-07-27 stsp goto done;
3679 72ea6654 2019-07-27 stsp
3680 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3681 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3682 b72f483a 2019-02-05 stsp free(id_str);
3683 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3684 b72f483a 2019-02-05 stsp goto done;
3685 b72f483a 2019-02-05 stsp }
3686 b72f483a 2019-02-05 stsp
3687 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3688 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3689 0adb7196 2019-08-11 stsp if (error)
3690 0adb7196 2019-08-11 stsp goto done;
3691 b00d56cd 2018-04-01 stsp
3692 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3693 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3694 0adb7196 2019-08-11 stsp if (error)
3695 0adb7196 2019-08-11 stsp goto done;
3696 b00d56cd 2018-04-01 stsp
3697 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3698 15a94983 2018-12-23 stsp if (error)
3699 15a94983 2018-12-23 stsp goto done;
3700 15a94983 2018-12-23 stsp
3701 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3702 15a94983 2018-12-23 stsp if (error)
3703 15a94983 2018-12-23 stsp goto done;
3704 15a94983 2018-12-23 stsp
3705 15a94983 2018-12-23 stsp if (type1 != type2) {
3706 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3707 b00d56cd 2018-04-01 stsp goto done;
3708 b00d56cd 2018-04-01 stsp }
3709 b00d56cd 2018-04-01 stsp
3710 15a94983 2018-12-23 stsp switch (type1) {
3711 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3712 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3713 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3714 b00d56cd 2018-04-01 stsp break;
3715 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3716 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3717 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3718 b00d56cd 2018-04-01 stsp break;
3719 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3720 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3721 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3722 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3723 b00d56cd 2018-04-01 stsp break;
3724 b00d56cd 2018-04-01 stsp default:
3725 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3726 b00d56cd 2018-04-01 stsp }
3727 b00d56cd 2018-04-01 stsp done:
3728 5e70831e 2019-05-28 stsp free(label1);
3729 5e70831e 2019-05-28 stsp free(label2);
3730 15a94983 2018-12-23 stsp free(id1);
3731 15a94983 2018-12-23 stsp free(id2);
3732 927df6b7 2019-02-10 stsp free(path);
3733 b72f483a 2019-02-05 stsp if (worktree)
3734 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3735 ad242220 2018-09-08 stsp if (repo) {
3736 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3737 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3738 ad242220 2018-09-08 stsp if (error == NULL)
3739 ad242220 2018-09-08 stsp error = repo_error;
3740 ad242220 2018-09-08 stsp }
3741 b00d56cd 2018-04-01 stsp return error;
3742 404c43c4 2018-06-21 stsp }
3743 404c43c4 2018-06-21 stsp
3744 404c43c4 2018-06-21 stsp __dead static void
3745 404c43c4 2018-06-21 stsp usage_blame(void)
3746 404c43c4 2018-06-21 stsp {
3747 9270e621 2019-02-05 stsp fprintf(stderr,
3748 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3749 404c43c4 2018-06-21 stsp getprogname());
3750 404c43c4 2018-06-21 stsp exit(1);
3751 b00d56cd 2018-04-01 stsp }
3752 b00d56cd 2018-04-01 stsp
3753 28315671 2019-08-14 stsp struct blame_line {
3754 28315671 2019-08-14 stsp int annotated;
3755 28315671 2019-08-14 stsp char *id_str;
3756 82f6abb8 2019-08-14 stsp char *committer;
3757 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3758 28315671 2019-08-14 stsp };
3759 28315671 2019-08-14 stsp
3760 28315671 2019-08-14 stsp struct blame_cb_args {
3761 28315671 2019-08-14 stsp struct blame_line *lines;
3762 28315671 2019-08-14 stsp int nlines;
3763 7ef28ff8 2019-08-14 stsp int nlines_prec;
3764 28315671 2019-08-14 stsp int lineno_cur;
3765 28315671 2019-08-14 stsp off_t *line_offsets;
3766 28315671 2019-08-14 stsp FILE *f;
3767 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3768 28315671 2019-08-14 stsp };
3769 28315671 2019-08-14 stsp
3770 404c43c4 2018-06-21 stsp static const struct got_error *
3771 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3772 28315671 2019-08-14 stsp {
3773 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3774 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3775 28315671 2019-08-14 stsp struct blame_line *bline;
3776 82f6abb8 2019-08-14 stsp char *line = NULL;
3777 28315671 2019-08-14 stsp size_t linesize = 0;
3778 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3779 28315671 2019-08-14 stsp off_t offset;
3780 bcb49d15 2019-08-14 stsp struct tm tm;
3781 bcb49d15 2019-08-14 stsp time_t committer_time;
3782 28315671 2019-08-14 stsp
3783 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3784 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3785 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3786 28315671 2019-08-14 stsp
3787 28315671 2019-08-14 stsp if (sigint_received)
3788 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3789 28315671 2019-08-14 stsp
3790 2839f8b9 2019-08-18 stsp if (lineno == -1)
3791 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3792 2839f8b9 2019-08-18 stsp
3793 28315671 2019-08-14 stsp /* Annotate this line. */
3794 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3795 28315671 2019-08-14 stsp if (bline->annotated)
3796 28315671 2019-08-14 stsp return NULL;
3797 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3798 28315671 2019-08-14 stsp if (err)
3799 28315671 2019-08-14 stsp return err;
3800 82f6abb8 2019-08-14 stsp
3801 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3802 82f6abb8 2019-08-14 stsp if (err)
3803 82f6abb8 2019-08-14 stsp goto done;
3804 82f6abb8 2019-08-14 stsp
3805 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3806 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3807 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3808 bcb49d15 2019-08-14 stsp goto done;
3809 bcb49d15 2019-08-14 stsp }
3810 bcb49d15 2019-08-14 stsp
3811 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3812 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3813 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3814 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3815 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3816 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3817 82f6abb8 2019-08-14 stsp goto done;
3818 82f6abb8 2019-08-14 stsp }
3819 28315671 2019-08-14 stsp bline->annotated = 1;
3820 28315671 2019-08-14 stsp
3821 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3822 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3823 28315671 2019-08-14 stsp if (!bline->annotated)
3824 82f6abb8 2019-08-14 stsp goto done;
3825 28315671 2019-08-14 stsp
3826 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3827 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3828 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3829 82f6abb8 2019-08-14 stsp goto done;
3830 82f6abb8 2019-08-14 stsp }
3831 28315671 2019-08-14 stsp
3832 28315671 2019-08-14 stsp while (bline->annotated) {
3833 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3834 82f6abb8 2019-08-14 stsp size_t len;
3835 82f6abb8 2019-08-14 stsp
3836 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3837 28315671 2019-08-14 stsp if (ferror(a->f))
3838 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3839 28315671 2019-08-14 stsp break;
3840 28315671 2019-08-14 stsp }
3841 82f6abb8 2019-08-14 stsp
3842 82f6abb8 2019-08-14 stsp committer = bline->committer;
3843 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3844 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3845 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3846 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3847 82f6abb8 2019-08-14 stsp if (at)
3848 82f6abb8 2019-08-14 stsp *at = '\0';
3849 82f6abb8 2019-08-14 stsp len = strlen(committer);
3850 82f6abb8 2019-08-14 stsp if (len >= 9)
3851 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3852 28315671 2019-08-14 stsp
3853 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3854 28315671 2019-08-14 stsp if (nl)
3855 28315671 2019-08-14 stsp *nl = '\0';
3856 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3857 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3858 28315671 2019-08-14 stsp
3859 28315671 2019-08-14 stsp a->lineno_cur++;
3860 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3861 28315671 2019-08-14 stsp }
3862 82f6abb8 2019-08-14 stsp done:
3863 82f6abb8 2019-08-14 stsp if (commit)
3864 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3865 28315671 2019-08-14 stsp free(line);
3866 28315671 2019-08-14 stsp return err;
3867 28315671 2019-08-14 stsp }
3868 28315671 2019-08-14 stsp
3869 28315671 2019-08-14 stsp static const struct got_error *
3870 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3871 404c43c4 2018-06-21 stsp {
3872 404c43c4 2018-06-21 stsp const struct got_error *error;
3873 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3874 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3875 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3876 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3877 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3878 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3879 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3880 28315671 2019-08-14 stsp struct blame_cb_args bca;
3881 28315671 2019-08-14 stsp int ch, obj_type, i;
3882 b02560ec 2019-08-19 stsp size_t filesize;
3883 90f3c347 2019-08-19 stsp
3884 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3885 404c43c4 2018-06-21 stsp
3886 404c43c4 2018-06-21 stsp #ifndef PROFILE
3887 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3888 36e2fb66 2019-01-04 stsp NULL) == -1)
3889 404c43c4 2018-06-21 stsp err(1, "pledge");
3890 404c43c4 2018-06-21 stsp #endif
3891 404c43c4 2018-06-21 stsp
3892 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3893 404c43c4 2018-06-21 stsp switch (ch) {
3894 404c43c4 2018-06-21 stsp case 'c':
3895 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3896 404c43c4 2018-06-21 stsp break;
3897 66bea077 2018-08-02 stsp case 'r':
3898 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3899 66bea077 2018-08-02 stsp if (repo_path == NULL)
3900 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3901 9ba1d308 2019-10-21 stsp optarg);
3902 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3903 66bea077 2018-08-02 stsp break;
3904 404c43c4 2018-06-21 stsp default:
3905 2deda0b9 2019-03-07 stsp usage_blame();
3906 404c43c4 2018-06-21 stsp /* NOTREACHED */
3907 404c43c4 2018-06-21 stsp }
3908 404c43c4 2018-06-21 stsp }
3909 404c43c4 2018-06-21 stsp
3910 404c43c4 2018-06-21 stsp argc -= optind;
3911 404c43c4 2018-06-21 stsp argv += optind;
3912 404c43c4 2018-06-21 stsp
3913 a39318fd 2018-08-02 stsp if (argc == 1)
3914 404c43c4 2018-06-21 stsp path = argv[0];
3915 a39318fd 2018-08-02 stsp else
3916 404c43c4 2018-06-21 stsp usage_blame();
3917 404c43c4 2018-06-21 stsp
3918 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3919 66bea077 2018-08-02 stsp if (cwd == NULL) {
3920 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3921 66bea077 2018-08-02 stsp goto done;
3922 66bea077 2018-08-02 stsp }
3923 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3924 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3925 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3926 66bea077 2018-08-02 stsp goto done;
3927 0c06baac 2019-02-05 stsp else
3928 0c06baac 2019-02-05 stsp error = NULL;
3929 0c06baac 2019-02-05 stsp if (worktree) {
3930 0c06baac 2019-02-05 stsp repo_path =
3931 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3932 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3933 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3934 1f03b8da 2020-03-20 stsp if (error)
3935 1f03b8da 2020-03-20 stsp goto done;
3936 1f03b8da 2020-03-20 stsp }
3937 0c06baac 2019-02-05 stsp } else {
3938 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3939 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3940 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3941 0c06baac 2019-02-05 stsp goto done;
3942 0c06baac 2019-02-05 stsp }
3943 66bea077 2018-08-02 stsp }
3944 66bea077 2018-08-02 stsp }
3945 36e2fb66 2019-01-04 stsp
3946 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3947 c02c541e 2019-03-29 stsp if (error != NULL)
3948 36e2fb66 2019-01-04 stsp goto done;
3949 66bea077 2018-08-02 stsp
3950 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3951 c02c541e 2019-03-29 stsp if (error)
3952 404c43c4 2018-06-21 stsp goto done;
3953 404c43c4 2018-06-21 stsp
3954 0c06baac 2019-02-05 stsp if (worktree) {
3955 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3956 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3957 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3958 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3959 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3960 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3961 6efaaa2d 2019-02-05 stsp path) == -1) {
3962 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3963 0c06baac 2019-02-05 stsp goto done;
3964 0c06baac 2019-02-05 stsp }
3965 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3966 0c06baac 2019-02-05 stsp free(p);
3967 0c06baac 2019-02-05 stsp } else {
3968 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3969 0c06baac 2019-02-05 stsp }
3970 0c06baac 2019-02-05 stsp if (error)
3971 66bea077 2018-08-02 stsp goto done;
3972 66bea077 2018-08-02 stsp
3973 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3974 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3975 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3976 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3977 404c43c4 2018-06-21 stsp if (error != NULL)
3978 66bea077 2018-08-02 stsp goto done;
3979 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3980 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3981 404c43c4 2018-06-21 stsp if (error != NULL)
3982 66bea077 2018-08-02 stsp goto done;
3983 404c43c4 2018-06-21 stsp } else {
3984 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3985 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3986 30837e32 2019-07-25 stsp if (error)
3987 66bea077 2018-08-02 stsp goto done;
3988 404c43c4 2018-06-21 stsp }
3989 404c43c4 2018-06-21 stsp
3990 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3991 28315671 2019-08-14 stsp if (error)
3992 28315671 2019-08-14 stsp goto done;
3993 28315671 2019-08-14 stsp
3994 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3995 28315671 2019-08-14 stsp if (error)
3996 28315671 2019-08-14 stsp goto done;
3997 28315671 2019-08-14 stsp
3998 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3999 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4000 28315671 2019-08-14 stsp goto done;
4001 28315671 2019-08-14 stsp }
4002 28315671 2019-08-14 stsp
4003 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4004 28315671 2019-08-14 stsp if (error)
4005 28315671 2019-08-14 stsp goto done;
4006 28315671 2019-08-14 stsp bca.f = got_opentemp();
4007 28315671 2019-08-14 stsp if (bca.f == NULL) {
4008 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4009 28315671 2019-08-14 stsp goto done;
4010 28315671 2019-08-14 stsp }
4011 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4012 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4013 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4014 28315671 2019-08-14 stsp goto done;
4015 28315671 2019-08-14 stsp
4016 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4017 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4018 b02560ec 2019-08-19 stsp bca.nlines--;
4019 b02560ec 2019-08-19 stsp
4020 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4021 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4022 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4023 28315671 2019-08-14 stsp goto done;
4024 28315671 2019-08-14 stsp }
4025 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4026 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4027 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4028 7ef28ff8 2019-08-14 stsp while (i > 0) {
4029 7ef28ff8 2019-08-14 stsp i /= 10;
4030 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4031 7ef28ff8 2019-08-14 stsp }
4032 82f6abb8 2019-08-14 stsp bca.repo = repo;
4033 7ef28ff8 2019-08-14 stsp
4034 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
4035 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4036 404c43c4 2018-06-21 stsp done:
4037 66bea077 2018-08-02 stsp free(in_repo_path);
4038 66bea077 2018-08-02 stsp free(repo_path);
4039 66bea077 2018-08-02 stsp free(cwd);
4040 404c43c4 2018-06-21 stsp free(commit_id);
4041 28315671 2019-08-14 stsp free(obj_id);
4042 28315671 2019-08-14 stsp if (blob)
4043 28315671 2019-08-14 stsp got_object_blob_close(blob);
4044 0c06baac 2019-02-05 stsp if (worktree)
4045 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4046 ad242220 2018-09-08 stsp if (repo) {
4047 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4048 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4049 ad242220 2018-09-08 stsp if (error == NULL)
4050 ad242220 2018-09-08 stsp error = repo_error;
4051 ad242220 2018-09-08 stsp }
4052 3affba96 2019-09-22 stsp if (bca.lines) {
4053 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4054 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4055 3affba96 2019-09-22 stsp free(bline->id_str);
4056 3affba96 2019-09-22 stsp free(bline->committer);
4057 3affba96 2019-09-22 stsp }
4058 3affba96 2019-09-22 stsp free(bca.lines);
4059 28315671 2019-08-14 stsp }
4060 28315671 2019-08-14 stsp free(bca.line_offsets);
4061 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4062 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4063 404c43c4 2018-06-21 stsp return error;
4064 5de5890b 2018-10-18 stsp }
4065 5de5890b 2018-10-18 stsp
4066 5de5890b 2018-10-18 stsp __dead static void
4067 5de5890b 2018-10-18 stsp usage_tree(void)
4068 5de5890b 2018-10-18 stsp {
4069 c1669e2e 2019-01-09 stsp fprintf(stderr,
4070 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
4071 5de5890b 2018-10-18 stsp getprogname());
4072 5de5890b 2018-10-18 stsp exit(1);
4073 5de5890b 2018-10-18 stsp }
4074 5de5890b 2018-10-18 stsp
4075 c1669e2e 2019-01-09 stsp static void
4076 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4077 c1669e2e 2019-01-09 stsp const char *root_path)
4078 c1669e2e 2019-01-09 stsp {
4079 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4080 848d6979 2019-08-12 stsp const char *modestr = "";
4081 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4082 5de5890b 2018-10-18 stsp
4083 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4084 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4085 c1669e2e 2019-01-09 stsp path++;
4086 c1669e2e 2019-01-09 stsp
4087 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4088 63c5ca5d 2019-08-24 stsp modestr = "$";
4089 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4090 848d6979 2019-08-12 stsp modestr = "@";
4091 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4092 848d6979 2019-08-12 stsp modestr = "/";
4093 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4094 848d6979 2019-08-12 stsp modestr = "*";
4095 848d6979 2019-08-12 stsp
4096 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
4097 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
4098 c1669e2e 2019-01-09 stsp }
4099 c1669e2e 2019-01-09 stsp
4100 5de5890b 2018-10-18 stsp static const struct got_error *
4101 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4102 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4103 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4104 5de5890b 2018-10-18 stsp {
4105 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4106 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4107 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4108 56e0773d 2019-11-28 stsp int nentries, i;
4109 5de5890b 2018-10-18 stsp
4110 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4111 5de5890b 2018-10-18 stsp if (err)
4112 5de5890b 2018-10-18 stsp goto done;
4113 5de5890b 2018-10-18 stsp
4114 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4115 5de5890b 2018-10-18 stsp if (err)
4116 5de5890b 2018-10-18 stsp goto done;
4117 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4118 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4119 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4120 5de5890b 2018-10-18 stsp char *id = NULL;
4121 84453469 2018-11-11 stsp
4122 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4123 84453469 2018-11-11 stsp break;
4124 84453469 2018-11-11 stsp
4125 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4126 5de5890b 2018-10-18 stsp if (show_ids) {
4127 5de5890b 2018-10-18 stsp char *id_str;
4128 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4129 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4130 5de5890b 2018-10-18 stsp if (err)
4131 5de5890b 2018-10-18 stsp goto done;
4132 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4133 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4134 5de5890b 2018-10-18 stsp free(id_str);
4135 5de5890b 2018-10-18 stsp goto done;
4136 5de5890b 2018-10-18 stsp }
4137 5de5890b 2018-10-18 stsp free(id_str);
4138 5de5890b 2018-10-18 stsp }
4139 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
4140 5de5890b 2018-10-18 stsp free(id);
4141 c1669e2e 2019-01-09 stsp
4142 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4143 c1669e2e 2019-01-09 stsp char *child_path;
4144 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4145 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4146 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4147 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4148 c1669e2e 2019-01-09 stsp goto done;
4149 c1669e2e 2019-01-09 stsp }
4150 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4151 c1669e2e 2019-01-09 stsp root_path, repo);
4152 c1669e2e 2019-01-09 stsp free(child_path);
4153 c1669e2e 2019-01-09 stsp if (err)
4154 c1669e2e 2019-01-09 stsp goto done;
4155 c1669e2e 2019-01-09 stsp }
4156 5de5890b 2018-10-18 stsp }
4157 5de5890b 2018-10-18 stsp done:
4158 5de5890b 2018-10-18 stsp if (tree)
4159 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4160 5de5890b 2018-10-18 stsp free(tree_id);
4161 5de5890b 2018-10-18 stsp return err;
4162 404c43c4 2018-06-21 stsp }
4163 404c43c4 2018-06-21 stsp
4164 5de5890b 2018-10-18 stsp static const struct got_error *
4165 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4166 5de5890b 2018-10-18 stsp {
4167 5de5890b 2018-10-18 stsp const struct got_error *error;
4168 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4169 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4170 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4171 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4172 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4173 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4174 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4175 5de5890b 2018-10-18 stsp int ch;
4176 5de5890b 2018-10-18 stsp
4177 5de5890b 2018-10-18 stsp #ifndef PROFILE
4178 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4179 0f8d269b 2019-01-04 stsp NULL) == -1)
4180 5de5890b 2018-10-18 stsp err(1, "pledge");
4181 5de5890b 2018-10-18 stsp #endif
4182 5de5890b 2018-10-18 stsp
4183 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4184 5de5890b 2018-10-18 stsp switch (ch) {
4185 5de5890b 2018-10-18 stsp case 'c':
4186 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4187 5de5890b 2018-10-18 stsp break;
4188 5de5890b 2018-10-18 stsp case 'r':
4189 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4190 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4191 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4192 9ba1d308 2019-10-21 stsp optarg);
4193 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4194 5de5890b 2018-10-18 stsp break;
4195 5de5890b 2018-10-18 stsp case 'i':
4196 5de5890b 2018-10-18 stsp show_ids = 1;
4197 5de5890b 2018-10-18 stsp break;
4198 c1669e2e 2019-01-09 stsp case 'R':
4199 c1669e2e 2019-01-09 stsp recurse = 1;
4200 c1669e2e 2019-01-09 stsp break;
4201 5de5890b 2018-10-18 stsp default:
4202 2deda0b9 2019-03-07 stsp usage_tree();
4203 5de5890b 2018-10-18 stsp /* NOTREACHED */
4204 5de5890b 2018-10-18 stsp }
4205 5de5890b 2018-10-18 stsp }
4206 5de5890b 2018-10-18 stsp
4207 5de5890b 2018-10-18 stsp argc -= optind;
4208 5de5890b 2018-10-18 stsp argv += optind;
4209 5de5890b 2018-10-18 stsp
4210 5de5890b 2018-10-18 stsp if (argc == 1)
4211 5de5890b 2018-10-18 stsp path = argv[0];
4212 5de5890b 2018-10-18 stsp else if (argc > 1)
4213 5de5890b 2018-10-18 stsp usage_tree();
4214 5de5890b 2018-10-18 stsp else
4215 7a2c19d6 2019-02-05 stsp path = NULL;
4216 7a2c19d6 2019-02-05 stsp
4217 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4218 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4219 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4220 9bf7a39b 2019-02-05 stsp goto done;
4221 9bf7a39b 2019-02-05 stsp }
4222 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4223 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4224 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4225 7a2c19d6 2019-02-05 stsp goto done;
4226 7a2c19d6 2019-02-05 stsp else
4227 7a2c19d6 2019-02-05 stsp error = NULL;
4228 7a2c19d6 2019-02-05 stsp if (worktree) {
4229 7a2c19d6 2019-02-05 stsp repo_path =
4230 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4231 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4232 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4233 7a2c19d6 2019-02-05 stsp if (error)
4234 7a2c19d6 2019-02-05 stsp goto done;
4235 7a2c19d6 2019-02-05 stsp } else {
4236 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4237 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4238 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4239 7a2c19d6 2019-02-05 stsp goto done;
4240 7a2c19d6 2019-02-05 stsp }
4241 5de5890b 2018-10-18 stsp }
4242 5de5890b 2018-10-18 stsp }
4243 5de5890b 2018-10-18 stsp
4244 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4245 5de5890b 2018-10-18 stsp if (error != NULL)
4246 c02c541e 2019-03-29 stsp goto done;
4247 c02c541e 2019-03-29 stsp
4248 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4249 c02c541e 2019-03-29 stsp if (error)
4250 5de5890b 2018-10-18 stsp goto done;
4251 5de5890b 2018-10-18 stsp
4252 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4253 9bf7a39b 2019-02-05 stsp if (worktree) {
4254 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4255 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4256 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4257 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4258 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4259 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4260 9bf7a39b 2019-02-05 stsp goto done;
4261 9bf7a39b 2019-02-05 stsp }
4262 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4263 9bf7a39b 2019-02-05 stsp free(p);
4264 9bf7a39b 2019-02-05 stsp if (error)
4265 9bf7a39b 2019-02-05 stsp goto done;
4266 9bf7a39b 2019-02-05 stsp } else
4267 9bf7a39b 2019-02-05 stsp path = "/";
4268 9bf7a39b 2019-02-05 stsp }
4269 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4270 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4271 9bf7a39b 2019-02-05 stsp if (error != NULL)
4272 9bf7a39b 2019-02-05 stsp goto done;
4273 9bf7a39b 2019-02-05 stsp }
4274 5de5890b 2018-10-18 stsp
4275 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4276 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4277 4e0a20a4 2020-03-23 tracey if (worktree)
4278 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4279 4e0a20a4 2020-03-23 tracey else
4280 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4281 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4282 5de5890b 2018-10-18 stsp if (error != NULL)
4283 5de5890b 2018-10-18 stsp goto done;
4284 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4285 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4286 5de5890b 2018-10-18 stsp if (error != NULL)
4287 5de5890b 2018-10-18 stsp goto done;
4288 5de5890b 2018-10-18 stsp } else {
4289 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4290 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4291 30837e32 2019-07-25 stsp if (error)
4292 5de5890b 2018-10-18 stsp goto done;
4293 5de5890b 2018-10-18 stsp }
4294 5de5890b 2018-10-18 stsp
4295 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4296 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4297 5de5890b 2018-10-18 stsp done:
4298 5de5890b 2018-10-18 stsp free(in_repo_path);
4299 5de5890b 2018-10-18 stsp free(repo_path);
4300 5de5890b 2018-10-18 stsp free(cwd);
4301 5de5890b 2018-10-18 stsp free(commit_id);
4302 7a2c19d6 2019-02-05 stsp if (worktree)
4303 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4304 5de5890b 2018-10-18 stsp if (repo) {
4305 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4306 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4307 5de5890b 2018-10-18 stsp if (error == NULL)
4308 5de5890b 2018-10-18 stsp error = repo_error;
4309 5de5890b 2018-10-18 stsp }
4310 5de5890b 2018-10-18 stsp return error;
4311 5de5890b 2018-10-18 stsp }
4312 5de5890b 2018-10-18 stsp
4313 6bad629b 2019-02-04 stsp __dead static void
4314 6bad629b 2019-02-04 stsp usage_status(void)
4315 6bad629b 2019-02-04 stsp {
4316 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4317 6bad629b 2019-02-04 stsp exit(1);
4318 6bad629b 2019-02-04 stsp }
4319 5c860e29 2018-03-12 stsp
4320 b72f483a 2019-02-05 stsp static const struct got_error *
4321 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4322 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4323 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4324 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4325 6bad629b 2019-02-04 stsp {
4326 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4327 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4328 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4329 b72f483a 2019-02-05 stsp return NULL;
4330 6bad629b 2019-02-04 stsp }
4331 5c860e29 2018-03-12 stsp
4332 6bad629b 2019-02-04 stsp static const struct got_error *
4333 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4334 6bad629b 2019-02-04 stsp {
4335 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4336 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4337 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4338 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4339 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4340 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4341 a5edda0a 2019-07-27 stsp int ch;
4342 5c860e29 2018-03-12 stsp
4343 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4344 72ea6654 2019-07-27 stsp
4345 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4346 6bad629b 2019-02-04 stsp switch (ch) {
4347 5c860e29 2018-03-12 stsp default:
4348 2deda0b9 2019-03-07 stsp usage_status();
4349 6bad629b 2019-02-04 stsp /* NOTREACHED */
4350 5c860e29 2018-03-12 stsp }
4351 5c860e29 2018-03-12 stsp }
4352 5c860e29 2018-03-12 stsp
4353 6bad629b 2019-02-04 stsp argc -= optind;
4354 6bad629b 2019-02-04 stsp argv += optind;
4355 5c860e29 2018-03-12 stsp
4356 6bad629b 2019-02-04 stsp #ifndef PROFILE
4357 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4358 6bad629b 2019-02-04 stsp NULL) == -1)
4359 6bad629b 2019-02-04 stsp err(1, "pledge");
4360 f42b1b34 2018-03-12 stsp #endif
4361 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4362 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4363 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4364 927df6b7 2019-02-10 stsp goto done;
4365 927df6b7 2019-02-10 stsp }
4366 927df6b7 2019-02-10 stsp
4367 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4368 927df6b7 2019-02-10 stsp if (error != NULL)
4369 a5edda0a 2019-07-27 stsp goto done;
4370 6bad629b 2019-02-04 stsp
4371 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4372 c9956ddf 2019-09-08 stsp NULL);
4373 6bad629b 2019-02-04 stsp if (error != NULL)
4374 6bad629b 2019-02-04 stsp goto done;
4375 6bad629b 2019-02-04 stsp
4376 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4377 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4378 087fb88c 2019-08-04 stsp if (error)
4379 087fb88c 2019-08-04 stsp goto done;
4380 087fb88c 2019-08-04 stsp
4381 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4382 6bad629b 2019-02-04 stsp if (error)
4383 6bad629b 2019-02-04 stsp goto done;
4384 6bad629b 2019-02-04 stsp
4385 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4386 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4387 6bad629b 2019-02-04 stsp done:
4388 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4389 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4390 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4391 927df6b7 2019-02-10 stsp free(cwd);
4392 d0eebce4 2019-03-11 stsp return error;
4393 d0eebce4 2019-03-11 stsp }
4394 d0eebce4 2019-03-11 stsp
4395 d0eebce4 2019-03-11 stsp __dead static void
4396 d0eebce4 2019-03-11 stsp usage_ref(void)
4397 d0eebce4 2019-03-11 stsp {
4398 d0eebce4 2019-03-11 stsp fprintf(stderr,
4399 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4400 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4401 d0eebce4 2019-03-11 stsp getprogname());
4402 d0eebce4 2019-03-11 stsp exit(1);
4403 d0eebce4 2019-03-11 stsp }
4404 d0eebce4 2019-03-11 stsp
4405 d0eebce4 2019-03-11 stsp static const struct got_error *
4406 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4407 d0eebce4 2019-03-11 stsp {
4408 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4409 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4410 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4411 d0eebce4 2019-03-11 stsp
4412 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4413 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4414 d0eebce4 2019-03-11 stsp if (err)
4415 d0eebce4 2019-03-11 stsp return err;
4416 d0eebce4 2019-03-11 stsp
4417 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4418 d0eebce4 2019-03-11 stsp char *refstr;
4419 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4420 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4421 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4422 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4423 d0eebce4 2019-03-11 stsp free(refstr);
4424 d0eebce4 2019-03-11 stsp }
4425 d0eebce4 2019-03-11 stsp
4426 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4427 d0eebce4 2019-03-11 stsp return NULL;
4428 d0eebce4 2019-03-11 stsp }
4429 d0eebce4 2019-03-11 stsp
4430 d0eebce4 2019-03-11 stsp static const struct got_error *
4431 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4432 d0eebce4 2019-03-11 stsp {
4433 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4434 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4435 d0eebce4 2019-03-11 stsp
4436 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4437 d0eebce4 2019-03-11 stsp if (err)
4438 d0eebce4 2019-03-11 stsp return err;
4439 d0eebce4 2019-03-11 stsp
4440 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4441 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4442 d0eebce4 2019-03-11 stsp return err;
4443 d0eebce4 2019-03-11 stsp }
4444 d0eebce4 2019-03-11 stsp
4445 d0eebce4 2019-03-11 stsp static const struct got_error *
4446 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4447 d0eebce4 2019-03-11 stsp {
4448 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4449 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4450 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4451 d1644381 2019-07-14 stsp
4452 d1644381 2019-07-14 stsp /*
4453 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4454 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4455 d1644381 2019-07-14 stsp * an unintended typo.
4456 d1644381 2019-07-14 stsp */
4457 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4458 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4459 d0eebce4 2019-03-11 stsp
4460 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4461 dd88155e 2019-06-29 stsp repo);
4462 d83d9d5c 2019-05-13 stsp if (err) {
4463 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4464 d0eebce4 2019-03-11 stsp
4465 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4466 d83d9d5c 2019-05-13 stsp return err;
4467 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4468 d83d9d5c 2019-05-13 stsp if (err)
4469 d83d9d5c 2019-05-13 stsp return err;
4470 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4471 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4472 d83d9d5c 2019-05-13 stsp if (err)
4473 d83d9d5c 2019-05-13 stsp return err;
4474 d83d9d5c 2019-05-13 stsp }
4475 d83d9d5c 2019-05-13 stsp
4476 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4477 d0eebce4 2019-03-11 stsp if (err)
4478 d0eebce4 2019-03-11 stsp goto done;
4479 d0eebce4 2019-03-11 stsp
4480 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4481 d0eebce4 2019-03-11 stsp done:
4482 d0eebce4 2019-03-11 stsp if (ref)
4483 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4484 d0eebce4 2019-03-11 stsp free(id);
4485 d1c1ae5f 2019-08-12 stsp return err;
4486 d1c1ae5f 2019-08-12 stsp }
4487 d1c1ae5f 2019-08-12 stsp
4488 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4489 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4490 d1c1ae5f 2019-08-12 stsp {
4491 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4492 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4493 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4494 d1c1ae5f 2019-08-12 stsp
4495 d1c1ae5f 2019-08-12 stsp /*
4496 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4497 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4498 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4499 d1c1ae5f 2019-08-12 stsp */
4500 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4501 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4502 d1c1ae5f 2019-08-12 stsp
4503 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4504 d1c1ae5f 2019-08-12 stsp if (err)
4505 d1c1ae5f 2019-08-12 stsp return err;
4506 d1c1ae5f 2019-08-12 stsp
4507 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4508 d1c1ae5f 2019-08-12 stsp if (err)
4509 d1c1ae5f 2019-08-12 stsp goto done;
4510 d1c1ae5f 2019-08-12 stsp
4511 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4512 d1c1ae5f 2019-08-12 stsp done:
4513 d1c1ae5f 2019-08-12 stsp if (target_ref)
4514 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4515 d1c1ae5f 2019-08-12 stsp if (ref)
4516 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4517 d0eebce4 2019-03-11 stsp return err;
4518 d0eebce4 2019-03-11 stsp }
4519 d0eebce4 2019-03-11 stsp
4520 d0eebce4 2019-03-11 stsp static const struct got_error *
4521 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4522 d0eebce4 2019-03-11 stsp {
4523 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4524 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4525 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4526 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4527 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4528 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4529 b2070a3f 2020-03-22 stsp char *refname = NULL;
4530 d0eebce4 2019-03-11 stsp
4531 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4532 d0eebce4 2019-03-11 stsp switch (ch) {
4533 e31abbf2 2020-03-22 stsp case 'c':
4534 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4535 e31abbf2 2020-03-22 stsp break;
4536 d0eebce4 2019-03-11 stsp case 'd':
4537 e31abbf2 2020-03-22 stsp do_delete = 1;
4538 d0eebce4 2019-03-11 stsp break;
4539 d0eebce4 2019-03-11 stsp case 'r':
4540 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4541 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4542 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4543 9ba1d308 2019-10-21 stsp optarg);
4544 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4545 d0eebce4 2019-03-11 stsp break;
4546 d0eebce4 2019-03-11 stsp case 'l':
4547 d0eebce4 2019-03-11 stsp do_list = 1;
4548 d1c1ae5f 2019-08-12 stsp break;
4549 d1c1ae5f 2019-08-12 stsp case 's':
4550 e31abbf2 2020-03-22 stsp symref_target = optarg;
4551 d0eebce4 2019-03-11 stsp break;
4552 d0eebce4 2019-03-11 stsp default:
4553 d0eebce4 2019-03-11 stsp usage_ref();
4554 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4555 d0eebce4 2019-03-11 stsp }
4556 d0eebce4 2019-03-11 stsp }
4557 d0eebce4 2019-03-11 stsp
4558 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4559 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4560 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4561 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4562 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4563 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4564 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4565 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4566 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4567 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4568 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4569 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4570 d0eebce4 2019-03-11 stsp
4571 d0eebce4 2019-03-11 stsp argc -= optind;
4572 d0eebce4 2019-03-11 stsp argv += optind;
4573 d0eebce4 2019-03-11 stsp
4574 e31abbf2 2020-03-22 stsp if (do_list) {
4575 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4576 d0eebce4 2019-03-11 stsp usage_ref();
4577 b2070a3f 2020-03-22 stsp if (argc == 1) {
4578 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4579 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4580 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4581 b2070a3f 2020-03-22 stsp goto done;
4582 b2070a3f 2020-03-22 stsp }
4583 b2070a3f 2020-03-22 stsp }
4584 e31abbf2 2020-03-22 stsp } else {
4585 e31abbf2 2020-03-22 stsp if (argc != 1)
4586 e31abbf2 2020-03-22 stsp usage_ref();
4587 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4588 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4589 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4590 b2070a3f 2020-03-22 stsp goto done;
4591 b2070a3f 2020-03-22 stsp }
4592 e31abbf2 2020-03-22 stsp }
4593 b2070a3f 2020-03-22 stsp
4594 b2070a3f 2020-03-22 stsp if (refname)
4595 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4596 d0eebce4 2019-03-11 stsp
4597 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4598 e0b57350 2019-03-12 stsp if (do_list) {
4599 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4600 e0b57350 2019-03-12 stsp NULL) == -1)
4601 e0b57350 2019-03-12 stsp err(1, "pledge");
4602 e0b57350 2019-03-12 stsp } else {
4603 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4604 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4605 e0b57350 2019-03-12 stsp err(1, "pledge");
4606 e0b57350 2019-03-12 stsp }
4607 d0eebce4 2019-03-11 stsp #endif
4608 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4609 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4610 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4611 d0eebce4 2019-03-11 stsp goto done;
4612 d0eebce4 2019-03-11 stsp }
4613 d0eebce4 2019-03-11 stsp
4614 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4615 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4616 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4617 d0eebce4 2019-03-11 stsp goto done;
4618 d0eebce4 2019-03-11 stsp else
4619 d0eebce4 2019-03-11 stsp error = NULL;
4620 d0eebce4 2019-03-11 stsp if (worktree) {
4621 d0eebce4 2019-03-11 stsp repo_path =
4622 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4623 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4624 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4625 d0eebce4 2019-03-11 stsp if (error)
4626 d0eebce4 2019-03-11 stsp goto done;
4627 d0eebce4 2019-03-11 stsp } else {
4628 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4629 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4630 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4631 d0eebce4 2019-03-11 stsp goto done;
4632 d0eebce4 2019-03-11 stsp }
4633 d0eebce4 2019-03-11 stsp }
4634 d0eebce4 2019-03-11 stsp }
4635 d0eebce4 2019-03-11 stsp
4636 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4637 d0eebce4 2019-03-11 stsp if (error != NULL)
4638 d0eebce4 2019-03-11 stsp goto done;
4639 d0eebce4 2019-03-11 stsp
4640 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4641 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4642 c02c541e 2019-03-29 stsp if (error)
4643 c02c541e 2019-03-29 stsp goto done;
4644 c02c541e 2019-03-29 stsp
4645 d0eebce4 2019-03-11 stsp if (do_list)
4646 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4647 e31abbf2 2020-03-22 stsp else if (do_delete)
4648 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4649 e31abbf2 2020-03-22 stsp else if (symref_target)
4650 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4651 e31abbf2 2020-03-22 stsp else {
4652 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4653 e31abbf2 2020-03-22 stsp usage_ref();
4654 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4655 e31abbf2 2020-03-22 stsp }
4656 4e759de4 2019-06-26 stsp done:
4657 b2070a3f 2020-03-22 stsp free(refname);
4658 4e759de4 2019-06-26 stsp if (repo)
4659 4e759de4 2019-06-26 stsp got_repo_close(repo);
4660 4e759de4 2019-06-26 stsp if (worktree)
4661 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4662 4e759de4 2019-06-26 stsp free(cwd);
4663 4e759de4 2019-06-26 stsp free(repo_path);
4664 4e759de4 2019-06-26 stsp return error;
4665 4e759de4 2019-06-26 stsp }
4666 4e759de4 2019-06-26 stsp
4667 4e759de4 2019-06-26 stsp __dead static void
4668 4e759de4 2019-06-26 stsp usage_branch(void)
4669 4e759de4 2019-06-26 stsp {
4670 4e759de4 2019-06-26 stsp fprintf(stderr,
4671 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4672 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4673 4e759de4 2019-06-26 stsp exit(1);
4674 4e759de4 2019-06-26 stsp }
4675 4e759de4 2019-06-26 stsp
4676 4e759de4 2019-06-26 stsp static const struct got_error *
4677 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4678 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4679 4e99b47e 2019-10-04 stsp {
4680 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4681 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4682 4e99b47e 2019-10-04 stsp char *refstr;
4683 4e99b47e 2019-10-04 stsp
4684 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4685 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4686 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4687 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4688 4e99b47e 2019-10-04 stsp
4689 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4690 4e99b47e 2019-10-04 stsp if (err)
4691 4e99b47e 2019-10-04 stsp return err;
4692 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4693 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4694 4e99b47e 2019-10-04 stsp marker = "* ";
4695 4e99b47e 2019-10-04 stsp else
4696 4e99b47e 2019-10-04 stsp marker = "~ ";
4697 4e99b47e 2019-10-04 stsp free(id);
4698 4e99b47e 2019-10-04 stsp }
4699 4e99b47e 2019-10-04 stsp
4700 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4701 4e99b47e 2019-10-04 stsp refname += 11;
4702 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4703 4e99b47e 2019-10-04 stsp refname += 18;
4704 4e99b47e 2019-10-04 stsp
4705 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4706 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4707 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4708 4e99b47e 2019-10-04 stsp
4709 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4710 4e99b47e 2019-10-04 stsp free(refstr);
4711 4e99b47e 2019-10-04 stsp return NULL;
4712 4e99b47e 2019-10-04 stsp }
4713 4e99b47e 2019-10-04 stsp
4714 4e99b47e 2019-10-04 stsp static const struct got_error *
4715 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4716 ad89fa31 2019-10-04 stsp {
4717 ad89fa31 2019-10-04 stsp const char *refname;
4718 ad89fa31 2019-10-04 stsp
4719 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4720 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4721 ad89fa31 2019-10-04 stsp
4722 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4723 ad89fa31 2019-10-04 stsp
4724 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4725 ad89fa31 2019-10-04 stsp refname += 11;
4726 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4727 ad89fa31 2019-10-04 stsp refname += 18;
4728 ad89fa31 2019-10-04 stsp
4729 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4730 ad89fa31 2019-10-04 stsp
4731 ad89fa31 2019-10-04 stsp return NULL;
4732 ad89fa31 2019-10-04 stsp }
4733 ad89fa31 2019-10-04 stsp
4734 ad89fa31 2019-10-04 stsp static const struct got_error *
4735 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4736 4e759de4 2019-06-26 stsp {
4737 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4738 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4739 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4740 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4741 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4742 4e759de4 2019-06-26 stsp
4743 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4744 ba882ee3 2019-07-11 stsp
4745 4e99b47e 2019-10-04 stsp if (worktree) {
4746 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4747 4e99b47e 2019-10-04 stsp worktree);
4748 4e99b47e 2019-10-04 stsp if (err)
4749 4e99b47e 2019-10-04 stsp return err;
4750 4e99b47e 2019-10-04 stsp
4751 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4752 4e99b47e 2019-10-04 stsp worktree);
4753 4e99b47e 2019-10-04 stsp if (err)
4754 4e99b47e 2019-10-04 stsp return err;
4755 4e99b47e 2019-10-04 stsp
4756 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4757 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4758 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4759 4e99b47e 2019-10-04 stsp if (err)
4760 4e99b47e 2019-10-04 stsp return err;
4761 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4762 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4763 4e99b47e 2019-10-04 stsp }
4764 4e99b47e 2019-10-04 stsp }
4765 4e99b47e 2019-10-04 stsp
4766 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4767 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4768 4e759de4 2019-06-26 stsp if (err)
4769 4e759de4 2019-06-26 stsp return err;
4770 4e759de4 2019-06-26 stsp
4771 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4772 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4773 4e759de4 2019-06-26 stsp
4774 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4775 4e759de4 2019-06-26 stsp return NULL;
4776 4e759de4 2019-06-26 stsp }
4777 4e759de4 2019-06-26 stsp
4778 4e759de4 2019-06-26 stsp static const struct got_error *
4779 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4780 45cd4e47 2019-08-25 stsp const char *branch_name)
4781 4e759de4 2019-06-26 stsp {
4782 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4783 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4784 4e759de4 2019-06-26 stsp char *refname;
4785 4e759de4 2019-06-26 stsp
4786 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4787 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4788 4e759de4 2019-06-26 stsp
4789 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4790 4e759de4 2019-06-26 stsp if (err)
4791 4e759de4 2019-06-26 stsp goto done;
4792 4e759de4 2019-06-26 stsp
4793 45cd4e47 2019-08-25 stsp if (worktree &&
4794 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4795 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4796 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4797 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4798 45cd4e47 2019-08-25 stsp goto done;
4799 45cd4e47 2019-08-25 stsp }
4800 45cd4e47 2019-08-25 stsp
4801 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4802 4e759de4 2019-06-26 stsp done:
4803 45cd4e47 2019-08-25 stsp if (ref)
4804 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4805 4e759de4 2019-06-26 stsp free(refname);
4806 4e759de4 2019-06-26 stsp return err;
4807 4e759de4 2019-06-26 stsp }
4808 4e759de4 2019-06-26 stsp
4809 4e759de4 2019-06-26 stsp static const struct got_error *
4810 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4811 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4812 4e759de4 2019-06-26 stsp {
4813 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4814 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4815 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4816 d3f84d51 2019-07-11 stsp
4817 d3f84d51 2019-07-11 stsp /*
4818 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4819 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4820 d3f84d51 2019-07-11 stsp * an unintended typo.
4821 d3f84d51 2019-07-11 stsp */
4822 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4823 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4824 4e759de4 2019-06-26 stsp
4825 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4826 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4827 4e759de4 2019-06-26 stsp goto done;
4828 4e759de4 2019-06-26 stsp }
4829 4e759de4 2019-06-26 stsp
4830 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4831 4e759de4 2019-06-26 stsp if (err == NULL) {
4832 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4833 4e759de4 2019-06-26 stsp goto done;
4834 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4835 4e759de4 2019-06-26 stsp goto done;
4836 4e759de4 2019-06-26 stsp
4837 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4838 4e759de4 2019-06-26 stsp if (err)
4839 4e759de4 2019-06-26 stsp goto done;
4840 4e759de4 2019-06-26 stsp
4841 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4842 d0eebce4 2019-03-11 stsp done:
4843 4e759de4 2019-06-26 stsp if (ref)
4844 4e759de4 2019-06-26 stsp got_ref_close(ref);
4845 4e759de4 2019-06-26 stsp free(base_refname);
4846 4e759de4 2019-06-26 stsp free(refname);
4847 4e759de4 2019-06-26 stsp return err;
4848 4e759de4 2019-06-26 stsp }
4849 4e759de4 2019-06-26 stsp
4850 4e759de4 2019-06-26 stsp static const struct got_error *
4851 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4852 4e759de4 2019-06-26 stsp {
4853 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4854 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4855 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4856 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4857 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4858 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4859 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4860 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4861 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4862 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4863 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4864 4e759de4 2019-06-26 stsp
4865 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4866 da76fce2 2020-02-24 stsp
4867 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4868 4e759de4 2019-06-26 stsp switch (ch) {
4869 a74f7e83 2019-11-10 stsp case 'c':
4870 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4871 a74f7e83 2019-11-10 stsp break;
4872 4e759de4 2019-06-26 stsp case 'd':
4873 4e759de4 2019-06-26 stsp delref = optarg;
4874 4e759de4 2019-06-26 stsp break;
4875 4e759de4 2019-06-26 stsp case 'r':
4876 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4877 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4878 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4879 9ba1d308 2019-10-21 stsp optarg);
4880 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4881 4e759de4 2019-06-26 stsp break;
4882 4e759de4 2019-06-26 stsp case 'l':
4883 4e759de4 2019-06-26 stsp do_list = 1;
4884 da76fce2 2020-02-24 stsp break;
4885 da76fce2 2020-02-24 stsp case 'n':
4886 da76fce2 2020-02-24 stsp do_update = 0;
4887 4e759de4 2019-06-26 stsp break;
4888 4e759de4 2019-06-26 stsp default:
4889 4e759de4 2019-06-26 stsp usage_branch();
4890 4e759de4 2019-06-26 stsp /* NOTREACHED */
4891 4e759de4 2019-06-26 stsp }
4892 4e759de4 2019-06-26 stsp }
4893 4e759de4 2019-06-26 stsp
4894 4e759de4 2019-06-26 stsp if (do_list && delref)
4895 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
4896 4e759de4 2019-06-26 stsp
4897 4e759de4 2019-06-26 stsp argc -= optind;
4898 4e759de4 2019-06-26 stsp argv += optind;
4899 ad89fa31 2019-10-04 stsp
4900 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4901 ad89fa31 2019-10-04 stsp do_show = 1;
4902 4e759de4 2019-06-26 stsp
4903 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4904 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4905 a74f7e83 2019-11-10 stsp
4906 4e759de4 2019-06-26 stsp if (do_list || delref) {
4907 4e759de4 2019-06-26 stsp if (argc > 0)
4908 4e759de4 2019-06-26 stsp usage_branch();
4909 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4910 4e759de4 2019-06-26 stsp usage_branch();
4911 4e759de4 2019-06-26 stsp
4912 4e759de4 2019-06-26 stsp #ifndef PROFILE
4913 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4914 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4915 4e759de4 2019-06-26 stsp NULL) == -1)
4916 4e759de4 2019-06-26 stsp err(1, "pledge");
4917 4e759de4 2019-06-26 stsp } else {
4918 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4919 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4920 4e759de4 2019-06-26 stsp err(1, "pledge");
4921 4e759de4 2019-06-26 stsp }
4922 4e759de4 2019-06-26 stsp #endif
4923 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4924 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4925 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4926 4e759de4 2019-06-26 stsp goto done;
4927 4e759de4 2019-06-26 stsp }
4928 4e759de4 2019-06-26 stsp
4929 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4930 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4931 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4932 4e759de4 2019-06-26 stsp goto done;
4933 4e759de4 2019-06-26 stsp else
4934 4e759de4 2019-06-26 stsp error = NULL;
4935 4e759de4 2019-06-26 stsp if (worktree) {
4936 4e759de4 2019-06-26 stsp repo_path =
4937 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4938 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4939 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4940 4e759de4 2019-06-26 stsp if (error)
4941 4e759de4 2019-06-26 stsp goto done;
4942 4e759de4 2019-06-26 stsp } else {
4943 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4944 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4945 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4946 4e759de4 2019-06-26 stsp goto done;
4947 4e759de4 2019-06-26 stsp }
4948 4e759de4 2019-06-26 stsp }
4949 4e759de4 2019-06-26 stsp }
4950 4e759de4 2019-06-26 stsp
4951 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4952 4e759de4 2019-06-26 stsp if (error != NULL)
4953 4e759de4 2019-06-26 stsp goto done;
4954 4e759de4 2019-06-26 stsp
4955 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4956 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4957 4e759de4 2019-06-26 stsp if (error)
4958 4e759de4 2019-06-26 stsp goto done;
4959 4e759de4 2019-06-26 stsp
4960 ad89fa31 2019-10-04 stsp if (do_show)
4961 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4962 ad89fa31 2019-10-04 stsp else if (do_list)
4963 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4964 4e759de4 2019-06-26 stsp else if (delref)
4965 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4966 4e759de4 2019-06-26 stsp else {
4967 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4968 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4969 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4970 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4971 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4972 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4973 a74f7e83 2019-11-10 stsp if (error)
4974 a74f7e83 2019-11-10 stsp goto done;
4975 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4976 da76fce2 2020-02-24 stsp if (error)
4977 da76fce2 2020-02-24 stsp goto done;
4978 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4979 da76fce2 2020-02-24 stsp int did_something = 0;
4980 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4981 da76fce2 2020-02-24 stsp
4982 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4983 da76fce2 2020-02-24 stsp if (error)
4984 da76fce2 2020-02-24 stsp goto done;
4985 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4986 da76fce2 2020-02-24 stsp worktree);
4987 da76fce2 2020-02-24 stsp if (error)
4988 da76fce2 2020-02-24 stsp goto done;
4989 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4990 da76fce2 2020-02-24 stsp == -1) {
4991 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4992 da76fce2 2020-02-24 stsp goto done;
4993 da76fce2 2020-02-24 stsp }
4994 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4995 da76fce2 2020-02-24 stsp free(branch_refname);
4996 da76fce2 2020-02-24 stsp if (error)
4997 da76fce2 2020-02-24 stsp goto done;
4998 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4999 da76fce2 2020-02-24 stsp repo);
5000 da76fce2 2020-02-24 stsp if (error)
5001 da76fce2 2020-02-24 stsp goto done;
5002 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5003 da76fce2 2020-02-24 stsp commit_id);
5004 da76fce2 2020-02-24 stsp if (error)
5005 da76fce2 2020-02-24 stsp goto done;
5006 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5007 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
5008 da76fce2 2020-02-24 stsp check_cancelled, NULL);
5009 da76fce2 2020-02-24 stsp if (error)
5010 da76fce2 2020-02-24 stsp goto done;
5011 da76fce2 2020-02-24 stsp if (did_something)
5012 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5013 da76fce2 2020-02-24 stsp }
5014 4e759de4 2019-06-26 stsp }
5015 4e759de4 2019-06-26 stsp done:
5016 da76fce2 2020-02-24 stsp if (ref)
5017 da76fce2 2020-02-24 stsp got_ref_close(ref);
5018 d0eebce4 2019-03-11 stsp if (repo)
5019 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5020 d0eebce4 2019-03-11 stsp if (worktree)
5021 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5022 d0eebce4 2019-03-11 stsp free(cwd);
5023 d0eebce4 2019-03-11 stsp free(repo_path);
5024 da76fce2 2020-02-24 stsp free(commit_id);
5025 da76fce2 2020-02-24 stsp free(commit_id_str);
5026 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5027 da76fce2 2020-02-24 stsp free((char *)pe->path);
5028 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5029 d00136be 2019-03-26 stsp return error;
5030 d00136be 2019-03-26 stsp }
5031 d00136be 2019-03-26 stsp
5032 8e7bd50a 2019-08-22 stsp
5033 d00136be 2019-03-26 stsp __dead static void
5034 8e7bd50a 2019-08-22 stsp usage_tag(void)
5035 8e7bd50a 2019-08-22 stsp {
5036 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5037 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5038 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5039 8e7bd50a 2019-08-22 stsp exit(1);
5040 b8bad2ba 2019-08-23 stsp }
5041 b8bad2ba 2019-08-23 stsp
5042 b8bad2ba 2019-08-23 stsp #if 0
5043 b8bad2ba 2019-08-23 stsp static const struct got_error *
5044 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5045 b8bad2ba 2019-08-23 stsp {
5046 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5047 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5048 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5049 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5050 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5051 b8bad2ba 2019-08-23 stsp
5052 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5053 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5054 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5055 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5056 b8bad2ba 2019-08-23 stsp if (err)
5057 b8bad2ba 2019-08-23 stsp return err;
5058 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5059 b8bad2ba 2019-08-23 stsp continue;
5060 b8bad2ba 2019-08-23 stsp } else {
5061 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5062 b8bad2ba 2019-08-23 stsp if (err)
5063 b8bad2ba 2019-08-23 stsp break;
5064 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5065 b8bad2ba 2019-08-23 stsp free(re_id);
5066 b8bad2ba 2019-08-23 stsp if (err)
5067 b8bad2ba 2019-08-23 stsp break;
5068 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5069 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5070 b8bad2ba 2019-08-23 stsp }
5071 b8bad2ba 2019-08-23 stsp
5072 b8bad2ba 2019-08-23 stsp while (se) {
5073 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5074 b8bad2ba 2019-08-23 stsp if (err)
5075 b8bad2ba 2019-08-23 stsp break;
5076 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5077 b8bad2ba 2019-08-23 stsp free(se_id);
5078 b8bad2ba 2019-08-23 stsp if (err)
5079 b8bad2ba 2019-08-23 stsp break;
5080 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5081 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5082 b8bad2ba 2019-08-23 stsp
5083 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5084 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5085 b8bad2ba 2019-08-23 stsp if (err)
5086 b8bad2ba 2019-08-23 stsp return err;
5087 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5088 b8bad2ba 2019-08-23 stsp break;
5089 b8bad2ba 2019-08-23 stsp }
5090 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5091 b8bad2ba 2019-08-23 stsp continue;
5092 b8bad2ba 2019-08-23 stsp }
5093 b8bad2ba 2019-08-23 stsp }
5094 b8bad2ba 2019-08-23 stsp done:
5095 b8bad2ba 2019-08-23 stsp return err;
5096 8e7bd50a 2019-08-22 stsp }
5097 b8bad2ba 2019-08-23 stsp #endif
5098 b8bad2ba 2019-08-23 stsp
5099 b8bad2ba 2019-08-23 stsp static const struct got_error *
5100 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5101 8e7bd50a 2019-08-22 stsp {
5102 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5103 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5104 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5105 8e7bd50a 2019-08-22 stsp
5106 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5107 8e7bd50a 2019-08-22 stsp
5108 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5109 8e7bd50a 2019-08-22 stsp if (err)
5110 8e7bd50a 2019-08-22 stsp return err;
5111 8e7bd50a 2019-08-22 stsp
5112 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5113 8e7bd50a 2019-08-22 stsp const char *refname;
5114 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5115 8e7bd50a 2019-08-22 stsp char datebuf[26];
5116 d4efa91b 2020-01-14 stsp const char *tagger;
5117 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5118 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5119 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5120 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5121 8e7bd50a 2019-08-22 stsp
5122 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5123 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5124 8e7bd50a 2019-08-22 stsp continue;
5125 8e7bd50a 2019-08-22 stsp refname += 10;
5126 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5127 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5128 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5129 8e7bd50a 2019-08-22 stsp break;
5130 8e7bd50a 2019-08-22 stsp }
5131 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5132 8e7bd50a 2019-08-22 stsp free(refstr);
5133 8e7bd50a 2019-08-22 stsp
5134 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5135 8e7bd50a 2019-08-22 stsp if (err)
5136 8e7bd50a 2019-08-22 stsp break;
5137 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5138 d4efa91b 2020-01-14 stsp if (err) {
5139 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5140 d4efa91b 2020-01-14 stsp free(id);
5141 d4efa91b 2020-01-14 stsp break;
5142 d4efa91b 2020-01-14 stsp }
5143 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5144 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5145 d4efa91b 2020-01-14 stsp if (err) {
5146 d4efa91b 2020-01-14 stsp free(id);
5147 d4efa91b 2020-01-14 stsp break;
5148 d4efa91b 2020-01-14 stsp }
5149 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5150 d4efa91b 2020-01-14 stsp tagger_time =
5151 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5152 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5153 d4efa91b 2020-01-14 stsp free(id);
5154 d4efa91b 2020-01-14 stsp if (err)
5155 d4efa91b 2020-01-14 stsp break;
5156 d4efa91b 2020-01-14 stsp } else {
5157 d4efa91b 2020-01-14 stsp free(id);
5158 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5159 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5160 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5161 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5162 d4efa91b 2020-01-14 stsp if (err)
5163 d4efa91b 2020-01-14 stsp break;
5164 d4efa91b 2020-01-14 stsp }
5165 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5166 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5167 2417344c 2019-08-23 stsp if (datestr)
5168 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5169 d4efa91b 2020-01-14 stsp if (commit)
5170 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5171 d4efa91b 2020-01-14 stsp else {
5172 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5173 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5174 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5175 d4efa91b 2020-01-14 stsp id_str);
5176 d4efa91b 2020-01-14 stsp break;
5177 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5178 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5179 d4efa91b 2020-01-14 stsp id_str);
5180 d4efa91b 2020-01-14 stsp break;
5181 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5182 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5183 d4efa91b 2020-01-14 stsp id_str);
5184 d4efa91b 2020-01-14 stsp break;
5185 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5186 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5187 d4efa91b 2020-01-14 stsp id_str);
5188 d4efa91b 2020-01-14 stsp break;
5189 d4efa91b 2020-01-14 stsp default:
5190 d4efa91b 2020-01-14 stsp break;
5191 d4efa91b 2020-01-14 stsp }
5192 8e7bd50a 2019-08-22 stsp }
5193 8e7bd50a 2019-08-22 stsp free(id_str);
5194 d4efa91b 2020-01-14 stsp if (commit) {
5195 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5196 d4efa91b 2020-01-14 stsp if (err)
5197 d4efa91b 2020-01-14 stsp break;
5198 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5199 d4efa91b 2020-01-14 stsp } else {
5200 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5201 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5202 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5203 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5204 d4efa91b 2020-01-14 stsp break;
5205 d4efa91b 2020-01-14 stsp }
5206 8e7bd50a 2019-08-22 stsp }
5207 8e7bd50a 2019-08-22 stsp
5208 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5209 8e7bd50a 2019-08-22 stsp do {
5210 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5211 8e7bd50a 2019-08-22 stsp if (line)
5212 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5213 8e7bd50a 2019-08-22 stsp } while (line);
5214 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5215 8e7bd50a 2019-08-22 stsp }
5216 8e7bd50a 2019-08-22 stsp
5217 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5218 8e7bd50a 2019-08-22 stsp return NULL;
5219 8e7bd50a 2019-08-22 stsp }
5220 8e7bd50a 2019-08-22 stsp
5221 8e7bd50a 2019-08-22 stsp static const struct got_error *
5222 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5223 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5224 8e7bd50a 2019-08-22 stsp {
5225 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5226 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5227 f372d5cd 2019-10-21 stsp char *editor = NULL;
5228 8e7bd50a 2019-08-22 stsp int fd = -1;
5229 8e7bd50a 2019-08-22 stsp
5230 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5231 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5232 8e7bd50a 2019-08-22 stsp goto done;
5233 8e7bd50a 2019-08-22 stsp }
5234 8e7bd50a 2019-08-22 stsp
5235 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5236 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5237 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5238 8e7bd50a 2019-08-22 stsp goto done;
5239 8e7bd50a 2019-08-22 stsp }
5240 8e7bd50a 2019-08-22 stsp
5241 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5242 8e7bd50a 2019-08-22 stsp if (err)
5243 8e7bd50a 2019-08-22 stsp goto done;
5244 8e7bd50a 2019-08-22 stsp
5245 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5246 8e7bd50a 2019-08-22 stsp close(fd);
5247 8e7bd50a 2019-08-22 stsp
5248 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5249 8e7bd50a 2019-08-22 stsp if (err)
5250 8e7bd50a 2019-08-22 stsp goto done;
5251 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5252 8e7bd50a 2019-08-22 stsp done:
5253 8e7bd50a 2019-08-22 stsp free(initial_content);
5254 8e7bd50a 2019-08-22 stsp free(template);
5255 8e7bd50a 2019-08-22 stsp free(editor);
5256 8e7bd50a 2019-08-22 stsp
5257 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5258 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5259 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5260 8e7bd50a 2019-08-22 stsp if (err) {
5261 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5262 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5263 8e7bd50a 2019-08-22 stsp }
5264 8e7bd50a 2019-08-22 stsp }
5265 8e7bd50a 2019-08-22 stsp return err;
5266 8e7bd50a 2019-08-22 stsp }
5267 8e7bd50a 2019-08-22 stsp
5268 8e7bd50a 2019-08-22 stsp static const struct got_error *
5269 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5270 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5271 8e7bd50a 2019-08-22 stsp {
5272 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5273 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5274 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5275 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5276 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5277 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5278 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5279 8e7bd50a 2019-08-22 stsp
5280 8e7bd50a 2019-08-22 stsp /*
5281 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5282 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5283 8e7bd50a 2019-08-22 stsp * an unintended typo.
5284 8e7bd50a 2019-08-22 stsp */
5285 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5286 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5287 8e7bd50a 2019-08-22 stsp
5288 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5289 8e7bd50a 2019-08-22 stsp if (err)
5290 8e7bd50a 2019-08-22 stsp return err;
5291 8e7bd50a 2019-08-22 stsp
5292 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5293 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5294 8e7bd50a 2019-08-22 stsp if (err)
5295 8e7bd50a 2019-08-22 stsp goto done;
5296 8e7bd50a 2019-08-22 stsp
5297 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5298 8e7bd50a 2019-08-22 stsp if (err)
5299 8e7bd50a 2019-08-22 stsp goto done;
5300 8e7bd50a 2019-08-22 stsp
5301 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5302 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5303 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5304 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5305 8e7bd50a 2019-08-22 stsp goto done;
5306 8e7bd50a 2019-08-22 stsp }
5307 8e7bd50a 2019-08-22 stsp tag_name += 10;
5308 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5309 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5310 8e7bd50a 2019-08-22 stsp goto done;
5311 8e7bd50a 2019-08-22 stsp }
5312 8e7bd50a 2019-08-22 stsp
5313 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5314 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5315 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5316 8e7bd50a 2019-08-22 stsp goto done;
5317 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5318 8e7bd50a 2019-08-22 stsp goto done;
5319 8e7bd50a 2019-08-22 stsp
5320 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5321 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5322 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5323 f372d5cd 2019-10-21 stsp if (err) {
5324 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5325 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5326 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5327 8e7bd50a 2019-08-22 stsp goto done;
5328 f372d5cd 2019-10-21 stsp }
5329 8e7bd50a 2019-08-22 stsp }
5330 8e7bd50a 2019-08-22 stsp
5331 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5332 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5333 f372d5cd 2019-10-21 stsp if (err) {
5334 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5335 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5336 8e7bd50a 2019-08-22 stsp goto done;
5337 f372d5cd 2019-10-21 stsp }
5338 8e7bd50a 2019-08-22 stsp
5339 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5340 f372d5cd 2019-10-21 stsp if (err) {
5341 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5342 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5343 8e7bd50a 2019-08-22 stsp goto done;
5344 f372d5cd 2019-10-21 stsp }
5345 8e7bd50a 2019-08-22 stsp
5346 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5347 f372d5cd 2019-10-21 stsp if (err) {
5348 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5349 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5350 f372d5cd 2019-10-21 stsp goto done;
5351 f372d5cd 2019-10-21 stsp }
5352 8e7bd50a 2019-08-22 stsp
5353 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5354 f372d5cd 2019-10-21 stsp if (err) {
5355 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5356 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5357 f372d5cd 2019-10-21 stsp goto done;
5358 8e7bd50a 2019-08-22 stsp }
5359 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5360 8e7bd50a 2019-08-22 stsp done:
5361 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5362 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5363 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5364 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5365 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5366 f372d5cd 2019-10-21 stsp free(tag_id_str);
5367 8e7bd50a 2019-08-22 stsp if (ref)
5368 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5369 8e7bd50a 2019-08-22 stsp free(commit_id);
5370 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5371 8e7bd50a 2019-08-22 stsp free(refname);
5372 8e7bd50a 2019-08-22 stsp free(tagmsg);
5373 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5374 aba9c984 2019-09-08 stsp free(tagger);
5375 8e7bd50a 2019-08-22 stsp return err;
5376 8e7bd50a 2019-08-22 stsp }
5377 8e7bd50a 2019-08-22 stsp
5378 8e7bd50a 2019-08-22 stsp static const struct got_error *
5379 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5380 8e7bd50a 2019-08-22 stsp {
5381 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5382 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5383 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5384 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5385 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5386 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5387 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5388 8e7bd50a 2019-08-22 stsp
5389 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5390 8e7bd50a 2019-08-22 stsp switch (ch) {
5391 80106605 2020-02-24 stsp case 'c':
5392 80106605 2020-02-24 stsp commit_id_arg = optarg;
5393 80106605 2020-02-24 stsp break;
5394 8e7bd50a 2019-08-22 stsp case 'm':
5395 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5396 8e7bd50a 2019-08-22 stsp break;
5397 8e7bd50a 2019-08-22 stsp case 'r':
5398 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5399 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5400 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5401 9ba1d308 2019-10-21 stsp optarg);
5402 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5403 8e7bd50a 2019-08-22 stsp break;
5404 8e7bd50a 2019-08-22 stsp case 'l':
5405 8e7bd50a 2019-08-22 stsp do_list = 1;
5406 8e7bd50a 2019-08-22 stsp break;
5407 8e7bd50a 2019-08-22 stsp default:
5408 8e7bd50a 2019-08-22 stsp usage_tag();
5409 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5410 8e7bd50a 2019-08-22 stsp }
5411 8e7bd50a 2019-08-22 stsp }
5412 8e7bd50a 2019-08-22 stsp
5413 8e7bd50a 2019-08-22 stsp argc -= optind;
5414 8e7bd50a 2019-08-22 stsp argv += optind;
5415 8e7bd50a 2019-08-22 stsp
5416 8e7bd50a 2019-08-22 stsp if (do_list) {
5417 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5418 775ce909 2020-03-22 stsp errx(1,
5419 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5420 8e7bd50a 2019-08-22 stsp if (tagmsg)
5421 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5422 8e7bd50a 2019-08-22 stsp if (argc > 0)
5423 8e7bd50a 2019-08-22 stsp usage_tag();
5424 80106605 2020-02-24 stsp } else if (argc != 1)
5425 8e7bd50a 2019-08-22 stsp usage_tag();
5426 80106605 2020-02-24 stsp
5427 a2887370 2019-08-23 stsp tag_name = argv[0];
5428 8e7bd50a 2019-08-22 stsp
5429 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5430 8e7bd50a 2019-08-22 stsp if (do_list) {
5431 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5432 8e7bd50a 2019-08-22 stsp NULL) == -1)
5433 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5434 8e7bd50a 2019-08-22 stsp } else {
5435 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5436 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5437 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5438 8e7bd50a 2019-08-22 stsp }
5439 8e7bd50a 2019-08-22 stsp #endif
5440 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5441 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5442 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5443 8e7bd50a 2019-08-22 stsp goto done;
5444 8e7bd50a 2019-08-22 stsp }
5445 8e7bd50a 2019-08-22 stsp
5446 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5447 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5448 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5449 8e7bd50a 2019-08-22 stsp goto done;
5450 8e7bd50a 2019-08-22 stsp else
5451 8e7bd50a 2019-08-22 stsp error = NULL;
5452 8e7bd50a 2019-08-22 stsp if (worktree) {
5453 8e7bd50a 2019-08-22 stsp repo_path =
5454 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5455 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5456 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5457 8e7bd50a 2019-08-22 stsp if (error)
5458 8e7bd50a 2019-08-22 stsp goto done;
5459 8e7bd50a 2019-08-22 stsp } else {
5460 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5461 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5462 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5463 8e7bd50a 2019-08-22 stsp goto done;
5464 8e7bd50a 2019-08-22 stsp }
5465 8e7bd50a 2019-08-22 stsp }
5466 8e7bd50a 2019-08-22 stsp }
5467 8e7bd50a 2019-08-22 stsp
5468 8e7bd50a 2019-08-22 stsp if (do_list) {
5469 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5470 c9956ddf 2019-09-08 stsp if (error != NULL)
5471 c9956ddf 2019-09-08 stsp goto done;
5472 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5473 8e7bd50a 2019-08-22 stsp if (error)
5474 8e7bd50a 2019-08-22 stsp goto done;
5475 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5476 8e7bd50a 2019-08-22 stsp } else {
5477 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5478 c9956ddf 2019-09-08 stsp if (error)
5479 c9956ddf 2019-09-08 stsp goto done;
5480 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5481 c9956ddf 2019-09-08 stsp if (error != NULL)
5482 c9956ddf 2019-09-08 stsp goto done;
5483 c9956ddf 2019-09-08 stsp
5484 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5485 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5486 8e7bd50a 2019-08-22 stsp if (error)
5487 8e7bd50a 2019-08-22 stsp goto done;
5488 8e7bd50a 2019-08-22 stsp }
5489 8e7bd50a 2019-08-22 stsp
5490 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5491 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5492 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5493 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5494 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5495 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5496 8e7bd50a 2019-08-22 stsp if (error)
5497 8e7bd50a 2019-08-22 stsp goto done;
5498 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5499 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5500 8e7bd50a 2019-08-22 stsp if (error)
5501 8e7bd50a 2019-08-22 stsp goto done;
5502 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5503 8e7bd50a 2019-08-22 stsp free(commit_id);
5504 8e7bd50a 2019-08-22 stsp if (error)
5505 8e7bd50a 2019-08-22 stsp goto done;
5506 8e7bd50a 2019-08-22 stsp }
5507 8e7bd50a 2019-08-22 stsp
5508 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5509 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5510 8e7bd50a 2019-08-22 stsp }
5511 8e7bd50a 2019-08-22 stsp done:
5512 8e7bd50a 2019-08-22 stsp if (repo)
5513 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5514 8e7bd50a 2019-08-22 stsp if (worktree)
5515 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5516 8e7bd50a 2019-08-22 stsp free(cwd);
5517 8e7bd50a 2019-08-22 stsp free(repo_path);
5518 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5519 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5520 8e7bd50a 2019-08-22 stsp return error;
5521 8e7bd50a 2019-08-22 stsp }
5522 8e7bd50a 2019-08-22 stsp
5523 8e7bd50a 2019-08-22 stsp __dead static void
5524 d00136be 2019-03-26 stsp usage_add(void)
5525 d00136be 2019-03-26 stsp {
5526 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5527 022fae89 2019-12-06 tracey getprogname());
5528 d00136be 2019-03-26 stsp exit(1);
5529 d00136be 2019-03-26 stsp }
5530 d00136be 2019-03-26 stsp
5531 d00136be 2019-03-26 stsp static const struct got_error *
5532 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5533 4e68cba3 2019-11-23 stsp {
5534 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5535 4e68cba3 2019-11-23 stsp path++;
5536 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5537 4e68cba3 2019-11-23 stsp return NULL;
5538 4e68cba3 2019-11-23 stsp }
5539 4e68cba3 2019-11-23 stsp
5540 4e68cba3 2019-11-23 stsp static const struct got_error *
5541 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5542 d00136be 2019-03-26 stsp {
5543 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5544 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5545 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5546 1dd54920 2019-05-11 stsp char *cwd = NULL;
5547 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5548 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5549 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5550 1dd54920 2019-05-11 stsp
5551 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5552 d00136be 2019-03-26 stsp
5553 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5554 d00136be 2019-03-26 stsp switch (ch) {
5555 022fae89 2019-12-06 tracey case 'I':
5556 022fae89 2019-12-06 tracey no_ignores = 1;
5557 022fae89 2019-12-06 tracey break;
5558 4e68cba3 2019-11-23 stsp case 'R':
5559 4e68cba3 2019-11-23 stsp can_recurse = 1;
5560 4e68cba3 2019-11-23 stsp break;
5561 d00136be 2019-03-26 stsp default:
5562 d00136be 2019-03-26 stsp usage_add();
5563 d00136be 2019-03-26 stsp /* NOTREACHED */
5564 d00136be 2019-03-26 stsp }
5565 d00136be 2019-03-26 stsp }
5566 d00136be 2019-03-26 stsp
5567 d00136be 2019-03-26 stsp argc -= optind;
5568 d00136be 2019-03-26 stsp argv += optind;
5569 d00136be 2019-03-26 stsp
5570 43012d58 2019-07-14 stsp #ifndef PROFILE
5571 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5572 43012d58 2019-07-14 stsp NULL) == -1)
5573 43012d58 2019-07-14 stsp err(1, "pledge");
5574 43012d58 2019-07-14 stsp #endif
5575 723c305c 2019-05-11 jcs if (argc < 1)
5576 d00136be 2019-03-26 stsp usage_add();
5577 d00136be 2019-03-26 stsp
5578 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5579 d00136be 2019-03-26 stsp if (cwd == NULL) {
5580 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5581 d00136be 2019-03-26 stsp goto done;
5582 d00136be 2019-03-26 stsp }
5583 723c305c 2019-05-11 jcs
5584 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5585 d00136be 2019-03-26 stsp if (error)
5586 d00136be 2019-03-26 stsp goto done;
5587 d00136be 2019-03-26 stsp
5588 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5589 c9956ddf 2019-09-08 stsp NULL);
5590 031a5338 2019-03-26 stsp if (error != NULL)
5591 031a5338 2019-03-26 stsp goto done;
5592 031a5338 2019-03-26 stsp
5593 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5594 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5595 d00136be 2019-03-26 stsp if (error)
5596 d00136be 2019-03-26 stsp goto done;
5597 d00136be 2019-03-26 stsp
5598 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5599 6d022e97 2019-08-04 stsp if (error)
5600 022fae89 2019-12-06 tracey goto done;
5601 022fae89 2019-12-06 tracey
5602 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5603 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5604 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5605 6d022e97 2019-08-04 stsp goto done;
5606 723c305c 2019-05-11 jcs
5607 022fae89 2019-12-06 tracey }
5608 022fae89 2019-12-06 tracey
5609 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5610 4e68cba3 2019-11-23 stsp char *ondisk_path;
5611 4e68cba3 2019-11-23 stsp struct stat sb;
5612 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5613 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5614 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5615 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5616 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5617 4e68cba3 2019-11-23 stsp goto done;
5618 4e68cba3 2019-11-23 stsp }
5619 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5620 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5621 4e68cba3 2019-11-23 stsp free(ondisk_path);
5622 4e68cba3 2019-11-23 stsp continue;
5623 4e68cba3 2019-11-23 stsp }
5624 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5625 4e68cba3 2019-11-23 stsp ondisk_path);
5626 4e68cba3 2019-11-23 stsp free(ondisk_path);
5627 4e68cba3 2019-11-23 stsp goto done;
5628 4e68cba3 2019-11-23 stsp }
5629 4e68cba3 2019-11-23 stsp free(ondisk_path);
5630 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5631 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5632 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5633 4e68cba3 2019-11-23 stsp goto done;
5634 4e68cba3 2019-11-23 stsp }
5635 4e68cba3 2019-11-23 stsp }
5636 4e68cba3 2019-11-23 stsp }
5637 022fae89 2019-12-06 tracey
5638 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5639 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5640 d00136be 2019-03-26 stsp done:
5641 031a5338 2019-03-26 stsp if (repo)
5642 031a5338 2019-03-26 stsp got_repo_close(repo);
5643 d00136be 2019-03-26 stsp if (worktree)
5644 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5645 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5646 1dd54920 2019-05-11 stsp free((char *)pe->path);
5647 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5648 2ec1f75b 2019-03-26 stsp free(cwd);
5649 2ec1f75b 2019-03-26 stsp return error;
5650 2ec1f75b 2019-03-26 stsp }
5651 2ec1f75b 2019-03-26 stsp
5652 2ec1f75b 2019-03-26 stsp __dead static void
5653 648e4ef7 2019-07-09 stsp usage_remove(void)
5654 2ec1f75b 2019-03-26 stsp {
5655 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5656 f2a9dc41 2019-12-13 tracey getprogname());
5657 2ec1f75b 2019-03-26 stsp exit(1);
5658 2a06fe5f 2019-08-24 stsp }
5659 2a06fe5f 2019-08-24 stsp
5660 2a06fe5f 2019-08-24 stsp static const struct got_error *
5661 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5662 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5663 2a06fe5f 2019-08-24 stsp {
5664 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5665 f2a9dc41 2019-12-13 tracey path++;
5666 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5667 2a06fe5f 2019-08-24 stsp return NULL;
5668 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5669 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5670 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5671 2a06fe5f 2019-08-24 stsp return NULL;
5672 2ec1f75b 2019-03-26 stsp }
5673 2ec1f75b 2019-03-26 stsp
5674 2ec1f75b 2019-03-26 stsp static const struct got_error *
5675 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5676 2ec1f75b 2019-03-26 stsp {
5677 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5678 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5679 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5680 17ed4618 2019-06-02 stsp char *cwd = NULL;
5681 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5682 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5683 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5684 2ec1f75b 2019-03-26 stsp
5685 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5686 17ed4618 2019-06-02 stsp
5687 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5688 2ec1f75b 2019-03-26 stsp switch (ch) {
5689 2ec1f75b 2019-03-26 stsp case 'f':
5690 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5691 2ec1f75b 2019-03-26 stsp break;
5692 70e3e7f5 2019-12-13 tracey case 'k':
5693 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5694 70e3e7f5 2019-12-13 tracey break;
5695 f2a9dc41 2019-12-13 tracey case 'R':
5696 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5697 f2a9dc41 2019-12-13 tracey break;
5698 2ec1f75b 2019-03-26 stsp default:
5699 f2a9dc41 2019-12-13 tracey usage_remove();
5700 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5701 2ec1f75b 2019-03-26 stsp }
5702 2ec1f75b 2019-03-26 stsp }
5703 2ec1f75b 2019-03-26 stsp
5704 2ec1f75b 2019-03-26 stsp argc -= optind;
5705 2ec1f75b 2019-03-26 stsp argv += optind;
5706 2ec1f75b 2019-03-26 stsp
5707 43012d58 2019-07-14 stsp #ifndef PROFILE
5708 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5709 43012d58 2019-07-14 stsp NULL) == -1)
5710 43012d58 2019-07-14 stsp err(1, "pledge");
5711 43012d58 2019-07-14 stsp #endif
5712 17ed4618 2019-06-02 stsp if (argc < 1)
5713 648e4ef7 2019-07-09 stsp usage_remove();
5714 2ec1f75b 2019-03-26 stsp
5715 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5716 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5717 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5718 2ec1f75b 2019-03-26 stsp goto done;
5719 2ec1f75b 2019-03-26 stsp }
5720 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5721 2ec1f75b 2019-03-26 stsp if (error)
5722 2ec1f75b 2019-03-26 stsp goto done;
5723 2ec1f75b 2019-03-26 stsp
5724 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5725 c9956ddf 2019-09-08 stsp NULL);
5726 2af4a041 2019-05-11 jcs if (error)
5727 2ec1f75b 2019-03-26 stsp goto done;
5728 2ec1f75b 2019-03-26 stsp
5729 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5730 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5731 2ec1f75b 2019-03-26 stsp if (error)
5732 2ec1f75b 2019-03-26 stsp goto done;
5733 2ec1f75b 2019-03-26 stsp
5734 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5735 6d022e97 2019-08-04 stsp if (error)
5736 6d022e97 2019-08-04 stsp goto done;
5737 17ed4618 2019-06-02 stsp
5738 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5739 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5740 f2a9dc41 2019-12-13 tracey struct stat sb;
5741 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5742 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5743 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5744 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5745 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5746 f2a9dc41 2019-12-13 tracey goto done;
5747 f2a9dc41 2019-12-13 tracey }
5748 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5749 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5750 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5751 f2a9dc41 2019-12-13 tracey continue;
5752 f2a9dc41 2019-12-13 tracey }
5753 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5754 f2a9dc41 2019-12-13 tracey ondisk_path);
5755 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5756 f2a9dc41 2019-12-13 tracey goto done;
5757 f2a9dc41 2019-12-13 tracey }
5758 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5759 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5760 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5761 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5762 f2a9dc41 2019-12-13 tracey goto done;
5763 f2a9dc41 2019-12-13 tracey }
5764 f2a9dc41 2019-12-13 tracey }
5765 f2a9dc41 2019-12-13 tracey }
5766 f2a9dc41 2019-12-13 tracey
5767 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5768 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5769 a129376b 2019-03-28 stsp done:
5770 a129376b 2019-03-28 stsp if (repo)
5771 a129376b 2019-03-28 stsp got_repo_close(repo);
5772 a129376b 2019-03-28 stsp if (worktree)
5773 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5774 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5775 17ed4618 2019-06-02 stsp free((char *)pe->path);
5776 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5777 a129376b 2019-03-28 stsp free(cwd);
5778 a129376b 2019-03-28 stsp return error;
5779 a129376b 2019-03-28 stsp }
5780 a129376b 2019-03-28 stsp
5781 a129376b 2019-03-28 stsp __dead static void
5782 a129376b 2019-03-28 stsp usage_revert(void)
5783 a129376b 2019-03-28 stsp {
5784 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5785 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5786 a129376b 2019-03-28 stsp exit(1);
5787 a129376b 2019-03-28 stsp }
5788 a129376b 2019-03-28 stsp
5789 1ee397ad 2019-07-12 stsp static const struct got_error *
5790 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5791 a129376b 2019-03-28 stsp {
5792 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5793 fb9704af 2020-01-27 stsp return NULL;
5794 fb9704af 2020-01-27 stsp
5795 a129376b 2019-03-28 stsp while (path[0] == '/')
5796 a129376b 2019-03-28 stsp path++;
5797 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5798 33aa809d 2019-08-08 stsp return NULL;
5799 33aa809d 2019-08-08 stsp }
5800 33aa809d 2019-08-08 stsp
5801 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5802 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5803 33aa809d 2019-08-08 stsp const char *action;
5804 33aa809d 2019-08-08 stsp };
5805 33aa809d 2019-08-08 stsp
5806 33aa809d 2019-08-08 stsp static const struct got_error *
5807 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5808 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5809 33aa809d 2019-08-08 stsp {
5810 33aa809d 2019-08-08 stsp char *line = NULL;
5811 33aa809d 2019-08-08 stsp size_t linesize = 0;
5812 33aa809d 2019-08-08 stsp ssize_t linelen;
5813 33aa809d 2019-08-08 stsp
5814 33aa809d 2019-08-08 stsp switch (status) {
5815 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5816 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5817 33aa809d 2019-08-08 stsp break;
5818 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5819 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5820 33aa809d 2019-08-08 stsp break;
5821 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5822 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5823 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5824 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5825 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5826 33aa809d 2019-08-08 stsp printf("%s", line);
5827 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5828 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5829 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5830 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5831 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5832 33aa809d 2019-08-08 stsp break;
5833 33aa809d 2019-08-08 stsp default:
5834 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5835 33aa809d 2019-08-08 stsp }
5836 33aa809d 2019-08-08 stsp
5837 33aa809d 2019-08-08 stsp return NULL;
5838 33aa809d 2019-08-08 stsp }
5839 33aa809d 2019-08-08 stsp
5840 33aa809d 2019-08-08 stsp static const struct got_error *
5841 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5842 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5843 33aa809d 2019-08-08 stsp {
5844 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5845 33aa809d 2019-08-08 stsp char *line = NULL;
5846 33aa809d 2019-08-08 stsp size_t linesize = 0;
5847 33aa809d 2019-08-08 stsp ssize_t linelen;
5848 33aa809d 2019-08-08 stsp int resp = ' ';
5849 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5850 33aa809d 2019-08-08 stsp
5851 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5852 33aa809d 2019-08-08 stsp
5853 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5854 33aa809d 2019-08-08 stsp char *nl;
5855 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5856 33aa809d 2019-08-08 stsp a->action);
5857 33aa809d 2019-08-08 stsp if (err)
5858 33aa809d 2019-08-08 stsp return err;
5859 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5860 33aa809d 2019-08-08 stsp if (linelen == -1) {
5861 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5862 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5863 33aa809d 2019-08-08 stsp return NULL;
5864 33aa809d 2019-08-08 stsp }
5865 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5866 33aa809d 2019-08-08 stsp if (nl)
5867 33aa809d 2019-08-08 stsp *nl = '\0';
5868 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5869 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5870 33aa809d 2019-08-08 stsp printf("y\n");
5871 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5872 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5873 33aa809d 2019-08-08 stsp printf("n\n");
5874 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5875 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5876 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5877 33aa809d 2019-08-08 stsp printf("q\n");
5878 33aa809d 2019-08-08 stsp } else
5879 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5880 33aa809d 2019-08-08 stsp free(line);
5881 33aa809d 2019-08-08 stsp return NULL;
5882 33aa809d 2019-08-08 stsp }
5883 33aa809d 2019-08-08 stsp
5884 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5885 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5886 33aa809d 2019-08-08 stsp a->action);
5887 33aa809d 2019-08-08 stsp if (err)
5888 33aa809d 2019-08-08 stsp return err;
5889 33aa809d 2019-08-08 stsp resp = getchar();
5890 33aa809d 2019-08-08 stsp if (resp == '\n')
5891 33aa809d 2019-08-08 stsp resp = getchar();
5892 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5893 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5894 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5895 33aa809d 2019-08-08 stsp resp = ' ';
5896 33aa809d 2019-08-08 stsp }
5897 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5898 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5899 33aa809d 2019-08-08 stsp resp = ' ';
5900 33aa809d 2019-08-08 stsp }
5901 33aa809d 2019-08-08 stsp }
5902 33aa809d 2019-08-08 stsp
5903 33aa809d 2019-08-08 stsp if (resp == 'y')
5904 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5905 33aa809d 2019-08-08 stsp else if (resp == 'n')
5906 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5907 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5908 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5909 33aa809d 2019-08-08 stsp
5910 1ee397ad 2019-07-12 stsp return NULL;
5911 a129376b 2019-03-28 stsp }
5912 a129376b 2019-03-28 stsp
5913 33aa809d 2019-08-08 stsp
5914 a129376b 2019-03-28 stsp static const struct got_error *
5915 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5916 a129376b 2019-03-28 stsp {
5917 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5918 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5919 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5920 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5921 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5922 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5923 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5924 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5925 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5926 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5927 a129376b 2019-03-28 stsp
5928 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5929 e20a8b6f 2019-06-04 stsp
5930 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5931 a129376b 2019-03-28 stsp switch (ch) {
5932 33aa809d 2019-08-08 stsp case 'p':
5933 33aa809d 2019-08-08 stsp pflag = 1;
5934 33aa809d 2019-08-08 stsp break;
5935 33aa809d 2019-08-08 stsp case 'F':
5936 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5937 33aa809d 2019-08-08 stsp break;
5938 0f6d7415 2019-08-08 stsp case 'R':
5939 0f6d7415 2019-08-08 stsp can_recurse = 1;
5940 0f6d7415 2019-08-08 stsp break;
5941 a129376b 2019-03-28 stsp default:
5942 a129376b 2019-03-28 stsp usage_revert();
5943 a129376b 2019-03-28 stsp /* NOTREACHED */
5944 a129376b 2019-03-28 stsp }
5945 a129376b 2019-03-28 stsp }
5946 a129376b 2019-03-28 stsp
5947 a129376b 2019-03-28 stsp argc -= optind;
5948 a129376b 2019-03-28 stsp argv += optind;
5949 a129376b 2019-03-28 stsp
5950 43012d58 2019-07-14 stsp #ifndef PROFILE
5951 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5952 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5953 43012d58 2019-07-14 stsp err(1, "pledge");
5954 43012d58 2019-07-14 stsp #endif
5955 e20a8b6f 2019-06-04 stsp if (argc < 1)
5956 a129376b 2019-03-28 stsp usage_revert();
5957 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5958 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5959 a129376b 2019-03-28 stsp
5960 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5961 a129376b 2019-03-28 stsp if (cwd == NULL) {
5962 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5963 a129376b 2019-03-28 stsp goto done;
5964 a129376b 2019-03-28 stsp }
5965 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5966 2ec1f75b 2019-03-26 stsp if (error)
5967 2ec1f75b 2019-03-26 stsp goto done;
5968 a129376b 2019-03-28 stsp
5969 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5970 c9956ddf 2019-09-08 stsp NULL);
5971 a129376b 2019-03-28 stsp if (error != NULL)
5972 a129376b 2019-03-28 stsp goto done;
5973 a129376b 2019-03-28 stsp
5974 33aa809d 2019-08-08 stsp if (patch_script_path) {
5975 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5976 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5977 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5978 33aa809d 2019-08-08 stsp patch_script_path);
5979 33aa809d 2019-08-08 stsp goto done;
5980 33aa809d 2019-08-08 stsp }
5981 33aa809d 2019-08-08 stsp }
5982 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5983 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5984 a129376b 2019-03-28 stsp if (error)
5985 a129376b 2019-03-28 stsp goto done;
5986 a129376b 2019-03-28 stsp
5987 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5988 6d022e97 2019-08-04 stsp if (error)
5989 6d022e97 2019-08-04 stsp goto done;
5990 00db391e 2019-08-03 stsp
5991 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5992 0f6d7415 2019-08-08 stsp char *ondisk_path;
5993 0f6d7415 2019-08-08 stsp struct stat sb;
5994 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5995 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5996 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5997 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5998 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5999 0f6d7415 2019-08-08 stsp goto done;
6000 0f6d7415 2019-08-08 stsp }
6001 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6002 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6003 0f6d7415 2019-08-08 stsp free(ondisk_path);
6004 0f6d7415 2019-08-08 stsp continue;
6005 0f6d7415 2019-08-08 stsp }
6006 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6007 0f6d7415 2019-08-08 stsp ondisk_path);
6008 0f6d7415 2019-08-08 stsp free(ondisk_path);
6009 0f6d7415 2019-08-08 stsp goto done;
6010 0f6d7415 2019-08-08 stsp }
6011 0f6d7415 2019-08-08 stsp free(ondisk_path);
6012 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6013 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6014 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6015 0f6d7415 2019-08-08 stsp goto done;
6016 0f6d7415 2019-08-08 stsp }
6017 0f6d7415 2019-08-08 stsp }
6018 0f6d7415 2019-08-08 stsp }
6019 0f6d7415 2019-08-08 stsp
6020 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6021 33aa809d 2019-08-08 stsp cpa.action = "revert";
6022 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6023 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6024 2ec1f75b 2019-03-26 stsp done:
6025 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6026 33aa809d 2019-08-08 stsp error == NULL)
6027 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6028 2ec1f75b 2019-03-26 stsp if (repo)
6029 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6030 2ec1f75b 2019-03-26 stsp if (worktree)
6031 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6032 2ec1f75b 2019-03-26 stsp free(path);
6033 d00136be 2019-03-26 stsp free(cwd);
6034 6bad629b 2019-02-04 stsp return error;
6035 c4296144 2019-05-09 stsp }
6036 c4296144 2019-05-09 stsp
6037 c4296144 2019-05-09 stsp __dead static void
6038 c4296144 2019-05-09 stsp usage_commit(void)
6039 c4296144 2019-05-09 stsp {
6040 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
6041 5c1e53bc 2019-07-28 stsp getprogname());
6042 c4296144 2019-05-09 stsp exit(1);
6043 33ad4cbe 2019-05-12 jcs }
6044 33ad4cbe 2019-05-12 jcs
6045 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6046 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6047 e2ba3d07 2019-05-13 stsp const char *editor;
6048 e0870e44 2019-05-13 stsp const char *worktree_path;
6049 76d98825 2019-06-03 stsp const char *branch_name;
6050 314a6357 2019-05-13 stsp const char *repo_path;
6051 e0870e44 2019-05-13 stsp char *logmsg_path;
6052 e2ba3d07 2019-05-13 stsp
6053 e2ba3d07 2019-05-13 stsp };
6054 e0870e44 2019-05-13 stsp
6055 33ad4cbe 2019-05-12 jcs static const struct got_error *
6056 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6057 33ad4cbe 2019-05-12 jcs void *arg)
6058 33ad4cbe 2019-05-12 jcs {
6059 76d98825 2019-06-03 stsp char *initial_content = NULL;
6060 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6061 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6062 e0870e44 2019-05-13 stsp char *template = NULL;
6063 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6064 3ce1b845 2019-07-15 stsp int fd;
6065 33ad4cbe 2019-05-12 jcs size_t len;
6066 33ad4cbe 2019-05-12 jcs
6067 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6068 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6069 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6070 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6071 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6072 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6073 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6074 33ad4cbe 2019-05-12 jcs return NULL;
6075 33ad4cbe 2019-05-12 jcs }
6076 33ad4cbe 2019-05-12 jcs
6077 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6078 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6079 76d98825 2019-06-03 stsp
6080 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
6081 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6082 76d98825 2019-06-03 stsp a->branch_name) == -1)
6083 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6084 e0870e44 2019-05-13 stsp
6085 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6086 793c30b5 2019-05-13 stsp if (err)
6087 e0870e44 2019-05-13 stsp goto done;
6088 33ad4cbe 2019-05-12 jcs
6089 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
6090 33ad4cbe 2019-05-12 jcs
6091 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6092 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6093 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6094 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6095 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6096 33ad4cbe 2019-05-12 jcs }
6097 33ad4cbe 2019-05-12 jcs close(fd);
6098 33ad4cbe 2019-05-12 jcs
6099 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6100 33ad4cbe 2019-05-12 jcs done:
6101 76d98825 2019-06-03 stsp free(initial_content);
6102 e0870e44 2019-05-13 stsp free(template);
6103 314a6357 2019-05-13 stsp
6104 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6105 3ce1b845 2019-07-15 stsp if (err == NULL) {
6106 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6107 3ce1b845 2019-07-15 stsp if (err) {
6108 3ce1b845 2019-07-15 stsp free(*logmsg);
6109 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6110 3ce1b845 2019-07-15 stsp }
6111 3ce1b845 2019-07-15 stsp }
6112 33ad4cbe 2019-05-12 jcs return err;
6113 6bad629b 2019-02-04 stsp }
6114 c4296144 2019-05-09 stsp
6115 c4296144 2019-05-09 stsp static const struct got_error *
6116 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6117 c4296144 2019-05-09 stsp {
6118 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6119 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6120 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6121 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6122 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6123 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6124 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6125 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6126 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6127 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6128 5c1e53bc 2019-07-28 stsp
6129 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6130 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6131 c4296144 2019-05-09 stsp
6132 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
6133 c4296144 2019-05-09 stsp switch (ch) {
6134 c4296144 2019-05-09 stsp case 'm':
6135 c4296144 2019-05-09 stsp logmsg = optarg;
6136 c4296144 2019-05-09 stsp break;
6137 c4296144 2019-05-09 stsp default:
6138 c4296144 2019-05-09 stsp usage_commit();
6139 c4296144 2019-05-09 stsp /* NOTREACHED */
6140 c4296144 2019-05-09 stsp }
6141 c4296144 2019-05-09 stsp }
6142 c4296144 2019-05-09 stsp
6143 c4296144 2019-05-09 stsp argc -= optind;
6144 c4296144 2019-05-09 stsp argv += optind;
6145 c4296144 2019-05-09 stsp
6146 43012d58 2019-07-14 stsp #ifndef PROFILE
6147 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6148 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6149 43012d58 2019-07-14 stsp err(1, "pledge");
6150 43012d58 2019-07-14 stsp #endif
6151 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6152 c4296144 2019-05-09 stsp if (cwd == NULL) {
6153 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6154 c4296144 2019-05-09 stsp goto done;
6155 c4296144 2019-05-09 stsp }
6156 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6157 7d5807f4 2019-07-11 stsp if (error)
6158 7d5807f4 2019-07-11 stsp goto done;
6159 7d5807f4 2019-07-11 stsp
6160 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6161 c4296144 2019-05-09 stsp if (error)
6162 a698f62e 2019-07-25 stsp goto done;
6163 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6164 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6165 c4296144 2019-05-09 stsp goto done;
6166 a698f62e 2019-07-25 stsp }
6167 5c1e53bc 2019-07-28 stsp
6168 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6169 916f288c 2019-07-30 stsp worktree);
6170 5c1e53bc 2019-07-28 stsp if (error)
6171 5c1e53bc 2019-07-28 stsp goto done;
6172 c4296144 2019-05-09 stsp
6173 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6174 c9956ddf 2019-09-08 stsp if (error)
6175 c9956ddf 2019-09-08 stsp goto done;
6176 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6177 c9956ddf 2019-09-08 stsp gitconfig_path);
6178 c4296144 2019-05-09 stsp if (error != NULL)
6179 c4296144 2019-05-09 stsp goto done;
6180 c4296144 2019-05-09 stsp
6181 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
6182 aba9c984 2019-09-08 stsp if (error)
6183 aba9c984 2019-09-08 stsp return error;
6184 aba9c984 2019-09-08 stsp
6185 314a6357 2019-05-13 stsp /*
6186 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6187 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6188 314a6357 2019-05-13 stsp */
6189 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6190 314a6357 2019-05-13 stsp error = get_editor(&editor);
6191 314a6357 2019-05-13 stsp else
6192 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6193 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6194 c4296144 2019-05-09 stsp if (error)
6195 c4296144 2019-05-09 stsp goto done;
6196 c4296144 2019-05-09 stsp
6197 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6198 087fb88c 2019-08-04 stsp if (error)
6199 087fb88c 2019-08-04 stsp goto done;
6200 087fb88c 2019-08-04 stsp
6201 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6202 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6203 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6204 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6205 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6206 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6207 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6208 916f288c 2019-07-30 stsp goto done;
6209 916f288c 2019-07-30 stsp }
6210 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6211 916f288c 2019-07-30 stsp }
6212 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6213 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6214 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6215 e0870e44 2019-05-13 stsp if (error) {
6216 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6217 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6218 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6219 c4296144 2019-05-09 stsp goto done;
6220 e0870e44 2019-05-13 stsp }
6221 c4296144 2019-05-09 stsp
6222 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6223 c4296144 2019-05-09 stsp if (error)
6224 c4296144 2019-05-09 stsp goto done;
6225 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6226 c4296144 2019-05-09 stsp done:
6227 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6228 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6229 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6230 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6231 7266f21f 2019-10-21 stsp error == NULL)
6232 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6233 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6234 c4296144 2019-05-09 stsp if (repo)
6235 c4296144 2019-05-09 stsp got_repo_close(repo);
6236 c4296144 2019-05-09 stsp if (worktree)
6237 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6238 c4296144 2019-05-09 stsp free(cwd);
6239 c4296144 2019-05-09 stsp free(id_str);
6240 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6241 e2ba3d07 2019-05-13 stsp free(editor);
6242 aba9c984 2019-09-08 stsp free(author);
6243 234035bc 2019-06-01 stsp return error;
6244 234035bc 2019-06-01 stsp }
6245 234035bc 2019-06-01 stsp
6246 234035bc 2019-06-01 stsp __dead static void
6247 234035bc 2019-06-01 stsp usage_cherrypick(void)
6248 234035bc 2019-06-01 stsp {
6249 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6250 234035bc 2019-06-01 stsp exit(1);
6251 234035bc 2019-06-01 stsp }
6252 234035bc 2019-06-01 stsp
6253 234035bc 2019-06-01 stsp static const struct got_error *
6254 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6255 234035bc 2019-06-01 stsp {
6256 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6257 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6258 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6259 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6260 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6261 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6262 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6263 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6264 234035bc 2019-06-01 stsp int ch, did_something = 0;
6265 234035bc 2019-06-01 stsp
6266 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6267 234035bc 2019-06-01 stsp switch (ch) {
6268 234035bc 2019-06-01 stsp default:
6269 234035bc 2019-06-01 stsp usage_cherrypick();
6270 234035bc 2019-06-01 stsp /* NOTREACHED */
6271 234035bc 2019-06-01 stsp }
6272 234035bc 2019-06-01 stsp }
6273 234035bc 2019-06-01 stsp
6274 234035bc 2019-06-01 stsp argc -= optind;
6275 234035bc 2019-06-01 stsp argv += optind;
6276 234035bc 2019-06-01 stsp
6277 43012d58 2019-07-14 stsp #ifndef PROFILE
6278 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6279 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6280 43012d58 2019-07-14 stsp err(1, "pledge");
6281 43012d58 2019-07-14 stsp #endif
6282 234035bc 2019-06-01 stsp if (argc != 1)
6283 234035bc 2019-06-01 stsp usage_cherrypick();
6284 234035bc 2019-06-01 stsp
6285 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6286 234035bc 2019-06-01 stsp if (cwd == NULL) {
6287 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6288 234035bc 2019-06-01 stsp goto done;
6289 234035bc 2019-06-01 stsp }
6290 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6291 234035bc 2019-06-01 stsp if (error)
6292 234035bc 2019-06-01 stsp goto done;
6293 234035bc 2019-06-01 stsp
6294 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6295 c9956ddf 2019-09-08 stsp NULL);
6296 234035bc 2019-06-01 stsp if (error != NULL)
6297 234035bc 2019-06-01 stsp goto done;
6298 234035bc 2019-06-01 stsp
6299 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6300 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6301 234035bc 2019-06-01 stsp if (error)
6302 234035bc 2019-06-01 stsp goto done;
6303 234035bc 2019-06-01 stsp
6304 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6305 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6306 234035bc 2019-06-01 stsp if (error != NULL) {
6307 234035bc 2019-06-01 stsp struct got_reference *ref;
6308 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6309 234035bc 2019-06-01 stsp goto done;
6310 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6311 234035bc 2019-06-01 stsp if (error != NULL)
6312 234035bc 2019-06-01 stsp goto done;
6313 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6314 234035bc 2019-06-01 stsp got_ref_close(ref);
6315 234035bc 2019-06-01 stsp if (error != NULL)
6316 234035bc 2019-06-01 stsp goto done;
6317 234035bc 2019-06-01 stsp }
6318 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6319 234035bc 2019-06-01 stsp if (error)
6320 234035bc 2019-06-01 stsp goto done;
6321 234035bc 2019-06-01 stsp
6322 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6323 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6324 234035bc 2019-06-01 stsp if (error != NULL)
6325 234035bc 2019-06-01 stsp goto done;
6326 234035bc 2019-06-01 stsp
6327 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6328 234035bc 2019-06-01 stsp if (error) {
6329 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6330 234035bc 2019-06-01 stsp goto done;
6331 234035bc 2019-06-01 stsp error = NULL;
6332 234035bc 2019-06-01 stsp } else {
6333 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6334 234035bc 2019-06-01 stsp goto done;
6335 234035bc 2019-06-01 stsp }
6336 234035bc 2019-06-01 stsp
6337 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6338 234035bc 2019-06-01 stsp if (error)
6339 234035bc 2019-06-01 stsp goto done;
6340 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6341 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6342 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
6343 03415a1a 2019-06-02 stsp NULL);
6344 234035bc 2019-06-01 stsp if (error != NULL)
6345 234035bc 2019-06-01 stsp goto done;
6346 234035bc 2019-06-01 stsp
6347 234035bc 2019-06-01 stsp if (did_something)
6348 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6349 234035bc 2019-06-01 stsp done:
6350 234035bc 2019-06-01 stsp if (commit)
6351 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6352 234035bc 2019-06-01 stsp free(commit_id_str);
6353 234035bc 2019-06-01 stsp if (head_ref)
6354 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6355 234035bc 2019-06-01 stsp if (worktree)
6356 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6357 234035bc 2019-06-01 stsp if (repo)
6358 234035bc 2019-06-01 stsp got_repo_close(repo);
6359 c4296144 2019-05-09 stsp return error;
6360 c4296144 2019-05-09 stsp }
6361 5ef14e63 2019-06-02 stsp
6362 5ef14e63 2019-06-02 stsp __dead static void
6363 5ef14e63 2019-06-02 stsp usage_backout(void)
6364 5ef14e63 2019-06-02 stsp {
6365 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6366 5ef14e63 2019-06-02 stsp exit(1);
6367 5ef14e63 2019-06-02 stsp }
6368 5ef14e63 2019-06-02 stsp
6369 5ef14e63 2019-06-02 stsp static const struct got_error *
6370 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6371 5ef14e63 2019-06-02 stsp {
6372 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6373 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6374 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6375 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6376 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6377 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6378 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6379 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6380 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
6381 5ef14e63 2019-06-02 stsp
6382 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6383 5ef14e63 2019-06-02 stsp switch (ch) {
6384 5ef14e63 2019-06-02 stsp default:
6385 5ef14e63 2019-06-02 stsp usage_backout();
6386 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6387 5ef14e63 2019-06-02 stsp }
6388 5ef14e63 2019-06-02 stsp }
6389 5ef14e63 2019-06-02 stsp
6390 5ef14e63 2019-06-02 stsp argc -= optind;
6391 5ef14e63 2019-06-02 stsp argv += optind;
6392 5ef14e63 2019-06-02 stsp
6393 43012d58 2019-07-14 stsp #ifndef PROFILE
6394 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6395 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6396 43012d58 2019-07-14 stsp err(1, "pledge");
6397 43012d58 2019-07-14 stsp #endif
6398 5ef14e63 2019-06-02 stsp if (argc != 1)
6399 5ef14e63 2019-06-02 stsp usage_backout();
6400 5ef14e63 2019-06-02 stsp
6401 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6402 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6403 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6404 5ef14e63 2019-06-02 stsp goto done;
6405 5ef14e63 2019-06-02 stsp }
6406 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6407 5ef14e63 2019-06-02 stsp if (error)
6408 5ef14e63 2019-06-02 stsp goto done;
6409 5ef14e63 2019-06-02 stsp
6410 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6411 c9956ddf 2019-09-08 stsp NULL);
6412 5ef14e63 2019-06-02 stsp if (error != NULL)
6413 5ef14e63 2019-06-02 stsp goto done;
6414 5ef14e63 2019-06-02 stsp
6415 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6416 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6417 5ef14e63 2019-06-02 stsp if (error)
6418 5ef14e63 2019-06-02 stsp goto done;
6419 5ef14e63 2019-06-02 stsp
6420 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6421 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6422 5ef14e63 2019-06-02 stsp if (error != NULL) {
6423 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6424 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6425 5ef14e63 2019-06-02 stsp goto done;
6426 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6427 5ef14e63 2019-06-02 stsp if (error != NULL)
6428 5ef14e63 2019-06-02 stsp goto done;
6429 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6430 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6431 5ef14e63 2019-06-02 stsp if (error != NULL)
6432 5ef14e63 2019-06-02 stsp goto done;
6433 5ef14e63 2019-06-02 stsp }
6434 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6435 5ef14e63 2019-06-02 stsp if (error)
6436 5ef14e63 2019-06-02 stsp goto done;
6437 5ef14e63 2019-06-02 stsp
6438 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6439 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6440 5ef14e63 2019-06-02 stsp if (error != NULL)
6441 5ef14e63 2019-06-02 stsp goto done;
6442 5ef14e63 2019-06-02 stsp
6443 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6444 5ef14e63 2019-06-02 stsp if (error)
6445 5ef14e63 2019-06-02 stsp goto done;
6446 5ef14e63 2019-06-02 stsp
6447 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6448 5ef14e63 2019-06-02 stsp if (error)
6449 5ef14e63 2019-06-02 stsp goto done;
6450 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6451 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6452 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6453 5ef14e63 2019-06-02 stsp goto done;
6454 5ef14e63 2019-06-02 stsp }
6455 5ef14e63 2019-06-02 stsp
6456 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6457 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6458 5ef14e63 2019-06-02 stsp if (error != NULL)
6459 5ef14e63 2019-06-02 stsp goto done;
6460 5ef14e63 2019-06-02 stsp
6461 5ef14e63 2019-06-02 stsp if (did_something)
6462 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6463 5ef14e63 2019-06-02 stsp done:
6464 5ef14e63 2019-06-02 stsp if (commit)
6465 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6466 5ef14e63 2019-06-02 stsp free(commit_id_str);
6467 5ef14e63 2019-06-02 stsp if (head_ref)
6468 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6469 818c7501 2019-07-11 stsp if (worktree)
6470 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6471 818c7501 2019-07-11 stsp if (repo)
6472 818c7501 2019-07-11 stsp got_repo_close(repo);
6473 818c7501 2019-07-11 stsp return error;
6474 818c7501 2019-07-11 stsp }
6475 818c7501 2019-07-11 stsp
6476 818c7501 2019-07-11 stsp __dead static void
6477 818c7501 2019-07-11 stsp usage_rebase(void)
6478 818c7501 2019-07-11 stsp {
6479 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6480 af54c8f8 2019-07-11 stsp getprogname());
6481 818c7501 2019-07-11 stsp exit(1);
6482 0ebf8283 2019-07-24 stsp }
6483 0ebf8283 2019-07-24 stsp
6484 0ebf8283 2019-07-24 stsp void
6485 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6486 0ebf8283 2019-07-24 stsp {
6487 0ebf8283 2019-07-24 stsp char *nl;
6488 0ebf8283 2019-07-24 stsp size_t len;
6489 0ebf8283 2019-07-24 stsp
6490 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6491 0ebf8283 2019-07-24 stsp if (len > limit)
6492 0ebf8283 2019-07-24 stsp len = limit;
6493 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6494 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6495 0ebf8283 2019-07-24 stsp if (nl)
6496 0ebf8283 2019-07-24 stsp *nl = '\0';
6497 0ebf8283 2019-07-24 stsp }
6498 0ebf8283 2019-07-24 stsp
6499 0ebf8283 2019-07-24 stsp static const struct got_error *
6500 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6501 0ebf8283 2019-07-24 stsp {
6502 5943eee2 2019-08-13 stsp const struct got_error *err;
6503 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6504 5943eee2 2019-08-13 stsp const char *s;
6505 0ebf8283 2019-07-24 stsp
6506 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6507 5943eee2 2019-08-13 stsp if (err)
6508 5943eee2 2019-08-13 stsp return err;
6509 0ebf8283 2019-07-24 stsp
6510 5943eee2 2019-08-13 stsp s = logmsg0;
6511 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6512 5943eee2 2019-08-13 stsp s++;
6513 5943eee2 2019-08-13 stsp
6514 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6515 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6516 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6517 5943eee2 2019-08-13 stsp goto done;
6518 5943eee2 2019-08-13 stsp }
6519 0ebf8283 2019-07-24 stsp
6520 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6521 5943eee2 2019-08-13 stsp done:
6522 5943eee2 2019-08-13 stsp free(logmsg0);
6523 a0ea4fc0 2020-02-28 stsp return err;
6524 a0ea4fc0 2020-02-28 stsp }
6525 a0ea4fc0 2020-02-28 stsp
6526 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6527 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6528 a0ea4fc0 2020-02-28 stsp {
6529 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6530 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6531 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6532 a0ea4fc0 2020-02-28 stsp
6533 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6534 a0ea4fc0 2020-02-28 stsp if (err)
6535 a0ea4fc0 2020-02-28 stsp return err;
6536 a0ea4fc0 2020-02-28 stsp
6537 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6538 a0ea4fc0 2020-02-28 stsp if (err)
6539 a0ea4fc0 2020-02-28 stsp goto done;
6540 a0ea4fc0 2020-02-28 stsp
6541 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6542 a0ea4fc0 2020-02-28 stsp
6543 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6544 a0ea4fc0 2020-02-28 stsp if (err)
6545 a0ea4fc0 2020-02-28 stsp goto done;
6546 a0ea4fc0 2020-02-28 stsp
6547 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6548 a0ea4fc0 2020-02-28 stsp done:
6549 a0ea4fc0 2020-02-28 stsp free(id_str);
6550 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6551 a0ea4fc0 2020-02-28 stsp free(logmsg);
6552 5943eee2 2019-08-13 stsp return err;
6553 818c7501 2019-07-11 stsp }
6554 818c7501 2019-07-11 stsp
6555 818c7501 2019-07-11 stsp static const struct got_error *
6556 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6557 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6558 818c7501 2019-07-11 stsp {
6559 818c7501 2019-07-11 stsp const struct got_error *err;
6560 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6561 818c7501 2019-07-11 stsp
6562 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6563 818c7501 2019-07-11 stsp if (err)
6564 818c7501 2019-07-11 stsp goto done;
6565 818c7501 2019-07-11 stsp
6566 ff0d2220 2019-07-11 stsp if (new_id) {
6567 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6568 ff0d2220 2019-07-11 stsp if (err)
6569 ff0d2220 2019-07-11 stsp goto done;
6570 ff0d2220 2019-07-11 stsp }
6571 818c7501 2019-07-11 stsp
6572 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6573 ff0d2220 2019-07-11 stsp if (new_id_str)
6574 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6575 0ebf8283 2019-07-24 stsp
6576 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6577 0ebf8283 2019-07-24 stsp if (err)
6578 0ebf8283 2019-07-24 stsp goto done;
6579 0ebf8283 2019-07-24 stsp
6580 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6581 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6582 818c7501 2019-07-11 stsp done:
6583 818c7501 2019-07-11 stsp free(old_id_str);
6584 818c7501 2019-07-11 stsp free(new_id_str);
6585 272a1371 2020-02-28 stsp free(logmsg);
6586 818c7501 2019-07-11 stsp return err;
6587 818c7501 2019-07-11 stsp }
6588 818c7501 2019-07-11 stsp
6589 1ee397ad 2019-07-12 stsp static const struct got_error *
6590 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6591 818c7501 2019-07-11 stsp {
6592 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6593 818c7501 2019-07-11 stsp
6594 818c7501 2019-07-11 stsp while (path[0] == '/')
6595 818c7501 2019-07-11 stsp path++;
6596 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6597 818c7501 2019-07-11 stsp
6598 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6599 1ee397ad 2019-07-12 stsp return NULL;
6600 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6601 818c7501 2019-07-11 stsp *rebase_status = status;
6602 1ee397ad 2019-07-12 stsp return NULL;
6603 818c7501 2019-07-11 stsp }
6604 818c7501 2019-07-11 stsp
6605 818c7501 2019-07-11 stsp static const struct got_error *
6606 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6607 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6608 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6609 818c7501 2019-07-11 stsp {
6610 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6611 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6612 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6613 818c7501 2019-07-11 stsp }
6614 818c7501 2019-07-11 stsp
6615 818c7501 2019-07-11 stsp static const struct got_error *
6616 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6617 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6618 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6619 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6620 ff0d2220 2019-07-11 stsp {
6621 ff0d2220 2019-07-11 stsp const struct got_error *error;
6622 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6623 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6624 ff0d2220 2019-07-11 stsp
6625 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6626 ff0d2220 2019-07-11 stsp if (error)
6627 ff0d2220 2019-07-11 stsp return error;
6628 ff0d2220 2019-07-11 stsp
6629 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6630 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6631 ff0d2220 2019-07-11 stsp if (error) {
6632 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6633 ff0d2220 2019-07-11 stsp goto done;
6634 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6635 ff0d2220 2019-07-11 stsp } else {
6636 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6637 ff0d2220 2019-07-11 stsp free(new_commit_id);
6638 ff0d2220 2019-07-11 stsp }
6639 ff0d2220 2019-07-11 stsp done:
6640 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6641 ff0d2220 2019-07-11 stsp return error;
6642 64c6d990 2019-07-11 stsp }
6643 64c6d990 2019-07-11 stsp
6644 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6645 64c6d990 2019-07-11 stsp const char *path_prefix;
6646 64c6d990 2019-07-11 stsp size_t len;
6647 8ca9bd68 2019-07-25 stsp int errcode;
6648 64c6d990 2019-07-11 stsp };
6649 64c6d990 2019-07-11 stsp
6650 64c6d990 2019-07-11 stsp static const struct got_error *
6651 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6652 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6653 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6654 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6655 64c6d990 2019-07-11 stsp {
6656 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6657 64c6d990 2019-07-11 stsp
6658 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6659 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6660 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6661 64c6d990 2019-07-11 stsp
6662 64c6d990 2019-07-11 stsp return NULL;
6663 64c6d990 2019-07-11 stsp }
6664 64c6d990 2019-07-11 stsp
6665 64c6d990 2019-07-11 stsp static const struct got_error *
6666 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6667 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6668 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6669 64c6d990 2019-07-11 stsp {
6670 64c6d990 2019-07-11 stsp const struct got_error *err;
6671 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6672 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6673 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6674 64c6d990 2019-07-11 stsp
6675 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6676 64c6d990 2019-07-11 stsp return NULL;
6677 64c6d990 2019-07-11 stsp
6678 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6679 64c6d990 2019-07-11 stsp if (err)
6680 64c6d990 2019-07-11 stsp goto done;
6681 64c6d990 2019-07-11 stsp
6682 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6683 64c6d990 2019-07-11 stsp if (err)
6684 64c6d990 2019-07-11 stsp goto done;
6685 64c6d990 2019-07-11 stsp
6686 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6687 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6688 64c6d990 2019-07-11 stsp if (err)
6689 64c6d990 2019-07-11 stsp goto done;
6690 64c6d990 2019-07-11 stsp
6691 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6692 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6693 64c6d990 2019-07-11 stsp if (err)
6694 64c6d990 2019-07-11 stsp goto done;
6695 64c6d990 2019-07-11 stsp
6696 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6697 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6698 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6699 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6700 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6701 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6702 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6703 64c6d990 2019-07-11 stsp done:
6704 64c6d990 2019-07-11 stsp if (tree1)
6705 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6706 64c6d990 2019-07-11 stsp if (tree2)
6707 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6708 64c6d990 2019-07-11 stsp if (commit)
6709 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6710 64c6d990 2019-07-11 stsp if (parent_commit)
6711 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6712 64c6d990 2019-07-11 stsp return err;
6713 ff0d2220 2019-07-11 stsp }
6714 ff0d2220 2019-07-11 stsp
6715 ff0d2220 2019-07-11 stsp static const struct got_error *
6716 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6717 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6718 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6719 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6720 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6721 af61c510 2019-07-19 stsp {
6722 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6723 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6724 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6725 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6726 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6727 af61c510 2019-07-19 stsp
6728 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6729 af61c510 2019-07-19 stsp if (err)
6730 af61c510 2019-07-19 stsp return err;
6731 af61c510 2019-07-19 stsp
6732 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6733 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6734 af61c510 2019-07-19 stsp if (err)
6735 af61c510 2019-07-19 stsp goto done;
6736 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6737 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6738 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6739 af61c510 2019-07-19 stsp if (err) {
6740 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6741 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6742 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6743 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6744 af61c510 2019-07-19 stsp "been reached?!?");
6745 ee780d5c 2020-01-04 stsp }
6746 ee780d5c 2020-01-04 stsp goto done;
6747 af61c510 2019-07-19 stsp } else {
6748 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6749 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6750 af61c510 2019-07-19 stsp if (err)
6751 af61c510 2019-07-19 stsp goto done;
6752 af61c510 2019-07-19 stsp
6753 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6754 af61c510 2019-07-19 stsp if (err)
6755 af61c510 2019-07-19 stsp goto done;
6756 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6757 af61c510 2019-07-19 stsp commit_id = parent_id;
6758 af61c510 2019-07-19 stsp }
6759 af61c510 2019-07-19 stsp }
6760 af61c510 2019-07-19 stsp done:
6761 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6762 af61c510 2019-07-19 stsp return err;
6763 af61c510 2019-07-19 stsp }
6764 af61c510 2019-07-19 stsp
6765 af61c510 2019-07-19 stsp static const struct got_error *
6766 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6767 818c7501 2019-07-11 stsp {
6768 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6769 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6770 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6771 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6772 818c7501 2019-07-11 stsp char *cwd = NULL;
6773 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6774 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6775 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6776 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6777 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6778 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6779 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6780 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6781 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6782 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6783 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6784 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6785 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6786 818c7501 2019-07-11 stsp
6787 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6788 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6789 818c7501 2019-07-11 stsp
6790 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6791 818c7501 2019-07-11 stsp switch (ch) {
6792 818c7501 2019-07-11 stsp case 'a':
6793 818c7501 2019-07-11 stsp abort_rebase = 1;
6794 818c7501 2019-07-11 stsp break;
6795 818c7501 2019-07-11 stsp case 'c':
6796 818c7501 2019-07-11 stsp continue_rebase = 1;
6797 818c7501 2019-07-11 stsp break;
6798 818c7501 2019-07-11 stsp default:
6799 818c7501 2019-07-11 stsp usage_rebase();
6800 818c7501 2019-07-11 stsp /* NOTREACHED */
6801 818c7501 2019-07-11 stsp }
6802 818c7501 2019-07-11 stsp }
6803 818c7501 2019-07-11 stsp
6804 818c7501 2019-07-11 stsp argc -= optind;
6805 818c7501 2019-07-11 stsp argv += optind;
6806 818c7501 2019-07-11 stsp
6807 43012d58 2019-07-14 stsp #ifndef PROFILE
6808 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6809 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6810 43012d58 2019-07-14 stsp err(1, "pledge");
6811 43012d58 2019-07-14 stsp #endif
6812 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6813 818c7501 2019-07-11 stsp usage_rebase();
6814 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6815 818c7501 2019-07-11 stsp if (argc != 0)
6816 818c7501 2019-07-11 stsp usage_rebase();
6817 818c7501 2019-07-11 stsp } else if (argc != 1)
6818 818c7501 2019-07-11 stsp usage_rebase();
6819 818c7501 2019-07-11 stsp
6820 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6821 818c7501 2019-07-11 stsp if (cwd == NULL) {
6822 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6823 818c7501 2019-07-11 stsp goto done;
6824 818c7501 2019-07-11 stsp }
6825 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6826 818c7501 2019-07-11 stsp if (error)
6827 818c7501 2019-07-11 stsp goto done;
6828 818c7501 2019-07-11 stsp
6829 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6830 c9956ddf 2019-09-08 stsp NULL);
6831 818c7501 2019-07-11 stsp if (error != NULL)
6832 818c7501 2019-07-11 stsp goto done;
6833 818c7501 2019-07-11 stsp
6834 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6835 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6836 7ef62c4e 2020-02-24 stsp if (error)
6837 7ef62c4e 2020-02-24 stsp goto done;
6838 7ef62c4e 2020-02-24 stsp
6839 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6840 7ef62c4e 2020-02-24 stsp worktree);
6841 818c7501 2019-07-11 stsp if (error)
6842 7ef62c4e 2020-02-24 stsp goto done;
6843 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6844 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6845 818c7501 2019-07-11 stsp goto done;
6846 7ef62c4e 2020-02-24 stsp }
6847 818c7501 2019-07-11 stsp
6848 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6849 818c7501 2019-07-11 stsp if (error)
6850 818c7501 2019-07-11 stsp goto done;
6851 818c7501 2019-07-11 stsp
6852 f6794adc 2019-07-23 stsp if (abort_rebase) {
6853 818c7501 2019-07-11 stsp int did_something;
6854 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6855 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6856 f6794adc 2019-07-23 stsp goto done;
6857 f6794adc 2019-07-23 stsp }
6858 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6859 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6860 3e3a69f1 2019-07-25 stsp worktree, repo);
6861 818c7501 2019-07-11 stsp if (error)
6862 818c7501 2019-07-11 stsp goto done;
6863 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6864 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6865 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6866 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6867 818c7501 2019-07-11 stsp if (error)
6868 818c7501 2019-07-11 stsp goto done;
6869 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6870 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6871 818c7501 2019-07-11 stsp }
6872 818c7501 2019-07-11 stsp
6873 818c7501 2019-07-11 stsp if (continue_rebase) {
6874 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6875 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6876 f6794adc 2019-07-23 stsp goto done;
6877 f6794adc 2019-07-23 stsp }
6878 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6879 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6880 3e3a69f1 2019-07-25 stsp worktree, repo);
6881 818c7501 2019-07-11 stsp if (error)
6882 818c7501 2019-07-11 stsp goto done;
6883 818c7501 2019-07-11 stsp
6884 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6885 01757395 2019-07-12 stsp resume_commit_id, repo);
6886 818c7501 2019-07-11 stsp if (error)
6887 818c7501 2019-07-11 stsp goto done;
6888 818c7501 2019-07-11 stsp
6889 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6890 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6891 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6892 818c7501 2019-07-11 stsp goto done;
6893 818c7501 2019-07-11 stsp }
6894 818c7501 2019-07-11 stsp } else {
6895 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6896 818c7501 2019-07-11 stsp if (error != NULL)
6897 818c7501 2019-07-11 stsp goto done;
6898 ff0d2220 2019-07-11 stsp }
6899 818c7501 2019-07-11 stsp
6900 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6901 ff0d2220 2019-07-11 stsp if (error)
6902 ff0d2220 2019-07-11 stsp goto done;
6903 ff0d2220 2019-07-11 stsp
6904 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6905 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6906 a51a74b3 2019-07-27 stsp
6907 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6908 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6909 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6910 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6911 818c7501 2019-07-11 stsp if (error)
6912 818c7501 2019-07-11 stsp goto done;
6913 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6914 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6915 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6916 818c7501 2019-07-11 stsp "with work tree's branch");
6917 818c7501 2019-07-11 stsp goto done;
6918 818c7501 2019-07-11 stsp }
6919 818c7501 2019-07-11 stsp
6920 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6921 a51a74b3 2019-07-27 stsp if (error) {
6922 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6923 a51a74b3 2019-07-27 stsp goto done;
6924 a51a74b3 2019-07-27 stsp error = NULL;
6925 a51a74b3 2019-07-27 stsp } else {
6926 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6927 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6928 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6929 a51a74b3 2019-07-27 stsp goto done;
6930 a51a74b3 2019-07-27 stsp }
6931 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6932 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6933 818c7501 2019-07-11 stsp if (error)
6934 818c7501 2019-07-11 stsp goto done;
6935 818c7501 2019-07-11 stsp }
6936 818c7501 2019-07-11 stsp
6937 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6938 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6939 818c7501 2019-07-11 stsp if (error)
6940 818c7501 2019-07-11 stsp goto done;
6941 818c7501 2019-07-11 stsp
6942 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6943 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6944 fc66b545 2019-08-12 stsp if (pid == NULL) {
6945 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6946 fc66b545 2019-08-12 stsp int did_something;
6947 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6948 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6949 fc66b545 2019-08-12 stsp &did_something);
6950 fc66b545 2019-08-12 stsp if (error)
6951 fc66b545 2019-08-12 stsp goto done;
6952 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6953 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6954 fc66b545 2019-08-12 stsp }
6955 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6956 fc66b545 2019-08-12 stsp goto done;
6957 fc66b545 2019-08-12 stsp }
6958 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6959 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6960 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6961 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6962 818c7501 2019-07-11 stsp commit = NULL;
6963 818c7501 2019-07-11 stsp if (error)
6964 818c7501 2019-07-11 stsp goto done;
6965 64c6d990 2019-07-11 stsp
6966 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6967 38b0338b 2019-11-29 stsp if (continue_rebase) {
6968 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6969 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6970 38b0338b 2019-11-29 stsp goto done;
6971 38b0338b 2019-11-29 stsp } else {
6972 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6973 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6974 38b0338b 2019-11-29 stsp char *id_str;
6975 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6976 38b0338b 2019-11-29 stsp new_base_branch);
6977 38b0338b 2019-11-29 stsp if (error)
6978 38b0338b 2019-11-29 stsp goto done;
6979 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6980 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6981 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6982 38b0338b 2019-11-29 stsp free(id_str);
6983 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6984 38b0338b 2019-11-29 stsp new_head_commit_id);
6985 38b0338b 2019-11-29 stsp if (error)
6986 38b0338b 2019-11-29 stsp goto done;
6987 38b0338b 2019-11-29 stsp }
6988 818c7501 2019-07-11 stsp }
6989 818c7501 2019-07-11 stsp
6990 818c7501 2019-07-11 stsp pid = NULL;
6991 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6992 818c7501 2019-07-11 stsp commit_id = qid->id;
6993 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6994 818c7501 2019-07-11 stsp pid = qid;
6995 818c7501 2019-07-11 stsp
6996 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6997 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6998 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6999 818c7501 2019-07-11 stsp if (error)
7000 818c7501 2019-07-11 stsp goto done;
7001 818c7501 2019-07-11 stsp
7002 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7003 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7004 a0ea4fc0 2020-02-28 stsp if (error)
7005 a0ea4fc0 2020-02-28 stsp goto done;
7006 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7007 818c7501 2019-07-11 stsp break;
7008 01757395 2019-07-12 stsp }
7009 818c7501 2019-07-11 stsp
7010 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7011 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7012 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7013 818c7501 2019-07-11 stsp if (error)
7014 818c7501 2019-07-11 stsp goto done;
7015 818c7501 2019-07-11 stsp }
7016 818c7501 2019-07-11 stsp
7017 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7018 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7019 818c7501 2019-07-11 stsp if (error)
7020 818c7501 2019-07-11 stsp goto done;
7021 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7022 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7023 818c7501 2019-07-11 stsp } else
7024 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7025 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7026 818c7501 2019-07-11 stsp done:
7027 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7028 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7029 818c7501 2019-07-11 stsp free(resume_commit_id);
7030 818c7501 2019-07-11 stsp free(yca_id);
7031 818c7501 2019-07-11 stsp if (commit)
7032 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7033 818c7501 2019-07-11 stsp if (branch)
7034 818c7501 2019-07-11 stsp got_ref_close(branch);
7035 818c7501 2019-07-11 stsp if (new_base_branch)
7036 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7037 818c7501 2019-07-11 stsp if (tmp_branch)
7038 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7039 5ef14e63 2019-06-02 stsp if (worktree)
7040 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7041 5ef14e63 2019-06-02 stsp if (repo)
7042 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7043 5ef14e63 2019-06-02 stsp return error;
7044 0ebf8283 2019-07-24 stsp }
7045 0ebf8283 2019-07-24 stsp
7046 0ebf8283 2019-07-24 stsp __dead static void
7047 0ebf8283 2019-07-24 stsp usage_histedit(void)
7048 0ebf8283 2019-07-24 stsp {
7049 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7050 0ebf8283 2019-07-24 stsp getprogname());
7051 0ebf8283 2019-07-24 stsp exit(1);
7052 0ebf8283 2019-07-24 stsp }
7053 0ebf8283 2019-07-24 stsp
7054 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7055 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7056 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7057 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7058 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7059 0ebf8283 2019-07-24 stsp
7060 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7061 0ebf8283 2019-07-24 stsp unsigned char code;
7062 0ebf8283 2019-07-24 stsp const char *name;
7063 0ebf8283 2019-07-24 stsp const char *desc;
7064 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7065 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7066 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7067 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7068 82997472 2020-01-29 stsp "be used" },
7069 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7070 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7071 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7072 0ebf8283 2019-07-24 stsp };
7073 0ebf8283 2019-07-24 stsp
7074 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7075 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7076 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7077 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7078 0ebf8283 2019-07-24 stsp char *logmsg;
7079 0ebf8283 2019-07-24 stsp };
7080 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7081 0ebf8283 2019-07-24 stsp
7082 0ebf8283 2019-07-24 stsp static const struct got_error *
7083 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7084 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7085 0ebf8283 2019-07-24 stsp {
7086 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7087 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7088 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7089 8138f3e1 2019-08-11 stsp int n;
7090 0ebf8283 2019-07-24 stsp
7091 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7092 0ebf8283 2019-07-24 stsp if (err)
7093 0ebf8283 2019-07-24 stsp goto done;
7094 0ebf8283 2019-07-24 stsp
7095 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7096 0ebf8283 2019-07-24 stsp if (err)
7097 0ebf8283 2019-07-24 stsp goto done;
7098 0ebf8283 2019-07-24 stsp
7099 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7100 0ebf8283 2019-07-24 stsp if (err)
7101 0ebf8283 2019-07-24 stsp goto done;
7102 0ebf8283 2019-07-24 stsp
7103 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7104 0ebf8283 2019-07-24 stsp if (n < 0)
7105 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7106 0ebf8283 2019-07-24 stsp done:
7107 0ebf8283 2019-07-24 stsp if (commit)
7108 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7109 0ebf8283 2019-07-24 stsp free(id_str);
7110 0ebf8283 2019-07-24 stsp free(logmsg);
7111 0ebf8283 2019-07-24 stsp return err;
7112 0ebf8283 2019-07-24 stsp }
7113 0ebf8283 2019-07-24 stsp
7114 0ebf8283 2019-07-24 stsp static const struct got_error *
7115 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7116 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7117 0ebf8283 2019-07-24 stsp {
7118 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7119 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7120 0ebf8283 2019-07-24 stsp
7121 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7122 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7123 0ebf8283 2019-07-24 stsp
7124 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7125 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7126 0ebf8283 2019-07-24 stsp f, repo);
7127 0ebf8283 2019-07-24 stsp if (err)
7128 0ebf8283 2019-07-24 stsp break;
7129 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7130 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7131 083957f4 2020-02-24 stsp if (n < 0) {
7132 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7133 083957f4 2020-02-24 stsp break;
7134 083957f4 2020-02-24 stsp }
7135 083957f4 2020-02-24 stsp }
7136 0ebf8283 2019-07-24 stsp }
7137 0ebf8283 2019-07-24 stsp
7138 0ebf8283 2019-07-24 stsp return err;
7139 0ebf8283 2019-07-24 stsp }
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp static const struct got_error *
7142 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7143 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7144 0ebf8283 2019-07-24 stsp {
7145 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7146 0ebf8283 2019-07-24 stsp int n, i;
7147 514f2ffe 2020-01-29 stsp char *id_str;
7148 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7149 514f2ffe 2020-01-29 stsp
7150 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7151 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7152 514f2ffe 2020-01-29 stsp if (err)
7153 514f2ffe 2020-01-29 stsp return err;
7154 514f2ffe 2020-01-29 stsp
7155 514f2ffe 2020-01-29 stsp n = fprintf(f,
7156 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7157 514f2ffe 2020-01-29 stsp "# commit %s\n"
7158 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7159 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7160 514f2ffe 2020-01-29 stsp if (n < 0) {
7161 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7162 514f2ffe 2020-01-29 stsp goto done;
7163 514f2ffe 2020-01-29 stsp }
7164 0ebf8283 2019-07-24 stsp
7165 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7166 514f2ffe 2020-01-29 stsp if (n < 0) {
7167 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7168 514f2ffe 2020-01-29 stsp goto done;
7169 514f2ffe 2020-01-29 stsp }
7170 0ebf8283 2019-07-24 stsp
7171 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7172 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7173 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7174 0ebf8283 2019-07-24 stsp cmd->desc);
7175 0ebf8283 2019-07-24 stsp if (n < 0) {
7176 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7177 0ebf8283 2019-07-24 stsp break;
7178 0ebf8283 2019-07-24 stsp }
7179 0ebf8283 2019-07-24 stsp }
7180 514f2ffe 2020-01-29 stsp done:
7181 514f2ffe 2020-01-29 stsp free(id_str);
7182 0ebf8283 2019-07-24 stsp return err;
7183 0ebf8283 2019-07-24 stsp }
7184 0ebf8283 2019-07-24 stsp
7185 0ebf8283 2019-07-24 stsp static const struct got_error *
7186 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7187 0ebf8283 2019-07-24 stsp {
7188 0ebf8283 2019-07-24 stsp static char msg[42];
7189 0ebf8283 2019-07-24 stsp int ret;
7190 0ebf8283 2019-07-24 stsp
7191 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7192 0ebf8283 2019-07-24 stsp lineno);
7193 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7194 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7195 0ebf8283 2019-07-24 stsp
7196 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7197 0ebf8283 2019-07-24 stsp }
7198 0ebf8283 2019-07-24 stsp
7199 0ebf8283 2019-07-24 stsp static const struct got_error *
7200 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7201 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7202 0ebf8283 2019-07-24 stsp {
7203 0ebf8283 2019-07-24 stsp const struct got_error *err;
7204 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7205 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7206 0ebf8283 2019-07-24 stsp
7207 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7208 0ebf8283 2019-07-24 stsp if (err)
7209 0ebf8283 2019-07-24 stsp return err;
7210 0ebf8283 2019-07-24 stsp
7211 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7212 0ebf8283 2019-07-24 stsp if (err)
7213 0ebf8283 2019-07-24 stsp goto done;
7214 0ebf8283 2019-07-24 stsp
7215 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7216 5943eee2 2019-08-13 stsp if (err)
7217 5943eee2 2019-08-13 stsp goto done;
7218 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7219 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7220 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7221 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7222 0ebf8283 2019-07-24 stsp }
7223 0ebf8283 2019-07-24 stsp done:
7224 0ebf8283 2019-07-24 stsp if (folded_commit)
7225 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7226 0ebf8283 2019-07-24 stsp free(id_str);
7227 5943eee2 2019-08-13 stsp free(folded_logmsg);
7228 0ebf8283 2019-07-24 stsp return err;
7229 0ebf8283 2019-07-24 stsp }
7230 0ebf8283 2019-07-24 stsp
7231 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7232 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7233 0ebf8283 2019-07-24 stsp {
7234 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7235 0ebf8283 2019-07-24 stsp
7236 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7237 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7238 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7239 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7240 3f9de99f 2019-07-24 stsp folded = prev;
7241 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7242 0ebf8283 2019-07-24 stsp }
7243 0ebf8283 2019-07-24 stsp
7244 0ebf8283 2019-07-24 stsp return folded;
7245 0ebf8283 2019-07-24 stsp }
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp static const struct got_error *
7248 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7249 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7250 0ebf8283 2019-07-24 stsp {
7251 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7252 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7253 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7254 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7255 0ebf8283 2019-07-24 stsp int fd;
7256 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7257 0ebf8283 2019-07-24 stsp
7258 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7259 0ebf8283 2019-07-24 stsp if (err)
7260 0ebf8283 2019-07-24 stsp return err;
7261 0ebf8283 2019-07-24 stsp
7262 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7263 0ebf8283 2019-07-24 stsp if (folded) {
7264 0ebf8283 2019-07-24 stsp while (folded != hle) {
7265 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7266 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7267 3f9de99f 2019-07-24 stsp continue;
7268 3f9de99f 2019-07-24 stsp }
7269 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7270 0ebf8283 2019-07-24 stsp logmsg, repo);
7271 0ebf8283 2019-07-24 stsp if (err)
7272 0ebf8283 2019-07-24 stsp goto done;
7273 0ebf8283 2019-07-24 stsp free(logmsg);
7274 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7275 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7276 0ebf8283 2019-07-24 stsp }
7277 0ebf8283 2019-07-24 stsp }
7278 0ebf8283 2019-07-24 stsp
7279 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7280 0ebf8283 2019-07-24 stsp if (err)
7281 0ebf8283 2019-07-24 stsp goto done;
7282 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7283 5943eee2 2019-08-13 stsp if (err)
7284 5943eee2 2019-08-13 stsp goto done;
7285 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7286 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7287 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7288 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7289 0ebf8283 2019-07-24 stsp goto done;
7290 0ebf8283 2019-07-24 stsp }
7291 0ebf8283 2019-07-24 stsp free(logmsg);
7292 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7293 0ebf8283 2019-07-24 stsp
7294 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7295 0ebf8283 2019-07-24 stsp if (err)
7296 0ebf8283 2019-07-24 stsp goto done;
7297 0ebf8283 2019-07-24 stsp
7298 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7299 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7300 0ebf8283 2019-07-24 stsp if (err)
7301 0ebf8283 2019-07-24 stsp goto done;
7302 0ebf8283 2019-07-24 stsp
7303 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7304 0ebf8283 2019-07-24 stsp close(fd);
7305 0ebf8283 2019-07-24 stsp
7306 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7307 0ebf8283 2019-07-24 stsp if (err)
7308 0ebf8283 2019-07-24 stsp goto done;
7309 0ebf8283 2019-07-24 stsp
7310 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7311 0ebf8283 2019-07-24 stsp if (err) {
7312 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7313 0ebf8283 2019-07-24 stsp goto done;
7314 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7315 0ebf8283 2019-07-24 stsp }
7316 0ebf8283 2019-07-24 stsp done:
7317 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7318 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7319 0ebf8283 2019-07-24 stsp free(logmsg_path);
7320 0ebf8283 2019-07-24 stsp free(logmsg);
7321 5943eee2 2019-08-13 stsp free(orig_logmsg);
7322 0ebf8283 2019-07-24 stsp free(editor);
7323 0ebf8283 2019-07-24 stsp if (commit)
7324 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7325 0ebf8283 2019-07-24 stsp return err;
7326 5ef14e63 2019-06-02 stsp }
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp static const struct got_error *
7329 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7330 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7331 0ebf8283 2019-07-24 stsp {
7332 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7333 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7334 0ebf8283 2019-07-24 stsp size_t size;
7335 0ebf8283 2019-07-24 stsp ssize_t len;
7336 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7337 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7338 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7339 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7340 0ebf8283 2019-07-24 stsp
7341 0ebf8283 2019-07-24 stsp for (;;) {
7342 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7343 0ebf8283 2019-07-24 stsp if (len == -1) {
7344 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7345 0ebf8283 2019-07-24 stsp if (feof(f))
7346 0ebf8283 2019-07-24 stsp break;
7347 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7348 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7349 0ebf8283 2019-07-24 stsp break;
7350 0ebf8283 2019-07-24 stsp }
7351 0ebf8283 2019-07-24 stsp lineno++;
7352 0ebf8283 2019-07-24 stsp p = line;
7353 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7354 0ebf8283 2019-07-24 stsp p++;
7355 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7356 0ebf8283 2019-07-24 stsp free(line);
7357 0ebf8283 2019-07-24 stsp line = NULL;
7358 0ebf8283 2019-07-24 stsp continue;
7359 0ebf8283 2019-07-24 stsp }
7360 0ebf8283 2019-07-24 stsp cmd = NULL;
7361 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7362 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7363 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7364 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7365 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7366 0ebf8283 2019-07-24 stsp break;
7367 0ebf8283 2019-07-24 stsp }
7368 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7369 0ebf8283 2019-07-24 stsp p++;
7370 0ebf8283 2019-07-24 stsp break;
7371 0ebf8283 2019-07-24 stsp }
7372 0ebf8283 2019-07-24 stsp }
7373 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7374 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7375 0ebf8283 2019-07-24 stsp break;
7376 0ebf8283 2019-07-24 stsp }
7377 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7378 0ebf8283 2019-07-24 stsp p++;
7379 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7380 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7381 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7382 0ebf8283 2019-07-24 stsp break;
7383 0ebf8283 2019-07-24 stsp }
7384 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7385 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7386 0ebf8283 2019-07-24 stsp if (err)
7387 0ebf8283 2019-07-24 stsp break;
7388 0ebf8283 2019-07-24 stsp } else {
7389 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7390 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7391 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7392 0ebf8283 2019-07-24 stsp break;
7393 0ebf8283 2019-07-24 stsp }
7394 0ebf8283 2019-07-24 stsp }
7395 0ebf8283 2019-07-24 stsp free(line);
7396 0ebf8283 2019-07-24 stsp line = NULL;
7397 0ebf8283 2019-07-24 stsp continue;
7398 0ebf8283 2019-07-24 stsp } else {
7399 0ebf8283 2019-07-24 stsp end = p;
7400 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7401 0ebf8283 2019-07-24 stsp end++;
7402 0ebf8283 2019-07-24 stsp *end = '\0';
7403 0ebf8283 2019-07-24 stsp
7404 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7405 0ebf8283 2019-07-24 stsp if (err) {
7406 0ebf8283 2019-07-24 stsp /* override error code */
7407 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7408 0ebf8283 2019-07-24 stsp break;
7409 0ebf8283 2019-07-24 stsp }
7410 0ebf8283 2019-07-24 stsp }
7411 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7412 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7413 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7414 0ebf8283 2019-07-24 stsp break;
7415 0ebf8283 2019-07-24 stsp }
7416 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7417 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7418 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7419 0ebf8283 2019-07-24 stsp commit_id = NULL;
7420 0ebf8283 2019-07-24 stsp free(line);
7421 0ebf8283 2019-07-24 stsp line = NULL;
7422 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7423 0ebf8283 2019-07-24 stsp }
7424 0ebf8283 2019-07-24 stsp
7425 0ebf8283 2019-07-24 stsp free(line);
7426 0ebf8283 2019-07-24 stsp free(commit_id);
7427 0ebf8283 2019-07-24 stsp return err;
7428 0ebf8283 2019-07-24 stsp }
7429 0ebf8283 2019-07-24 stsp
7430 0ebf8283 2019-07-24 stsp static const struct got_error *
7431 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7432 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7433 bfce7f83 2019-07-27 stsp {
7434 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7435 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7436 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7437 5b87815e 2020-03-05 stsp static char msg[92];
7438 bfce7f83 2019-07-27 stsp char *id_str;
7439 bfce7f83 2019-07-27 stsp
7440 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7441 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7442 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7443 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7444 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7445 5b87815e 2020-03-05 stsp
7446 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7447 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7448 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7449 5b87815e 2020-03-05 stsp if (hle == hle2)
7450 5b87815e 2020-03-05 stsp continue;
7451 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7452 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7453 5b87815e 2020-03-05 stsp continue;
7454 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7455 5b87815e 2020-03-05 stsp if (err)
7456 5b87815e 2020-03-05 stsp return err;
7457 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7458 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7459 5b87815e 2020-03-05 stsp free(id_str);
7460 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7461 5b87815e 2020-03-05 stsp }
7462 5b87815e 2020-03-05 stsp }
7463 bfce7f83 2019-07-27 stsp
7464 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7465 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7466 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7467 bfce7f83 2019-07-27 stsp break;
7468 bfce7f83 2019-07-27 stsp }
7469 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7470 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7471 bfce7f83 2019-07-27 stsp if (err)
7472 bfce7f83 2019-07-27 stsp return err;
7473 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7474 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7475 bfce7f83 2019-07-27 stsp free(id_str);
7476 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7477 bfce7f83 2019-07-27 stsp }
7478 bfce7f83 2019-07-27 stsp }
7479 bfce7f83 2019-07-27 stsp
7480 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7481 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7482 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7483 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7484 bfce7f83 2019-07-27 stsp
7485 bfce7f83 2019-07-27 stsp return NULL;
7486 bfce7f83 2019-07-27 stsp }
7487 bfce7f83 2019-07-27 stsp
7488 bfce7f83 2019-07-27 stsp static const struct got_error *
7489 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7490 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7491 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7492 0ebf8283 2019-07-24 stsp {
7493 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7494 0ebf8283 2019-07-24 stsp char *editor;
7495 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7496 0ebf8283 2019-07-24 stsp
7497 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7498 0ebf8283 2019-07-24 stsp if (err)
7499 0ebf8283 2019-07-24 stsp return err;
7500 0ebf8283 2019-07-24 stsp
7501 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7502 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7503 0ebf8283 2019-07-24 stsp goto done;
7504 0ebf8283 2019-07-24 stsp }
7505 0ebf8283 2019-07-24 stsp
7506 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7507 0ebf8283 2019-07-24 stsp if (f == NULL) {
7508 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7509 0ebf8283 2019-07-24 stsp goto done;
7510 0ebf8283 2019-07-24 stsp }
7511 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7512 bfce7f83 2019-07-27 stsp if (err)
7513 bfce7f83 2019-07-27 stsp goto done;
7514 bfce7f83 2019-07-27 stsp
7515 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7516 0ebf8283 2019-07-24 stsp done:
7517 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7518 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7519 0ebf8283 2019-07-24 stsp free(editor);
7520 0ebf8283 2019-07-24 stsp return err;
7521 0ebf8283 2019-07-24 stsp }
7522 0ebf8283 2019-07-24 stsp
7523 0ebf8283 2019-07-24 stsp static const struct got_error *
7524 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7525 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7526 514f2ffe 2020-01-29 stsp struct got_repository *);
7527 0ebf8283 2019-07-24 stsp
7528 0ebf8283 2019-07-24 stsp static const struct got_error *
7529 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7530 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7531 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7532 0ebf8283 2019-07-24 stsp {
7533 0ebf8283 2019-07-24 stsp const struct got_error *err;
7534 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7535 0ebf8283 2019-07-24 stsp char *path = NULL;
7536 0ebf8283 2019-07-24 stsp
7537 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7538 0ebf8283 2019-07-24 stsp if (err)
7539 0ebf8283 2019-07-24 stsp return err;
7540 0ebf8283 2019-07-24 stsp
7541 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7542 0ebf8283 2019-07-24 stsp if (err)
7543 0ebf8283 2019-07-24 stsp goto done;
7544 0ebf8283 2019-07-24 stsp
7545 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7546 0ebf8283 2019-07-24 stsp if (err)
7547 0ebf8283 2019-07-24 stsp goto done;
7548 0ebf8283 2019-07-24 stsp
7549 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7550 083957f4 2020-02-24 stsp rewind(f);
7551 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7552 083957f4 2020-02-24 stsp } else {
7553 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7554 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7555 0ebf8283 2019-07-24 stsp goto done;
7556 083957f4 2020-02-24 stsp }
7557 083957f4 2020-02-24 stsp f = NULL;
7558 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7559 083957f4 2020-02-24 stsp if (err) {
7560 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7561 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7562 083957f4 2020-02-24 stsp goto done;
7563 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7564 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7565 083957f4 2020-02-24 stsp }
7566 0ebf8283 2019-07-24 stsp }
7567 0ebf8283 2019-07-24 stsp done:
7568 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7569 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7570 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7571 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7572 0ebf8283 2019-07-24 stsp free(path);
7573 0ebf8283 2019-07-24 stsp return err;
7574 0ebf8283 2019-07-24 stsp }
7575 0ebf8283 2019-07-24 stsp
7576 0ebf8283 2019-07-24 stsp static const struct got_error *
7577 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7578 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7579 0ebf8283 2019-07-24 stsp {
7580 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7581 0ebf8283 2019-07-24 stsp char *path = NULL;
7582 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7583 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7584 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7585 0ebf8283 2019-07-24 stsp
7586 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7587 0ebf8283 2019-07-24 stsp if (err)
7588 0ebf8283 2019-07-24 stsp return err;
7589 0ebf8283 2019-07-24 stsp
7590 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7591 0ebf8283 2019-07-24 stsp if (f == NULL) {
7592 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7593 0ebf8283 2019-07-24 stsp goto done;
7594 0ebf8283 2019-07-24 stsp }
7595 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7596 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7597 0ebf8283 2019-07-24 stsp repo);
7598 0ebf8283 2019-07-24 stsp if (err)
7599 0ebf8283 2019-07-24 stsp break;
7600 0ebf8283 2019-07-24 stsp
7601 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7602 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7603 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7604 0ebf8283 2019-07-24 stsp if (n < 0) {
7605 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7606 0ebf8283 2019-07-24 stsp break;
7607 0ebf8283 2019-07-24 stsp }
7608 0ebf8283 2019-07-24 stsp }
7609 0ebf8283 2019-07-24 stsp }
7610 0ebf8283 2019-07-24 stsp done:
7611 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7612 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7613 0ebf8283 2019-07-24 stsp free(path);
7614 0ebf8283 2019-07-24 stsp if (commit)
7615 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7616 0ebf8283 2019-07-24 stsp return err;
7617 0ebf8283 2019-07-24 stsp }
7618 0ebf8283 2019-07-24 stsp
7619 bfce7f83 2019-07-27 stsp void
7620 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7621 bfce7f83 2019-07-27 stsp {
7622 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7623 bfce7f83 2019-07-27 stsp
7624 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7625 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7626 bfce7f83 2019-07-27 stsp free(hle);
7627 bfce7f83 2019-07-27 stsp }
7628 bfce7f83 2019-07-27 stsp }
7629 bfce7f83 2019-07-27 stsp
7630 0ebf8283 2019-07-24 stsp static const struct got_error *
7631 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7632 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7633 0ebf8283 2019-07-24 stsp {
7634 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7635 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7636 0ebf8283 2019-07-24 stsp
7637 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7638 0ebf8283 2019-07-24 stsp if (f == NULL) {
7639 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7640 0ebf8283 2019-07-24 stsp goto done;
7641 0ebf8283 2019-07-24 stsp }
7642 0ebf8283 2019-07-24 stsp
7643 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7644 0ebf8283 2019-07-24 stsp done:
7645 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7646 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7647 0ebf8283 2019-07-24 stsp return err;
7648 0ebf8283 2019-07-24 stsp }
7649 0ebf8283 2019-07-24 stsp
7650 0ebf8283 2019-07-24 stsp static const struct got_error *
7651 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7652 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7653 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7654 0ebf8283 2019-07-24 stsp {
7655 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7656 0ebf8283 2019-07-24 stsp int resp = ' ';
7657 0ebf8283 2019-07-24 stsp
7658 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7659 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7660 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7661 0ebf8283 2019-07-24 stsp resp = getchar();
7662 a61a4414 2019-08-07 stsp if (resp == '\n')
7663 a61a4414 2019-08-07 stsp resp = getchar();
7664 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7665 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7666 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7667 bfce7f83 2019-07-27 stsp repo);
7668 426ebf2e 2019-07-25 stsp if (err) {
7669 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7670 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7671 426ebf2e 2019-07-25 stsp break;
7672 bfce7f83 2019-07-27 stsp prev_err = err;
7673 426ebf2e 2019-07-25 stsp resp = ' ';
7674 426ebf2e 2019-07-25 stsp continue;
7675 426ebf2e 2019-07-25 stsp }
7676 bfce7f83 2019-07-27 stsp break;
7677 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7678 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7679 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7680 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7681 426ebf2e 2019-07-25 stsp if (err) {
7682 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7683 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7684 426ebf2e 2019-07-25 stsp break;
7685 bfce7f83 2019-07-27 stsp prev_err = err;
7686 426ebf2e 2019-07-25 stsp resp = ' ';
7687 426ebf2e 2019-07-25 stsp continue;
7688 426ebf2e 2019-07-25 stsp }
7689 bfce7f83 2019-07-27 stsp break;
7690 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7691 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7692 0ebf8283 2019-07-24 stsp break;
7693 426ebf2e 2019-07-25 stsp } else
7694 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7695 0ebf8283 2019-07-24 stsp }
7696 0ebf8283 2019-07-24 stsp
7697 0ebf8283 2019-07-24 stsp return err;
7698 0ebf8283 2019-07-24 stsp }
7699 0ebf8283 2019-07-24 stsp
7700 0ebf8283 2019-07-24 stsp static const struct got_error *
7701 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7702 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7703 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7704 0ebf8283 2019-07-24 stsp {
7705 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7706 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7707 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7708 3e3a69f1 2019-07-25 stsp branch, repo);
7709 0ebf8283 2019-07-24 stsp }
7710 0ebf8283 2019-07-24 stsp
7711 0ebf8283 2019-07-24 stsp static const struct got_error *
7712 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7713 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7714 0ebf8283 2019-07-24 stsp {
7715 0ebf8283 2019-07-24 stsp const struct got_error *err;
7716 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7717 0ebf8283 2019-07-24 stsp
7718 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7719 0ebf8283 2019-07-24 stsp if (err)
7720 0ebf8283 2019-07-24 stsp goto done;
7721 0ebf8283 2019-07-24 stsp
7722 0ebf8283 2019-07-24 stsp if (new_id) {
7723 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7724 0ebf8283 2019-07-24 stsp if (err)
7725 0ebf8283 2019-07-24 stsp goto done;
7726 0ebf8283 2019-07-24 stsp }
7727 0ebf8283 2019-07-24 stsp
7728 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7729 0ebf8283 2019-07-24 stsp if (new_id_str)
7730 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7731 0ebf8283 2019-07-24 stsp
7732 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7733 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7734 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7735 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7736 0ebf8283 2019-07-24 stsp goto done;
7737 0ebf8283 2019-07-24 stsp }
7738 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7739 0ebf8283 2019-07-24 stsp } else {
7740 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7741 0ebf8283 2019-07-24 stsp if (err)
7742 0ebf8283 2019-07-24 stsp goto done;
7743 0ebf8283 2019-07-24 stsp }
7744 0ebf8283 2019-07-24 stsp
7745 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7746 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7747 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7748 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7749 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7750 0ebf8283 2019-07-24 stsp break;
7751 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7752 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7753 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7754 0ebf8283 2019-07-24 stsp logmsg);
7755 0ebf8283 2019-07-24 stsp break;
7756 0ebf8283 2019-07-24 stsp default:
7757 0ebf8283 2019-07-24 stsp break;
7758 0ebf8283 2019-07-24 stsp }
7759 0ebf8283 2019-07-24 stsp done:
7760 0ebf8283 2019-07-24 stsp free(old_id_str);
7761 0ebf8283 2019-07-24 stsp free(new_id_str);
7762 0ebf8283 2019-07-24 stsp return err;
7763 0ebf8283 2019-07-24 stsp }
7764 0ebf8283 2019-07-24 stsp
7765 0ebf8283 2019-07-24 stsp static const struct got_error *
7766 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7767 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7768 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7769 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7770 0ebf8283 2019-07-24 stsp {
7771 0ebf8283 2019-07-24 stsp const struct got_error *err;
7772 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7773 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7774 0ebf8283 2019-07-24 stsp
7775 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7776 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7777 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7778 0ebf8283 2019-07-24 stsp if (err)
7779 0ebf8283 2019-07-24 stsp return err;
7780 0ebf8283 2019-07-24 stsp }
7781 0ebf8283 2019-07-24 stsp
7782 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7783 0ebf8283 2019-07-24 stsp if (err)
7784 0ebf8283 2019-07-24 stsp return err;
7785 0ebf8283 2019-07-24 stsp
7786 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7787 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7788 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7789 0ebf8283 2019-07-24 stsp if (err) {
7790 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7791 0ebf8283 2019-07-24 stsp goto done;
7792 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7793 0ebf8283 2019-07-24 stsp } else {
7794 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7795 0ebf8283 2019-07-24 stsp free(new_commit_id);
7796 0ebf8283 2019-07-24 stsp }
7797 0ebf8283 2019-07-24 stsp done:
7798 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7799 0ebf8283 2019-07-24 stsp return err;
7800 0ebf8283 2019-07-24 stsp }
7801 0ebf8283 2019-07-24 stsp
7802 0ebf8283 2019-07-24 stsp static const struct got_error *
7803 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7804 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7805 0ebf8283 2019-07-24 stsp {
7806 0ebf8283 2019-07-24 stsp const struct got_error *error;
7807 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7808 0ebf8283 2019-07-24 stsp
7809 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7810 0ebf8283 2019-07-24 stsp repo);
7811 0ebf8283 2019-07-24 stsp if (error)
7812 0ebf8283 2019-07-24 stsp return error;
7813 0ebf8283 2019-07-24 stsp
7814 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7815 0ebf8283 2019-07-24 stsp if (error)
7816 0ebf8283 2019-07-24 stsp return error;
7817 0ebf8283 2019-07-24 stsp
7818 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7819 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7820 0ebf8283 2019-07-24 stsp return error;
7821 ab20a43a 2020-01-29 stsp }
7822 ab20a43a 2020-01-29 stsp
7823 ab20a43a 2020-01-29 stsp static const struct got_error *
7824 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7825 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7826 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7827 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7828 ab20a43a 2020-01-29 stsp {
7829 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7830 ab20a43a 2020-01-29 stsp
7831 ab20a43a 2020-01-29 stsp switch (status) {
7832 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7833 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7834 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7835 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7836 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7837 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7838 ab20a43a 2020-01-29 stsp default:
7839 ab20a43a 2020-01-29 stsp break;
7840 ab20a43a 2020-01-29 stsp }
7841 ab20a43a 2020-01-29 stsp
7842 ab20a43a 2020-01-29 stsp switch (staged_status) {
7843 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7844 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7845 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7846 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7847 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7848 ab20a43a 2020-01-29 stsp default:
7849 ab20a43a 2020-01-29 stsp break;
7850 ab20a43a 2020-01-29 stsp }
7851 ab20a43a 2020-01-29 stsp
7852 ab20a43a 2020-01-29 stsp return NULL;
7853 0ebf8283 2019-07-24 stsp }
7854 0ebf8283 2019-07-24 stsp
7855 0ebf8283 2019-07-24 stsp static const struct got_error *
7856 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7857 0ebf8283 2019-07-24 stsp {
7858 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7859 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7860 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7861 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7862 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7863 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7864 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7865 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7866 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7867 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7868 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7869 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7870 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7871 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7872 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7873 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7874 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7875 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7876 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7877 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7878 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7879 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7880 0ebf8283 2019-07-24 stsp
7881 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7882 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7883 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7884 0ebf8283 2019-07-24 stsp
7885 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7886 0ebf8283 2019-07-24 stsp switch (ch) {
7887 0ebf8283 2019-07-24 stsp case 'a':
7888 0ebf8283 2019-07-24 stsp abort_edit = 1;
7889 0ebf8283 2019-07-24 stsp break;
7890 0ebf8283 2019-07-24 stsp case 'c':
7891 0ebf8283 2019-07-24 stsp continue_edit = 1;
7892 0ebf8283 2019-07-24 stsp break;
7893 0ebf8283 2019-07-24 stsp case 'F':
7894 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7895 0ebf8283 2019-07-24 stsp break;
7896 083957f4 2020-02-24 stsp case 'm':
7897 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7898 083957f4 2020-02-24 stsp break;
7899 0ebf8283 2019-07-24 stsp default:
7900 0ebf8283 2019-07-24 stsp usage_histedit();
7901 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7902 0ebf8283 2019-07-24 stsp }
7903 0ebf8283 2019-07-24 stsp }
7904 0ebf8283 2019-07-24 stsp
7905 0ebf8283 2019-07-24 stsp argc -= optind;
7906 0ebf8283 2019-07-24 stsp argv += optind;
7907 0ebf8283 2019-07-24 stsp
7908 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7909 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7910 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7911 0ebf8283 2019-07-24 stsp err(1, "pledge");
7912 0ebf8283 2019-07-24 stsp #endif
7913 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7914 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7915 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7916 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7917 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7918 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7919 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7920 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7921 0ebf8283 2019-07-24 stsp if (argc != 0)
7922 0ebf8283 2019-07-24 stsp usage_histedit();
7923 0ebf8283 2019-07-24 stsp
7924 0ebf8283 2019-07-24 stsp /*
7925 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7926 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7927 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7928 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7929 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7930 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7931 0ebf8283 2019-07-24 stsp */
7932 0ebf8283 2019-07-24 stsp
7933 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7934 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7935 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7936 0ebf8283 2019-07-24 stsp goto done;
7937 0ebf8283 2019-07-24 stsp }
7938 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7939 0ebf8283 2019-07-24 stsp if (error)
7940 0ebf8283 2019-07-24 stsp goto done;
7941 0ebf8283 2019-07-24 stsp
7942 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7943 c9956ddf 2019-09-08 stsp NULL);
7944 0ebf8283 2019-07-24 stsp if (error != NULL)
7945 0ebf8283 2019-07-24 stsp goto done;
7946 0ebf8283 2019-07-24 stsp
7947 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7948 0ebf8283 2019-07-24 stsp if (error)
7949 0ebf8283 2019-07-24 stsp goto done;
7950 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7951 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7952 0ebf8283 2019-07-24 stsp goto done;
7953 0ebf8283 2019-07-24 stsp }
7954 0ebf8283 2019-07-24 stsp
7955 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7956 0ebf8283 2019-07-24 stsp if (error)
7957 0ebf8283 2019-07-24 stsp goto done;
7958 0ebf8283 2019-07-24 stsp
7959 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7960 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7961 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7962 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7963 083957f4 2020-02-24 stsp "before the -m option can be used");
7964 083957f4 2020-02-24 stsp goto done;
7965 083957f4 2020-02-24 stsp }
7966 083957f4 2020-02-24 stsp
7967 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7968 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7969 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7970 3e3a69f1 2019-07-25 stsp worktree, repo);
7971 0ebf8283 2019-07-24 stsp if (error)
7972 0ebf8283 2019-07-24 stsp goto done;
7973 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7974 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7975 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7976 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7977 0ebf8283 2019-07-24 stsp if (error)
7978 0ebf8283 2019-07-24 stsp goto done;
7979 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7980 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7981 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7982 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7983 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7984 0ebf8283 2019-07-24 stsp goto done;
7985 0ebf8283 2019-07-24 stsp }
7986 0ebf8283 2019-07-24 stsp
7987 0ebf8283 2019-07-24 stsp if (continue_edit) {
7988 0ebf8283 2019-07-24 stsp char *path;
7989 0ebf8283 2019-07-24 stsp
7990 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7991 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7992 0ebf8283 2019-07-24 stsp goto done;
7993 0ebf8283 2019-07-24 stsp }
7994 0ebf8283 2019-07-24 stsp
7995 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7996 0ebf8283 2019-07-24 stsp if (error)
7997 0ebf8283 2019-07-24 stsp goto done;
7998 0ebf8283 2019-07-24 stsp
7999 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8000 0ebf8283 2019-07-24 stsp free(path);
8001 0ebf8283 2019-07-24 stsp if (error)
8002 0ebf8283 2019-07-24 stsp goto done;
8003 0ebf8283 2019-07-24 stsp
8004 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8005 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8006 3e3a69f1 2019-07-25 stsp worktree, repo);
8007 0ebf8283 2019-07-24 stsp if (error)
8008 0ebf8283 2019-07-24 stsp goto done;
8009 0ebf8283 2019-07-24 stsp
8010 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8011 0ebf8283 2019-07-24 stsp if (error)
8012 0ebf8283 2019-07-24 stsp goto done;
8013 0ebf8283 2019-07-24 stsp
8014 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8015 0ebf8283 2019-07-24 stsp head_commit_id);
8016 0ebf8283 2019-07-24 stsp if (error)
8017 0ebf8283 2019-07-24 stsp goto done;
8018 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8019 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8020 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8021 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8022 3aac7cf7 2019-07-25 stsp goto done;
8023 3aac7cf7 2019-07-25 stsp }
8024 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8025 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8026 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8027 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8028 0ebf8283 2019-07-24 stsp commit = NULL;
8029 0ebf8283 2019-07-24 stsp if (error)
8030 0ebf8283 2019-07-24 stsp goto done;
8031 0ebf8283 2019-07-24 stsp } else {
8032 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8033 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8034 0ebf8283 2019-07-24 stsp goto done;
8035 0ebf8283 2019-07-24 stsp }
8036 0ebf8283 2019-07-24 stsp
8037 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8038 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8039 0ebf8283 2019-07-24 stsp if (error != NULL)
8040 0ebf8283 2019-07-24 stsp goto done;
8041 0ebf8283 2019-07-24 stsp
8042 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8043 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8044 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8045 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8046 c7d20a3f 2019-07-30 stsp goto done;
8047 c7d20a3f 2019-07-30 stsp }
8048 c7d20a3f 2019-07-30 stsp
8049 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8050 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8051 bfce7f83 2019-07-27 stsp branch = NULL;
8052 0ebf8283 2019-07-24 stsp if (error)
8053 0ebf8283 2019-07-24 stsp goto done;
8054 0ebf8283 2019-07-24 stsp
8055 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8056 0ebf8283 2019-07-24 stsp head_commit_id);
8057 0ebf8283 2019-07-24 stsp if (error)
8058 0ebf8283 2019-07-24 stsp goto done;
8059 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8060 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8061 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8062 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8063 3aac7cf7 2019-07-25 stsp goto done;
8064 3aac7cf7 2019-07-25 stsp }
8065 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8066 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8067 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8068 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8069 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8070 0ebf8283 2019-07-24 stsp commit = NULL;
8071 0ebf8283 2019-07-24 stsp if (error)
8072 0ebf8283 2019-07-24 stsp goto done;
8073 0ebf8283 2019-07-24 stsp
8074 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8075 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8076 c5996fff 2020-01-29 stsp goto done;
8077 c5996fff 2020-01-29 stsp }
8078 c5996fff 2020-01-29 stsp
8079 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8080 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8081 bfce7f83 2019-07-27 stsp if (error)
8082 bfce7f83 2019-07-27 stsp goto done;
8083 bfce7f83 2019-07-27 stsp
8084 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8085 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8086 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8087 6ba2bea2 2019-07-27 stsp if (error) {
8088 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8089 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8090 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8091 0ebf8283 2019-07-24 stsp goto done;
8092 6ba2bea2 2019-07-27 stsp }
8093 0ebf8283 2019-07-24 stsp } else {
8094 514f2ffe 2020-01-29 stsp const char *branch_name;
8095 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8096 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8097 514f2ffe 2020-01-29 stsp branch_name += 11;
8098 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8099 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8100 6ba2bea2 2019-07-27 stsp if (error) {
8101 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8102 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8103 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8104 0ebf8283 2019-07-24 stsp goto done;
8105 6ba2bea2 2019-07-27 stsp }
8106 0ebf8283 2019-07-24 stsp
8107 0ebf8283 2019-07-24 stsp }
8108 0ebf8283 2019-07-24 stsp
8109 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8110 0ebf8283 2019-07-24 stsp repo);
8111 6ba2bea2 2019-07-27 stsp if (error) {
8112 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8113 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8114 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8115 0ebf8283 2019-07-24 stsp goto done;
8116 6ba2bea2 2019-07-27 stsp }
8117 0ebf8283 2019-07-24 stsp
8118 0ebf8283 2019-07-24 stsp }
8119 0ebf8283 2019-07-24 stsp
8120 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8121 4ec14e60 2019-08-28 hiltjo if (error)
8122 0ebf8283 2019-07-24 stsp goto done;
8123 0ebf8283 2019-07-24 stsp
8124 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8125 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8126 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8127 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8128 0ebf8283 2019-07-24 stsp continue;
8129 0ebf8283 2019-07-24 stsp
8130 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8131 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8132 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8133 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8134 0ebf8283 2019-07-24 stsp repo);
8135 ab20a43a 2020-01-29 stsp if (error)
8136 ab20a43a 2020-01-29 stsp goto done;
8137 0ebf8283 2019-07-24 stsp } else {
8138 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8139 ab20a43a 2020-01-29 stsp int have_changes = 0;
8140 ab20a43a 2020-01-29 stsp
8141 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8142 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8143 ab20a43a 2020-01-29 stsp if (error)
8144 ab20a43a 2020-01-29 stsp goto done;
8145 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8146 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8147 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8148 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8149 ab20a43a 2020-01-29 stsp if (error) {
8150 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8151 ab20a43a 2020-01-29 stsp goto done;
8152 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8153 ab20a43a 2020-01-29 stsp goto done;
8154 ab20a43a 2020-01-29 stsp }
8155 ab20a43a 2020-01-29 stsp if (have_changes) {
8156 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8157 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8158 ab20a43a 2020-01-29 stsp if (error)
8159 ab20a43a 2020-01-29 stsp goto done;
8160 ab20a43a 2020-01-29 stsp } else {
8161 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8162 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8163 ab20a43a 2020-01-29 stsp if (error)
8164 ab20a43a 2020-01-29 stsp goto done;
8165 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8166 ab20a43a 2020-01-29 stsp hle, NULL);
8167 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8168 ab20a43a 2020-01-29 stsp commit = NULL;
8169 ab20a43a 2020-01-29 stsp if (error)
8170 ab20a43a 2020-01-29 stsp goto done;
8171 ab20a43a 2020-01-29 stsp }
8172 0ebf8283 2019-07-24 stsp }
8173 0ebf8283 2019-07-24 stsp continue;
8174 0ebf8283 2019-07-24 stsp }
8175 0ebf8283 2019-07-24 stsp
8176 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8177 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8178 0ebf8283 2019-07-24 stsp if (error)
8179 0ebf8283 2019-07-24 stsp goto done;
8180 0ebf8283 2019-07-24 stsp continue;
8181 0ebf8283 2019-07-24 stsp }
8182 0ebf8283 2019-07-24 stsp
8183 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8184 0ebf8283 2019-07-24 stsp hle->commit_id);
8185 0ebf8283 2019-07-24 stsp if (error)
8186 0ebf8283 2019-07-24 stsp goto done;
8187 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8188 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8189 0ebf8283 2019-07-24 stsp
8190 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8191 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8192 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
8193 0ebf8283 2019-07-24 stsp if (error)
8194 0ebf8283 2019-07-24 stsp goto done;
8195 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8196 0ebf8283 2019-07-24 stsp commit = NULL;
8197 0ebf8283 2019-07-24 stsp
8198 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8199 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8200 a0ea4fc0 2020-02-28 stsp repo);
8201 a0ea4fc0 2020-02-28 stsp if (error)
8202 a0ea4fc0 2020-02-28 stsp goto done;
8203 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8204 0ebf8283 2019-07-24 stsp break;
8205 0ebf8283 2019-07-24 stsp }
8206 0ebf8283 2019-07-24 stsp
8207 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8208 0ebf8283 2019-07-24 stsp char *id_str;
8209 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8210 0ebf8283 2019-07-24 stsp if (error)
8211 0ebf8283 2019-07-24 stsp goto done;
8212 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8213 0ebf8283 2019-07-24 stsp id_str);
8214 0ebf8283 2019-07-24 stsp free(id_str);
8215 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8216 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8217 3e3a69f1 2019-07-25 stsp fileindex);
8218 0ebf8283 2019-07-24 stsp goto done;
8219 0ebf8283 2019-07-24 stsp }
8220 0ebf8283 2019-07-24 stsp
8221 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8222 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8223 0ebf8283 2019-07-24 stsp if (error)
8224 0ebf8283 2019-07-24 stsp goto done;
8225 0ebf8283 2019-07-24 stsp continue;
8226 0ebf8283 2019-07-24 stsp }
8227 0ebf8283 2019-07-24 stsp
8228 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8229 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8230 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8231 0ebf8283 2019-07-24 stsp if (error)
8232 0ebf8283 2019-07-24 stsp goto done;
8233 0ebf8283 2019-07-24 stsp }
8234 0ebf8283 2019-07-24 stsp
8235 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8236 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8237 0ebf8283 2019-07-24 stsp if (error)
8238 0ebf8283 2019-07-24 stsp goto done;
8239 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8240 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8241 0ebf8283 2019-07-24 stsp } else
8242 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8243 3e3a69f1 2019-07-25 stsp branch, repo);
8244 0ebf8283 2019-07-24 stsp done:
8245 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8246 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8247 0ebf8283 2019-07-24 stsp free(head_commit_id);
8248 0ebf8283 2019-07-24 stsp free(base_commit_id);
8249 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8250 0ebf8283 2019-07-24 stsp if (commit)
8251 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8252 0ebf8283 2019-07-24 stsp if (branch)
8253 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8254 0ebf8283 2019-07-24 stsp if (tmp_branch)
8255 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8256 0ebf8283 2019-07-24 stsp if (worktree)
8257 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8258 2822a352 2019-10-15 stsp if (repo)
8259 2822a352 2019-10-15 stsp got_repo_close(repo);
8260 2822a352 2019-10-15 stsp return error;
8261 2822a352 2019-10-15 stsp }
8262 2822a352 2019-10-15 stsp
8263 2822a352 2019-10-15 stsp __dead static void
8264 2822a352 2019-10-15 stsp usage_integrate(void)
8265 2822a352 2019-10-15 stsp {
8266 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8267 2822a352 2019-10-15 stsp exit(1);
8268 2822a352 2019-10-15 stsp }
8269 2822a352 2019-10-15 stsp
8270 2822a352 2019-10-15 stsp static const struct got_error *
8271 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8272 2822a352 2019-10-15 stsp {
8273 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8274 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8275 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8276 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8277 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8278 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8279 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8280 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8281 2822a352 2019-10-15 stsp int ch, did_something = 0;
8282 2822a352 2019-10-15 stsp
8283 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8284 2822a352 2019-10-15 stsp switch (ch) {
8285 2822a352 2019-10-15 stsp default:
8286 2822a352 2019-10-15 stsp usage_integrate();
8287 2822a352 2019-10-15 stsp /* NOTREACHED */
8288 2822a352 2019-10-15 stsp }
8289 2822a352 2019-10-15 stsp }
8290 2822a352 2019-10-15 stsp
8291 2822a352 2019-10-15 stsp argc -= optind;
8292 2822a352 2019-10-15 stsp argv += optind;
8293 2822a352 2019-10-15 stsp
8294 2822a352 2019-10-15 stsp if (argc != 1)
8295 2822a352 2019-10-15 stsp usage_integrate();
8296 2822a352 2019-10-15 stsp branch_arg = argv[0];
8297 2822a352 2019-10-15 stsp
8298 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8299 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8300 2822a352 2019-10-15 stsp err(1, "pledge");
8301 2822a352 2019-10-15 stsp
8302 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8303 2822a352 2019-10-15 stsp if (cwd == NULL) {
8304 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8305 2822a352 2019-10-15 stsp goto done;
8306 2822a352 2019-10-15 stsp }
8307 2822a352 2019-10-15 stsp
8308 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8309 2822a352 2019-10-15 stsp if (error)
8310 2822a352 2019-10-15 stsp goto done;
8311 2822a352 2019-10-15 stsp
8312 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8313 2822a352 2019-10-15 stsp if (error)
8314 2822a352 2019-10-15 stsp goto done;
8315 2822a352 2019-10-15 stsp
8316 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8317 2822a352 2019-10-15 stsp NULL);
8318 2822a352 2019-10-15 stsp if (error != NULL)
8319 2822a352 2019-10-15 stsp goto done;
8320 2822a352 2019-10-15 stsp
8321 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8322 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8323 2822a352 2019-10-15 stsp if (error)
8324 2822a352 2019-10-15 stsp goto done;
8325 2822a352 2019-10-15 stsp
8326 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8327 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8328 2822a352 2019-10-15 stsp goto done;
8329 2822a352 2019-10-15 stsp }
8330 2822a352 2019-10-15 stsp
8331 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8332 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8333 2822a352 2019-10-15 stsp if (error)
8334 2822a352 2019-10-15 stsp goto done;
8335 2822a352 2019-10-15 stsp
8336 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8337 2822a352 2019-10-15 stsp if (refname == NULL) {
8338 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8339 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8340 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8341 2822a352 2019-10-15 stsp goto done;
8342 2822a352 2019-10-15 stsp }
8343 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8344 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8345 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8346 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8347 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8348 2822a352 2019-10-15 stsp goto done;
8349 2822a352 2019-10-15 stsp }
8350 2822a352 2019-10-15 stsp
8351 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8352 2822a352 2019-10-15 stsp if (error)
8353 2822a352 2019-10-15 stsp goto done;
8354 2822a352 2019-10-15 stsp
8355 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8356 2822a352 2019-10-15 stsp if (error)
8357 2822a352 2019-10-15 stsp goto done;
8358 2822a352 2019-10-15 stsp
8359 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8360 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8361 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8362 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8363 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8364 2822a352 2019-10-15 stsp goto done;
8365 2822a352 2019-10-15 stsp }
8366 2822a352 2019-10-15 stsp
8367 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8368 2822a352 2019-10-15 stsp if (error) {
8369 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8370 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8371 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8372 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8373 2822a352 2019-10-15 stsp goto done;
8374 2822a352 2019-10-15 stsp }
8375 2822a352 2019-10-15 stsp
8376 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8377 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
8378 2822a352 2019-10-15 stsp check_cancelled, NULL);
8379 2822a352 2019-10-15 stsp if (error)
8380 2822a352 2019-10-15 stsp goto done;
8381 2822a352 2019-10-15 stsp
8382 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8383 2822a352 2019-10-15 stsp done:
8384 715dc77e 2019-08-03 stsp if (repo)
8385 715dc77e 2019-08-03 stsp got_repo_close(repo);
8386 2822a352 2019-10-15 stsp if (worktree)
8387 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8388 2822a352 2019-10-15 stsp free(cwd);
8389 2822a352 2019-10-15 stsp free(base_commit_id);
8390 2822a352 2019-10-15 stsp free(commit_id);
8391 2822a352 2019-10-15 stsp free(refname);
8392 2822a352 2019-10-15 stsp free(base_refname);
8393 715dc77e 2019-08-03 stsp return error;
8394 715dc77e 2019-08-03 stsp }
8395 715dc77e 2019-08-03 stsp
8396 715dc77e 2019-08-03 stsp __dead static void
8397 715dc77e 2019-08-03 stsp usage_stage(void)
8398 715dc77e 2019-08-03 stsp {
8399 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8400 f3055044 2019-08-07 stsp "[file-path ...]\n",
8401 715dc77e 2019-08-03 stsp getprogname());
8402 715dc77e 2019-08-03 stsp exit(1);
8403 715dc77e 2019-08-03 stsp }
8404 715dc77e 2019-08-03 stsp
8405 715dc77e 2019-08-03 stsp static const struct got_error *
8406 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8407 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8408 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8409 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8410 408b4ebc 2019-08-03 stsp {
8411 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8412 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8413 408b4ebc 2019-08-03 stsp
8414 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8415 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8416 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8417 408b4ebc 2019-08-03 stsp return NULL;
8418 408b4ebc 2019-08-03 stsp
8419 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8420 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8421 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8422 408b4ebc 2019-08-03 stsp else
8423 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8424 408b4ebc 2019-08-03 stsp if (err)
8425 408b4ebc 2019-08-03 stsp return err;
8426 408b4ebc 2019-08-03 stsp
8427 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8428 408b4ebc 2019-08-03 stsp free(id_str);
8429 dc424a06 2019-08-07 stsp return NULL;
8430 dc424a06 2019-08-07 stsp }
8431 2e1f37b0 2019-08-08 stsp
8432 dc424a06 2019-08-07 stsp static const struct got_error *
8433 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8434 715dc77e 2019-08-03 stsp {
8435 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8436 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8437 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8438 715dc77e 2019-08-03 stsp char *cwd = NULL;
8439 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8440 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8441 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8442 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8443 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8444 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8445 715dc77e 2019-08-03 stsp
8446 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8447 715dc77e 2019-08-03 stsp
8448 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8449 715dc77e 2019-08-03 stsp switch (ch) {
8450 408b4ebc 2019-08-03 stsp case 'l':
8451 408b4ebc 2019-08-03 stsp list_stage = 1;
8452 408b4ebc 2019-08-03 stsp break;
8453 dc424a06 2019-08-07 stsp case 'p':
8454 dc424a06 2019-08-07 stsp pflag = 1;
8455 dc424a06 2019-08-07 stsp break;
8456 dc424a06 2019-08-07 stsp case 'F':
8457 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8458 dc424a06 2019-08-07 stsp break;
8459 715dc77e 2019-08-03 stsp default:
8460 715dc77e 2019-08-03 stsp usage_stage();
8461 715dc77e 2019-08-03 stsp /* NOTREACHED */
8462 715dc77e 2019-08-03 stsp }
8463 715dc77e 2019-08-03 stsp }
8464 715dc77e 2019-08-03 stsp
8465 715dc77e 2019-08-03 stsp argc -= optind;
8466 715dc77e 2019-08-03 stsp argv += optind;
8467 715dc77e 2019-08-03 stsp
8468 715dc77e 2019-08-03 stsp #ifndef PROFILE
8469 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8470 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8471 715dc77e 2019-08-03 stsp err(1, "pledge");
8472 715dc77e 2019-08-03 stsp #endif
8473 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8474 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8475 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8476 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8477 715dc77e 2019-08-03 stsp
8478 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8479 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8480 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8481 715dc77e 2019-08-03 stsp goto done;
8482 715dc77e 2019-08-03 stsp }
8483 715dc77e 2019-08-03 stsp
8484 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8485 715dc77e 2019-08-03 stsp if (error)
8486 715dc77e 2019-08-03 stsp goto done;
8487 715dc77e 2019-08-03 stsp
8488 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8489 c9956ddf 2019-09-08 stsp NULL);
8490 715dc77e 2019-08-03 stsp if (error != NULL)
8491 715dc77e 2019-08-03 stsp goto done;
8492 715dc77e 2019-08-03 stsp
8493 dc424a06 2019-08-07 stsp if (patch_script_path) {
8494 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8495 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8496 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8497 dc424a06 2019-08-07 stsp patch_script_path);
8498 dc424a06 2019-08-07 stsp goto done;
8499 dc424a06 2019-08-07 stsp }
8500 dc424a06 2019-08-07 stsp }
8501 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8502 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8503 715dc77e 2019-08-03 stsp if (error)
8504 715dc77e 2019-08-03 stsp goto done;
8505 715dc77e 2019-08-03 stsp
8506 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8507 6d022e97 2019-08-04 stsp if (error)
8508 6d022e97 2019-08-04 stsp goto done;
8509 715dc77e 2019-08-03 stsp
8510 6d022e97 2019-08-04 stsp if (list_stage)
8511 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8512 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8513 2e1f37b0 2019-08-08 stsp else {
8514 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8515 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8516 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8517 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8518 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8519 2e1f37b0 2019-08-08 stsp }
8520 ad493afc 2019-08-03 stsp done:
8521 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8522 2e1f37b0 2019-08-08 stsp error == NULL)
8523 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8524 ad493afc 2019-08-03 stsp if (repo)
8525 ad493afc 2019-08-03 stsp got_repo_close(repo);
8526 ad493afc 2019-08-03 stsp if (worktree)
8527 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8528 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8529 ad493afc 2019-08-03 stsp free((char *)pe->path);
8530 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8531 ad493afc 2019-08-03 stsp free(cwd);
8532 ad493afc 2019-08-03 stsp return error;
8533 ad493afc 2019-08-03 stsp }
8534 ad493afc 2019-08-03 stsp
8535 ad493afc 2019-08-03 stsp __dead static void
8536 ad493afc 2019-08-03 stsp usage_unstage(void)
8537 ad493afc 2019-08-03 stsp {
8538 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8539 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8540 ad493afc 2019-08-03 stsp getprogname());
8541 ad493afc 2019-08-03 stsp exit(1);
8542 ad493afc 2019-08-03 stsp }
8543 ad493afc 2019-08-03 stsp
8544 ad493afc 2019-08-03 stsp
8545 ad493afc 2019-08-03 stsp static const struct got_error *
8546 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8547 ad493afc 2019-08-03 stsp {
8548 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8549 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8550 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8551 ad493afc 2019-08-03 stsp char *cwd = NULL;
8552 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8553 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8554 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8555 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8556 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8557 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8558 ad493afc 2019-08-03 stsp
8559 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8560 ad493afc 2019-08-03 stsp
8561 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8562 ad493afc 2019-08-03 stsp switch (ch) {
8563 2e1f37b0 2019-08-08 stsp case 'p':
8564 2e1f37b0 2019-08-08 stsp pflag = 1;
8565 2e1f37b0 2019-08-08 stsp break;
8566 2e1f37b0 2019-08-08 stsp case 'F':
8567 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8568 2e1f37b0 2019-08-08 stsp break;
8569 ad493afc 2019-08-03 stsp default:
8570 ad493afc 2019-08-03 stsp usage_unstage();
8571 ad493afc 2019-08-03 stsp /* NOTREACHED */
8572 ad493afc 2019-08-03 stsp }
8573 ad493afc 2019-08-03 stsp }
8574 ad493afc 2019-08-03 stsp
8575 ad493afc 2019-08-03 stsp argc -= optind;
8576 ad493afc 2019-08-03 stsp argv += optind;
8577 ad493afc 2019-08-03 stsp
8578 ad493afc 2019-08-03 stsp #ifndef PROFILE
8579 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8580 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8581 ad493afc 2019-08-03 stsp err(1, "pledge");
8582 ad493afc 2019-08-03 stsp #endif
8583 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8584 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8585 2e1f37b0 2019-08-08 stsp
8586 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8587 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8588 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8589 ad493afc 2019-08-03 stsp goto done;
8590 ad493afc 2019-08-03 stsp }
8591 ad493afc 2019-08-03 stsp
8592 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8593 ad493afc 2019-08-03 stsp if (error)
8594 ad493afc 2019-08-03 stsp goto done;
8595 ad493afc 2019-08-03 stsp
8596 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8597 c9956ddf 2019-09-08 stsp NULL);
8598 ad493afc 2019-08-03 stsp if (error != NULL)
8599 ad493afc 2019-08-03 stsp goto done;
8600 ad493afc 2019-08-03 stsp
8601 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8602 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8603 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8604 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8605 2e1f37b0 2019-08-08 stsp patch_script_path);
8606 2e1f37b0 2019-08-08 stsp goto done;
8607 2e1f37b0 2019-08-08 stsp }
8608 2e1f37b0 2019-08-08 stsp }
8609 2e1f37b0 2019-08-08 stsp
8610 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8611 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8612 ad493afc 2019-08-03 stsp if (error)
8613 ad493afc 2019-08-03 stsp goto done;
8614 ad493afc 2019-08-03 stsp
8615 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8616 ad493afc 2019-08-03 stsp if (error)
8617 ad493afc 2019-08-03 stsp goto done;
8618 ad493afc 2019-08-03 stsp
8619 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8620 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8621 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8622 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8623 715dc77e 2019-08-03 stsp done:
8624 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8625 2e1f37b0 2019-08-08 stsp error == NULL)
8626 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8627 0ebf8283 2019-07-24 stsp if (repo)
8628 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8629 715dc77e 2019-08-03 stsp if (worktree)
8630 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8631 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8632 715dc77e 2019-08-03 stsp free((char *)pe->path);
8633 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8634 715dc77e 2019-08-03 stsp free(cwd);
8635 01073a5d 2019-08-22 stsp return error;
8636 01073a5d 2019-08-22 stsp }
8637 01073a5d 2019-08-22 stsp
8638 01073a5d 2019-08-22 stsp __dead static void
8639 01073a5d 2019-08-22 stsp usage_cat(void)
8640 01073a5d 2019-08-22 stsp {
8641 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8642 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8643 01073a5d 2019-08-22 stsp exit(1);
8644 01073a5d 2019-08-22 stsp }
8645 01073a5d 2019-08-22 stsp
8646 01073a5d 2019-08-22 stsp static const struct got_error *
8647 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8648 01073a5d 2019-08-22 stsp {
8649 01073a5d 2019-08-22 stsp const struct got_error *err;
8650 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8651 01073a5d 2019-08-22 stsp
8652 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8653 01073a5d 2019-08-22 stsp if (err)
8654 01073a5d 2019-08-22 stsp return err;
8655 01073a5d 2019-08-22 stsp
8656 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8657 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8658 01073a5d 2019-08-22 stsp return err;
8659 01073a5d 2019-08-22 stsp }
8660 01073a5d 2019-08-22 stsp
8661 01073a5d 2019-08-22 stsp static const struct got_error *
8662 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8663 01073a5d 2019-08-22 stsp {
8664 01073a5d 2019-08-22 stsp const struct got_error *err;
8665 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8666 56e0773d 2019-11-28 stsp int nentries, i;
8667 01073a5d 2019-08-22 stsp
8668 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8669 01073a5d 2019-08-22 stsp if (err)
8670 01073a5d 2019-08-22 stsp return err;
8671 01073a5d 2019-08-22 stsp
8672 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8673 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8674 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8675 01073a5d 2019-08-22 stsp char *id_str;
8676 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8677 01073a5d 2019-08-22 stsp break;
8678 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8679 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8680 01073a5d 2019-08-22 stsp if (err)
8681 01073a5d 2019-08-22 stsp break;
8682 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8683 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8684 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8685 01073a5d 2019-08-22 stsp free(id_str);
8686 01073a5d 2019-08-22 stsp }
8687 01073a5d 2019-08-22 stsp
8688 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8689 01073a5d 2019-08-22 stsp return err;
8690 01073a5d 2019-08-22 stsp }
8691 01073a5d 2019-08-22 stsp
8692 01073a5d 2019-08-22 stsp static const struct got_error *
8693 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8694 01073a5d 2019-08-22 stsp {
8695 01073a5d 2019-08-22 stsp const struct got_error *err;
8696 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8697 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8698 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8699 01073a5d 2019-08-22 stsp char *id_str = NULL;
8700 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8701 01073a5d 2019-08-22 stsp
8702 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8703 01073a5d 2019-08-22 stsp if (err)
8704 01073a5d 2019-08-22 stsp return err;
8705 01073a5d 2019-08-22 stsp
8706 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8707 01073a5d 2019-08-22 stsp if (err)
8708 01073a5d 2019-08-22 stsp goto done;
8709 01073a5d 2019-08-22 stsp
8710 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8711 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8712 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8713 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8714 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8715 01073a5d 2019-08-22 stsp char *pid_str;
8716 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8717 01073a5d 2019-08-22 stsp if (err)
8718 01073a5d 2019-08-22 stsp goto done;
8719 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8720 01073a5d 2019-08-22 stsp free(pid_str);
8721 01073a5d 2019-08-22 stsp }
8722 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8723 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8724 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8725 01073a5d 2019-08-22 stsp
8726 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8727 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8728 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8729 01073a5d 2019-08-22 stsp
8730 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8731 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8732 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8733 01073a5d 2019-08-22 stsp done:
8734 01073a5d 2019-08-22 stsp free(id_str);
8735 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8736 01073a5d 2019-08-22 stsp return err;
8737 01073a5d 2019-08-22 stsp }
8738 01073a5d 2019-08-22 stsp
8739 01073a5d 2019-08-22 stsp static const struct got_error *
8740 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8741 01073a5d 2019-08-22 stsp {
8742 01073a5d 2019-08-22 stsp const struct got_error *err;
8743 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8744 01073a5d 2019-08-22 stsp char *id_str = NULL;
8745 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8746 01073a5d 2019-08-22 stsp
8747 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8748 01073a5d 2019-08-22 stsp if (err)
8749 01073a5d 2019-08-22 stsp return err;
8750 01073a5d 2019-08-22 stsp
8751 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8752 01073a5d 2019-08-22 stsp if (err)
8753 01073a5d 2019-08-22 stsp goto done;
8754 01073a5d 2019-08-22 stsp
8755 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8756 e15d5241 2019-08-22 stsp
8757 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8758 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8759 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8760 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8761 01073a5d 2019-08-22 stsp break;
8762 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8763 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8764 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8765 01073a5d 2019-08-22 stsp break;
8766 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8767 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8768 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8769 01073a5d 2019-08-22 stsp break;
8770 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8771 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8772 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8773 01073a5d 2019-08-22 stsp break;
8774 01073a5d 2019-08-22 stsp default:
8775 01073a5d 2019-08-22 stsp break;
8776 01073a5d 2019-08-22 stsp }
8777 01073a5d 2019-08-22 stsp
8778 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8779 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8780 e15d5241 2019-08-22 stsp
8781 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8782 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8783 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8784 01073a5d 2019-08-22 stsp
8785 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8786 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8787 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8788 01073a5d 2019-08-22 stsp done:
8789 01073a5d 2019-08-22 stsp free(id_str);
8790 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8791 01073a5d 2019-08-22 stsp return err;
8792 01073a5d 2019-08-22 stsp }
8793 01073a5d 2019-08-22 stsp
8794 01073a5d 2019-08-22 stsp static const struct got_error *
8795 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8796 01073a5d 2019-08-22 stsp {
8797 01073a5d 2019-08-22 stsp const struct got_error *error;
8798 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8799 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8800 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8801 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8802 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8803 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8804 01073a5d 2019-08-22 stsp
8805 01073a5d 2019-08-22 stsp #ifndef PROFILE
8806 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8807 01073a5d 2019-08-22 stsp NULL) == -1)
8808 01073a5d 2019-08-22 stsp err(1, "pledge");
8809 01073a5d 2019-08-22 stsp #endif
8810 01073a5d 2019-08-22 stsp
8811 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8812 01073a5d 2019-08-22 stsp switch (ch) {
8813 896e9b6f 2019-08-26 stsp case 'c':
8814 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8815 896e9b6f 2019-08-26 stsp break;
8816 01073a5d 2019-08-22 stsp case 'r':
8817 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8818 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8819 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8820 9ba1d308 2019-10-21 stsp optarg);
8821 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8822 01073a5d 2019-08-22 stsp break;
8823 896e9b6f 2019-08-26 stsp case 'P':
8824 896e9b6f 2019-08-26 stsp force_path = 1;
8825 896e9b6f 2019-08-26 stsp break;
8826 01073a5d 2019-08-22 stsp default:
8827 01073a5d 2019-08-22 stsp usage_cat();
8828 01073a5d 2019-08-22 stsp /* NOTREACHED */
8829 01073a5d 2019-08-22 stsp }
8830 01073a5d 2019-08-22 stsp }
8831 01073a5d 2019-08-22 stsp
8832 01073a5d 2019-08-22 stsp argc -= optind;
8833 01073a5d 2019-08-22 stsp argv += optind;
8834 01073a5d 2019-08-22 stsp
8835 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8836 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8837 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8838 01073a5d 2019-08-22 stsp goto done;
8839 01073a5d 2019-08-22 stsp }
8840 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8841 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8842 01073a5d 2019-08-22 stsp goto done;
8843 01073a5d 2019-08-22 stsp if (worktree) {
8844 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8845 01073a5d 2019-08-22 stsp repo_path = strdup(
8846 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8847 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8848 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8849 01073a5d 2019-08-22 stsp goto done;
8850 01073a5d 2019-08-22 stsp }
8851 01073a5d 2019-08-22 stsp }
8852 01073a5d 2019-08-22 stsp }
8853 01073a5d 2019-08-22 stsp
8854 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8855 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8856 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8857 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8858 01073a5d 2019-08-22 stsp }
8859 01073a5d 2019-08-22 stsp
8860 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8861 01073a5d 2019-08-22 stsp free(repo_path);
8862 01073a5d 2019-08-22 stsp if (error != NULL)
8863 01073a5d 2019-08-22 stsp goto done;
8864 01073a5d 2019-08-22 stsp
8865 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8866 896e9b6f 2019-08-26 stsp if (error)
8867 896e9b6f 2019-08-26 stsp goto done;
8868 896e9b6f 2019-08-26 stsp
8869 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8870 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8871 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8872 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8873 01073a5d 2019-08-22 stsp if (error)
8874 01073a5d 2019-08-22 stsp goto done;
8875 01073a5d 2019-08-22 stsp
8876 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8877 896e9b6f 2019-08-26 stsp if (force_path) {
8878 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8879 896e9b6f 2019-08-26 stsp argv[i]);
8880 896e9b6f 2019-08-26 stsp if (error)
8881 896e9b6f 2019-08-26 stsp break;
8882 896e9b6f 2019-08-26 stsp } else {
8883 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8884 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8885 896e9b6f 2019-08-26 stsp if (error) {
8886 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8887 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8888 896e9b6f 2019-08-26 stsp break;
8889 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8890 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8891 896e9b6f 2019-08-26 stsp if (error)
8892 896e9b6f 2019-08-26 stsp break;
8893 896e9b6f 2019-08-26 stsp }
8894 896e9b6f 2019-08-26 stsp }
8895 01073a5d 2019-08-22 stsp
8896 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8897 01073a5d 2019-08-22 stsp if (error)
8898 01073a5d 2019-08-22 stsp break;
8899 01073a5d 2019-08-22 stsp
8900 01073a5d 2019-08-22 stsp switch (obj_type) {
8901 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8902 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8903 01073a5d 2019-08-22 stsp break;
8904 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8905 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8906 01073a5d 2019-08-22 stsp break;
8907 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8908 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8909 01073a5d 2019-08-22 stsp break;
8910 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8911 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8912 01073a5d 2019-08-22 stsp break;
8913 01073a5d 2019-08-22 stsp default:
8914 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8915 01073a5d 2019-08-22 stsp break;
8916 01073a5d 2019-08-22 stsp }
8917 01073a5d 2019-08-22 stsp if (error)
8918 01073a5d 2019-08-22 stsp break;
8919 01073a5d 2019-08-22 stsp free(label);
8920 01073a5d 2019-08-22 stsp label = NULL;
8921 01073a5d 2019-08-22 stsp free(id);
8922 01073a5d 2019-08-22 stsp id = NULL;
8923 01073a5d 2019-08-22 stsp }
8924 01073a5d 2019-08-22 stsp done:
8925 01073a5d 2019-08-22 stsp free(label);
8926 01073a5d 2019-08-22 stsp free(id);
8927 896e9b6f 2019-08-26 stsp free(commit_id);
8928 01073a5d 2019-08-22 stsp if (worktree)
8929 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8930 01073a5d 2019-08-22 stsp if (repo) {
8931 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8932 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8933 01073a5d 2019-08-22 stsp if (error == NULL)
8934 01073a5d 2019-08-22 stsp error = repo_error;
8935 01073a5d 2019-08-22 stsp }
8936 0ebf8283 2019-07-24 stsp return error;
8937 0ebf8283 2019-07-24 stsp }