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 68999b92 2020-03-18 stsp fprintf(stderr, "usage: %s clone [-q] [-v] repository-url [target-directory]\n",
815 9df6f38b 2020-03-18 stsp 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 b2409d58 2020-03-18 stsp }
842 b2409d58 2020-03-18 stsp
843 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
844 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
845 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
846 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
847 892ac3b6 2020-03-18 stsp print_size = 1;
848 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
849 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
850 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
851 892ac3b6 2020-03-18 stsp }
852 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
853 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
854 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
855 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
856 892ac3b6 2020-03-18 stsp print_indexed = 1;
857 892ac3b6 2020-03-18 stsp print_size = 1;
858 892ac3b6 2020-03-18 stsp }
859 61cc1a7a 2020-03-18 stsp }
860 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
861 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
862 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
863 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
864 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
865 892ac3b6 2020-03-18 stsp print_resolved = 1;
866 892ac3b6 2020-03-18 stsp print_indexed = 1;
867 892ac3b6 2020-03-18 stsp print_size = 1;
868 892ac3b6 2020-03-18 stsp }
869 61cc1a7a 2020-03-18 stsp }
870 b2409d58 2020-03-18 stsp
871 d2cdc636 2020-03-18 stsp }
872 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
873 892ac3b6 2020-03-18 stsp printf("\r");
874 892ac3b6 2020-03-18 stsp if (print_size)
875 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
876 68999b92 2020-03-18 stsp if (print_indexed) {
877 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
878 68999b92 2020-03-18 stsp if (a->verbosity > 0)
879 68999b92 2020-03-18 stsp printf(" (%d/%d)", nobj_indexed, nobj_total);
880 68999b92 2020-03-18 stsp }
881 68999b92 2020-03-18 stsp if (print_resolved) {
882 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
883 68999b92 2020-03-18 stsp if (a->verbosity > 0)
884 68999b92 2020-03-18 stsp printf(" (%d/%d)", nobj_resolved,
885 68999b92 2020-03-18 stsp nobj_total - nobj_loose);
886 68999b92 2020-03-18 stsp }
887 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
888 892ac3b6 2020-03-18 stsp fflush(stdout);
889 892ac3b6 2020-03-18 stsp
890 892ac3b6 2020-03-18 stsp if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
891 892ac3b6 2020-03-18 stsp nobj_resolved == nobj_total - nobj_loose)
892 892ac3b6 2020-03-18 stsp printf("\nWriting pack index...\n");
893 892ac3b6 2020-03-18 stsp
894 531c3985 2020-03-18 stsp return NULL;
895 531c3985 2020-03-18 stsp }
896 531c3985 2020-03-18 stsp
897 531c3985 2020-03-18 stsp static const struct got_error *
898 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
899 93658fb9 2020-03-18 stsp {
900 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
901 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
902 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
903 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
904 bb64b798 2020-03-18 stsp const char *repo_path;
905 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
906 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
907 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
908 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
909 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
910 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
911 b46f3e71 2020-03-18 stsp char *git_url = NULL;
912 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
913 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
914 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
915 b46f3e71 2020-03-18 stsp ssize_t n;
916 68999b92 2020-03-18 stsp int verbosity = 0;
917 93658fb9 2020-03-18 stsp
918 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
919 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
920 d9b4d0c0 2020-03-18 stsp
921 68999b92 2020-03-18 stsp while ((ch = getopt(argc, argv, "vq")) != -1) {
922 93658fb9 2020-03-18 stsp switch (ch) {
923 68999b92 2020-03-18 stsp case 'v':
924 68999b92 2020-03-18 stsp if (verbosity < 0)
925 68999b92 2020-03-18 stsp verbosity = 0;
926 68999b92 2020-03-18 stsp else if (verbosity < 3)
927 68999b92 2020-03-18 stsp verbosity++;
928 68999b92 2020-03-18 stsp break;
929 68999b92 2020-03-18 stsp case 'q':
930 68999b92 2020-03-18 stsp verbosity = -1;
931 68999b92 2020-03-18 stsp break;
932 93658fb9 2020-03-18 stsp default:
933 93658fb9 2020-03-18 stsp usage_clone();
934 93658fb9 2020-03-18 stsp break;
935 93658fb9 2020-03-18 stsp }
936 93658fb9 2020-03-18 stsp }
937 93658fb9 2020-03-18 stsp argc -= optind;
938 93658fb9 2020-03-18 stsp argv += optind;
939 39c64a6a 2020-03-18 stsp
940 93658fb9 2020-03-18 stsp uri = argv[0];
941 9df6f38b 2020-03-18 stsp
942 9df6f38b 2020-03-18 stsp if (argc == 1)
943 93658fb9 2020-03-18 stsp dirname = NULL;
944 9df6f38b 2020-03-18 stsp else if (argc == 2)
945 93658fb9 2020-03-18 stsp dirname = argv[1];
946 93658fb9 2020-03-18 stsp else
947 93658fb9 2020-03-18 stsp usage_clone();
948 09838ffc 2020-03-18 stsp
949 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
950 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
951 39c64a6a 2020-03-18 stsp if (error)
952 09838ffc 2020-03-18 stsp goto done;
953 09838ffc 2020-03-18 stsp
954 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
955 b46f3e71 2020-03-18 stsp #ifndef PROFILE
956 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
957 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
958 39c64a6a 2020-03-18 stsp err(1, "pledge");
959 b46f3e71 2020-03-18 stsp #endif
960 b46f3e71 2020-03-18 stsp git_url = strdup(argv[0]);
961 b46f3e71 2020-03-18 stsp if (git_url == NULL) {
962 b46f3e71 2020-03-18 stsp error = got_error_from_errno("strdup");
963 b46f3e71 2020-03-18 stsp goto done;
964 b46f3e71 2020-03-18 stsp }
965 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
966 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
967 b46f3e71 2020-03-18 stsp #ifndef PROFILE
968 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
969 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
970 39c64a6a 2020-03-18 stsp err(1, "pledge");
971 b46f3e71 2020-03-18 stsp #endif
972 b46f3e71 2020-03-18 stsp if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
973 b46f3e71 2020-03-18 stsp error = got_error_from_errno("asprintf");
974 b46f3e71 2020-03-18 stsp goto done;
975 b46f3e71 2020-03-18 stsp }
976 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
977 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
978 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
979 39c64a6a 2020-03-18 stsp goto done;
980 39c64a6a 2020-03-18 stsp } else {
981 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
982 39c64a6a 2020-03-18 stsp goto done;
983 39c64a6a 2020-03-18 stsp }
984 bb64b798 2020-03-18 stsp if (dirname == NULL) {
985 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
986 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
987 bb64b798 2020-03-18 stsp goto done;
988 bb64b798 2020-03-18 stsp }
989 bb64b798 2020-03-18 stsp repo_path = default_destdir;
990 bb64b798 2020-03-18 stsp } else
991 bb64b798 2020-03-18 stsp repo_path = dirname;
992 bb64b798 2020-03-18 stsp
993 39c64a6a 2020-03-18 stsp error = got_path_mkdir(repo_path);
994 39c64a6a 2020-03-18 stsp if (error)
995 bb64b798 2020-03-18 stsp goto done;
996 bb64b798 2020-03-18 stsp
997 39c64a6a 2020-03-18 stsp error = got_repo_init(repo_path);
998 39c64a6a 2020-03-18 stsp if (error)
999 bb64b798 2020-03-18 stsp goto done;
1000 bb64b798 2020-03-18 stsp
1001 39c64a6a 2020-03-18 stsp error = got_repo_open(&repo, repo_path, NULL);
1002 39c64a6a 2020-03-18 stsp if (error)
1003 294dfefd 2020-03-18 stsp goto done;
1004 294dfefd 2020-03-18 stsp
1005 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1006 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1007 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1008 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1009 ee448f5f 2020-03-18 stsp goto done;
1010 ee448f5f 2020-03-18 stsp }
1011 ee448f5f 2020-03-18 stsp }
1012 ee448f5f 2020-03-18 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1013 ee448f5f 2020-03-18 stsp if (error)
1014 ee448f5f 2020-03-18 stsp goto done;
1015 ee448f5f 2020-03-18 stsp
1016 68999b92 2020-03-18 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1017 68999b92 2020-03-18 stsp verbosity);
1018 39c64a6a 2020-03-18 stsp if (error)
1019 bb64b798 2020-03-18 stsp goto done;
1020 294dfefd 2020-03-18 stsp
1021 68999b92 2020-03-18 stsp if (verbosity >= 0)
1022 68999b92 2020-03-18 stsp printf("Connected to %s:%s\n", host, port);
1023 bb64b798 2020-03-18 stsp
1024 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1025 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1026 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1027 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1028 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1029 7848a0e1 2020-03-19 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, fetchfd, repo,
1030 7848a0e1 2020-03-19 stsp fetch_progress, &fpa);
1031 39c64a6a 2020-03-18 stsp if (error)
1032 d9b4d0c0 2020-03-18 stsp goto done;
1033 d9b4d0c0 2020-03-18 stsp
1034 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1035 39c64a6a 2020-03-18 stsp if (error)
1036 d9b4d0c0 2020-03-18 stsp goto done;
1037 68999b92 2020-03-18 stsp if (verbosity >= 0)
1038 68999b92 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1039 d9b4d0c0 2020-03-18 stsp free(id_str);
1040 d9b4d0c0 2020-03-18 stsp
1041 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1042 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1043 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1044 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1045 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1046 7ebc0570 2020-03-18 stsp char *remote_refname;
1047 668a20f6 2020-03-18 stsp
1048 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1049 39c64a6a 2020-03-18 stsp if (error)
1050 d9b4d0c0 2020-03-18 stsp goto done;
1051 7ebc0570 2020-03-18 stsp error = got_ref_write(ref, repo);
1052 7ebc0570 2020-03-18 stsp got_ref_close(ref);
1053 7ebc0570 2020-03-18 stsp if (error)
1054 7ebc0570 2020-03-18 stsp goto done;
1055 d9b4d0c0 2020-03-18 stsp
1056 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1057 7ebc0570 2020-03-18 stsp continue;
1058 7ebc0570 2020-03-18 stsp
1059 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1060 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1061 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1062 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1063 7ebc0570 2020-03-18 stsp goto done;
1064 7ebc0570 2020-03-18 stsp }
1065 7ebc0570 2020-03-18 stsp error = got_ref_alloc(&ref, remote_refname, id);
1066 39c64a6a 2020-03-18 stsp if (error)
1067 d9b4d0c0 2020-03-18 stsp goto done;
1068 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1069 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1070 39c64a6a 2020-03-18 stsp if (error)
1071 d9b4d0c0 2020-03-18 stsp goto done;
1072 d9b4d0c0 2020-03-18 stsp }
1073 d9b4d0c0 2020-03-18 stsp
1074 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1075 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1076 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1077 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1078 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1079 d9b4d0c0 2020-03-18 stsp
1080 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1081 d9b4d0c0 2020-03-18 stsp continue;
1082 d9b4d0c0 2020-03-18 stsp
1083 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1084 39c64a6a 2020-03-18 stsp if (error) {
1085 39c64a6a 2020-03-18 stsp if (error->code == GOT_ERR_NOT_REF)
1086 d9b4d0c0 2020-03-18 stsp continue;
1087 d9b4d0c0 2020-03-18 stsp goto done;
1088 d9b4d0c0 2020-03-18 stsp }
1089 d9b4d0c0 2020-03-18 stsp
1090 39c64a6a 2020-03-18 stsp error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1091 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1092 39c64a6a 2020-03-18 stsp if (error)
1093 d9b4d0c0 2020-03-18 stsp goto done;
1094 d9b4d0c0 2020-03-18 stsp
1095 68999b92 2020-03-18 stsp if (verbosity > 1) {
1096 68999b92 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 68999b92 2020-03-18 stsp got_ref_get_symref_target(symref));
1098 68999b92 2020-03-18 stsp }
1099 d52aaa3d 2020-03-18 stsp if (verbosity >= 0)
1100 d52aaa3d 2020-03-18 stsp printf("Created cloned repository '%s'\n", repo_path);
1101 d9b4d0c0 2020-03-18 stsp
1102 39c64a6a 2020-03-18 stsp error = got_ref_write(symref, repo);
1103 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1104 d9b4d0c0 2020-03-18 stsp break;
1105 d9b4d0c0 2020-03-18 stsp }
1106 d9b4d0c0 2020-03-18 stsp
1107 b46f3e71 2020-03-18 stsp /* Create a config file so Git can understand this repository. */
1108 b46f3e71 2020-03-18 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1109 b46f3e71 2020-03-18 stsp if (gitconfig_path == NULL) {
1110 b46f3e71 2020-03-18 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1111 b46f3e71 2020-03-18 stsp goto done;
1112 b46f3e71 2020-03-18 stsp }
1113 b46f3e71 2020-03-18 stsp gitconfig_file = fopen(gitconfig_path, "w");
1114 b46f3e71 2020-03-18 stsp if (gitconfig_file == NULL) {
1115 b46f3e71 2020-03-18 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1116 b46f3e71 2020-03-18 stsp goto done;
1117 b46f3e71 2020-03-18 stsp }
1118 b46f3e71 2020-03-18 stsp if (asprintf(&gitconfig,
1119 b46f3e71 2020-03-18 stsp "[core]\n"
1120 b46f3e71 2020-03-18 stsp "\trepositoryformatversion = 0\n"
1121 b46f3e71 2020-03-18 stsp "\tbare = true\n"
1122 7ebc0570 2020-03-18 stsp "[remote \"%s\"]\n"
1123 b46f3e71 2020-03-18 stsp "\turl = %s\n"
1124 7ebc0570 2020-03-18 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1125 7ebc0570 2020-03-18 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1126 7ebc0570 2020-03-18 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1127 b46f3e71 2020-03-18 stsp error = got_error_from_errno("asprintf");
1128 b46f3e71 2020-03-18 stsp goto done;
1129 b46f3e71 2020-03-18 stsp }
1130 b46f3e71 2020-03-18 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1131 b46f3e71 2020-03-18 stsp if (n != strlen(gitconfig))
1132 b46f3e71 2020-03-18 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1133 09838ffc 2020-03-18 stsp done:
1134 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1135 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1136 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1137 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1138 bb64b798 2020-03-18 stsp if (repo)
1139 bb64b798 2020-03-18 stsp got_repo_close(repo);
1140 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1141 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1142 d9b4d0c0 2020-03-18 stsp free(pe->data);
1143 d9b4d0c0 2020-03-18 stsp }
1144 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1145 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1146 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1147 d9b4d0c0 2020-03-18 stsp free(pe->data);
1148 d9b4d0c0 2020-03-18 stsp }
1149 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1150 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1151 09838ffc 2020-03-18 stsp free(proto);
1152 09838ffc 2020-03-18 stsp free(host);
1153 09838ffc 2020-03-18 stsp free(port);
1154 09838ffc 2020-03-18 stsp free(server_path);
1155 09838ffc 2020-03-18 stsp free(repo_name);
1156 bb64b798 2020-03-18 stsp free(default_destdir);
1157 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1158 b46f3e71 2020-03-18 stsp free(git_url);
1159 39c64a6a 2020-03-18 stsp return error;
1160 7848a0e1 2020-03-19 stsp }
1161 7848a0e1 2020-03-19 stsp
1162 7848a0e1 2020-03-19 stsp static const struct got_error *
1163 7848a0e1 2020-03-19 stsp create_ref(const char *refname, struct got_object_id *id,
1164 7848a0e1 2020-03-19 stsp const char *id_str, struct got_repository *repo)
1165 7848a0e1 2020-03-19 stsp {
1166 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1167 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1168 7848a0e1 2020-03-19 stsp
1169 7848a0e1 2020-03-19 stsp printf("Creating %s: %s\n", refname, id_str);
1170 7848a0e1 2020-03-19 stsp
1171 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&ref, refname, id);
1172 7848a0e1 2020-03-19 stsp if (err)
1173 7848a0e1 2020-03-19 stsp return err;
1174 7848a0e1 2020-03-19 stsp
1175 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1176 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1177 7848a0e1 2020-03-19 stsp return err;
1178 7848a0e1 2020-03-19 stsp }
1179 7848a0e1 2020-03-19 stsp
1180 7848a0e1 2020-03-19 stsp static const struct got_error *
1181 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1182 7848a0e1 2020-03-19 stsp struct got_repository *repo)
1183 7848a0e1 2020-03-19 stsp {
1184 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1185 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1186 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1187 7848a0e1 2020-03-19 stsp
1188 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1189 7848a0e1 2020-03-19 stsp if (err)
1190 7848a0e1 2020-03-19 stsp goto done;
1191 7848a0e1 2020-03-19 stsp
1192 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1193 7848a0e1 2020-03-19 stsp struct got_reference *new_ref;
1194 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1195 7848a0e1 2020-03-19 stsp if (err)
1196 7848a0e1 2020-03-19 stsp goto done;
1197 7848a0e1 2020-03-19 stsp printf("Deleting symbolic reference %s -> %s\n",
1198 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), got_ref_get_symref_target(ref));
1199 7848a0e1 2020-03-19 stsp err = got_ref_delete(ref, repo);
1200 7848a0e1 2020-03-19 stsp if (err)
1201 7848a0e1 2020-03-19 stsp goto done;
1202 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n", got_ref_get_name(ref),
1203 7848a0e1 2020-03-19 stsp new_id_str);
1204 7848a0e1 2020-03-19 stsp err = got_ref_write(new_ref, repo);
1205 7848a0e1 2020-03-19 stsp if (err)
1206 7848a0e1 2020-03-19 stsp goto done;
1207 7848a0e1 2020-03-19 stsp } else {
1208 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1209 7848a0e1 2020-03-19 stsp if (err)
1210 7848a0e1 2020-03-19 stsp goto done;
1211 7848a0e1 2020-03-19 stsp if (got_object_id_cmp(old_id, new_id) != 0) {
1212 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n",
1213 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), new_id_str);
1214 7848a0e1 2020-03-19 stsp err = got_ref_change_ref(ref, new_id);
1215 7848a0e1 2020-03-19 stsp if (err)
1216 7848a0e1 2020-03-19 stsp goto done;
1217 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1218 7848a0e1 2020-03-19 stsp if (err)
1219 7848a0e1 2020-03-19 stsp goto done;
1220 7848a0e1 2020-03-19 stsp }
1221 7848a0e1 2020-03-19 stsp }
1222 7848a0e1 2020-03-19 stsp done:
1223 7848a0e1 2020-03-19 stsp free(old_id);
1224 7848a0e1 2020-03-19 stsp free(new_id_str);
1225 7848a0e1 2020-03-19 stsp return err;
1226 2ab43947 2020-03-18 stsp }
1227 2ab43947 2020-03-18 stsp
1228 2ab43947 2020-03-18 stsp __dead static void
1229 7848a0e1 2020-03-19 stsp usage_fetch(void)
1230 7848a0e1 2020-03-19 stsp {
1231 7848a0e1 2020-03-19 stsp fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1232 7848a0e1 2020-03-19 stsp "[remote-repository-name]\n", getprogname());
1233 7848a0e1 2020-03-19 stsp exit(1);
1234 7848a0e1 2020-03-19 stsp }
1235 7848a0e1 2020-03-19 stsp
1236 7848a0e1 2020-03-19 stsp static const struct got_error *
1237 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1238 7848a0e1 2020-03-19 stsp {
1239 7848a0e1 2020-03-19 stsp const struct got_error *error = NULL;
1240 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1241 7848a0e1 2020-03-19 stsp const char *remote_name;
1242 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1243 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1244 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1245 7848a0e1 2020-03-19 stsp int nremotes;
1246 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1247 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1248 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1249 7848a0e1 2020-03-19 stsp struct got_pathlist_head refs, symrefs;
1250 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1251 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1252 7848a0e1 2020-03-19 stsp int i, ch, fetchfd = -1;
1253 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1254 7848a0e1 2020-03-19 stsp int verbosity = 0;
1255 7848a0e1 2020-03-19 stsp
1256 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1257 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1258 7848a0e1 2020-03-19 stsp
1259 7848a0e1 2020-03-19 stsp while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1260 7848a0e1 2020-03-19 stsp switch (ch) {
1261 7848a0e1 2020-03-19 stsp case 'r':
1262 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1263 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1264 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1265 7848a0e1 2020-03-19 stsp optarg);
1266 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1267 7848a0e1 2020-03-19 stsp break;
1268 7848a0e1 2020-03-19 stsp case 'v':
1269 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1270 7848a0e1 2020-03-19 stsp verbosity = 0;
1271 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1272 7848a0e1 2020-03-19 stsp verbosity++;
1273 7848a0e1 2020-03-19 stsp break;
1274 7848a0e1 2020-03-19 stsp case 'q':
1275 7848a0e1 2020-03-19 stsp verbosity = -1;
1276 7848a0e1 2020-03-19 stsp break;
1277 7848a0e1 2020-03-19 stsp default:
1278 7848a0e1 2020-03-19 stsp usage_fetch();
1279 7848a0e1 2020-03-19 stsp break;
1280 7848a0e1 2020-03-19 stsp }
1281 7848a0e1 2020-03-19 stsp }
1282 7848a0e1 2020-03-19 stsp argc -= optind;
1283 7848a0e1 2020-03-19 stsp argv += optind;
1284 7848a0e1 2020-03-19 stsp
1285 7848a0e1 2020-03-19 stsp if (argc == 0)
1286 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1287 7848a0e1 2020-03-19 stsp else if (argc == 1)
1288 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1289 7848a0e1 2020-03-19 stsp else
1290 7848a0e1 2020-03-19 stsp usage_fetch();
1291 7848a0e1 2020-03-19 stsp
1292 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1293 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1294 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1295 7848a0e1 2020-03-19 stsp goto done;
1296 7848a0e1 2020-03-19 stsp }
1297 7848a0e1 2020-03-19 stsp
1298 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1299 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1300 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1301 7848a0e1 2020-03-19 stsp goto done;
1302 7848a0e1 2020-03-19 stsp else
1303 7848a0e1 2020-03-19 stsp error = NULL;
1304 7848a0e1 2020-03-19 stsp if (worktree) {
1305 7848a0e1 2020-03-19 stsp repo_path =
1306 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1307 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1308 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1309 7848a0e1 2020-03-19 stsp if (error)
1310 7848a0e1 2020-03-19 stsp goto done;
1311 7848a0e1 2020-03-19 stsp } else {
1312 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1313 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1314 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1315 7848a0e1 2020-03-19 stsp goto done;
1316 7848a0e1 2020-03-19 stsp }
1317 7848a0e1 2020-03-19 stsp }
1318 7848a0e1 2020-03-19 stsp }
1319 7848a0e1 2020-03-19 stsp
1320 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1321 7848a0e1 2020-03-19 stsp if (error)
1322 7848a0e1 2020-03-19 stsp goto done;
1323 7848a0e1 2020-03-19 stsp
1324 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1325 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1326 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1327 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1328 7848a0e1 2020-03-19 stsp break;
1329 7848a0e1 2020-03-19 stsp }
1330 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1331 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1332 7848a0e1 2020-03-19 stsp goto done;
1333 7848a0e1 2020-03-19 stsp }
1334 7848a0e1 2020-03-19 stsp
1335 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1336 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1337 7848a0e1 2020-03-19 stsp if (error)
1338 7848a0e1 2020-03-19 stsp goto done;
1339 7848a0e1 2020-03-19 stsp
1340 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1341 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1342 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1343 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1344 7848a0e1 2020-03-19 stsp err(1, "pledge");
1345 7848a0e1 2020-03-19 stsp #endif
1346 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1347 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1348 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1349 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1350 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1351 7848a0e1 2020-03-19 stsp err(1, "pledge");
1352 7848a0e1 2020-03-19 stsp #endif
1353 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1354 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1355 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1356 7848a0e1 2020-03-19 stsp goto done;
1357 7848a0e1 2020-03-19 stsp } else {
1358 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1359 7848a0e1 2020-03-19 stsp goto done;
1360 7848a0e1 2020-03-19 stsp }
1361 7848a0e1 2020-03-19 stsp
1362 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1363 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1364 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1365 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1366 7848a0e1 2020-03-19 stsp goto done;
1367 7848a0e1 2020-03-19 stsp }
1368 7848a0e1 2020-03-19 stsp }
1369 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1370 7848a0e1 2020-03-19 stsp if (error)
1371 7848a0e1 2020-03-19 stsp goto done;
1372 7848a0e1 2020-03-19 stsp
1373 7848a0e1 2020-03-19 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1374 7848a0e1 2020-03-19 stsp verbosity);
1375 7848a0e1 2020-03-19 stsp if (error)
1376 7848a0e1 2020-03-19 stsp goto done;
1377 7848a0e1 2020-03-19 stsp
1378 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1379 7848a0e1 2020-03-19 stsp printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1380 7848a0e1 2020-03-19 stsp
1381 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1382 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1383 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1384 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1385 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1386 7848a0e1 2020-03-19 stsp fetchfd, repo, fetch_progress, &fpa);
1387 7848a0e1 2020-03-19 stsp if (error)
1388 7848a0e1 2020-03-19 stsp goto done;
1389 7848a0e1 2020-03-19 stsp
1390 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1391 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1392 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1393 7848a0e1 2020-03-19 stsp goto done;
1394 7848a0e1 2020-03-19 stsp }
1395 7848a0e1 2020-03-19 stsp
1396 7848a0e1 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1397 7848a0e1 2020-03-19 stsp if (error)
1398 7848a0e1 2020-03-19 stsp goto done;
1399 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1400 7848a0e1 2020-03-19 stsp printf("Fetched %s.pack\n", id_str);
1401 7848a0e1 2020-03-19 stsp free(id_str);
1402 7848a0e1 2020-03-19 stsp id_str = NULL;
1403 7848a0e1 2020-03-19 stsp
1404 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1405 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1406 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1407 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1408 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1409 7848a0e1 2020-03-19 stsp char *remote_refname;
1410 7848a0e1 2020-03-19 stsp
1411 7848a0e1 2020-03-19 stsp error = got_object_id_str(&id_str, id);
1412 7848a0e1 2020-03-19 stsp if (error)
1413 7848a0e1 2020-03-19 stsp goto done;
1414 7848a0e1 2020-03-19 stsp
1415 7848a0e1 2020-03-19 stsp if (strncmp("refs/tags/", refname, 10) == 0) {
1416 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, refname, 0);
1417 7848a0e1 2020-03-19 stsp if (error) {
1418 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1419 7848a0e1 2020-03-19 stsp goto done;
1420 7848a0e1 2020-03-19 stsp error = create_ref(refname, id, id_str, repo);
1421 7848a0e1 2020-03-19 stsp if (error)
1422 7848a0e1 2020-03-19 stsp goto done;
1423 7848a0e1 2020-03-19 stsp } else {
1424 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1425 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1426 7848a0e1 2020-03-19 stsp if (error)
1427 7848a0e1 2020-03-19 stsp goto done;
1428 7848a0e1 2020-03-19 stsp }
1429 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1430 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1431 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1432 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1433 7848a0e1 2020-03-19 stsp goto done;
1434 7848a0e1 2020-03-19 stsp }
1435 7848a0e1 2020-03-19 stsp
1436 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, remote_refname, 0);
1437 7848a0e1 2020-03-19 stsp if (error) {
1438 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1439 7848a0e1 2020-03-19 stsp goto done;
1440 7848a0e1 2020-03-19 stsp error = create_ref(refname, id, id_str, repo);
1441 7848a0e1 2020-03-19 stsp if (error)
1442 7848a0e1 2020-03-19 stsp goto done;
1443 7848a0e1 2020-03-19 stsp } else {
1444 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1445 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1446 7848a0e1 2020-03-19 stsp if (error)
1447 7848a0e1 2020-03-19 stsp goto done;
1448 7848a0e1 2020-03-19 stsp }
1449 7848a0e1 2020-03-19 stsp }
1450 7848a0e1 2020-03-19 stsp free(id_str);
1451 7848a0e1 2020-03-19 stsp id_str = NULL;
1452 7848a0e1 2020-03-19 stsp }
1453 7848a0e1 2020-03-19 stsp done:
1454 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1455 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1456 7848a0e1 2020-03-19 stsp if (repo)
1457 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1458 7848a0e1 2020-03-19 stsp if (worktree)
1459 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1460 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1461 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1462 7848a0e1 2020-03-19 stsp free(pe->data);
1463 7848a0e1 2020-03-19 stsp }
1464 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1465 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1466 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1467 7848a0e1 2020-03-19 stsp free(pe->data);
1468 7848a0e1 2020-03-19 stsp }
1469 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1470 7848a0e1 2020-03-19 stsp free(id_str);
1471 7848a0e1 2020-03-19 stsp free(cwd);
1472 7848a0e1 2020-03-19 stsp free(repo_path);
1473 7848a0e1 2020-03-19 stsp free(pack_hash);
1474 7848a0e1 2020-03-19 stsp free(proto);
1475 7848a0e1 2020-03-19 stsp free(host);
1476 7848a0e1 2020-03-19 stsp free(port);
1477 7848a0e1 2020-03-19 stsp free(server_path);
1478 7848a0e1 2020-03-19 stsp free(repo_name);
1479 7848a0e1 2020-03-19 stsp return error;
1480 7848a0e1 2020-03-19 stsp }
1481 7848a0e1 2020-03-19 stsp
1482 7848a0e1 2020-03-19 stsp
1483 7848a0e1 2020-03-19 stsp __dead static void
1484 2ab43947 2020-03-18 stsp usage_checkout(void)
1485 2ab43947 2020-03-18 stsp {
1486 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1487 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1488 2ab43947 2020-03-18 stsp exit(1);
1489 2ab43947 2020-03-18 stsp }
1490 2ab43947 2020-03-18 stsp
1491 2ab43947 2020-03-18 stsp static void
1492 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1493 2ab43947 2020-03-18 stsp {
1494 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1495 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1496 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1497 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1498 2ab43947 2020-03-18 stsp getprogname());
1499 2ab43947 2020-03-18 stsp }
1500 2ab43947 2020-03-18 stsp
1501 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1502 2ab43947 2020-03-18 stsp const char *worktree_path;
1503 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1504 2ab43947 2020-03-18 stsp };
1505 2ab43947 2020-03-18 stsp
1506 2ab43947 2020-03-18 stsp static const struct got_error *
1507 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1508 2ab43947 2020-03-18 stsp {
1509 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1510 2ab43947 2020-03-18 stsp
1511 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1512 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1513 2ab43947 2020-03-18 stsp return NULL;
1514 2ab43947 2020-03-18 stsp
1515 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1516 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1517 2ab43947 2020-03-18 stsp return NULL;
1518 2ab43947 2020-03-18 stsp }
1519 2ab43947 2020-03-18 stsp
1520 2ab43947 2020-03-18 stsp while (path[0] == '/')
1521 2ab43947 2020-03-18 stsp path++;
1522 2ab43947 2020-03-18 stsp
1523 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1524 2ab43947 2020-03-18 stsp return NULL;
1525 2ab43947 2020-03-18 stsp }
1526 2ab43947 2020-03-18 stsp
1527 2ab43947 2020-03-18 stsp static const struct got_error *
1528 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1529 2ab43947 2020-03-18 stsp {
1530 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1531 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1532 2ab43947 2020-03-18 stsp return NULL;
1533 2ab43947 2020-03-18 stsp }
1534 2ab43947 2020-03-18 stsp
1535 2ab43947 2020-03-18 stsp static const struct got_error *
1536 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1537 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1538 2ab43947 2020-03-18 stsp struct got_repository *repo)
1539 2ab43947 2020-03-18 stsp {
1540 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1541 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1542 2ab43947 2020-03-18 stsp
1543 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1544 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1545 2ab43947 2020-03-18 stsp if (err)
1546 2ab43947 2020-03-18 stsp return err;
1547 2ab43947 2020-03-18 stsp
1548 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1549 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1550 2ab43947 2020-03-18 stsp
1551 2ab43947 2020-03-18 stsp /*
1552 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1553 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1554 2ab43947 2020-03-18 stsp *
1555 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1556 2ab43947 2020-03-18 stsp *
1557 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1558 2ab43947 2020-03-18 stsp * \ /
1559 2ab43947 2020-03-18 stsp * C E
1560 2ab43947 2020-03-18 stsp * \ /
1561 2ab43947 2020-03-18 stsp * B (yca)
1562 2ab43947 2020-03-18 stsp * |
1563 2ab43947 2020-03-18 stsp * A
1564 2ab43947 2020-03-18 stsp *
1565 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1566 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1567 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1568 2ab43947 2020-03-18 stsp */
1569 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1570 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1571 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1572 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1573 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1574 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1575 2ab43947 2020-03-18 stsp
1576 2ab43947 2020-03-18 stsp free(yca_id);
1577 2ab43947 2020-03-18 stsp return NULL;
1578 2ab43947 2020-03-18 stsp }
1579 2ab43947 2020-03-18 stsp
1580 2ab43947 2020-03-18 stsp static const struct got_error *
1581 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1582 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1583 2ab43947 2020-03-18 stsp struct got_repository *repo)
1584 2ab43947 2020-03-18 stsp {
1585 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1586 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1587 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1588 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1589 2ab43947 2020-03-18 stsp
1590 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1591 2ab43947 2020-03-18 stsp if (err)
1592 2ab43947 2020-03-18 stsp goto done;
1593 2ab43947 2020-03-18 stsp
1594 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1595 2ab43947 2020-03-18 stsp is_same_branch = 1;
1596 2ab43947 2020-03-18 stsp goto done;
1597 2ab43947 2020-03-18 stsp }
1598 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1599 2ab43947 2020-03-18 stsp is_same_branch = 1;
1600 2ab43947 2020-03-18 stsp goto done;
1601 2ab43947 2020-03-18 stsp }
1602 2ab43947 2020-03-18 stsp
1603 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1604 2ab43947 2020-03-18 stsp if (err)
1605 2ab43947 2020-03-18 stsp goto done;
1606 2ab43947 2020-03-18 stsp
1607 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1608 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1609 2ab43947 2020-03-18 stsp if (err)
1610 2ab43947 2020-03-18 stsp goto done;
1611 2ab43947 2020-03-18 stsp
1612 2ab43947 2020-03-18 stsp for (;;) {
1613 2ab43947 2020-03-18 stsp struct got_object_id *id;
1614 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1615 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1616 2ab43947 2020-03-18 stsp if (err) {
1617 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1618 2ab43947 2020-03-18 stsp err = NULL;
1619 2ab43947 2020-03-18 stsp break;
1620 2ab43947 2020-03-18 stsp }
1621 2ab43947 2020-03-18 stsp
1622 2ab43947 2020-03-18 stsp if (id) {
1623 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1624 2ab43947 2020-03-18 stsp break;
1625 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1626 2ab43947 2020-03-18 stsp is_same_branch = 1;
1627 2ab43947 2020-03-18 stsp break;
1628 2ab43947 2020-03-18 stsp }
1629 2ab43947 2020-03-18 stsp }
1630 2ab43947 2020-03-18 stsp }
1631 2ab43947 2020-03-18 stsp done:
1632 2ab43947 2020-03-18 stsp if (graph)
1633 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1634 2ab43947 2020-03-18 stsp free(head_commit_id);
1635 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1636 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1637 2ab43947 2020-03-18 stsp return err;
1638 a367ff0f 2019-05-14 stsp }
1639 8069f636 2019-01-12 stsp
1640 4ed7e80c 2018-05-20 stsp static const struct got_error *
1641 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1642 4b6c9460 2020-03-05 stsp {
1643 4b6c9460 2020-03-05 stsp static char msg[512];
1644 4b6c9460 2020-03-05 stsp const char *branch_name;
1645 4b6c9460 2020-03-05 stsp
1646 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1647 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1648 4b6c9460 2020-03-05 stsp else
1649 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1650 4b6c9460 2020-03-05 stsp
1651 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1652 4b6c9460 2020-03-05 stsp branch_name += 11;
1653 4b6c9460 2020-03-05 stsp
1654 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1655 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1656 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1657 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1658 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1659 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1660 4b6c9460 2020-03-05 stsp
1661 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1662 4b6c9460 2020-03-05 stsp }
1663 4b6c9460 2020-03-05 stsp
1664 4b6c9460 2020-03-05 stsp static const struct got_error *
1665 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1666 c09a553d 2018-03-12 stsp {
1667 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1668 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1669 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1670 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1671 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1672 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1673 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1674 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1675 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1676 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1677 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1678 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1679 f2ea84fa 2019-07-27 stsp
1680 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1681 c09a553d 2018-03-12 stsp
1682 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1683 0bb8a95e 2018-03-12 stsp switch (ch) {
1684 08573d5b 2019-05-14 stsp case 'b':
1685 08573d5b 2019-05-14 stsp branch_name = optarg;
1686 08573d5b 2019-05-14 stsp break;
1687 8069f636 2019-01-12 stsp case 'c':
1688 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1689 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1690 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1691 bb51a5b4 2020-01-13 stsp break;
1692 bb51a5b4 2020-01-13 stsp case 'E':
1693 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1694 8069f636 2019-01-12 stsp break;
1695 0bb8a95e 2018-03-12 stsp case 'p':
1696 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1697 0bb8a95e 2018-03-12 stsp break;
1698 0bb8a95e 2018-03-12 stsp default:
1699 2deda0b9 2019-03-07 stsp usage_checkout();
1700 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1701 0bb8a95e 2018-03-12 stsp }
1702 0bb8a95e 2018-03-12 stsp }
1703 0bb8a95e 2018-03-12 stsp
1704 0bb8a95e 2018-03-12 stsp argc -= optind;
1705 0bb8a95e 2018-03-12 stsp argv += optind;
1706 0bb8a95e 2018-03-12 stsp
1707 6715a751 2018-03-16 stsp #ifndef PROFILE
1708 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1709 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1710 c09a553d 2018-03-12 stsp err(1, "pledge");
1711 6715a751 2018-03-16 stsp #endif
1712 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1713 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1714 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1715 76089277 2018-04-01 stsp if (repo_path == NULL)
1716 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1717 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1718 76089277 2018-04-01 stsp if (cwd == NULL) {
1719 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1720 76089277 2018-04-01 stsp goto done;
1721 76089277 2018-04-01 stsp }
1722 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1723 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1724 230a42bd 2019-05-11 jcs if (base == NULL) {
1725 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1726 230a42bd 2019-05-11 jcs path_prefix);
1727 230a42bd 2019-05-11 jcs goto done;
1728 230a42bd 2019-05-11 jcs }
1729 230a42bd 2019-05-11 jcs } else {
1730 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1731 230a42bd 2019-05-11 jcs if (base == NULL) {
1732 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1733 230a42bd 2019-05-11 jcs repo_path);
1734 230a42bd 2019-05-11 jcs goto done;
1735 230a42bd 2019-05-11 jcs }
1736 76089277 2018-04-01 stsp }
1737 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1738 c09a553d 2018-03-12 stsp if (dotgit)
1739 c09a553d 2018-03-12 stsp *dotgit = '\0';
1740 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1741 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1742 c09a553d 2018-03-12 stsp free(cwd);
1743 76089277 2018-04-01 stsp goto done;
1744 c09a553d 2018-03-12 stsp }
1745 c09a553d 2018-03-12 stsp free(cwd);
1746 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1747 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1748 76089277 2018-04-01 stsp if (repo_path == NULL) {
1749 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1750 76089277 2018-04-01 stsp goto done;
1751 76089277 2018-04-01 stsp }
1752 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1753 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1754 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1755 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1756 b4b3a7dd 2019-07-22 stsp argv[1]);
1757 b4b3a7dd 2019-07-22 stsp goto done;
1758 b4b3a7dd 2019-07-22 stsp }
1759 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1760 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1761 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1762 b4b3a7dd 2019-07-22 stsp goto done;
1763 b4b3a7dd 2019-07-22 stsp }
1764 76089277 2018-04-01 stsp }
1765 c09a553d 2018-03-12 stsp } else
1766 c09a553d 2018-03-12 stsp usage_checkout();
1767 c09a553d 2018-03-12 stsp
1768 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1769 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1770 13bfb272 2019-05-10 jcs
1771 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1772 c09a553d 2018-03-12 stsp if (error != NULL)
1773 c02c541e 2019-03-29 stsp goto done;
1774 c02c541e 2019-03-29 stsp
1775 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1776 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1777 c530dc23 2019-07-23 stsp if (error) {
1778 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1779 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1780 c530dc23 2019-07-23 stsp goto done;
1781 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1782 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1783 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1784 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1785 c530dc23 2019-07-23 stsp goto done;
1786 c530dc23 2019-07-23 stsp }
1787 c530dc23 2019-07-23 stsp }
1788 c530dc23 2019-07-23 stsp
1789 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1790 c02c541e 2019-03-29 stsp if (error)
1791 c09a553d 2018-03-12 stsp goto done;
1792 8069f636 2019-01-12 stsp
1793 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1794 c09a553d 2018-03-12 stsp if (error != NULL)
1795 c09a553d 2018-03-12 stsp goto done;
1796 c09a553d 2018-03-12 stsp
1797 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1798 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1799 c09a553d 2018-03-12 stsp goto done;
1800 c09a553d 2018-03-12 stsp
1801 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1802 c09a553d 2018-03-12 stsp if (error != NULL)
1803 c09a553d 2018-03-12 stsp goto done;
1804 c09a553d 2018-03-12 stsp
1805 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1806 e5dc7198 2018-12-29 stsp path_prefix);
1807 e5dc7198 2018-12-29 stsp if (error != NULL)
1808 e5dc7198 2018-12-29 stsp goto done;
1809 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1810 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1811 49520a32 2018-12-29 stsp goto done;
1812 49520a32 2018-12-29 stsp }
1813 49520a32 2018-12-29 stsp
1814 8069f636 2019-01-12 stsp if (commit_id_str) {
1815 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1816 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1817 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1818 30837e32 2019-07-25 stsp if (error)
1819 8069f636 2019-01-12 stsp goto done;
1820 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1821 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1822 8069f636 2019-01-12 stsp if (error != NULL) {
1823 8069f636 2019-01-12 stsp free(commit_id);
1824 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1825 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1826 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1827 4b6c9460 2020-03-05 stsp }
1828 8069f636 2019-01-12 stsp goto done;
1829 8069f636 2019-01-12 stsp }
1830 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1831 4b6c9460 2020-03-05 stsp if (error) {
1832 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1833 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1834 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1835 4b6c9460 2020-03-05 stsp }
1836 45d344f6 2019-05-14 stsp goto done;
1837 4b6c9460 2020-03-05 stsp }
1838 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1839 8069f636 2019-01-12 stsp commit_id);
1840 8069f636 2019-01-12 stsp free(commit_id);
1841 8069f636 2019-01-12 stsp if (error)
1842 8069f636 2019-01-12 stsp goto done;
1843 8069f636 2019-01-12 stsp }
1844 8069f636 2019-01-12 stsp
1845 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1846 f2ea84fa 2019-07-27 stsp if (error)
1847 f2ea84fa 2019-07-27 stsp goto done;
1848 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1849 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1850 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1851 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1852 c09a553d 2018-03-12 stsp if (error != NULL)
1853 c09a553d 2018-03-12 stsp goto done;
1854 c09a553d 2018-03-12 stsp
1855 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1856 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1857 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1858 c09a553d 2018-03-12 stsp done:
1859 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1860 8069f636 2019-01-12 stsp free(commit_id_str);
1861 76089277 2018-04-01 stsp free(repo_path);
1862 507dc3bb 2018-12-29 stsp free(worktree_path);
1863 507dc3bb 2018-12-29 stsp return error;
1864 507dc3bb 2018-12-29 stsp }
1865 507dc3bb 2018-12-29 stsp
1866 507dc3bb 2018-12-29 stsp __dead static void
1867 507dc3bb 2018-12-29 stsp usage_update(void)
1868 507dc3bb 2018-12-29 stsp {
1869 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1870 507dc3bb 2018-12-29 stsp getprogname());
1871 507dc3bb 2018-12-29 stsp exit(1);
1872 507dc3bb 2018-12-29 stsp }
1873 507dc3bb 2018-12-29 stsp
1874 1ee397ad 2019-07-12 stsp static const struct got_error *
1875 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1876 507dc3bb 2018-12-29 stsp {
1877 784955db 2019-01-12 stsp int *did_something = arg;
1878 784955db 2019-01-12 stsp
1879 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1880 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1881 1ee397ad 2019-07-12 stsp return NULL;
1882 507dc3bb 2018-12-29 stsp
1883 1545c615 2019-02-10 stsp *did_something = 1;
1884 a484d721 2019-06-10 stsp
1885 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1886 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1887 1ee397ad 2019-07-12 stsp return NULL;
1888 a484d721 2019-06-10 stsp
1889 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1890 507dc3bb 2018-12-29 stsp path++;
1891 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1892 1ee397ad 2019-07-12 stsp return NULL;
1893 be7061eb 2018-12-30 stsp }
1894 be7061eb 2018-12-30 stsp
1895 be7061eb 2018-12-30 stsp static const struct got_error *
1896 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1897 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1898 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1899 a1fb16d8 2019-05-24 stsp {
1900 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1901 a1fb16d8 2019-05-24 stsp char *base_id_str;
1902 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1903 a1fb16d8 2019-05-24 stsp
1904 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1905 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1906 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1907 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1908 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1909 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1910 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1911 a1fb16d8 2019-05-24 stsp }
1912 a1fb16d8 2019-05-24 stsp
1913 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1914 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1915 a1fb16d8 2019-05-24 stsp if (err) {
1916 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1917 a1fb16d8 2019-05-24 stsp return err;
1918 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1919 a1fb16d8 2019-05-24 stsp }
1920 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1921 a1fb16d8 2019-05-24 stsp return NULL;
1922 a1fb16d8 2019-05-24 stsp
1923 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1924 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1925 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1926 a1fb16d8 2019-05-24 stsp if (err)
1927 a1fb16d8 2019-05-24 stsp return err;
1928 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1929 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1930 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1931 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1932 0ebf8283 2019-07-24 stsp return NULL;
1933 0ebf8283 2019-07-24 stsp }
1934 0ebf8283 2019-07-24 stsp
1935 0ebf8283 2019-07-24 stsp static const struct got_error *
1936 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1937 0ebf8283 2019-07-24 stsp {
1938 0ebf8283 2019-07-24 stsp const struct got_error *err;
1939 0ebf8283 2019-07-24 stsp int in_progress;
1940 0ebf8283 2019-07-24 stsp
1941 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1942 0ebf8283 2019-07-24 stsp if (err)
1943 0ebf8283 2019-07-24 stsp return err;
1944 0ebf8283 2019-07-24 stsp if (in_progress)
1945 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1946 0ebf8283 2019-07-24 stsp
1947 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1948 0ebf8283 2019-07-24 stsp if (err)
1949 0ebf8283 2019-07-24 stsp return err;
1950 0ebf8283 2019-07-24 stsp if (in_progress)
1951 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1952 0ebf8283 2019-07-24 stsp
1953 a1fb16d8 2019-05-24 stsp return NULL;
1954 a5edda0a 2019-07-27 stsp }
1955 a5edda0a 2019-07-27 stsp
1956 a5edda0a 2019-07-27 stsp static const struct got_error *
1957 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1958 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1959 a5edda0a 2019-07-27 stsp {
1960 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1961 a5edda0a 2019-07-27 stsp char *path;
1962 a5edda0a 2019-07-27 stsp int i;
1963 a5edda0a 2019-07-27 stsp
1964 a5edda0a 2019-07-27 stsp if (argc == 0) {
1965 a5edda0a 2019-07-27 stsp path = strdup("");
1966 a5edda0a 2019-07-27 stsp if (path == NULL)
1967 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1968 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1969 a5edda0a 2019-07-27 stsp }
1970 a5edda0a 2019-07-27 stsp
1971 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1972 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1973 a5edda0a 2019-07-27 stsp if (err)
1974 a5edda0a 2019-07-27 stsp break;
1975 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1976 a5edda0a 2019-07-27 stsp if (err) {
1977 a5edda0a 2019-07-27 stsp free(path);
1978 a5edda0a 2019-07-27 stsp break;
1979 a5edda0a 2019-07-27 stsp }
1980 a5edda0a 2019-07-27 stsp }
1981 a5edda0a 2019-07-27 stsp
1982 a5edda0a 2019-07-27 stsp return err;
1983 a1fb16d8 2019-05-24 stsp }
1984 a1fb16d8 2019-05-24 stsp
1985 a1fb16d8 2019-05-24 stsp static const struct got_error *
1986 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1987 507dc3bb 2018-12-29 stsp {
1988 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1989 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1990 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1991 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1992 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1993 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1994 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1995 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1996 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1997 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1998 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1999 507dc3bb 2018-12-29 stsp
2000 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2001 f2ea84fa 2019-07-27 stsp
2002 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2003 507dc3bb 2018-12-29 stsp switch (ch) {
2004 024e9686 2019-05-14 stsp case 'b':
2005 024e9686 2019-05-14 stsp branch_name = optarg;
2006 024e9686 2019-05-14 stsp break;
2007 507dc3bb 2018-12-29 stsp case 'c':
2008 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2009 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2010 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2011 507dc3bb 2018-12-29 stsp break;
2012 507dc3bb 2018-12-29 stsp default:
2013 2deda0b9 2019-03-07 stsp usage_update();
2014 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2015 507dc3bb 2018-12-29 stsp }
2016 507dc3bb 2018-12-29 stsp }
2017 507dc3bb 2018-12-29 stsp
2018 507dc3bb 2018-12-29 stsp argc -= optind;
2019 507dc3bb 2018-12-29 stsp argv += optind;
2020 507dc3bb 2018-12-29 stsp
2021 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2022 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2023 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2024 507dc3bb 2018-12-29 stsp err(1, "pledge");
2025 507dc3bb 2018-12-29 stsp #endif
2026 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2027 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2028 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2029 c4cdcb68 2019-04-03 stsp goto done;
2030 c4cdcb68 2019-04-03 stsp }
2031 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2032 7d5807f4 2019-07-11 stsp if (error)
2033 7d5807f4 2019-07-11 stsp goto done;
2034 7d5807f4 2019-07-11 stsp
2035 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2036 f2ea84fa 2019-07-27 stsp if (error)
2037 f2ea84fa 2019-07-27 stsp goto done;
2038 507dc3bb 2018-12-29 stsp
2039 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2040 c9956ddf 2019-09-08 stsp NULL);
2041 507dc3bb 2018-12-29 stsp if (error != NULL)
2042 507dc3bb 2018-12-29 stsp goto done;
2043 507dc3bb 2018-12-29 stsp
2044 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2045 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2046 087fb88c 2019-08-04 stsp if (error)
2047 087fb88c 2019-08-04 stsp goto done;
2048 087fb88c 2019-08-04 stsp
2049 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2050 0266afb7 2019-01-04 stsp if (error)
2051 0266afb7 2019-01-04 stsp goto done;
2052 0266afb7 2019-01-04 stsp
2053 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2054 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2055 024e9686 2019-05-14 stsp if (error != NULL)
2056 024e9686 2019-05-14 stsp goto done;
2057 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2058 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2059 9c4b8182 2019-01-02 stsp if (error != NULL)
2060 9c4b8182 2019-01-02 stsp goto done;
2061 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2062 507dc3bb 2018-12-29 stsp if (error != NULL)
2063 507dc3bb 2018-12-29 stsp goto done;
2064 507dc3bb 2018-12-29 stsp } else {
2065 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2066 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2067 04f57cb3 2019-07-25 stsp free(commit_id_str);
2068 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2069 30837e32 2019-07-25 stsp if (error)
2070 a297e751 2019-07-11 stsp goto done;
2071 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2072 a297e751 2019-07-11 stsp if (error)
2073 507dc3bb 2018-12-29 stsp goto done;
2074 507dc3bb 2018-12-29 stsp }
2075 35c965b2 2018-12-29 stsp
2076 a1fb16d8 2019-05-24 stsp if (branch_name) {
2077 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2078 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2079 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2080 f2ea84fa 2019-07-27 stsp continue;
2081 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2082 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2083 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2084 024e9686 2019-05-14 stsp goto done;
2085 024e9686 2019-05-14 stsp }
2086 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2087 024e9686 2019-05-14 stsp if (error)
2088 024e9686 2019-05-14 stsp goto done;
2089 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2090 3aef623b 2019-10-15 stsp repo);
2091 a367ff0f 2019-05-14 stsp free(head_commit_id);
2092 024e9686 2019-05-14 stsp if (error != NULL)
2093 024e9686 2019-05-14 stsp goto done;
2094 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2095 a367ff0f 2019-05-14 stsp if (error)
2096 a367ff0f 2019-05-14 stsp goto done;
2097 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2098 024e9686 2019-05-14 stsp if (error)
2099 024e9686 2019-05-14 stsp goto done;
2100 024e9686 2019-05-14 stsp } else {
2101 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2102 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2103 a367ff0f 2019-05-14 stsp if (error != NULL) {
2104 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2105 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2106 024e9686 2019-05-14 stsp goto done;
2107 a367ff0f 2019-05-14 stsp }
2108 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2109 a367ff0f 2019-05-14 stsp if (error)
2110 a367ff0f 2019-05-14 stsp goto done;
2111 024e9686 2019-05-14 stsp }
2112 507dc3bb 2018-12-29 stsp
2113 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2114 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2115 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2116 507dc3bb 2018-12-29 stsp commit_id);
2117 507dc3bb 2018-12-29 stsp if (error)
2118 507dc3bb 2018-12-29 stsp goto done;
2119 507dc3bb 2018-12-29 stsp }
2120 507dc3bb 2018-12-29 stsp
2121 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2122 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2123 507dc3bb 2018-12-29 stsp if (error != NULL)
2124 507dc3bb 2018-12-29 stsp goto done;
2125 9c4b8182 2019-01-02 stsp
2126 784955db 2019-01-12 stsp if (did_something)
2127 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2128 784955db 2019-01-12 stsp else
2129 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2130 507dc3bb 2018-12-29 stsp done:
2131 c09a553d 2018-03-12 stsp free(worktree_path);
2132 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2133 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2134 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2135 507dc3bb 2018-12-29 stsp free(commit_id);
2136 9c4b8182 2019-01-02 stsp free(commit_id_str);
2137 c09a553d 2018-03-12 stsp return error;
2138 c09a553d 2018-03-12 stsp }
2139 c09a553d 2018-03-12 stsp
2140 f42b1b34 2018-03-12 stsp static const struct got_error *
2141 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2142 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2143 63035f9f 2019-10-06 stsp struct got_repository *repo)
2144 5c860e29 2018-03-12 stsp {
2145 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2146 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2147 79109fed 2018-03-27 stsp
2148 44392932 2019-08-25 stsp if (blob_id1) {
2149 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2150 44392932 2019-08-25 stsp if (err)
2151 44392932 2019-08-25 stsp goto done;
2152 44392932 2019-08-25 stsp }
2153 79109fed 2018-03-27 stsp
2154 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2155 44392932 2019-08-25 stsp if (err)
2156 44392932 2019-08-25 stsp goto done;
2157 79109fed 2018-03-27 stsp
2158 44392932 2019-08-25 stsp while (path[0] == '/')
2159 44392932 2019-08-25 stsp path++;
2160 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2161 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2162 44392932 2019-08-25 stsp done:
2163 44392932 2019-08-25 stsp if (blob1)
2164 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2165 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2166 44392932 2019-08-25 stsp return err;
2167 44392932 2019-08-25 stsp }
2168 44392932 2019-08-25 stsp
2169 44392932 2019-08-25 stsp static const struct got_error *
2170 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2171 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2172 63035f9f 2019-10-06 stsp struct got_repository *repo)
2173 44392932 2019-08-25 stsp {
2174 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2175 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2176 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2177 44392932 2019-08-25 stsp
2178 44392932 2019-08-25 stsp if (tree_id1) {
2179 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2180 79109fed 2018-03-27 stsp if (err)
2181 44392932 2019-08-25 stsp goto done;
2182 79109fed 2018-03-27 stsp }
2183 79109fed 2018-03-27 stsp
2184 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2185 0f2b3dca 2018-12-22 stsp if (err)
2186 0f2b3dca 2018-12-22 stsp goto done;
2187 0f2b3dca 2018-12-22 stsp
2188 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2189 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2190 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2191 44392932 2019-08-25 stsp while (path[0] == '/')
2192 44392932 2019-08-25 stsp path++;
2193 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2194 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2195 0f2b3dca 2018-12-22 stsp done:
2196 79109fed 2018-03-27 stsp if (tree1)
2197 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2198 366e0a5f 2019-10-10 stsp if (tree2)
2199 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2200 44392932 2019-08-25 stsp return err;
2201 44392932 2019-08-25 stsp }
2202 44392932 2019-08-25 stsp
2203 44392932 2019-08-25 stsp static const struct got_error *
2204 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2205 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2206 44392932 2019-08-25 stsp {
2207 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2208 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2209 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2210 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2211 44392932 2019-08-25 stsp struct got_object_qid *qid;
2212 44392932 2019-08-25 stsp
2213 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2214 44392932 2019-08-25 stsp if (qid != NULL) {
2215 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2216 44392932 2019-08-25 stsp qid->id);
2217 44392932 2019-08-25 stsp if (err)
2218 44392932 2019-08-25 stsp return err;
2219 44392932 2019-08-25 stsp }
2220 44392932 2019-08-25 stsp
2221 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2222 44392932 2019-08-25 stsp int obj_type;
2223 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2224 44392932 2019-08-25 stsp if (err)
2225 44392932 2019-08-25 stsp goto done;
2226 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2227 44392932 2019-08-25 stsp if (err) {
2228 44392932 2019-08-25 stsp free(obj_id2);
2229 44392932 2019-08-25 stsp goto done;
2230 44392932 2019-08-25 stsp }
2231 44392932 2019-08-25 stsp if (pcommit) {
2232 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2233 44392932 2019-08-25 stsp qid->id, path);
2234 44392932 2019-08-25 stsp if (err) {
2235 44392932 2019-08-25 stsp free(obj_id2);
2236 44392932 2019-08-25 stsp goto done;
2237 44392932 2019-08-25 stsp }
2238 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2239 44392932 2019-08-25 stsp if (err) {
2240 44392932 2019-08-25 stsp free(obj_id2);
2241 44392932 2019-08-25 stsp goto done;
2242 44392932 2019-08-25 stsp }
2243 44392932 2019-08-25 stsp }
2244 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2245 44392932 2019-08-25 stsp if (err) {
2246 44392932 2019-08-25 stsp free(obj_id2);
2247 44392932 2019-08-25 stsp goto done;
2248 44392932 2019-08-25 stsp }
2249 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2250 44392932 2019-08-25 stsp switch (obj_type) {
2251 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2252 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2253 63035f9f 2019-10-06 stsp 0, repo);
2254 44392932 2019-08-25 stsp break;
2255 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2256 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2257 63035f9f 2019-10-06 stsp 0, repo);
2258 44392932 2019-08-25 stsp break;
2259 44392932 2019-08-25 stsp default:
2260 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2261 44392932 2019-08-25 stsp break;
2262 44392932 2019-08-25 stsp }
2263 44392932 2019-08-25 stsp free(obj_id1);
2264 44392932 2019-08-25 stsp free(obj_id2);
2265 44392932 2019-08-25 stsp } else {
2266 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2267 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2268 44392932 2019-08-25 stsp if (err)
2269 44392932 2019-08-25 stsp goto done;
2270 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2271 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2272 44392932 2019-08-25 stsp if (err)
2273 44392932 2019-08-25 stsp goto done;
2274 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2275 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2276 44392932 2019-08-25 stsp }
2277 44392932 2019-08-25 stsp done:
2278 0f2b3dca 2018-12-22 stsp free(id_str1);
2279 0f2b3dca 2018-12-22 stsp free(id_str2);
2280 44392932 2019-08-25 stsp if (pcommit)
2281 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2282 79109fed 2018-03-27 stsp return err;
2283 79109fed 2018-03-27 stsp }
2284 79109fed 2018-03-27 stsp
2285 4bb494d5 2018-06-16 stsp static char *
2286 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2287 6c281f94 2018-06-11 stsp {
2288 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2289 09867e48 2019-08-13 stsp char *p, *s;
2290 09867e48 2019-08-13 stsp
2291 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2292 09867e48 2019-08-13 stsp if (tm == NULL)
2293 09867e48 2019-08-13 stsp return NULL;
2294 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2295 09867e48 2019-08-13 stsp if (s == NULL)
2296 09867e48 2019-08-13 stsp return NULL;
2297 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2298 6c281f94 2018-06-11 stsp if (p)
2299 6c281f94 2018-06-11 stsp *p = '\0';
2300 6c281f94 2018-06-11 stsp return s;
2301 6c281f94 2018-06-11 stsp }
2302 dc424a06 2019-08-07 stsp
2303 6841bf13 2019-11-29 kn static const struct got_error *
2304 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2305 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2306 6841bf13 2019-11-29 kn {
2307 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2308 6841bf13 2019-11-29 kn regmatch_t regmatch;
2309 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2310 6841bf13 2019-11-29 kn
2311 6841bf13 2019-11-29 kn *have_match = 0;
2312 6841bf13 2019-11-29 kn
2313 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2314 6841bf13 2019-11-29 kn if (err)
2315 6841bf13 2019-11-29 kn return err;
2316 6841bf13 2019-11-29 kn
2317 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2318 6841bf13 2019-11-29 kn if (err)
2319 6841bf13 2019-11-29 kn goto done;
2320 6841bf13 2019-11-29 kn
2321 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2322 6841bf13 2019-11-29 kn *have_match = 1;
2323 6841bf13 2019-11-29 kn done:
2324 6841bf13 2019-11-29 kn free(id_str);
2325 6841bf13 2019-11-29 kn free(logmsg);
2326 6841bf13 2019-11-29 kn return err;
2327 6841bf13 2019-11-29 kn }
2328 6841bf13 2019-11-29 kn
2329 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2330 6c281f94 2018-06-11 stsp
2331 79109fed 2018-03-27 stsp static const struct got_error *
2332 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2333 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2334 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2335 79109fed 2018-03-27 stsp {
2336 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2337 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2338 6c281f94 2018-06-11 stsp char datebuf[26];
2339 45d799e2 2018-12-23 stsp time_t committer_time;
2340 45d799e2 2018-12-23 stsp const char *author, *committer;
2341 199a4027 2019-02-02 stsp char *refs_str = NULL;
2342 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2343 5c860e29 2018-03-12 stsp
2344 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2345 199a4027 2019-02-02 stsp char *s;
2346 199a4027 2019-02-02 stsp const char *name;
2347 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2348 a436ad14 2019-08-13 stsp int cmp;
2349 a436ad14 2019-08-13 stsp
2350 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2351 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2352 d9498b20 2019-02-05 stsp continue;
2353 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2354 199a4027 2019-02-02 stsp name += 5;
2355 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2356 7143d404 2019-03-12 stsp continue;
2357 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2358 e34f9ed6 2019-02-02 stsp name += 6;
2359 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2360 141c2bff 2019-02-04 stsp name += 8;
2361 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2362 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2363 5d844a1e 2019-08-13 stsp if (err) {
2364 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2365 5d844a1e 2019-08-13 stsp return err;
2366 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2367 5d844a1e 2019-08-13 stsp err = NULL;
2368 5d844a1e 2019-08-13 stsp tag = NULL;
2369 5d844a1e 2019-08-13 stsp }
2370 a436ad14 2019-08-13 stsp }
2371 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2372 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2373 a436ad14 2019-08-13 stsp if (tag)
2374 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2375 a436ad14 2019-08-13 stsp if (cmp != 0)
2376 a436ad14 2019-08-13 stsp continue;
2377 199a4027 2019-02-02 stsp s = refs_str;
2378 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2379 199a4027 2019-02-02 stsp name) == -1) {
2380 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2381 199a4027 2019-02-02 stsp free(s);
2382 34ca4898 2019-08-13 stsp return err;
2383 199a4027 2019-02-02 stsp }
2384 199a4027 2019-02-02 stsp free(s);
2385 199a4027 2019-02-02 stsp }
2386 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2387 8bf5b3c9 2018-03-17 stsp if (err)
2388 8bf5b3c9 2018-03-17 stsp return err;
2389 788c352e 2018-06-16 stsp
2390 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2391 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2392 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2393 832c249c 2018-06-10 stsp free(id_str);
2394 d3d493d7 2019-02-21 stsp id_str = NULL;
2395 d3d493d7 2019-02-21 stsp free(refs_str);
2396 d3d493d7 2019-02-21 stsp refs_str = NULL;
2397 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2398 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2399 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2400 09867e48 2019-08-13 stsp if (datestr)
2401 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2402 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2403 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2404 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2405 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2406 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2407 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2408 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2409 3fe1abad 2018-06-10 stsp int n = 1;
2410 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2411 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2412 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2413 a0603db2 2018-06-10 stsp if (err)
2414 a0603db2 2018-06-10 stsp return err;
2415 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2416 a0603db2 2018-06-10 stsp free(id_str);
2417 a0603db2 2018-06-10 stsp }
2418 a0603db2 2018-06-10 stsp }
2419 832c249c 2018-06-10 stsp
2420 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2421 5943eee2 2019-08-13 stsp if (err)
2422 5943eee2 2019-08-13 stsp return err;
2423 8bf5b3c9 2018-03-17 stsp
2424 621015ac 2018-07-23 stsp logmsg = logmsg0;
2425 832c249c 2018-06-10 stsp do {
2426 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2427 832c249c 2018-06-10 stsp if (line)
2428 832c249c 2018-06-10 stsp printf(" %s\n", line);
2429 832c249c 2018-06-10 stsp } while (line);
2430 621015ac 2018-07-23 stsp free(logmsg0);
2431 832c249c 2018-06-10 stsp
2432 971751ac 2018-03-27 stsp if (show_patch) {
2433 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2434 971751ac 2018-03-27 stsp if (err == 0)
2435 971751ac 2018-03-27 stsp printf("\n");
2436 971751ac 2018-03-27 stsp }
2437 07862c20 2018-09-15 stsp
2438 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2439 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2440 07862c20 2018-09-15 stsp return err;
2441 f42b1b34 2018-03-12 stsp }
2442 5c860e29 2018-03-12 stsp
2443 f42b1b34 2018-03-12 stsp static const struct got_error *
2444 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2445 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2446 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2447 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2448 f42b1b34 2018-03-12 stsp {
2449 f42b1b34 2018-03-12 stsp const struct got_error *err;
2450 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2451 6841bf13 2019-11-29 kn regex_t regex;
2452 6841bf13 2019-11-29 kn int have_match;
2453 372ccdbb 2018-06-10 stsp
2454 6841bf13 2019-11-29 kn if (search_pattern &&
2455 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2456 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2457 6841bf13 2019-11-29 kn
2458 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2459 f42b1b34 2018-03-12 stsp if (err)
2460 f42b1b34 2018-03-12 stsp return err;
2461 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2462 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2463 372ccdbb 2018-06-10 stsp if (err)
2464 fcc85cad 2018-07-22 stsp goto done;
2465 656b1f76 2019-05-11 jcs for (;;) {
2466 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2467 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2468 8bf5b3c9 2018-03-17 stsp
2469 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2470 84453469 2018-11-11 stsp break;
2471 84453469 2018-11-11 stsp
2472 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2473 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2474 372ccdbb 2018-06-10 stsp if (err) {
2475 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2476 9ba79e04 2018-06-11 stsp err = NULL;
2477 ee780d5c 2020-01-04 stsp break;
2478 7e665116 2018-04-02 stsp }
2479 b43fbaa0 2018-06-11 stsp if (id == NULL)
2480 7e665116 2018-04-02 stsp break;
2481 b43fbaa0 2018-06-11 stsp
2482 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2483 b43fbaa0 2018-06-11 stsp if (err)
2484 fcc85cad 2018-07-22 stsp break;
2485 6841bf13 2019-11-29 kn
2486 6841bf13 2019-11-29 kn if (search_pattern) {
2487 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2488 6841bf13 2019-11-29 kn if (err) {
2489 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2490 6841bf13 2019-11-29 kn break;
2491 6841bf13 2019-11-29 kn }
2492 6841bf13 2019-11-29 kn if (have_match == 0) {
2493 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2494 6841bf13 2019-11-29 kn continue;
2495 6841bf13 2019-11-29 kn }
2496 6841bf13 2019-11-29 kn }
2497 6841bf13 2019-11-29 kn
2498 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2499 44392932 2019-08-25 stsp diff_context, refs);
2500 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2501 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2502 7e665116 2018-04-02 stsp break;
2503 31cedeaf 2018-09-15 stsp }
2504 fcc85cad 2018-07-22 stsp done:
2505 6841bf13 2019-11-29 kn if (search_pattern)
2506 6841bf13 2019-11-29 kn regfree(&regex);
2507 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2508 f42b1b34 2018-03-12 stsp return err;
2509 f42b1b34 2018-03-12 stsp }
2510 5c860e29 2018-03-12 stsp
2511 4ed7e80c 2018-05-20 stsp __dead static void
2512 6f3d1eb0 2018-03-12 stsp usage_log(void)
2513 6f3d1eb0 2018-03-12 stsp {
2514 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2515 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2516 6f3d1eb0 2018-03-12 stsp exit(1);
2517 b1ebc001 2019-08-13 stsp }
2518 b1ebc001 2019-08-13 stsp
2519 b1ebc001 2019-08-13 stsp static int
2520 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2521 b1ebc001 2019-08-13 stsp {
2522 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2523 b1ebc001 2019-08-13 stsp long long n;
2524 b1ebc001 2019-08-13 stsp const char *errstr;
2525 b1ebc001 2019-08-13 stsp
2526 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2527 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2528 b1ebc001 2019-08-13 stsp return 0;
2529 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2530 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2531 b1ebc001 2019-08-13 stsp return 0;
2532 b1ebc001 2019-08-13 stsp return n;
2533 6f3d1eb0 2018-03-12 stsp }
2534 6f3d1eb0 2018-03-12 stsp
2535 4ed7e80c 2018-05-20 stsp static const struct got_error *
2536 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2537 f42b1b34 2018-03-12 stsp {
2538 f42b1b34 2018-03-12 stsp const struct got_error *error;
2539 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2540 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2541 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2542 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2543 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2544 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2545 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2546 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2547 64a96a6d 2018-04-01 stsp const char *errstr;
2548 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2549 1b3893a2 2019-03-18 stsp
2550 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2551 5c860e29 2018-03-12 stsp
2552 6715a751 2018-03-16 stsp #ifndef PROFILE
2553 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2554 6098196c 2019-01-04 stsp NULL)
2555 ad242220 2018-09-08 stsp == -1)
2556 f42b1b34 2018-03-12 stsp err(1, "pledge");
2557 6715a751 2018-03-16 stsp #endif
2558 79109fed 2018-03-27 stsp
2559 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2560 b1ebc001 2019-08-13 stsp
2561 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2562 79109fed 2018-03-27 stsp switch (ch) {
2563 79109fed 2018-03-27 stsp case 'p':
2564 79109fed 2018-03-27 stsp show_patch = 1;
2565 d142fc45 2018-04-01 stsp break;
2566 d142fc45 2018-04-01 stsp case 'c':
2567 d142fc45 2018-04-01 stsp start_commit = optarg;
2568 64a96a6d 2018-04-01 stsp break;
2569 c0cc5c62 2018-10-18 stsp case 'C':
2570 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2571 4a8520aa 2018-10-18 stsp &errstr);
2572 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2573 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2574 c0cc5c62 2018-10-18 stsp break;
2575 64a96a6d 2018-04-01 stsp case 'l':
2576 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2577 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2578 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2579 cc54c501 2019-07-15 stsp break;
2580 48c8c60d 2020-01-27 stsp case 'b':
2581 48c8c60d 2020-01-27 stsp log_branches = 1;
2582 a0603db2 2018-06-10 stsp break;
2583 04ca23f4 2018-07-16 stsp case 'r':
2584 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2585 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2586 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2587 9ba1d308 2019-10-21 stsp optarg);
2588 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2589 04ca23f4 2018-07-16 stsp break;
2590 6841bf13 2019-11-29 kn case 's':
2591 6841bf13 2019-11-29 kn search_pattern = optarg;
2592 6841bf13 2019-11-29 kn break;
2593 79109fed 2018-03-27 stsp default:
2594 2deda0b9 2019-03-07 stsp usage_log();
2595 79109fed 2018-03-27 stsp /* NOTREACHED */
2596 79109fed 2018-03-27 stsp }
2597 79109fed 2018-03-27 stsp }
2598 79109fed 2018-03-27 stsp
2599 79109fed 2018-03-27 stsp argc -= optind;
2600 79109fed 2018-03-27 stsp argv += optind;
2601 f42b1b34 2018-03-12 stsp
2602 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2603 dc1edbfa 2019-11-29 kn diff_context = 3;
2604 dc1edbfa 2019-11-29 kn else if (!show_patch)
2605 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2606 dc1edbfa 2019-11-29 kn
2607 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2608 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2609 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2610 04ca23f4 2018-07-16 stsp goto done;
2611 04ca23f4 2018-07-16 stsp }
2612 cffc0aa4 2019-02-05 stsp
2613 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2614 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2615 cffc0aa4 2019-02-05 stsp goto done;
2616 cffc0aa4 2019-02-05 stsp error = NULL;
2617 cffc0aa4 2019-02-05 stsp
2618 e7301579 2019-03-18 stsp if (argc == 0) {
2619 cbd1af7a 2019-03-18 stsp path = strdup("");
2620 e7301579 2019-03-18 stsp if (path == NULL) {
2621 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2622 cbd1af7a 2019-03-18 stsp goto done;
2623 e7301579 2019-03-18 stsp }
2624 e7301579 2019-03-18 stsp } else if (argc == 1) {
2625 e7301579 2019-03-18 stsp if (worktree) {
2626 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2627 e7301579 2019-03-18 stsp argv[0]);
2628 e7301579 2019-03-18 stsp if (error)
2629 e7301579 2019-03-18 stsp goto done;
2630 e7301579 2019-03-18 stsp } else {
2631 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2632 e7301579 2019-03-18 stsp if (path == NULL) {
2633 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2634 e7301579 2019-03-18 stsp goto done;
2635 e7301579 2019-03-18 stsp }
2636 e7301579 2019-03-18 stsp }
2637 cbd1af7a 2019-03-18 stsp } else
2638 cbd1af7a 2019-03-18 stsp usage_log();
2639 cbd1af7a 2019-03-18 stsp
2640 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2641 5486daa2 2019-05-11 stsp repo_path = worktree ?
2642 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2643 5486daa2 2019-05-11 stsp }
2644 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2645 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2646 cffc0aa4 2019-02-05 stsp goto done;
2647 04ca23f4 2018-07-16 stsp }
2648 6098196c 2019-01-04 stsp
2649 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2650 d7d4f210 2018-03-12 stsp if (error != NULL)
2651 04ca23f4 2018-07-16 stsp goto done;
2652 f42b1b34 2018-03-12 stsp
2653 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2654 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2655 c02c541e 2019-03-29 stsp if (error)
2656 c02c541e 2019-03-29 stsp goto done;
2657 c02c541e 2019-03-29 stsp
2658 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2659 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2660 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2661 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2662 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2663 3235492e 2018-04-01 stsp if (error != NULL)
2664 3235492e 2018-04-01 stsp return error;
2665 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2666 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2667 3235492e 2018-04-01 stsp if (error != NULL)
2668 3235492e 2018-04-01 stsp return error;
2669 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2670 3235492e 2018-04-01 stsp } else {
2671 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2672 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2673 3235492e 2018-04-01 stsp if (error == NULL) {
2674 1caad61b 2019-02-01 stsp int obj_type;
2675 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2676 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2677 d1f2edc9 2018-06-13 stsp if (error != NULL)
2678 1caad61b 2019-02-01 stsp goto done;
2679 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2680 1caad61b 2019-02-01 stsp if (error != NULL)
2681 1caad61b 2019-02-01 stsp goto done;
2682 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2683 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2684 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2685 1caad61b 2019-02-01 stsp if (error != NULL)
2686 1caad61b 2019-02-01 stsp goto done;
2687 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2688 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2689 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2690 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2691 1caad61b 2019-02-01 stsp goto done;
2692 1caad61b 2019-02-01 stsp }
2693 1caad61b 2019-02-01 stsp free(id);
2694 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2695 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2696 1caad61b 2019-02-01 stsp if (id == NULL)
2697 638f9024 2019-05-13 stsp error = got_error_from_errno(
2698 230a42bd 2019-05-11 jcs "got_object_id_dup");
2699 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2700 1caad61b 2019-02-01 stsp if (error)
2701 1caad61b 2019-02-01 stsp goto done;
2702 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2703 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2704 1caad61b 2019-02-01 stsp goto done;
2705 1caad61b 2019-02-01 stsp }
2706 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2707 d1f2edc9 2018-06-13 stsp if (error != NULL)
2708 1caad61b 2019-02-01 stsp goto done;
2709 d1f2edc9 2018-06-13 stsp }
2710 15a94983 2018-12-23 stsp if (commit == NULL) {
2711 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2712 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2713 d1f2edc9 2018-06-13 stsp if (error != NULL)
2714 d1f2edc9 2018-06-13 stsp return error;
2715 3235492e 2018-04-01 stsp }
2716 3235492e 2018-04-01 stsp }
2717 d7d4f210 2018-03-12 stsp if (error != NULL)
2718 04ca23f4 2018-07-16 stsp goto done;
2719 04ca23f4 2018-07-16 stsp
2720 deeabeae 2019-08-27 stsp if (worktree) {
2721 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2722 deeabeae 2019-08-27 stsp char *p;
2723 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2724 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2725 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2726 deeabeae 2019-08-27 stsp goto done;
2727 deeabeae 2019-08-27 stsp }
2728 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2729 deeabeae 2019-08-27 stsp free(p);
2730 deeabeae 2019-08-27 stsp } else
2731 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2732 04ca23f4 2018-07-16 stsp if (error != NULL)
2733 04ca23f4 2018-07-16 stsp goto done;
2734 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2735 04ca23f4 2018-07-16 stsp free(path);
2736 04ca23f4 2018-07-16 stsp path = in_repo_path;
2737 04ca23f4 2018-07-16 stsp }
2738 199a4027 2019-02-02 stsp
2739 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2740 199a4027 2019-02-02 stsp if (error)
2741 199a4027 2019-02-02 stsp goto done;
2742 04ca23f4 2018-07-16 stsp
2743 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2744 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2745 04ca23f4 2018-07-16 stsp done:
2746 04ca23f4 2018-07-16 stsp free(path);
2747 04ca23f4 2018-07-16 stsp free(repo_path);
2748 04ca23f4 2018-07-16 stsp free(cwd);
2749 f42b1b34 2018-03-12 stsp free(id);
2750 cffc0aa4 2019-02-05 stsp if (worktree)
2751 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2752 ad242220 2018-09-08 stsp if (repo) {
2753 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2754 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2755 ad242220 2018-09-08 stsp if (error == NULL)
2756 ad242220 2018-09-08 stsp error = repo_error;
2757 ad242220 2018-09-08 stsp }
2758 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2759 8bf5b3c9 2018-03-17 stsp return error;
2760 5c860e29 2018-03-12 stsp }
2761 5c860e29 2018-03-12 stsp
2762 4ed7e80c 2018-05-20 stsp __dead static void
2763 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2764 3f8b7d6a 2018-04-01 stsp {
2765 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2766 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2767 3f8b7d6a 2018-04-01 stsp exit(1);
2768 b00d56cd 2018-04-01 stsp }
2769 b00d56cd 2018-04-01 stsp
2770 b72f483a 2019-02-05 stsp struct print_diff_arg {
2771 b72f483a 2019-02-05 stsp struct got_repository *repo;
2772 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2773 b72f483a 2019-02-05 stsp int diff_context;
2774 3fc0c068 2019-02-10 stsp const char *id_str;
2775 3fc0c068 2019-02-10 stsp int header_shown;
2776 98eaaa12 2019-08-03 stsp int diff_staged;
2777 63035f9f 2019-10-06 stsp int ignore_whitespace;
2778 b72f483a 2019-02-05 stsp };
2779 b72f483a 2019-02-05 stsp
2780 4ed7e80c 2018-05-20 stsp static const struct got_error *
2781 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2782 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2783 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2784 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2785 b72f483a 2019-02-05 stsp {
2786 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2787 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2788 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2789 12463d8b 2019-12-13 stsp int fd = -1;
2790 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2791 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2792 b72f483a 2019-02-05 stsp struct stat sb;
2793 b72f483a 2019-02-05 stsp
2794 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2795 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2796 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2797 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2798 98eaaa12 2019-08-03 stsp return NULL;
2799 98eaaa12 2019-08-03 stsp } else {
2800 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2801 98eaaa12 2019-08-03 stsp return NULL;
2802 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2803 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2804 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2805 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2806 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2807 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2808 98eaaa12 2019-08-03 stsp return NULL;
2809 98eaaa12 2019-08-03 stsp }
2810 3fc0c068 2019-02-10 stsp
2811 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2812 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2813 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2814 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2815 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2816 3fc0c068 2019-02-10 stsp }
2817 b72f483a 2019-02-05 stsp
2818 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2819 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2820 98eaaa12 2019-08-03 stsp switch (staged_status) {
2821 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2822 98eaaa12 2019-08-03 stsp label1 = path;
2823 98eaaa12 2019-08-03 stsp label2 = path;
2824 98eaaa12 2019-08-03 stsp break;
2825 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2826 98eaaa12 2019-08-03 stsp label2 = path;
2827 98eaaa12 2019-08-03 stsp break;
2828 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2829 98eaaa12 2019-08-03 stsp label1 = path;
2830 98eaaa12 2019-08-03 stsp break;
2831 98eaaa12 2019-08-03 stsp default:
2832 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2833 98eaaa12 2019-08-03 stsp }
2834 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2835 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2836 63035f9f 2019-10-06 stsp a->repo, stdout);
2837 98eaaa12 2019-08-03 stsp }
2838 98eaaa12 2019-08-03 stsp
2839 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2840 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2841 4ce46740 2019-08-08 stsp char *id_str;
2842 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2843 408b4ebc 2019-08-03 stsp 8192);
2844 4ce46740 2019-08-08 stsp if (err)
2845 4ce46740 2019-08-08 stsp goto done;
2846 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2847 4ce46740 2019-08-08 stsp if (err)
2848 4ce46740 2019-08-08 stsp goto done;
2849 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2850 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2851 4ce46740 2019-08-08 stsp free(id_str);
2852 4ce46740 2019-08-08 stsp goto done;
2853 4ce46740 2019-08-08 stsp }
2854 4ce46740 2019-08-08 stsp free(id_str);
2855 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2856 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2857 4ce46740 2019-08-08 stsp if (err)
2858 4ce46740 2019-08-08 stsp goto done;
2859 4ce46740 2019-08-08 stsp }
2860 d00136be 2019-03-26 stsp
2861 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2862 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2863 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2864 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2865 2ec1f75b 2019-03-26 stsp goto done;
2866 2ec1f75b 2019-03-26 stsp }
2867 b72f483a 2019-02-05 stsp
2868 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2869 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2870 12463d8b 2019-12-13 stsp if (fd == -1) {
2871 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2872 12463d8b 2019-12-13 stsp goto done;
2873 12463d8b 2019-12-13 stsp }
2874 12463d8b 2019-12-13 stsp } else {
2875 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2876 12463d8b 2019-12-13 stsp if (fd == -1) {
2877 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2878 12463d8b 2019-12-13 stsp goto done;
2879 12463d8b 2019-12-13 stsp }
2880 12463d8b 2019-12-13 stsp }
2881 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2882 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2883 2ec1f75b 2019-03-26 stsp goto done;
2884 2ec1f75b 2019-03-26 stsp }
2885 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2886 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2887 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2888 2ec1f75b 2019-03-26 stsp goto done;
2889 2ec1f75b 2019-03-26 stsp }
2890 12463d8b 2019-12-13 stsp fd = -1;
2891 2ec1f75b 2019-03-26 stsp } else
2892 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2893 b72f483a 2019-02-05 stsp
2894 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2895 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2896 b72f483a 2019-02-05 stsp done:
2897 b72f483a 2019-02-05 stsp if (blob1)
2898 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2899 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2900 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2901 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2902 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2903 b72f483a 2019-02-05 stsp free(abspath);
2904 927df6b7 2019-02-10 stsp return err;
2905 927df6b7 2019-02-10 stsp }
2906 927df6b7 2019-02-10 stsp
2907 927df6b7 2019-02-10 stsp static const struct got_error *
2908 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2909 b00d56cd 2018-04-01 stsp {
2910 b00d56cd 2018-04-01 stsp const struct got_error *error;
2911 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2912 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2913 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2914 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2915 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2916 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2917 15a94983 2018-12-23 stsp int type1, type2;
2918 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2919 c0cc5c62 2018-10-18 stsp const char *errstr;
2920 927df6b7 2019-02-10 stsp char *path = NULL;
2921 b00d56cd 2018-04-01 stsp
2922 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2923 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2924 25eccc22 2019-01-04 stsp NULL) == -1)
2925 b00d56cd 2018-04-01 stsp err(1, "pledge");
2926 b00d56cd 2018-04-01 stsp #endif
2927 b00d56cd 2018-04-01 stsp
2928 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2929 b00d56cd 2018-04-01 stsp switch (ch) {
2930 c0cc5c62 2018-10-18 stsp case 'C':
2931 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2932 dfcab68b 2019-11-29 kn &errstr);
2933 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2934 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2935 c0cc5c62 2018-10-18 stsp break;
2936 b72f483a 2019-02-05 stsp case 'r':
2937 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2938 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2939 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2940 9ba1d308 2019-10-21 stsp optarg);
2941 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2942 b72f483a 2019-02-05 stsp break;
2943 98eaaa12 2019-08-03 stsp case 's':
2944 98eaaa12 2019-08-03 stsp diff_staged = 1;
2945 63035f9f 2019-10-06 stsp break;
2946 63035f9f 2019-10-06 stsp case 'w':
2947 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2948 98eaaa12 2019-08-03 stsp break;
2949 b00d56cd 2018-04-01 stsp default:
2950 2deda0b9 2019-03-07 stsp usage_diff();
2951 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2952 b00d56cd 2018-04-01 stsp }
2953 b00d56cd 2018-04-01 stsp }
2954 b00d56cd 2018-04-01 stsp
2955 b00d56cd 2018-04-01 stsp argc -= optind;
2956 b00d56cd 2018-04-01 stsp argv += optind;
2957 b00d56cd 2018-04-01 stsp
2958 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2959 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2960 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2961 b72f483a 2019-02-05 stsp goto done;
2962 b72f483a 2019-02-05 stsp }
2963 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2964 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2965 927df6b7 2019-02-10 stsp goto done;
2966 927df6b7 2019-02-10 stsp if (argc <= 1) {
2967 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2968 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2969 927df6b7 2019-02-10 stsp goto done;
2970 927df6b7 2019-02-10 stsp }
2971 b72f483a 2019-02-05 stsp if (repo_path)
2972 b72f483a 2019-02-05 stsp errx(1,
2973 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2974 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2975 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2976 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2977 927df6b7 2019-02-10 stsp goto done;
2978 927df6b7 2019-02-10 stsp }
2979 927df6b7 2019-02-10 stsp if (argc == 1) {
2980 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2981 cbd1af7a 2019-03-18 stsp argv[0]);
2982 927df6b7 2019-02-10 stsp if (error)
2983 927df6b7 2019-02-10 stsp goto done;
2984 927df6b7 2019-02-10 stsp } else {
2985 927df6b7 2019-02-10 stsp path = strdup("");
2986 927df6b7 2019-02-10 stsp if (path == NULL) {
2987 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2988 927df6b7 2019-02-10 stsp goto done;
2989 927df6b7 2019-02-10 stsp }
2990 927df6b7 2019-02-10 stsp }
2991 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2992 98eaaa12 2019-08-03 stsp if (diff_staged)
2993 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2994 98eaaa12 2019-08-03 stsp "objects in repository");
2995 15a94983 2018-12-23 stsp id_str1 = argv[0];
2996 15a94983 2018-12-23 stsp id_str2 = argv[1];
2997 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2998 30db809c 2019-06-05 stsp repo_path =
2999 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
3000 30db809c 2019-06-05 stsp if (repo_path == NULL) {
3001 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
3002 30db809c 2019-06-05 stsp goto done;
3003 30db809c 2019-06-05 stsp }
3004 30db809c 2019-06-05 stsp }
3005 b00d56cd 2018-04-01 stsp } else
3006 b00d56cd 2018-04-01 stsp usage_diff();
3007 25eccc22 2019-01-04 stsp
3008 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
3009 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
3010 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3011 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3012 b72f483a 2019-02-05 stsp }
3013 b00d56cd 2018-04-01 stsp
3014 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3015 76089277 2018-04-01 stsp free(repo_path);
3016 b00d56cd 2018-04-01 stsp if (error != NULL)
3017 b00d56cd 2018-04-01 stsp goto done;
3018 b00d56cd 2018-04-01 stsp
3019 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3020 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3021 c02c541e 2019-03-29 stsp if (error)
3022 c02c541e 2019-03-29 stsp goto done;
3023 c02c541e 2019-03-29 stsp
3024 30db809c 2019-06-05 stsp if (argc <= 1) {
3025 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3026 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3027 b72f483a 2019-02-05 stsp char *id_str;
3028 72ea6654 2019-07-27 stsp
3029 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3030 72ea6654 2019-07-27 stsp
3031 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3032 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3033 b72f483a 2019-02-05 stsp if (error)
3034 b72f483a 2019-02-05 stsp goto done;
3035 b72f483a 2019-02-05 stsp arg.repo = repo;
3036 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3037 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3038 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3039 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3040 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3041 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3042 b72f483a 2019-02-05 stsp
3043 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3044 72ea6654 2019-07-27 stsp if (error)
3045 72ea6654 2019-07-27 stsp goto done;
3046 72ea6654 2019-07-27 stsp
3047 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3048 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3049 b72f483a 2019-02-05 stsp free(id_str);
3050 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3051 b72f483a 2019-02-05 stsp goto done;
3052 b72f483a 2019-02-05 stsp }
3053 b72f483a 2019-02-05 stsp
3054 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3055 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3056 0adb7196 2019-08-11 stsp if (error)
3057 0adb7196 2019-08-11 stsp goto done;
3058 b00d56cd 2018-04-01 stsp
3059 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3060 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3061 0adb7196 2019-08-11 stsp if (error)
3062 0adb7196 2019-08-11 stsp goto done;
3063 b00d56cd 2018-04-01 stsp
3064 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3065 15a94983 2018-12-23 stsp if (error)
3066 15a94983 2018-12-23 stsp goto done;
3067 15a94983 2018-12-23 stsp
3068 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3069 15a94983 2018-12-23 stsp if (error)
3070 15a94983 2018-12-23 stsp goto done;
3071 15a94983 2018-12-23 stsp
3072 15a94983 2018-12-23 stsp if (type1 != type2) {
3073 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3074 b00d56cd 2018-04-01 stsp goto done;
3075 b00d56cd 2018-04-01 stsp }
3076 b00d56cd 2018-04-01 stsp
3077 15a94983 2018-12-23 stsp switch (type1) {
3078 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3079 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3080 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3081 b00d56cd 2018-04-01 stsp break;
3082 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3083 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3084 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3085 b00d56cd 2018-04-01 stsp break;
3086 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3087 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3088 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3089 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3090 b00d56cd 2018-04-01 stsp break;
3091 b00d56cd 2018-04-01 stsp default:
3092 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3093 b00d56cd 2018-04-01 stsp }
3094 b00d56cd 2018-04-01 stsp done:
3095 5e70831e 2019-05-28 stsp free(label1);
3096 5e70831e 2019-05-28 stsp free(label2);
3097 15a94983 2018-12-23 stsp free(id1);
3098 15a94983 2018-12-23 stsp free(id2);
3099 927df6b7 2019-02-10 stsp free(path);
3100 b72f483a 2019-02-05 stsp if (worktree)
3101 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3102 ad242220 2018-09-08 stsp if (repo) {
3103 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3104 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3105 ad242220 2018-09-08 stsp if (error == NULL)
3106 ad242220 2018-09-08 stsp error = repo_error;
3107 ad242220 2018-09-08 stsp }
3108 b00d56cd 2018-04-01 stsp return error;
3109 404c43c4 2018-06-21 stsp }
3110 404c43c4 2018-06-21 stsp
3111 404c43c4 2018-06-21 stsp __dead static void
3112 404c43c4 2018-06-21 stsp usage_blame(void)
3113 404c43c4 2018-06-21 stsp {
3114 9270e621 2019-02-05 stsp fprintf(stderr,
3115 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3116 404c43c4 2018-06-21 stsp getprogname());
3117 404c43c4 2018-06-21 stsp exit(1);
3118 b00d56cd 2018-04-01 stsp }
3119 b00d56cd 2018-04-01 stsp
3120 28315671 2019-08-14 stsp struct blame_line {
3121 28315671 2019-08-14 stsp int annotated;
3122 28315671 2019-08-14 stsp char *id_str;
3123 82f6abb8 2019-08-14 stsp char *committer;
3124 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3125 28315671 2019-08-14 stsp };
3126 28315671 2019-08-14 stsp
3127 28315671 2019-08-14 stsp struct blame_cb_args {
3128 28315671 2019-08-14 stsp struct blame_line *lines;
3129 28315671 2019-08-14 stsp int nlines;
3130 7ef28ff8 2019-08-14 stsp int nlines_prec;
3131 28315671 2019-08-14 stsp int lineno_cur;
3132 28315671 2019-08-14 stsp off_t *line_offsets;
3133 28315671 2019-08-14 stsp FILE *f;
3134 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3135 28315671 2019-08-14 stsp };
3136 28315671 2019-08-14 stsp
3137 404c43c4 2018-06-21 stsp static const struct got_error *
3138 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3139 28315671 2019-08-14 stsp {
3140 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3141 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3142 28315671 2019-08-14 stsp struct blame_line *bline;
3143 82f6abb8 2019-08-14 stsp char *line = NULL;
3144 28315671 2019-08-14 stsp size_t linesize = 0;
3145 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3146 28315671 2019-08-14 stsp off_t offset;
3147 bcb49d15 2019-08-14 stsp struct tm tm;
3148 bcb49d15 2019-08-14 stsp time_t committer_time;
3149 28315671 2019-08-14 stsp
3150 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3151 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3152 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3153 28315671 2019-08-14 stsp
3154 28315671 2019-08-14 stsp if (sigint_received)
3155 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3156 28315671 2019-08-14 stsp
3157 2839f8b9 2019-08-18 stsp if (lineno == -1)
3158 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3159 2839f8b9 2019-08-18 stsp
3160 28315671 2019-08-14 stsp /* Annotate this line. */
3161 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3162 28315671 2019-08-14 stsp if (bline->annotated)
3163 28315671 2019-08-14 stsp return NULL;
3164 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3165 28315671 2019-08-14 stsp if (err)
3166 28315671 2019-08-14 stsp return err;
3167 82f6abb8 2019-08-14 stsp
3168 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3169 82f6abb8 2019-08-14 stsp if (err)
3170 82f6abb8 2019-08-14 stsp goto done;
3171 82f6abb8 2019-08-14 stsp
3172 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3173 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3174 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3175 bcb49d15 2019-08-14 stsp goto done;
3176 bcb49d15 2019-08-14 stsp }
3177 bcb49d15 2019-08-14 stsp
3178 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3179 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3180 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3181 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3182 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3183 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3184 82f6abb8 2019-08-14 stsp goto done;
3185 82f6abb8 2019-08-14 stsp }
3186 28315671 2019-08-14 stsp bline->annotated = 1;
3187 28315671 2019-08-14 stsp
3188 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3189 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3190 28315671 2019-08-14 stsp if (!bline->annotated)
3191 82f6abb8 2019-08-14 stsp goto done;
3192 28315671 2019-08-14 stsp
3193 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3194 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3195 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3196 82f6abb8 2019-08-14 stsp goto done;
3197 82f6abb8 2019-08-14 stsp }
3198 28315671 2019-08-14 stsp
3199 28315671 2019-08-14 stsp while (bline->annotated) {
3200 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3201 82f6abb8 2019-08-14 stsp size_t len;
3202 82f6abb8 2019-08-14 stsp
3203 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3204 28315671 2019-08-14 stsp if (ferror(a->f))
3205 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3206 28315671 2019-08-14 stsp break;
3207 28315671 2019-08-14 stsp }
3208 82f6abb8 2019-08-14 stsp
3209 82f6abb8 2019-08-14 stsp committer = bline->committer;
3210 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3211 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3212 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3213 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3214 82f6abb8 2019-08-14 stsp if (at)
3215 82f6abb8 2019-08-14 stsp *at = '\0';
3216 82f6abb8 2019-08-14 stsp len = strlen(committer);
3217 82f6abb8 2019-08-14 stsp if (len >= 9)
3218 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3219 28315671 2019-08-14 stsp
3220 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3221 28315671 2019-08-14 stsp if (nl)
3222 28315671 2019-08-14 stsp *nl = '\0';
3223 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3224 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3225 28315671 2019-08-14 stsp
3226 28315671 2019-08-14 stsp a->lineno_cur++;
3227 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3228 28315671 2019-08-14 stsp }
3229 82f6abb8 2019-08-14 stsp done:
3230 82f6abb8 2019-08-14 stsp if (commit)
3231 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3232 28315671 2019-08-14 stsp free(line);
3233 28315671 2019-08-14 stsp return err;
3234 28315671 2019-08-14 stsp }
3235 28315671 2019-08-14 stsp
3236 28315671 2019-08-14 stsp static const struct got_error *
3237 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3238 404c43c4 2018-06-21 stsp {
3239 404c43c4 2018-06-21 stsp const struct got_error *error;
3240 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3241 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3242 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3243 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3244 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3245 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3246 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3247 28315671 2019-08-14 stsp struct blame_cb_args bca;
3248 28315671 2019-08-14 stsp int ch, obj_type, i;
3249 b02560ec 2019-08-19 stsp size_t filesize;
3250 90f3c347 2019-08-19 stsp
3251 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3252 404c43c4 2018-06-21 stsp
3253 404c43c4 2018-06-21 stsp #ifndef PROFILE
3254 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3255 36e2fb66 2019-01-04 stsp NULL) == -1)
3256 404c43c4 2018-06-21 stsp err(1, "pledge");
3257 404c43c4 2018-06-21 stsp #endif
3258 404c43c4 2018-06-21 stsp
3259 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3260 404c43c4 2018-06-21 stsp switch (ch) {
3261 404c43c4 2018-06-21 stsp case 'c':
3262 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3263 404c43c4 2018-06-21 stsp break;
3264 66bea077 2018-08-02 stsp case 'r':
3265 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3266 66bea077 2018-08-02 stsp if (repo_path == NULL)
3267 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3268 9ba1d308 2019-10-21 stsp optarg);
3269 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3270 66bea077 2018-08-02 stsp break;
3271 404c43c4 2018-06-21 stsp default:
3272 2deda0b9 2019-03-07 stsp usage_blame();
3273 404c43c4 2018-06-21 stsp /* NOTREACHED */
3274 404c43c4 2018-06-21 stsp }
3275 404c43c4 2018-06-21 stsp }
3276 404c43c4 2018-06-21 stsp
3277 404c43c4 2018-06-21 stsp argc -= optind;
3278 404c43c4 2018-06-21 stsp argv += optind;
3279 404c43c4 2018-06-21 stsp
3280 a39318fd 2018-08-02 stsp if (argc == 1)
3281 404c43c4 2018-06-21 stsp path = argv[0];
3282 a39318fd 2018-08-02 stsp else
3283 404c43c4 2018-06-21 stsp usage_blame();
3284 404c43c4 2018-06-21 stsp
3285 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3286 66bea077 2018-08-02 stsp if (cwd == NULL) {
3287 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3288 66bea077 2018-08-02 stsp goto done;
3289 66bea077 2018-08-02 stsp }
3290 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3291 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3292 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3293 66bea077 2018-08-02 stsp goto done;
3294 0c06baac 2019-02-05 stsp else
3295 0c06baac 2019-02-05 stsp error = NULL;
3296 0c06baac 2019-02-05 stsp if (worktree) {
3297 0c06baac 2019-02-05 stsp repo_path =
3298 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3299 0c06baac 2019-02-05 stsp if (repo_path == NULL)
3300 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3301 0c06baac 2019-02-05 stsp if (error)
3302 0c06baac 2019-02-05 stsp goto done;
3303 0c06baac 2019-02-05 stsp } else {
3304 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3305 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3306 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3307 0c06baac 2019-02-05 stsp goto done;
3308 0c06baac 2019-02-05 stsp }
3309 66bea077 2018-08-02 stsp }
3310 66bea077 2018-08-02 stsp }
3311 36e2fb66 2019-01-04 stsp
3312 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3313 c02c541e 2019-03-29 stsp if (error != NULL)
3314 36e2fb66 2019-01-04 stsp goto done;
3315 66bea077 2018-08-02 stsp
3316 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3317 c02c541e 2019-03-29 stsp if (error)
3318 404c43c4 2018-06-21 stsp goto done;
3319 404c43c4 2018-06-21 stsp
3320 0c06baac 2019-02-05 stsp if (worktree) {
3321 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3322 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3323 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3324 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3325 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3326 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3327 6efaaa2d 2019-02-05 stsp path) == -1) {
3328 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3329 0c06baac 2019-02-05 stsp goto done;
3330 0c06baac 2019-02-05 stsp }
3331 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3332 0c06baac 2019-02-05 stsp free(p);
3333 0c06baac 2019-02-05 stsp } else {
3334 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3335 0c06baac 2019-02-05 stsp }
3336 0c06baac 2019-02-05 stsp if (error)
3337 66bea077 2018-08-02 stsp goto done;
3338 66bea077 2018-08-02 stsp
3339 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3340 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3341 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3342 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3343 404c43c4 2018-06-21 stsp if (error != NULL)
3344 66bea077 2018-08-02 stsp goto done;
3345 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3346 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3347 404c43c4 2018-06-21 stsp if (error != NULL)
3348 66bea077 2018-08-02 stsp goto done;
3349 404c43c4 2018-06-21 stsp } else {
3350 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3351 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3352 30837e32 2019-07-25 stsp if (error)
3353 66bea077 2018-08-02 stsp goto done;
3354 404c43c4 2018-06-21 stsp }
3355 404c43c4 2018-06-21 stsp
3356 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3357 28315671 2019-08-14 stsp if (error)
3358 28315671 2019-08-14 stsp goto done;
3359 28315671 2019-08-14 stsp
3360 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3361 28315671 2019-08-14 stsp if (error)
3362 28315671 2019-08-14 stsp goto done;
3363 28315671 2019-08-14 stsp
3364 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3365 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3366 28315671 2019-08-14 stsp goto done;
3367 28315671 2019-08-14 stsp }
3368 28315671 2019-08-14 stsp
3369 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3370 28315671 2019-08-14 stsp if (error)
3371 28315671 2019-08-14 stsp goto done;
3372 28315671 2019-08-14 stsp bca.f = got_opentemp();
3373 28315671 2019-08-14 stsp if (bca.f == NULL) {
3374 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3375 28315671 2019-08-14 stsp goto done;
3376 28315671 2019-08-14 stsp }
3377 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3378 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3379 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3380 28315671 2019-08-14 stsp goto done;
3381 28315671 2019-08-14 stsp
3382 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3383 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3384 b02560ec 2019-08-19 stsp bca.nlines--;
3385 b02560ec 2019-08-19 stsp
3386 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3387 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3388 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3389 28315671 2019-08-14 stsp goto done;
3390 28315671 2019-08-14 stsp }
3391 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3392 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3393 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3394 7ef28ff8 2019-08-14 stsp while (i > 0) {
3395 7ef28ff8 2019-08-14 stsp i /= 10;
3396 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3397 7ef28ff8 2019-08-14 stsp }
3398 82f6abb8 2019-08-14 stsp bca.repo = repo;
3399 7ef28ff8 2019-08-14 stsp
3400 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3401 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3402 404c43c4 2018-06-21 stsp done:
3403 66bea077 2018-08-02 stsp free(in_repo_path);
3404 66bea077 2018-08-02 stsp free(repo_path);
3405 66bea077 2018-08-02 stsp free(cwd);
3406 404c43c4 2018-06-21 stsp free(commit_id);
3407 28315671 2019-08-14 stsp free(obj_id);
3408 28315671 2019-08-14 stsp if (blob)
3409 28315671 2019-08-14 stsp got_object_blob_close(blob);
3410 0c06baac 2019-02-05 stsp if (worktree)
3411 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3412 ad242220 2018-09-08 stsp if (repo) {
3413 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3414 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3415 ad242220 2018-09-08 stsp if (error == NULL)
3416 ad242220 2018-09-08 stsp error = repo_error;
3417 ad242220 2018-09-08 stsp }
3418 3affba96 2019-09-22 stsp if (bca.lines) {
3419 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3420 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3421 3affba96 2019-09-22 stsp free(bline->id_str);
3422 3affba96 2019-09-22 stsp free(bline->committer);
3423 3affba96 2019-09-22 stsp }
3424 3affba96 2019-09-22 stsp free(bca.lines);
3425 28315671 2019-08-14 stsp }
3426 28315671 2019-08-14 stsp free(bca.line_offsets);
3427 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3428 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3429 404c43c4 2018-06-21 stsp return error;
3430 5de5890b 2018-10-18 stsp }
3431 5de5890b 2018-10-18 stsp
3432 5de5890b 2018-10-18 stsp __dead static void
3433 5de5890b 2018-10-18 stsp usage_tree(void)
3434 5de5890b 2018-10-18 stsp {
3435 c1669e2e 2019-01-09 stsp fprintf(stderr,
3436 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3437 5de5890b 2018-10-18 stsp getprogname());
3438 5de5890b 2018-10-18 stsp exit(1);
3439 5de5890b 2018-10-18 stsp }
3440 5de5890b 2018-10-18 stsp
3441 c1669e2e 2019-01-09 stsp static void
3442 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3443 c1669e2e 2019-01-09 stsp const char *root_path)
3444 c1669e2e 2019-01-09 stsp {
3445 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3446 848d6979 2019-08-12 stsp const char *modestr = "";
3447 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3448 5de5890b 2018-10-18 stsp
3449 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3450 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3451 c1669e2e 2019-01-09 stsp path++;
3452 c1669e2e 2019-01-09 stsp
3453 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3454 63c5ca5d 2019-08-24 stsp modestr = "$";
3455 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3456 848d6979 2019-08-12 stsp modestr = "@";
3457 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3458 848d6979 2019-08-12 stsp modestr = "/";
3459 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3460 848d6979 2019-08-12 stsp modestr = "*";
3461 848d6979 2019-08-12 stsp
3462 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3463 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3464 c1669e2e 2019-01-09 stsp }
3465 c1669e2e 2019-01-09 stsp
3466 5de5890b 2018-10-18 stsp static const struct got_error *
3467 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3468 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3469 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3470 5de5890b 2018-10-18 stsp {
3471 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3472 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3473 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3474 56e0773d 2019-11-28 stsp int nentries, i;
3475 5de5890b 2018-10-18 stsp
3476 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3477 5de5890b 2018-10-18 stsp if (err)
3478 5de5890b 2018-10-18 stsp goto done;
3479 5de5890b 2018-10-18 stsp
3480 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3481 5de5890b 2018-10-18 stsp if (err)
3482 5de5890b 2018-10-18 stsp goto done;
3483 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3484 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3485 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3486 5de5890b 2018-10-18 stsp char *id = NULL;
3487 84453469 2018-11-11 stsp
3488 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3489 84453469 2018-11-11 stsp break;
3490 84453469 2018-11-11 stsp
3491 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3492 5de5890b 2018-10-18 stsp if (show_ids) {
3493 5de5890b 2018-10-18 stsp char *id_str;
3494 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3495 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3496 5de5890b 2018-10-18 stsp if (err)
3497 5de5890b 2018-10-18 stsp goto done;
3498 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3499 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3500 5de5890b 2018-10-18 stsp free(id_str);
3501 5de5890b 2018-10-18 stsp goto done;
3502 5de5890b 2018-10-18 stsp }
3503 5de5890b 2018-10-18 stsp free(id_str);
3504 5de5890b 2018-10-18 stsp }
3505 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3506 5de5890b 2018-10-18 stsp free(id);
3507 c1669e2e 2019-01-09 stsp
3508 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3509 c1669e2e 2019-01-09 stsp char *child_path;
3510 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3511 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3512 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3513 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3514 c1669e2e 2019-01-09 stsp goto done;
3515 c1669e2e 2019-01-09 stsp }
3516 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3517 c1669e2e 2019-01-09 stsp root_path, repo);
3518 c1669e2e 2019-01-09 stsp free(child_path);
3519 c1669e2e 2019-01-09 stsp if (err)
3520 c1669e2e 2019-01-09 stsp goto done;
3521 c1669e2e 2019-01-09 stsp }
3522 5de5890b 2018-10-18 stsp }
3523 5de5890b 2018-10-18 stsp done:
3524 5de5890b 2018-10-18 stsp if (tree)
3525 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3526 5de5890b 2018-10-18 stsp free(tree_id);
3527 5de5890b 2018-10-18 stsp return err;
3528 404c43c4 2018-06-21 stsp }
3529 404c43c4 2018-06-21 stsp
3530 5de5890b 2018-10-18 stsp static const struct got_error *
3531 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3532 5de5890b 2018-10-18 stsp {
3533 5de5890b 2018-10-18 stsp const struct got_error *error;
3534 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3535 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3536 7a2c19d6 2019-02-05 stsp const char *path;
3537 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3538 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3539 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3540 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3541 5de5890b 2018-10-18 stsp int ch;
3542 5de5890b 2018-10-18 stsp
3543 5de5890b 2018-10-18 stsp #ifndef PROFILE
3544 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3545 0f8d269b 2019-01-04 stsp NULL) == -1)
3546 5de5890b 2018-10-18 stsp err(1, "pledge");
3547 5de5890b 2018-10-18 stsp #endif
3548 5de5890b 2018-10-18 stsp
3549 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3550 5de5890b 2018-10-18 stsp switch (ch) {
3551 5de5890b 2018-10-18 stsp case 'c':
3552 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3553 5de5890b 2018-10-18 stsp break;
3554 5de5890b 2018-10-18 stsp case 'r':
3555 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3556 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3557 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3558 9ba1d308 2019-10-21 stsp optarg);
3559 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3560 5de5890b 2018-10-18 stsp break;
3561 5de5890b 2018-10-18 stsp case 'i':
3562 5de5890b 2018-10-18 stsp show_ids = 1;
3563 5de5890b 2018-10-18 stsp break;
3564 c1669e2e 2019-01-09 stsp case 'R':
3565 c1669e2e 2019-01-09 stsp recurse = 1;
3566 c1669e2e 2019-01-09 stsp break;
3567 5de5890b 2018-10-18 stsp default:
3568 2deda0b9 2019-03-07 stsp usage_tree();
3569 5de5890b 2018-10-18 stsp /* NOTREACHED */
3570 5de5890b 2018-10-18 stsp }
3571 5de5890b 2018-10-18 stsp }
3572 5de5890b 2018-10-18 stsp
3573 5de5890b 2018-10-18 stsp argc -= optind;
3574 5de5890b 2018-10-18 stsp argv += optind;
3575 5de5890b 2018-10-18 stsp
3576 5de5890b 2018-10-18 stsp if (argc == 1)
3577 5de5890b 2018-10-18 stsp path = argv[0];
3578 5de5890b 2018-10-18 stsp else if (argc > 1)
3579 5de5890b 2018-10-18 stsp usage_tree();
3580 5de5890b 2018-10-18 stsp else
3581 7a2c19d6 2019-02-05 stsp path = NULL;
3582 7a2c19d6 2019-02-05 stsp
3583 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3584 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3585 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3586 9bf7a39b 2019-02-05 stsp goto done;
3587 9bf7a39b 2019-02-05 stsp }
3588 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3589 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3590 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3591 7a2c19d6 2019-02-05 stsp goto done;
3592 7a2c19d6 2019-02-05 stsp else
3593 7a2c19d6 2019-02-05 stsp error = NULL;
3594 7a2c19d6 2019-02-05 stsp if (worktree) {
3595 7a2c19d6 2019-02-05 stsp repo_path =
3596 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3597 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3598 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3599 7a2c19d6 2019-02-05 stsp if (error)
3600 7a2c19d6 2019-02-05 stsp goto done;
3601 7a2c19d6 2019-02-05 stsp } else {
3602 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3603 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3604 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3605 7a2c19d6 2019-02-05 stsp goto done;
3606 7a2c19d6 2019-02-05 stsp }
3607 5de5890b 2018-10-18 stsp }
3608 5de5890b 2018-10-18 stsp }
3609 5de5890b 2018-10-18 stsp
3610 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3611 5de5890b 2018-10-18 stsp if (error != NULL)
3612 c02c541e 2019-03-29 stsp goto done;
3613 c02c541e 2019-03-29 stsp
3614 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3615 c02c541e 2019-03-29 stsp if (error)
3616 5de5890b 2018-10-18 stsp goto done;
3617 5de5890b 2018-10-18 stsp
3618 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3619 9bf7a39b 2019-02-05 stsp if (worktree) {
3620 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3621 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3622 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3623 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3624 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3625 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3626 9bf7a39b 2019-02-05 stsp goto done;
3627 9bf7a39b 2019-02-05 stsp }
3628 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3629 9bf7a39b 2019-02-05 stsp free(p);
3630 9bf7a39b 2019-02-05 stsp if (error)
3631 9bf7a39b 2019-02-05 stsp goto done;
3632 9bf7a39b 2019-02-05 stsp } else
3633 9bf7a39b 2019-02-05 stsp path = "/";
3634 9bf7a39b 2019-02-05 stsp }
3635 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3636 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3637 9bf7a39b 2019-02-05 stsp if (error != NULL)
3638 9bf7a39b 2019-02-05 stsp goto done;
3639 9bf7a39b 2019-02-05 stsp }
3640 5de5890b 2018-10-18 stsp
3641 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3642 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3643 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3644 5de5890b 2018-10-18 stsp if (error != NULL)
3645 5de5890b 2018-10-18 stsp goto done;
3646 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3647 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3648 5de5890b 2018-10-18 stsp if (error != NULL)
3649 5de5890b 2018-10-18 stsp goto done;
3650 5de5890b 2018-10-18 stsp } else {
3651 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3652 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3653 30837e32 2019-07-25 stsp if (error)
3654 5de5890b 2018-10-18 stsp goto done;
3655 5de5890b 2018-10-18 stsp }
3656 5de5890b 2018-10-18 stsp
3657 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3658 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3659 5de5890b 2018-10-18 stsp done:
3660 5de5890b 2018-10-18 stsp free(in_repo_path);
3661 5de5890b 2018-10-18 stsp free(repo_path);
3662 5de5890b 2018-10-18 stsp free(cwd);
3663 5de5890b 2018-10-18 stsp free(commit_id);
3664 7a2c19d6 2019-02-05 stsp if (worktree)
3665 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3666 5de5890b 2018-10-18 stsp if (repo) {
3667 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3668 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3669 5de5890b 2018-10-18 stsp if (error == NULL)
3670 5de5890b 2018-10-18 stsp error = repo_error;
3671 5de5890b 2018-10-18 stsp }
3672 5de5890b 2018-10-18 stsp return error;
3673 5de5890b 2018-10-18 stsp }
3674 5de5890b 2018-10-18 stsp
3675 6bad629b 2019-02-04 stsp __dead static void
3676 6bad629b 2019-02-04 stsp usage_status(void)
3677 6bad629b 2019-02-04 stsp {
3678 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3679 6bad629b 2019-02-04 stsp exit(1);
3680 6bad629b 2019-02-04 stsp }
3681 5c860e29 2018-03-12 stsp
3682 b72f483a 2019-02-05 stsp static const struct got_error *
3683 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3684 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3685 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3686 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3687 6bad629b 2019-02-04 stsp {
3688 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3689 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3690 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3691 b72f483a 2019-02-05 stsp return NULL;
3692 6bad629b 2019-02-04 stsp }
3693 5c860e29 2018-03-12 stsp
3694 6bad629b 2019-02-04 stsp static const struct got_error *
3695 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3696 6bad629b 2019-02-04 stsp {
3697 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3698 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3699 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3700 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3701 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3702 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3703 a5edda0a 2019-07-27 stsp int ch;
3704 5c860e29 2018-03-12 stsp
3705 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3706 72ea6654 2019-07-27 stsp
3707 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3708 6bad629b 2019-02-04 stsp switch (ch) {
3709 5c860e29 2018-03-12 stsp default:
3710 2deda0b9 2019-03-07 stsp usage_status();
3711 6bad629b 2019-02-04 stsp /* NOTREACHED */
3712 5c860e29 2018-03-12 stsp }
3713 5c860e29 2018-03-12 stsp }
3714 5c860e29 2018-03-12 stsp
3715 6bad629b 2019-02-04 stsp argc -= optind;
3716 6bad629b 2019-02-04 stsp argv += optind;
3717 5c860e29 2018-03-12 stsp
3718 6bad629b 2019-02-04 stsp #ifndef PROFILE
3719 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3720 6bad629b 2019-02-04 stsp NULL) == -1)
3721 6bad629b 2019-02-04 stsp err(1, "pledge");
3722 f42b1b34 2018-03-12 stsp #endif
3723 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3724 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3725 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3726 927df6b7 2019-02-10 stsp goto done;
3727 927df6b7 2019-02-10 stsp }
3728 927df6b7 2019-02-10 stsp
3729 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3730 927df6b7 2019-02-10 stsp if (error != NULL)
3731 a5edda0a 2019-07-27 stsp goto done;
3732 6bad629b 2019-02-04 stsp
3733 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3734 c9956ddf 2019-09-08 stsp NULL);
3735 6bad629b 2019-02-04 stsp if (error != NULL)
3736 6bad629b 2019-02-04 stsp goto done;
3737 6bad629b 2019-02-04 stsp
3738 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3739 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3740 087fb88c 2019-08-04 stsp if (error)
3741 087fb88c 2019-08-04 stsp goto done;
3742 087fb88c 2019-08-04 stsp
3743 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3744 6bad629b 2019-02-04 stsp if (error)
3745 6bad629b 2019-02-04 stsp goto done;
3746 6bad629b 2019-02-04 stsp
3747 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3748 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3749 6bad629b 2019-02-04 stsp done:
3750 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3751 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3752 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3753 927df6b7 2019-02-10 stsp free(cwd);
3754 d0eebce4 2019-03-11 stsp return error;
3755 d0eebce4 2019-03-11 stsp }
3756 d0eebce4 2019-03-11 stsp
3757 d0eebce4 2019-03-11 stsp __dead static void
3758 d0eebce4 2019-03-11 stsp usage_ref(void)
3759 d0eebce4 2019-03-11 stsp {
3760 d0eebce4 2019-03-11 stsp fprintf(stderr,
3761 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3762 d0eebce4 2019-03-11 stsp getprogname());
3763 d0eebce4 2019-03-11 stsp exit(1);
3764 d0eebce4 2019-03-11 stsp }
3765 d0eebce4 2019-03-11 stsp
3766 d0eebce4 2019-03-11 stsp static const struct got_error *
3767 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3768 d0eebce4 2019-03-11 stsp {
3769 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3770 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3771 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3772 d0eebce4 2019-03-11 stsp
3773 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3774 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3775 d0eebce4 2019-03-11 stsp if (err)
3776 d0eebce4 2019-03-11 stsp return err;
3777 d0eebce4 2019-03-11 stsp
3778 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3779 d0eebce4 2019-03-11 stsp char *refstr;
3780 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3781 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3782 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3783 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3784 d0eebce4 2019-03-11 stsp free(refstr);
3785 d0eebce4 2019-03-11 stsp }
3786 d0eebce4 2019-03-11 stsp
3787 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3788 d0eebce4 2019-03-11 stsp return NULL;
3789 d0eebce4 2019-03-11 stsp }
3790 d0eebce4 2019-03-11 stsp
3791 d0eebce4 2019-03-11 stsp static const struct got_error *
3792 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3793 d0eebce4 2019-03-11 stsp {
3794 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3795 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3796 d0eebce4 2019-03-11 stsp
3797 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3798 d0eebce4 2019-03-11 stsp if (err)
3799 d0eebce4 2019-03-11 stsp return err;
3800 d0eebce4 2019-03-11 stsp
3801 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3802 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3803 d0eebce4 2019-03-11 stsp return err;
3804 d0eebce4 2019-03-11 stsp }
3805 d0eebce4 2019-03-11 stsp
3806 d0eebce4 2019-03-11 stsp static const struct got_error *
3807 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3808 d0eebce4 2019-03-11 stsp {
3809 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3810 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3811 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3812 d1644381 2019-07-14 stsp
3813 d1644381 2019-07-14 stsp /*
3814 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3815 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3816 d1644381 2019-07-14 stsp * an unintended typo.
3817 d1644381 2019-07-14 stsp */
3818 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3819 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3820 d0eebce4 2019-03-11 stsp
3821 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3822 dd88155e 2019-06-29 stsp repo);
3823 d83d9d5c 2019-05-13 stsp if (err) {
3824 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3825 d0eebce4 2019-03-11 stsp
3826 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3827 d83d9d5c 2019-05-13 stsp return err;
3828 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3829 d83d9d5c 2019-05-13 stsp if (err)
3830 d83d9d5c 2019-05-13 stsp return err;
3831 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3832 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3833 d83d9d5c 2019-05-13 stsp if (err)
3834 d83d9d5c 2019-05-13 stsp return err;
3835 d83d9d5c 2019-05-13 stsp }
3836 d83d9d5c 2019-05-13 stsp
3837 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3838 d0eebce4 2019-03-11 stsp if (err)
3839 d0eebce4 2019-03-11 stsp goto done;
3840 d0eebce4 2019-03-11 stsp
3841 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3842 d0eebce4 2019-03-11 stsp done:
3843 d0eebce4 2019-03-11 stsp if (ref)
3844 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3845 d0eebce4 2019-03-11 stsp free(id);
3846 d1c1ae5f 2019-08-12 stsp return err;
3847 d1c1ae5f 2019-08-12 stsp }
3848 d1c1ae5f 2019-08-12 stsp
3849 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3850 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3851 d1c1ae5f 2019-08-12 stsp {
3852 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3853 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3854 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3855 d1c1ae5f 2019-08-12 stsp
3856 d1c1ae5f 2019-08-12 stsp /*
3857 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3858 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3859 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3860 d1c1ae5f 2019-08-12 stsp */
3861 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3862 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3863 d1c1ae5f 2019-08-12 stsp
3864 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3865 d1c1ae5f 2019-08-12 stsp if (err)
3866 d1c1ae5f 2019-08-12 stsp return err;
3867 d1c1ae5f 2019-08-12 stsp
3868 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3869 d1c1ae5f 2019-08-12 stsp if (err)
3870 d1c1ae5f 2019-08-12 stsp goto done;
3871 d1c1ae5f 2019-08-12 stsp
3872 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3873 d1c1ae5f 2019-08-12 stsp done:
3874 d1c1ae5f 2019-08-12 stsp if (target_ref)
3875 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3876 d1c1ae5f 2019-08-12 stsp if (ref)
3877 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3878 d0eebce4 2019-03-11 stsp return err;
3879 d0eebce4 2019-03-11 stsp }
3880 d0eebce4 2019-03-11 stsp
3881 d0eebce4 2019-03-11 stsp static const struct got_error *
3882 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3883 d0eebce4 2019-03-11 stsp {
3884 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3885 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3886 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3887 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3888 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3889 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3890 d0eebce4 2019-03-11 stsp
3891 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3892 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3893 d0eebce4 2019-03-11 stsp switch (ch) {
3894 d0eebce4 2019-03-11 stsp case 'd':
3895 d0eebce4 2019-03-11 stsp delref = optarg;
3896 d0eebce4 2019-03-11 stsp break;
3897 d0eebce4 2019-03-11 stsp case 'r':
3898 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3899 d0eebce4 2019-03-11 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 d0eebce4 2019-03-11 stsp break;
3904 d0eebce4 2019-03-11 stsp case 'l':
3905 d0eebce4 2019-03-11 stsp do_list = 1;
3906 d1c1ae5f 2019-08-12 stsp break;
3907 d1c1ae5f 2019-08-12 stsp case 's':
3908 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3909 d0eebce4 2019-03-11 stsp break;
3910 d0eebce4 2019-03-11 stsp default:
3911 d0eebce4 2019-03-11 stsp usage_ref();
3912 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3913 d0eebce4 2019-03-11 stsp }
3914 d0eebce4 2019-03-11 stsp }
3915 d0eebce4 2019-03-11 stsp
3916 d0eebce4 2019-03-11 stsp if (do_list && delref)
3917 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3918 d0eebce4 2019-03-11 stsp
3919 d0eebce4 2019-03-11 stsp argc -= optind;
3920 d0eebce4 2019-03-11 stsp argv += optind;
3921 d0eebce4 2019-03-11 stsp
3922 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3923 d1c1ae5f 2019-08-12 stsp if (create_symref)
3924 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3925 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3926 d0eebce4 2019-03-11 stsp if (argc > 0)
3927 d0eebce4 2019-03-11 stsp usage_ref();
3928 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3929 d0eebce4 2019-03-11 stsp usage_ref();
3930 d0eebce4 2019-03-11 stsp
3931 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3932 e0b57350 2019-03-12 stsp if (do_list) {
3933 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3934 e0b57350 2019-03-12 stsp NULL) == -1)
3935 e0b57350 2019-03-12 stsp err(1, "pledge");
3936 e0b57350 2019-03-12 stsp } else {
3937 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3938 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3939 e0b57350 2019-03-12 stsp err(1, "pledge");
3940 e0b57350 2019-03-12 stsp }
3941 d0eebce4 2019-03-11 stsp #endif
3942 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3943 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3944 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3945 d0eebce4 2019-03-11 stsp goto done;
3946 d0eebce4 2019-03-11 stsp }
3947 d0eebce4 2019-03-11 stsp
3948 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3949 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3950 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3951 d0eebce4 2019-03-11 stsp goto done;
3952 d0eebce4 2019-03-11 stsp else
3953 d0eebce4 2019-03-11 stsp error = NULL;
3954 d0eebce4 2019-03-11 stsp if (worktree) {
3955 d0eebce4 2019-03-11 stsp repo_path =
3956 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3957 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3958 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3959 d0eebce4 2019-03-11 stsp if (error)
3960 d0eebce4 2019-03-11 stsp goto done;
3961 d0eebce4 2019-03-11 stsp } else {
3962 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3963 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3964 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3965 d0eebce4 2019-03-11 stsp goto done;
3966 d0eebce4 2019-03-11 stsp }
3967 d0eebce4 2019-03-11 stsp }
3968 d0eebce4 2019-03-11 stsp }
3969 d0eebce4 2019-03-11 stsp
3970 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3971 d0eebce4 2019-03-11 stsp if (error != NULL)
3972 d0eebce4 2019-03-11 stsp goto done;
3973 d0eebce4 2019-03-11 stsp
3974 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3975 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3976 c02c541e 2019-03-29 stsp if (error)
3977 c02c541e 2019-03-29 stsp goto done;
3978 c02c541e 2019-03-29 stsp
3979 d0eebce4 2019-03-11 stsp if (do_list)
3980 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3981 d0eebce4 2019-03-11 stsp else if (delref)
3982 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3983 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3984 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3985 d0eebce4 2019-03-11 stsp else
3986 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3987 4e759de4 2019-06-26 stsp done:
3988 4e759de4 2019-06-26 stsp if (repo)
3989 4e759de4 2019-06-26 stsp got_repo_close(repo);
3990 4e759de4 2019-06-26 stsp if (worktree)
3991 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3992 4e759de4 2019-06-26 stsp free(cwd);
3993 4e759de4 2019-06-26 stsp free(repo_path);
3994 4e759de4 2019-06-26 stsp return error;
3995 4e759de4 2019-06-26 stsp }
3996 4e759de4 2019-06-26 stsp
3997 4e759de4 2019-06-26 stsp __dead static void
3998 4e759de4 2019-06-26 stsp usage_branch(void)
3999 4e759de4 2019-06-26 stsp {
4000 4e759de4 2019-06-26 stsp fprintf(stderr,
4001 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4002 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4003 4e759de4 2019-06-26 stsp exit(1);
4004 4e759de4 2019-06-26 stsp }
4005 4e759de4 2019-06-26 stsp
4006 4e759de4 2019-06-26 stsp static const struct got_error *
4007 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4008 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4009 4e99b47e 2019-10-04 stsp {
4010 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4011 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4012 4e99b47e 2019-10-04 stsp char *refstr;
4013 4e99b47e 2019-10-04 stsp
4014 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4015 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4016 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4017 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4018 4e99b47e 2019-10-04 stsp
4019 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4020 4e99b47e 2019-10-04 stsp if (err)
4021 4e99b47e 2019-10-04 stsp return err;
4022 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4023 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4024 4e99b47e 2019-10-04 stsp marker = "* ";
4025 4e99b47e 2019-10-04 stsp else
4026 4e99b47e 2019-10-04 stsp marker = "~ ";
4027 4e99b47e 2019-10-04 stsp free(id);
4028 4e99b47e 2019-10-04 stsp }
4029 4e99b47e 2019-10-04 stsp
4030 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4031 4e99b47e 2019-10-04 stsp refname += 11;
4032 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4033 4e99b47e 2019-10-04 stsp refname += 18;
4034 4e99b47e 2019-10-04 stsp
4035 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4036 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4037 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4038 4e99b47e 2019-10-04 stsp
4039 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4040 4e99b47e 2019-10-04 stsp free(refstr);
4041 4e99b47e 2019-10-04 stsp return NULL;
4042 4e99b47e 2019-10-04 stsp }
4043 4e99b47e 2019-10-04 stsp
4044 4e99b47e 2019-10-04 stsp static const struct got_error *
4045 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4046 ad89fa31 2019-10-04 stsp {
4047 ad89fa31 2019-10-04 stsp const char *refname;
4048 ad89fa31 2019-10-04 stsp
4049 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4050 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4051 ad89fa31 2019-10-04 stsp
4052 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4053 ad89fa31 2019-10-04 stsp
4054 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4055 ad89fa31 2019-10-04 stsp refname += 11;
4056 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4057 ad89fa31 2019-10-04 stsp refname += 18;
4058 ad89fa31 2019-10-04 stsp
4059 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4060 ad89fa31 2019-10-04 stsp
4061 ad89fa31 2019-10-04 stsp return NULL;
4062 ad89fa31 2019-10-04 stsp }
4063 ad89fa31 2019-10-04 stsp
4064 ad89fa31 2019-10-04 stsp static const struct got_error *
4065 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4066 4e759de4 2019-06-26 stsp {
4067 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4068 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4069 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4070 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4071 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4072 4e759de4 2019-06-26 stsp
4073 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4074 ba882ee3 2019-07-11 stsp
4075 4e99b47e 2019-10-04 stsp if (worktree) {
4076 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4077 4e99b47e 2019-10-04 stsp worktree);
4078 4e99b47e 2019-10-04 stsp if (err)
4079 4e99b47e 2019-10-04 stsp return err;
4080 4e99b47e 2019-10-04 stsp
4081 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4082 4e99b47e 2019-10-04 stsp worktree);
4083 4e99b47e 2019-10-04 stsp if (err)
4084 4e99b47e 2019-10-04 stsp return err;
4085 4e99b47e 2019-10-04 stsp
4086 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4087 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4088 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4089 4e99b47e 2019-10-04 stsp if (err)
4090 4e99b47e 2019-10-04 stsp return err;
4091 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4092 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4093 4e99b47e 2019-10-04 stsp }
4094 4e99b47e 2019-10-04 stsp }
4095 4e99b47e 2019-10-04 stsp
4096 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4097 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4098 4e759de4 2019-06-26 stsp if (err)
4099 4e759de4 2019-06-26 stsp return err;
4100 4e759de4 2019-06-26 stsp
4101 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4102 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4103 4e759de4 2019-06-26 stsp
4104 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4105 4e759de4 2019-06-26 stsp return NULL;
4106 4e759de4 2019-06-26 stsp }
4107 4e759de4 2019-06-26 stsp
4108 4e759de4 2019-06-26 stsp static const struct got_error *
4109 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4110 45cd4e47 2019-08-25 stsp const char *branch_name)
4111 4e759de4 2019-06-26 stsp {
4112 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4113 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4114 4e759de4 2019-06-26 stsp char *refname;
4115 4e759de4 2019-06-26 stsp
4116 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4117 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4118 4e759de4 2019-06-26 stsp
4119 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4120 4e759de4 2019-06-26 stsp if (err)
4121 4e759de4 2019-06-26 stsp goto done;
4122 4e759de4 2019-06-26 stsp
4123 45cd4e47 2019-08-25 stsp if (worktree &&
4124 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4125 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4126 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4127 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4128 45cd4e47 2019-08-25 stsp goto done;
4129 45cd4e47 2019-08-25 stsp }
4130 45cd4e47 2019-08-25 stsp
4131 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4132 4e759de4 2019-06-26 stsp done:
4133 45cd4e47 2019-08-25 stsp if (ref)
4134 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4135 4e759de4 2019-06-26 stsp free(refname);
4136 4e759de4 2019-06-26 stsp return err;
4137 4e759de4 2019-06-26 stsp }
4138 4e759de4 2019-06-26 stsp
4139 4e759de4 2019-06-26 stsp static const struct got_error *
4140 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4141 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4142 4e759de4 2019-06-26 stsp {
4143 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4144 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4145 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4146 d3f84d51 2019-07-11 stsp
4147 d3f84d51 2019-07-11 stsp /*
4148 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4149 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4150 d3f84d51 2019-07-11 stsp * an unintended typo.
4151 d3f84d51 2019-07-11 stsp */
4152 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4153 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4154 4e759de4 2019-06-26 stsp
4155 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4156 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4157 4e759de4 2019-06-26 stsp goto done;
4158 4e759de4 2019-06-26 stsp }
4159 4e759de4 2019-06-26 stsp
4160 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4161 4e759de4 2019-06-26 stsp if (err == NULL) {
4162 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4163 4e759de4 2019-06-26 stsp goto done;
4164 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4165 4e759de4 2019-06-26 stsp goto done;
4166 4e759de4 2019-06-26 stsp
4167 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4168 4e759de4 2019-06-26 stsp if (err)
4169 4e759de4 2019-06-26 stsp goto done;
4170 4e759de4 2019-06-26 stsp
4171 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4172 d0eebce4 2019-03-11 stsp done:
4173 4e759de4 2019-06-26 stsp if (ref)
4174 4e759de4 2019-06-26 stsp got_ref_close(ref);
4175 4e759de4 2019-06-26 stsp free(base_refname);
4176 4e759de4 2019-06-26 stsp free(refname);
4177 4e759de4 2019-06-26 stsp return err;
4178 4e759de4 2019-06-26 stsp }
4179 4e759de4 2019-06-26 stsp
4180 4e759de4 2019-06-26 stsp static const struct got_error *
4181 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4182 4e759de4 2019-06-26 stsp {
4183 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4184 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4185 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4186 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4187 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4188 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4189 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4190 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4191 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4192 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4193 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4194 4e759de4 2019-06-26 stsp
4195 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4196 da76fce2 2020-02-24 stsp
4197 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4198 4e759de4 2019-06-26 stsp switch (ch) {
4199 a74f7e83 2019-11-10 stsp case 'c':
4200 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4201 a74f7e83 2019-11-10 stsp break;
4202 4e759de4 2019-06-26 stsp case 'd':
4203 4e759de4 2019-06-26 stsp delref = optarg;
4204 4e759de4 2019-06-26 stsp break;
4205 4e759de4 2019-06-26 stsp case 'r':
4206 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4207 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4208 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4209 9ba1d308 2019-10-21 stsp optarg);
4210 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4211 4e759de4 2019-06-26 stsp break;
4212 4e759de4 2019-06-26 stsp case 'l':
4213 4e759de4 2019-06-26 stsp do_list = 1;
4214 da76fce2 2020-02-24 stsp break;
4215 da76fce2 2020-02-24 stsp case 'n':
4216 da76fce2 2020-02-24 stsp do_update = 0;
4217 4e759de4 2019-06-26 stsp break;
4218 4e759de4 2019-06-26 stsp default:
4219 4e759de4 2019-06-26 stsp usage_branch();
4220 4e759de4 2019-06-26 stsp /* NOTREACHED */
4221 4e759de4 2019-06-26 stsp }
4222 4e759de4 2019-06-26 stsp }
4223 4e759de4 2019-06-26 stsp
4224 4e759de4 2019-06-26 stsp if (do_list && delref)
4225 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4226 4e759de4 2019-06-26 stsp
4227 4e759de4 2019-06-26 stsp argc -= optind;
4228 4e759de4 2019-06-26 stsp argv += optind;
4229 ad89fa31 2019-10-04 stsp
4230 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4231 ad89fa31 2019-10-04 stsp do_show = 1;
4232 4e759de4 2019-06-26 stsp
4233 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4234 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4235 a74f7e83 2019-11-10 stsp
4236 4e759de4 2019-06-26 stsp if (do_list || delref) {
4237 4e759de4 2019-06-26 stsp if (argc > 0)
4238 4e759de4 2019-06-26 stsp usage_branch();
4239 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4240 4e759de4 2019-06-26 stsp usage_branch();
4241 4e759de4 2019-06-26 stsp
4242 4e759de4 2019-06-26 stsp #ifndef PROFILE
4243 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4244 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4245 4e759de4 2019-06-26 stsp NULL) == -1)
4246 4e759de4 2019-06-26 stsp err(1, "pledge");
4247 4e759de4 2019-06-26 stsp } else {
4248 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4249 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4250 4e759de4 2019-06-26 stsp err(1, "pledge");
4251 4e759de4 2019-06-26 stsp }
4252 4e759de4 2019-06-26 stsp #endif
4253 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4254 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4255 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4256 4e759de4 2019-06-26 stsp goto done;
4257 4e759de4 2019-06-26 stsp }
4258 4e759de4 2019-06-26 stsp
4259 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4260 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4261 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4262 4e759de4 2019-06-26 stsp goto done;
4263 4e759de4 2019-06-26 stsp else
4264 4e759de4 2019-06-26 stsp error = NULL;
4265 4e759de4 2019-06-26 stsp if (worktree) {
4266 4e759de4 2019-06-26 stsp repo_path =
4267 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4268 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4269 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4270 4e759de4 2019-06-26 stsp if (error)
4271 4e759de4 2019-06-26 stsp goto done;
4272 4e759de4 2019-06-26 stsp } else {
4273 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4274 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4275 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4276 4e759de4 2019-06-26 stsp goto done;
4277 4e759de4 2019-06-26 stsp }
4278 4e759de4 2019-06-26 stsp }
4279 4e759de4 2019-06-26 stsp }
4280 4e759de4 2019-06-26 stsp
4281 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4282 4e759de4 2019-06-26 stsp if (error != NULL)
4283 4e759de4 2019-06-26 stsp goto done;
4284 4e759de4 2019-06-26 stsp
4285 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4286 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4287 4e759de4 2019-06-26 stsp if (error)
4288 4e759de4 2019-06-26 stsp goto done;
4289 4e759de4 2019-06-26 stsp
4290 ad89fa31 2019-10-04 stsp if (do_show)
4291 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4292 ad89fa31 2019-10-04 stsp else if (do_list)
4293 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4294 4e759de4 2019-06-26 stsp else if (delref)
4295 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4296 4e759de4 2019-06-26 stsp else {
4297 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4298 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4299 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4300 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4301 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4302 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4303 a74f7e83 2019-11-10 stsp if (error)
4304 a74f7e83 2019-11-10 stsp goto done;
4305 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4306 da76fce2 2020-02-24 stsp if (error)
4307 da76fce2 2020-02-24 stsp goto done;
4308 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4309 da76fce2 2020-02-24 stsp int did_something = 0;
4310 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4311 da76fce2 2020-02-24 stsp
4312 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4313 da76fce2 2020-02-24 stsp if (error)
4314 da76fce2 2020-02-24 stsp goto done;
4315 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4316 da76fce2 2020-02-24 stsp worktree);
4317 da76fce2 2020-02-24 stsp if (error)
4318 da76fce2 2020-02-24 stsp goto done;
4319 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4320 da76fce2 2020-02-24 stsp == -1) {
4321 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4322 da76fce2 2020-02-24 stsp goto done;
4323 da76fce2 2020-02-24 stsp }
4324 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4325 da76fce2 2020-02-24 stsp free(branch_refname);
4326 da76fce2 2020-02-24 stsp if (error)
4327 da76fce2 2020-02-24 stsp goto done;
4328 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4329 da76fce2 2020-02-24 stsp repo);
4330 da76fce2 2020-02-24 stsp if (error)
4331 da76fce2 2020-02-24 stsp goto done;
4332 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4333 da76fce2 2020-02-24 stsp commit_id);
4334 da76fce2 2020-02-24 stsp if (error)
4335 da76fce2 2020-02-24 stsp goto done;
4336 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4337 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4338 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4339 da76fce2 2020-02-24 stsp if (error)
4340 da76fce2 2020-02-24 stsp goto done;
4341 da76fce2 2020-02-24 stsp if (did_something)
4342 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4343 da76fce2 2020-02-24 stsp }
4344 4e759de4 2019-06-26 stsp }
4345 4e759de4 2019-06-26 stsp done:
4346 da76fce2 2020-02-24 stsp if (ref)
4347 da76fce2 2020-02-24 stsp got_ref_close(ref);
4348 d0eebce4 2019-03-11 stsp if (repo)
4349 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4350 d0eebce4 2019-03-11 stsp if (worktree)
4351 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4352 d0eebce4 2019-03-11 stsp free(cwd);
4353 d0eebce4 2019-03-11 stsp free(repo_path);
4354 da76fce2 2020-02-24 stsp free(commit_id);
4355 da76fce2 2020-02-24 stsp free(commit_id_str);
4356 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4357 da76fce2 2020-02-24 stsp free((char *)pe->path);
4358 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4359 d00136be 2019-03-26 stsp return error;
4360 d00136be 2019-03-26 stsp }
4361 d00136be 2019-03-26 stsp
4362 8e7bd50a 2019-08-22 stsp
4363 d00136be 2019-03-26 stsp __dead static void
4364 8e7bd50a 2019-08-22 stsp usage_tag(void)
4365 8e7bd50a 2019-08-22 stsp {
4366 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4367 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4368 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4369 8e7bd50a 2019-08-22 stsp exit(1);
4370 b8bad2ba 2019-08-23 stsp }
4371 b8bad2ba 2019-08-23 stsp
4372 b8bad2ba 2019-08-23 stsp #if 0
4373 b8bad2ba 2019-08-23 stsp static const struct got_error *
4374 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4375 b8bad2ba 2019-08-23 stsp {
4376 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4377 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4378 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4379 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4380 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4381 b8bad2ba 2019-08-23 stsp
4382 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4383 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4384 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4385 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4386 b8bad2ba 2019-08-23 stsp if (err)
4387 b8bad2ba 2019-08-23 stsp return err;
4388 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4389 b8bad2ba 2019-08-23 stsp continue;
4390 b8bad2ba 2019-08-23 stsp } else {
4391 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4392 b8bad2ba 2019-08-23 stsp if (err)
4393 b8bad2ba 2019-08-23 stsp break;
4394 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4395 b8bad2ba 2019-08-23 stsp free(re_id);
4396 b8bad2ba 2019-08-23 stsp if (err)
4397 b8bad2ba 2019-08-23 stsp break;
4398 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4399 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4400 b8bad2ba 2019-08-23 stsp }
4401 b8bad2ba 2019-08-23 stsp
4402 b8bad2ba 2019-08-23 stsp while (se) {
4403 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4404 b8bad2ba 2019-08-23 stsp if (err)
4405 b8bad2ba 2019-08-23 stsp break;
4406 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4407 b8bad2ba 2019-08-23 stsp free(se_id);
4408 b8bad2ba 2019-08-23 stsp if (err)
4409 b8bad2ba 2019-08-23 stsp break;
4410 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4411 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4412 b8bad2ba 2019-08-23 stsp
4413 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4414 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4415 b8bad2ba 2019-08-23 stsp if (err)
4416 b8bad2ba 2019-08-23 stsp return err;
4417 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4418 b8bad2ba 2019-08-23 stsp break;
4419 b8bad2ba 2019-08-23 stsp }
4420 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4421 b8bad2ba 2019-08-23 stsp continue;
4422 b8bad2ba 2019-08-23 stsp }
4423 b8bad2ba 2019-08-23 stsp }
4424 b8bad2ba 2019-08-23 stsp done:
4425 b8bad2ba 2019-08-23 stsp return err;
4426 8e7bd50a 2019-08-22 stsp }
4427 b8bad2ba 2019-08-23 stsp #endif
4428 b8bad2ba 2019-08-23 stsp
4429 b8bad2ba 2019-08-23 stsp static const struct got_error *
4430 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4431 8e7bd50a 2019-08-22 stsp {
4432 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4433 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4434 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4435 8e7bd50a 2019-08-22 stsp
4436 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4437 8e7bd50a 2019-08-22 stsp
4438 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4439 8e7bd50a 2019-08-22 stsp if (err)
4440 8e7bd50a 2019-08-22 stsp return err;
4441 8e7bd50a 2019-08-22 stsp
4442 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4443 8e7bd50a 2019-08-22 stsp const char *refname;
4444 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4445 8e7bd50a 2019-08-22 stsp char datebuf[26];
4446 d4efa91b 2020-01-14 stsp const char *tagger;
4447 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4448 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4449 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4450 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4451 8e7bd50a 2019-08-22 stsp
4452 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4453 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4454 8e7bd50a 2019-08-22 stsp continue;
4455 8e7bd50a 2019-08-22 stsp refname += 10;
4456 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4457 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4458 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4459 8e7bd50a 2019-08-22 stsp break;
4460 8e7bd50a 2019-08-22 stsp }
4461 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4462 8e7bd50a 2019-08-22 stsp free(refstr);
4463 8e7bd50a 2019-08-22 stsp
4464 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4465 8e7bd50a 2019-08-22 stsp if (err)
4466 8e7bd50a 2019-08-22 stsp break;
4467 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4468 d4efa91b 2020-01-14 stsp if (err) {
4469 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4470 d4efa91b 2020-01-14 stsp free(id);
4471 d4efa91b 2020-01-14 stsp break;
4472 d4efa91b 2020-01-14 stsp }
4473 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4474 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4475 d4efa91b 2020-01-14 stsp if (err) {
4476 d4efa91b 2020-01-14 stsp free(id);
4477 d4efa91b 2020-01-14 stsp break;
4478 d4efa91b 2020-01-14 stsp }
4479 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4480 d4efa91b 2020-01-14 stsp tagger_time =
4481 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4482 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4483 d4efa91b 2020-01-14 stsp free(id);
4484 d4efa91b 2020-01-14 stsp if (err)
4485 d4efa91b 2020-01-14 stsp break;
4486 d4efa91b 2020-01-14 stsp } else {
4487 d4efa91b 2020-01-14 stsp free(id);
4488 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4489 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4490 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4491 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4492 d4efa91b 2020-01-14 stsp if (err)
4493 d4efa91b 2020-01-14 stsp break;
4494 d4efa91b 2020-01-14 stsp }
4495 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4496 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4497 2417344c 2019-08-23 stsp if (datestr)
4498 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4499 d4efa91b 2020-01-14 stsp if (commit)
4500 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4501 d4efa91b 2020-01-14 stsp else {
4502 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4503 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4504 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4505 d4efa91b 2020-01-14 stsp id_str);
4506 d4efa91b 2020-01-14 stsp break;
4507 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4508 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4509 d4efa91b 2020-01-14 stsp id_str);
4510 d4efa91b 2020-01-14 stsp break;
4511 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4512 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4513 d4efa91b 2020-01-14 stsp id_str);
4514 d4efa91b 2020-01-14 stsp break;
4515 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4516 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4517 d4efa91b 2020-01-14 stsp id_str);
4518 d4efa91b 2020-01-14 stsp break;
4519 d4efa91b 2020-01-14 stsp default:
4520 d4efa91b 2020-01-14 stsp break;
4521 d4efa91b 2020-01-14 stsp }
4522 8e7bd50a 2019-08-22 stsp }
4523 8e7bd50a 2019-08-22 stsp free(id_str);
4524 d4efa91b 2020-01-14 stsp if (commit) {
4525 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4526 d4efa91b 2020-01-14 stsp if (err)
4527 d4efa91b 2020-01-14 stsp break;
4528 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4529 d4efa91b 2020-01-14 stsp } else {
4530 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4531 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4532 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4533 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4534 d4efa91b 2020-01-14 stsp break;
4535 d4efa91b 2020-01-14 stsp }
4536 8e7bd50a 2019-08-22 stsp }
4537 8e7bd50a 2019-08-22 stsp
4538 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4539 8e7bd50a 2019-08-22 stsp do {
4540 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4541 8e7bd50a 2019-08-22 stsp if (line)
4542 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4543 8e7bd50a 2019-08-22 stsp } while (line);
4544 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4545 8e7bd50a 2019-08-22 stsp }
4546 8e7bd50a 2019-08-22 stsp
4547 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4548 8e7bd50a 2019-08-22 stsp return NULL;
4549 8e7bd50a 2019-08-22 stsp }
4550 8e7bd50a 2019-08-22 stsp
4551 8e7bd50a 2019-08-22 stsp static const struct got_error *
4552 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4553 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4554 8e7bd50a 2019-08-22 stsp {
4555 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4556 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4557 f372d5cd 2019-10-21 stsp char *editor = NULL;
4558 8e7bd50a 2019-08-22 stsp int fd = -1;
4559 8e7bd50a 2019-08-22 stsp
4560 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4561 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4562 8e7bd50a 2019-08-22 stsp goto done;
4563 8e7bd50a 2019-08-22 stsp }
4564 8e7bd50a 2019-08-22 stsp
4565 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4566 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4567 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4568 8e7bd50a 2019-08-22 stsp goto done;
4569 8e7bd50a 2019-08-22 stsp }
4570 8e7bd50a 2019-08-22 stsp
4571 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4572 8e7bd50a 2019-08-22 stsp if (err)
4573 8e7bd50a 2019-08-22 stsp goto done;
4574 8e7bd50a 2019-08-22 stsp
4575 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4576 8e7bd50a 2019-08-22 stsp close(fd);
4577 8e7bd50a 2019-08-22 stsp
4578 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4579 8e7bd50a 2019-08-22 stsp if (err)
4580 8e7bd50a 2019-08-22 stsp goto done;
4581 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4582 8e7bd50a 2019-08-22 stsp done:
4583 8e7bd50a 2019-08-22 stsp free(initial_content);
4584 8e7bd50a 2019-08-22 stsp free(template);
4585 8e7bd50a 2019-08-22 stsp free(editor);
4586 8e7bd50a 2019-08-22 stsp
4587 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4588 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4589 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4590 8e7bd50a 2019-08-22 stsp if (err) {
4591 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4592 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4593 8e7bd50a 2019-08-22 stsp }
4594 8e7bd50a 2019-08-22 stsp }
4595 8e7bd50a 2019-08-22 stsp return err;
4596 8e7bd50a 2019-08-22 stsp }
4597 8e7bd50a 2019-08-22 stsp
4598 8e7bd50a 2019-08-22 stsp static const struct got_error *
4599 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4600 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4601 8e7bd50a 2019-08-22 stsp {
4602 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4603 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4604 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4605 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4606 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4607 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4608 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4609 8e7bd50a 2019-08-22 stsp
4610 8e7bd50a 2019-08-22 stsp /*
4611 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4612 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4613 8e7bd50a 2019-08-22 stsp * an unintended typo.
4614 8e7bd50a 2019-08-22 stsp */
4615 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4616 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4617 8e7bd50a 2019-08-22 stsp
4618 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4619 8e7bd50a 2019-08-22 stsp if (err)
4620 8e7bd50a 2019-08-22 stsp return err;
4621 8e7bd50a 2019-08-22 stsp
4622 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4623 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4624 8e7bd50a 2019-08-22 stsp if (err)
4625 8e7bd50a 2019-08-22 stsp goto done;
4626 8e7bd50a 2019-08-22 stsp
4627 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4628 8e7bd50a 2019-08-22 stsp if (err)
4629 8e7bd50a 2019-08-22 stsp goto done;
4630 8e7bd50a 2019-08-22 stsp
4631 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4632 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4633 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4634 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4635 8e7bd50a 2019-08-22 stsp goto done;
4636 8e7bd50a 2019-08-22 stsp }
4637 8e7bd50a 2019-08-22 stsp tag_name += 10;
4638 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4639 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4640 8e7bd50a 2019-08-22 stsp goto done;
4641 8e7bd50a 2019-08-22 stsp }
4642 8e7bd50a 2019-08-22 stsp
4643 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4644 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4645 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4646 8e7bd50a 2019-08-22 stsp goto done;
4647 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4648 8e7bd50a 2019-08-22 stsp goto done;
4649 8e7bd50a 2019-08-22 stsp
4650 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4651 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4652 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4653 f372d5cd 2019-10-21 stsp if (err) {
4654 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4655 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4656 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4657 8e7bd50a 2019-08-22 stsp goto done;
4658 f372d5cd 2019-10-21 stsp }
4659 8e7bd50a 2019-08-22 stsp }
4660 8e7bd50a 2019-08-22 stsp
4661 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4662 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4663 f372d5cd 2019-10-21 stsp if (err) {
4664 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4665 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4666 8e7bd50a 2019-08-22 stsp goto done;
4667 f372d5cd 2019-10-21 stsp }
4668 8e7bd50a 2019-08-22 stsp
4669 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4670 f372d5cd 2019-10-21 stsp if (err) {
4671 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4672 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4673 8e7bd50a 2019-08-22 stsp goto done;
4674 f372d5cd 2019-10-21 stsp }
4675 8e7bd50a 2019-08-22 stsp
4676 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4677 f372d5cd 2019-10-21 stsp if (err) {
4678 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4679 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4680 f372d5cd 2019-10-21 stsp goto done;
4681 f372d5cd 2019-10-21 stsp }
4682 8e7bd50a 2019-08-22 stsp
4683 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4684 f372d5cd 2019-10-21 stsp if (err) {
4685 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4686 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4687 f372d5cd 2019-10-21 stsp goto done;
4688 8e7bd50a 2019-08-22 stsp }
4689 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4690 8e7bd50a 2019-08-22 stsp done:
4691 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4692 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4693 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4694 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4695 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4696 f372d5cd 2019-10-21 stsp free(tag_id_str);
4697 8e7bd50a 2019-08-22 stsp if (ref)
4698 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4699 8e7bd50a 2019-08-22 stsp free(commit_id);
4700 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4701 8e7bd50a 2019-08-22 stsp free(refname);
4702 8e7bd50a 2019-08-22 stsp free(tagmsg);
4703 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4704 aba9c984 2019-09-08 stsp free(tagger);
4705 8e7bd50a 2019-08-22 stsp return err;
4706 8e7bd50a 2019-08-22 stsp }
4707 8e7bd50a 2019-08-22 stsp
4708 8e7bd50a 2019-08-22 stsp static const struct got_error *
4709 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4710 8e7bd50a 2019-08-22 stsp {
4711 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4712 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4713 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4714 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4715 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4716 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4717 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4718 8e7bd50a 2019-08-22 stsp
4719 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4720 8e7bd50a 2019-08-22 stsp switch (ch) {
4721 80106605 2020-02-24 stsp case 'c':
4722 80106605 2020-02-24 stsp commit_id_arg = optarg;
4723 80106605 2020-02-24 stsp break;
4724 8e7bd50a 2019-08-22 stsp case 'm':
4725 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4726 8e7bd50a 2019-08-22 stsp break;
4727 8e7bd50a 2019-08-22 stsp case 'r':
4728 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4729 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4730 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4731 9ba1d308 2019-10-21 stsp optarg);
4732 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4733 8e7bd50a 2019-08-22 stsp break;
4734 8e7bd50a 2019-08-22 stsp case 'l':
4735 8e7bd50a 2019-08-22 stsp do_list = 1;
4736 8e7bd50a 2019-08-22 stsp break;
4737 8e7bd50a 2019-08-22 stsp default:
4738 8e7bd50a 2019-08-22 stsp usage_tag();
4739 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4740 8e7bd50a 2019-08-22 stsp }
4741 8e7bd50a 2019-08-22 stsp }
4742 8e7bd50a 2019-08-22 stsp
4743 8e7bd50a 2019-08-22 stsp argc -= optind;
4744 8e7bd50a 2019-08-22 stsp argv += optind;
4745 8e7bd50a 2019-08-22 stsp
4746 8e7bd50a 2019-08-22 stsp if (do_list) {
4747 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4748 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4749 8e7bd50a 2019-08-22 stsp if (tagmsg)
4750 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4751 8e7bd50a 2019-08-22 stsp if (argc > 0)
4752 8e7bd50a 2019-08-22 stsp usage_tag();
4753 80106605 2020-02-24 stsp } else if (argc != 1)
4754 8e7bd50a 2019-08-22 stsp usage_tag();
4755 80106605 2020-02-24 stsp
4756 a2887370 2019-08-23 stsp tag_name = argv[0];
4757 8e7bd50a 2019-08-22 stsp
4758 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4759 8e7bd50a 2019-08-22 stsp if (do_list) {
4760 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4761 8e7bd50a 2019-08-22 stsp NULL) == -1)
4762 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4763 8e7bd50a 2019-08-22 stsp } else {
4764 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4765 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4766 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4767 8e7bd50a 2019-08-22 stsp }
4768 8e7bd50a 2019-08-22 stsp #endif
4769 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4770 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4771 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4772 8e7bd50a 2019-08-22 stsp goto done;
4773 8e7bd50a 2019-08-22 stsp }
4774 8e7bd50a 2019-08-22 stsp
4775 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4776 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4777 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4778 8e7bd50a 2019-08-22 stsp goto done;
4779 8e7bd50a 2019-08-22 stsp else
4780 8e7bd50a 2019-08-22 stsp error = NULL;
4781 8e7bd50a 2019-08-22 stsp if (worktree) {
4782 8e7bd50a 2019-08-22 stsp repo_path =
4783 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4784 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4785 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4786 8e7bd50a 2019-08-22 stsp if (error)
4787 8e7bd50a 2019-08-22 stsp goto done;
4788 8e7bd50a 2019-08-22 stsp } else {
4789 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4790 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4791 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4792 8e7bd50a 2019-08-22 stsp goto done;
4793 8e7bd50a 2019-08-22 stsp }
4794 8e7bd50a 2019-08-22 stsp }
4795 8e7bd50a 2019-08-22 stsp }
4796 8e7bd50a 2019-08-22 stsp
4797 8e7bd50a 2019-08-22 stsp if (do_list) {
4798 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4799 c9956ddf 2019-09-08 stsp if (error != NULL)
4800 c9956ddf 2019-09-08 stsp goto done;
4801 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4802 8e7bd50a 2019-08-22 stsp if (error)
4803 8e7bd50a 2019-08-22 stsp goto done;
4804 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4805 8e7bd50a 2019-08-22 stsp } else {
4806 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4807 c9956ddf 2019-09-08 stsp if (error)
4808 c9956ddf 2019-09-08 stsp goto done;
4809 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4810 c9956ddf 2019-09-08 stsp if (error != NULL)
4811 c9956ddf 2019-09-08 stsp goto done;
4812 c9956ddf 2019-09-08 stsp
4813 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4814 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4815 8e7bd50a 2019-08-22 stsp if (error)
4816 8e7bd50a 2019-08-22 stsp goto done;
4817 8e7bd50a 2019-08-22 stsp }
4818 8e7bd50a 2019-08-22 stsp
4819 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4820 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4821 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4822 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4823 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4824 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4825 8e7bd50a 2019-08-22 stsp if (error)
4826 8e7bd50a 2019-08-22 stsp goto done;
4827 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4828 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4829 8e7bd50a 2019-08-22 stsp if (error)
4830 8e7bd50a 2019-08-22 stsp goto done;
4831 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4832 8e7bd50a 2019-08-22 stsp free(commit_id);
4833 8e7bd50a 2019-08-22 stsp if (error)
4834 8e7bd50a 2019-08-22 stsp goto done;
4835 8e7bd50a 2019-08-22 stsp }
4836 8e7bd50a 2019-08-22 stsp
4837 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4838 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4839 8e7bd50a 2019-08-22 stsp }
4840 8e7bd50a 2019-08-22 stsp done:
4841 8e7bd50a 2019-08-22 stsp if (repo)
4842 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4843 8e7bd50a 2019-08-22 stsp if (worktree)
4844 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4845 8e7bd50a 2019-08-22 stsp free(cwd);
4846 8e7bd50a 2019-08-22 stsp free(repo_path);
4847 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4848 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4849 8e7bd50a 2019-08-22 stsp return error;
4850 8e7bd50a 2019-08-22 stsp }
4851 8e7bd50a 2019-08-22 stsp
4852 8e7bd50a 2019-08-22 stsp __dead static void
4853 d00136be 2019-03-26 stsp usage_add(void)
4854 d00136be 2019-03-26 stsp {
4855 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4856 022fae89 2019-12-06 tracey getprogname());
4857 d00136be 2019-03-26 stsp exit(1);
4858 d00136be 2019-03-26 stsp }
4859 d00136be 2019-03-26 stsp
4860 d00136be 2019-03-26 stsp static const struct got_error *
4861 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4862 4e68cba3 2019-11-23 stsp {
4863 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4864 4e68cba3 2019-11-23 stsp path++;
4865 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4866 4e68cba3 2019-11-23 stsp return NULL;
4867 4e68cba3 2019-11-23 stsp }
4868 4e68cba3 2019-11-23 stsp
4869 4e68cba3 2019-11-23 stsp static const struct got_error *
4870 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4871 d00136be 2019-03-26 stsp {
4872 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4873 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4874 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4875 1dd54920 2019-05-11 stsp char *cwd = NULL;
4876 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4877 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4878 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4879 1dd54920 2019-05-11 stsp
4880 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4881 d00136be 2019-03-26 stsp
4882 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4883 d00136be 2019-03-26 stsp switch (ch) {
4884 022fae89 2019-12-06 tracey case 'I':
4885 022fae89 2019-12-06 tracey no_ignores = 1;
4886 022fae89 2019-12-06 tracey break;
4887 4e68cba3 2019-11-23 stsp case 'R':
4888 4e68cba3 2019-11-23 stsp can_recurse = 1;
4889 4e68cba3 2019-11-23 stsp break;
4890 d00136be 2019-03-26 stsp default:
4891 d00136be 2019-03-26 stsp usage_add();
4892 d00136be 2019-03-26 stsp /* NOTREACHED */
4893 d00136be 2019-03-26 stsp }
4894 d00136be 2019-03-26 stsp }
4895 d00136be 2019-03-26 stsp
4896 d00136be 2019-03-26 stsp argc -= optind;
4897 d00136be 2019-03-26 stsp argv += optind;
4898 d00136be 2019-03-26 stsp
4899 43012d58 2019-07-14 stsp #ifndef PROFILE
4900 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4901 43012d58 2019-07-14 stsp NULL) == -1)
4902 43012d58 2019-07-14 stsp err(1, "pledge");
4903 43012d58 2019-07-14 stsp #endif
4904 723c305c 2019-05-11 jcs if (argc < 1)
4905 d00136be 2019-03-26 stsp usage_add();
4906 d00136be 2019-03-26 stsp
4907 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4908 d00136be 2019-03-26 stsp if (cwd == NULL) {
4909 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4910 d00136be 2019-03-26 stsp goto done;
4911 d00136be 2019-03-26 stsp }
4912 723c305c 2019-05-11 jcs
4913 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4914 d00136be 2019-03-26 stsp if (error)
4915 d00136be 2019-03-26 stsp goto done;
4916 d00136be 2019-03-26 stsp
4917 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4918 c9956ddf 2019-09-08 stsp NULL);
4919 031a5338 2019-03-26 stsp if (error != NULL)
4920 031a5338 2019-03-26 stsp goto done;
4921 031a5338 2019-03-26 stsp
4922 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4923 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4924 d00136be 2019-03-26 stsp if (error)
4925 d00136be 2019-03-26 stsp goto done;
4926 d00136be 2019-03-26 stsp
4927 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4928 6d022e97 2019-08-04 stsp if (error)
4929 022fae89 2019-12-06 tracey goto done;
4930 022fae89 2019-12-06 tracey
4931 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4932 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4933 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4934 6d022e97 2019-08-04 stsp goto done;
4935 723c305c 2019-05-11 jcs
4936 022fae89 2019-12-06 tracey }
4937 022fae89 2019-12-06 tracey
4938 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4939 4e68cba3 2019-11-23 stsp char *ondisk_path;
4940 4e68cba3 2019-11-23 stsp struct stat sb;
4941 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4942 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4943 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4944 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4945 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4946 4e68cba3 2019-11-23 stsp goto done;
4947 4e68cba3 2019-11-23 stsp }
4948 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4949 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4950 4e68cba3 2019-11-23 stsp free(ondisk_path);
4951 4e68cba3 2019-11-23 stsp continue;
4952 4e68cba3 2019-11-23 stsp }
4953 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4954 4e68cba3 2019-11-23 stsp ondisk_path);
4955 4e68cba3 2019-11-23 stsp free(ondisk_path);
4956 4e68cba3 2019-11-23 stsp goto done;
4957 4e68cba3 2019-11-23 stsp }
4958 4e68cba3 2019-11-23 stsp free(ondisk_path);
4959 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4960 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4961 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4962 4e68cba3 2019-11-23 stsp goto done;
4963 4e68cba3 2019-11-23 stsp }
4964 4e68cba3 2019-11-23 stsp }
4965 4e68cba3 2019-11-23 stsp }
4966 022fae89 2019-12-06 tracey
4967 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4968 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4969 d00136be 2019-03-26 stsp done:
4970 031a5338 2019-03-26 stsp if (repo)
4971 031a5338 2019-03-26 stsp got_repo_close(repo);
4972 d00136be 2019-03-26 stsp if (worktree)
4973 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4974 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4975 1dd54920 2019-05-11 stsp free((char *)pe->path);
4976 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4977 2ec1f75b 2019-03-26 stsp free(cwd);
4978 2ec1f75b 2019-03-26 stsp return error;
4979 2ec1f75b 2019-03-26 stsp }
4980 2ec1f75b 2019-03-26 stsp
4981 2ec1f75b 2019-03-26 stsp __dead static void
4982 648e4ef7 2019-07-09 stsp usage_remove(void)
4983 2ec1f75b 2019-03-26 stsp {
4984 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4985 f2a9dc41 2019-12-13 tracey getprogname());
4986 2ec1f75b 2019-03-26 stsp exit(1);
4987 2a06fe5f 2019-08-24 stsp }
4988 2a06fe5f 2019-08-24 stsp
4989 2a06fe5f 2019-08-24 stsp static const struct got_error *
4990 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4991 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4992 2a06fe5f 2019-08-24 stsp {
4993 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4994 f2a9dc41 2019-12-13 tracey path++;
4995 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4996 2a06fe5f 2019-08-24 stsp return NULL;
4997 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4998 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4999 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5000 2a06fe5f 2019-08-24 stsp return NULL;
5001 2ec1f75b 2019-03-26 stsp }
5002 2ec1f75b 2019-03-26 stsp
5003 2ec1f75b 2019-03-26 stsp static const struct got_error *
5004 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5005 2ec1f75b 2019-03-26 stsp {
5006 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5007 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5008 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5009 17ed4618 2019-06-02 stsp char *cwd = NULL;
5010 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5011 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5012 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5013 2ec1f75b 2019-03-26 stsp
5014 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5015 17ed4618 2019-06-02 stsp
5016 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5017 2ec1f75b 2019-03-26 stsp switch (ch) {
5018 2ec1f75b 2019-03-26 stsp case 'f':
5019 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5020 2ec1f75b 2019-03-26 stsp break;
5021 70e3e7f5 2019-12-13 tracey case 'k':
5022 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5023 70e3e7f5 2019-12-13 tracey break;
5024 f2a9dc41 2019-12-13 tracey case 'R':
5025 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5026 f2a9dc41 2019-12-13 tracey break;
5027 2ec1f75b 2019-03-26 stsp default:
5028 f2a9dc41 2019-12-13 tracey usage_remove();
5029 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5030 2ec1f75b 2019-03-26 stsp }
5031 2ec1f75b 2019-03-26 stsp }
5032 2ec1f75b 2019-03-26 stsp
5033 2ec1f75b 2019-03-26 stsp argc -= optind;
5034 2ec1f75b 2019-03-26 stsp argv += optind;
5035 2ec1f75b 2019-03-26 stsp
5036 43012d58 2019-07-14 stsp #ifndef PROFILE
5037 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5038 43012d58 2019-07-14 stsp NULL) == -1)
5039 43012d58 2019-07-14 stsp err(1, "pledge");
5040 43012d58 2019-07-14 stsp #endif
5041 17ed4618 2019-06-02 stsp if (argc < 1)
5042 648e4ef7 2019-07-09 stsp usage_remove();
5043 2ec1f75b 2019-03-26 stsp
5044 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5045 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5046 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5047 2ec1f75b 2019-03-26 stsp goto done;
5048 2ec1f75b 2019-03-26 stsp }
5049 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5050 2ec1f75b 2019-03-26 stsp if (error)
5051 2ec1f75b 2019-03-26 stsp goto done;
5052 2ec1f75b 2019-03-26 stsp
5053 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5054 c9956ddf 2019-09-08 stsp NULL);
5055 2af4a041 2019-05-11 jcs if (error)
5056 2ec1f75b 2019-03-26 stsp goto done;
5057 2ec1f75b 2019-03-26 stsp
5058 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5059 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5060 2ec1f75b 2019-03-26 stsp if (error)
5061 2ec1f75b 2019-03-26 stsp goto done;
5062 2ec1f75b 2019-03-26 stsp
5063 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5064 6d022e97 2019-08-04 stsp if (error)
5065 6d022e97 2019-08-04 stsp goto done;
5066 17ed4618 2019-06-02 stsp
5067 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5068 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5069 f2a9dc41 2019-12-13 tracey struct stat sb;
5070 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5071 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5072 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5073 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5074 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5075 f2a9dc41 2019-12-13 tracey goto done;
5076 f2a9dc41 2019-12-13 tracey }
5077 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5078 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5079 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5080 f2a9dc41 2019-12-13 tracey continue;
5081 f2a9dc41 2019-12-13 tracey }
5082 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5083 f2a9dc41 2019-12-13 tracey ondisk_path);
5084 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5085 f2a9dc41 2019-12-13 tracey goto done;
5086 f2a9dc41 2019-12-13 tracey }
5087 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5088 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5089 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5090 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5091 f2a9dc41 2019-12-13 tracey goto done;
5092 f2a9dc41 2019-12-13 tracey }
5093 f2a9dc41 2019-12-13 tracey }
5094 f2a9dc41 2019-12-13 tracey }
5095 f2a9dc41 2019-12-13 tracey
5096 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5097 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5098 a129376b 2019-03-28 stsp done:
5099 a129376b 2019-03-28 stsp if (repo)
5100 a129376b 2019-03-28 stsp got_repo_close(repo);
5101 a129376b 2019-03-28 stsp if (worktree)
5102 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5103 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5104 17ed4618 2019-06-02 stsp free((char *)pe->path);
5105 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5106 a129376b 2019-03-28 stsp free(cwd);
5107 a129376b 2019-03-28 stsp return error;
5108 a129376b 2019-03-28 stsp }
5109 a129376b 2019-03-28 stsp
5110 a129376b 2019-03-28 stsp __dead static void
5111 a129376b 2019-03-28 stsp usage_revert(void)
5112 a129376b 2019-03-28 stsp {
5113 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5114 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5115 a129376b 2019-03-28 stsp exit(1);
5116 a129376b 2019-03-28 stsp }
5117 a129376b 2019-03-28 stsp
5118 1ee397ad 2019-07-12 stsp static const struct got_error *
5119 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5120 a129376b 2019-03-28 stsp {
5121 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5122 fb9704af 2020-01-27 stsp return NULL;
5123 fb9704af 2020-01-27 stsp
5124 a129376b 2019-03-28 stsp while (path[0] == '/')
5125 a129376b 2019-03-28 stsp path++;
5126 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5127 33aa809d 2019-08-08 stsp return NULL;
5128 33aa809d 2019-08-08 stsp }
5129 33aa809d 2019-08-08 stsp
5130 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5131 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5132 33aa809d 2019-08-08 stsp const char *action;
5133 33aa809d 2019-08-08 stsp };
5134 33aa809d 2019-08-08 stsp
5135 33aa809d 2019-08-08 stsp static const struct got_error *
5136 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5137 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5138 33aa809d 2019-08-08 stsp {
5139 33aa809d 2019-08-08 stsp char *line = NULL;
5140 33aa809d 2019-08-08 stsp size_t linesize = 0;
5141 33aa809d 2019-08-08 stsp ssize_t linelen;
5142 33aa809d 2019-08-08 stsp
5143 33aa809d 2019-08-08 stsp switch (status) {
5144 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5145 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5146 33aa809d 2019-08-08 stsp break;
5147 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5148 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5149 33aa809d 2019-08-08 stsp break;
5150 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5151 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5152 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5153 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5154 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5155 33aa809d 2019-08-08 stsp printf("%s", line);
5156 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5157 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5158 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5159 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5160 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5161 33aa809d 2019-08-08 stsp break;
5162 33aa809d 2019-08-08 stsp default:
5163 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5164 33aa809d 2019-08-08 stsp }
5165 33aa809d 2019-08-08 stsp
5166 33aa809d 2019-08-08 stsp return NULL;
5167 33aa809d 2019-08-08 stsp }
5168 33aa809d 2019-08-08 stsp
5169 33aa809d 2019-08-08 stsp static const struct got_error *
5170 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5171 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5172 33aa809d 2019-08-08 stsp {
5173 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5174 33aa809d 2019-08-08 stsp char *line = NULL;
5175 33aa809d 2019-08-08 stsp size_t linesize = 0;
5176 33aa809d 2019-08-08 stsp ssize_t linelen;
5177 33aa809d 2019-08-08 stsp int resp = ' ';
5178 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5179 33aa809d 2019-08-08 stsp
5180 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5181 33aa809d 2019-08-08 stsp
5182 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5183 33aa809d 2019-08-08 stsp char *nl;
5184 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5185 33aa809d 2019-08-08 stsp a->action);
5186 33aa809d 2019-08-08 stsp if (err)
5187 33aa809d 2019-08-08 stsp return err;
5188 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5189 33aa809d 2019-08-08 stsp if (linelen == -1) {
5190 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5191 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5192 33aa809d 2019-08-08 stsp return NULL;
5193 33aa809d 2019-08-08 stsp }
5194 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5195 33aa809d 2019-08-08 stsp if (nl)
5196 33aa809d 2019-08-08 stsp *nl = '\0';
5197 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5198 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5199 33aa809d 2019-08-08 stsp printf("y\n");
5200 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5201 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5202 33aa809d 2019-08-08 stsp printf("n\n");
5203 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5204 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5205 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5206 33aa809d 2019-08-08 stsp printf("q\n");
5207 33aa809d 2019-08-08 stsp } else
5208 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5209 33aa809d 2019-08-08 stsp free(line);
5210 33aa809d 2019-08-08 stsp return NULL;
5211 33aa809d 2019-08-08 stsp }
5212 33aa809d 2019-08-08 stsp
5213 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5214 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5215 33aa809d 2019-08-08 stsp a->action);
5216 33aa809d 2019-08-08 stsp if (err)
5217 33aa809d 2019-08-08 stsp return err;
5218 33aa809d 2019-08-08 stsp resp = getchar();
5219 33aa809d 2019-08-08 stsp if (resp == '\n')
5220 33aa809d 2019-08-08 stsp resp = getchar();
5221 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5222 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5223 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5224 33aa809d 2019-08-08 stsp resp = ' ';
5225 33aa809d 2019-08-08 stsp }
5226 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5227 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5228 33aa809d 2019-08-08 stsp resp = ' ';
5229 33aa809d 2019-08-08 stsp }
5230 33aa809d 2019-08-08 stsp }
5231 33aa809d 2019-08-08 stsp
5232 33aa809d 2019-08-08 stsp if (resp == 'y')
5233 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5234 33aa809d 2019-08-08 stsp else if (resp == 'n')
5235 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5236 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5237 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5238 33aa809d 2019-08-08 stsp
5239 1ee397ad 2019-07-12 stsp return NULL;
5240 a129376b 2019-03-28 stsp }
5241 a129376b 2019-03-28 stsp
5242 33aa809d 2019-08-08 stsp
5243 a129376b 2019-03-28 stsp static const struct got_error *
5244 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5245 a129376b 2019-03-28 stsp {
5246 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5247 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5248 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5249 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5250 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5251 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5252 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5253 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5254 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5255 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5256 a129376b 2019-03-28 stsp
5257 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5258 e20a8b6f 2019-06-04 stsp
5259 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5260 a129376b 2019-03-28 stsp switch (ch) {
5261 33aa809d 2019-08-08 stsp case 'p':
5262 33aa809d 2019-08-08 stsp pflag = 1;
5263 33aa809d 2019-08-08 stsp break;
5264 33aa809d 2019-08-08 stsp case 'F':
5265 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5266 33aa809d 2019-08-08 stsp break;
5267 0f6d7415 2019-08-08 stsp case 'R':
5268 0f6d7415 2019-08-08 stsp can_recurse = 1;
5269 0f6d7415 2019-08-08 stsp break;
5270 a129376b 2019-03-28 stsp default:
5271 a129376b 2019-03-28 stsp usage_revert();
5272 a129376b 2019-03-28 stsp /* NOTREACHED */
5273 a129376b 2019-03-28 stsp }
5274 a129376b 2019-03-28 stsp }
5275 a129376b 2019-03-28 stsp
5276 a129376b 2019-03-28 stsp argc -= optind;
5277 a129376b 2019-03-28 stsp argv += optind;
5278 a129376b 2019-03-28 stsp
5279 43012d58 2019-07-14 stsp #ifndef PROFILE
5280 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5281 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5282 43012d58 2019-07-14 stsp err(1, "pledge");
5283 43012d58 2019-07-14 stsp #endif
5284 e20a8b6f 2019-06-04 stsp if (argc < 1)
5285 a129376b 2019-03-28 stsp usage_revert();
5286 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5287 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5288 a129376b 2019-03-28 stsp
5289 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5290 a129376b 2019-03-28 stsp if (cwd == NULL) {
5291 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5292 a129376b 2019-03-28 stsp goto done;
5293 a129376b 2019-03-28 stsp }
5294 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5295 2ec1f75b 2019-03-26 stsp if (error)
5296 2ec1f75b 2019-03-26 stsp goto done;
5297 a129376b 2019-03-28 stsp
5298 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5299 c9956ddf 2019-09-08 stsp NULL);
5300 a129376b 2019-03-28 stsp if (error != NULL)
5301 a129376b 2019-03-28 stsp goto done;
5302 a129376b 2019-03-28 stsp
5303 33aa809d 2019-08-08 stsp if (patch_script_path) {
5304 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5305 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5306 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5307 33aa809d 2019-08-08 stsp patch_script_path);
5308 33aa809d 2019-08-08 stsp goto done;
5309 33aa809d 2019-08-08 stsp }
5310 33aa809d 2019-08-08 stsp }
5311 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5312 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5313 a129376b 2019-03-28 stsp if (error)
5314 a129376b 2019-03-28 stsp goto done;
5315 a129376b 2019-03-28 stsp
5316 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5317 6d022e97 2019-08-04 stsp if (error)
5318 6d022e97 2019-08-04 stsp goto done;
5319 00db391e 2019-08-03 stsp
5320 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5321 0f6d7415 2019-08-08 stsp char *ondisk_path;
5322 0f6d7415 2019-08-08 stsp struct stat sb;
5323 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5324 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5325 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5326 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5327 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5328 0f6d7415 2019-08-08 stsp goto done;
5329 0f6d7415 2019-08-08 stsp }
5330 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5331 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5332 0f6d7415 2019-08-08 stsp free(ondisk_path);
5333 0f6d7415 2019-08-08 stsp continue;
5334 0f6d7415 2019-08-08 stsp }
5335 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5336 0f6d7415 2019-08-08 stsp ondisk_path);
5337 0f6d7415 2019-08-08 stsp free(ondisk_path);
5338 0f6d7415 2019-08-08 stsp goto done;
5339 0f6d7415 2019-08-08 stsp }
5340 0f6d7415 2019-08-08 stsp free(ondisk_path);
5341 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5342 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5343 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5344 0f6d7415 2019-08-08 stsp goto done;
5345 0f6d7415 2019-08-08 stsp }
5346 0f6d7415 2019-08-08 stsp }
5347 0f6d7415 2019-08-08 stsp }
5348 0f6d7415 2019-08-08 stsp
5349 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5350 33aa809d 2019-08-08 stsp cpa.action = "revert";
5351 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5352 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5353 2ec1f75b 2019-03-26 stsp done:
5354 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5355 33aa809d 2019-08-08 stsp error == NULL)
5356 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5357 2ec1f75b 2019-03-26 stsp if (repo)
5358 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5359 2ec1f75b 2019-03-26 stsp if (worktree)
5360 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5361 2ec1f75b 2019-03-26 stsp free(path);
5362 d00136be 2019-03-26 stsp free(cwd);
5363 6bad629b 2019-02-04 stsp return error;
5364 c4296144 2019-05-09 stsp }
5365 c4296144 2019-05-09 stsp
5366 c4296144 2019-05-09 stsp __dead static void
5367 c4296144 2019-05-09 stsp usage_commit(void)
5368 c4296144 2019-05-09 stsp {
5369 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5370 5c1e53bc 2019-07-28 stsp getprogname());
5371 c4296144 2019-05-09 stsp exit(1);
5372 33ad4cbe 2019-05-12 jcs }
5373 33ad4cbe 2019-05-12 jcs
5374 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5375 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5376 e2ba3d07 2019-05-13 stsp const char *editor;
5377 e0870e44 2019-05-13 stsp const char *worktree_path;
5378 76d98825 2019-06-03 stsp const char *branch_name;
5379 314a6357 2019-05-13 stsp const char *repo_path;
5380 e0870e44 2019-05-13 stsp char *logmsg_path;
5381 e2ba3d07 2019-05-13 stsp
5382 e2ba3d07 2019-05-13 stsp };
5383 e0870e44 2019-05-13 stsp
5384 33ad4cbe 2019-05-12 jcs static const struct got_error *
5385 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5386 33ad4cbe 2019-05-12 jcs void *arg)
5387 33ad4cbe 2019-05-12 jcs {
5388 76d98825 2019-06-03 stsp char *initial_content = NULL;
5389 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5390 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5391 e0870e44 2019-05-13 stsp char *template = NULL;
5392 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5393 3ce1b845 2019-07-15 stsp int fd;
5394 33ad4cbe 2019-05-12 jcs size_t len;
5395 33ad4cbe 2019-05-12 jcs
5396 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5397 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5398 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5399 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5400 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5401 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5402 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5403 33ad4cbe 2019-05-12 jcs return NULL;
5404 33ad4cbe 2019-05-12 jcs }
5405 33ad4cbe 2019-05-12 jcs
5406 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5407 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5408 76d98825 2019-06-03 stsp
5409 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5410 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5411 76d98825 2019-06-03 stsp a->branch_name) == -1)
5412 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5413 e0870e44 2019-05-13 stsp
5414 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5415 793c30b5 2019-05-13 stsp if (err)
5416 e0870e44 2019-05-13 stsp goto done;
5417 33ad4cbe 2019-05-12 jcs
5418 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5419 33ad4cbe 2019-05-12 jcs
5420 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5421 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5422 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5423 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5424 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5425 33ad4cbe 2019-05-12 jcs }
5426 33ad4cbe 2019-05-12 jcs close(fd);
5427 33ad4cbe 2019-05-12 jcs
5428 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5429 33ad4cbe 2019-05-12 jcs done:
5430 76d98825 2019-06-03 stsp free(initial_content);
5431 e0870e44 2019-05-13 stsp free(template);
5432 314a6357 2019-05-13 stsp
5433 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5434 3ce1b845 2019-07-15 stsp if (err == NULL) {
5435 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5436 3ce1b845 2019-07-15 stsp if (err) {
5437 3ce1b845 2019-07-15 stsp free(*logmsg);
5438 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5439 3ce1b845 2019-07-15 stsp }
5440 3ce1b845 2019-07-15 stsp }
5441 33ad4cbe 2019-05-12 jcs return err;
5442 6bad629b 2019-02-04 stsp }
5443 c4296144 2019-05-09 stsp
5444 c4296144 2019-05-09 stsp static const struct got_error *
5445 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5446 c4296144 2019-05-09 stsp {
5447 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5448 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5449 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5450 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5451 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5452 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5453 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5454 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5455 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5456 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5457 5c1e53bc 2019-07-28 stsp
5458 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5459 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5460 c4296144 2019-05-09 stsp
5461 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5462 c4296144 2019-05-09 stsp switch (ch) {
5463 c4296144 2019-05-09 stsp case 'm':
5464 c4296144 2019-05-09 stsp logmsg = optarg;
5465 c4296144 2019-05-09 stsp break;
5466 c4296144 2019-05-09 stsp default:
5467 c4296144 2019-05-09 stsp usage_commit();
5468 c4296144 2019-05-09 stsp /* NOTREACHED */
5469 c4296144 2019-05-09 stsp }
5470 c4296144 2019-05-09 stsp }
5471 c4296144 2019-05-09 stsp
5472 c4296144 2019-05-09 stsp argc -= optind;
5473 c4296144 2019-05-09 stsp argv += optind;
5474 c4296144 2019-05-09 stsp
5475 43012d58 2019-07-14 stsp #ifndef PROFILE
5476 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5477 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5478 43012d58 2019-07-14 stsp err(1, "pledge");
5479 43012d58 2019-07-14 stsp #endif
5480 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5481 c4296144 2019-05-09 stsp if (cwd == NULL) {
5482 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5483 c4296144 2019-05-09 stsp goto done;
5484 c4296144 2019-05-09 stsp }
5485 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5486 7d5807f4 2019-07-11 stsp if (error)
5487 7d5807f4 2019-07-11 stsp goto done;
5488 7d5807f4 2019-07-11 stsp
5489 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5490 c4296144 2019-05-09 stsp if (error)
5491 a698f62e 2019-07-25 stsp goto done;
5492 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5493 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5494 c4296144 2019-05-09 stsp goto done;
5495 a698f62e 2019-07-25 stsp }
5496 5c1e53bc 2019-07-28 stsp
5497 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5498 916f288c 2019-07-30 stsp worktree);
5499 5c1e53bc 2019-07-28 stsp if (error)
5500 5c1e53bc 2019-07-28 stsp goto done;
5501 c4296144 2019-05-09 stsp
5502 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5503 c9956ddf 2019-09-08 stsp if (error)
5504 c9956ddf 2019-09-08 stsp goto done;
5505 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5506 c9956ddf 2019-09-08 stsp gitconfig_path);
5507 c4296144 2019-05-09 stsp if (error != NULL)
5508 c4296144 2019-05-09 stsp goto done;
5509 c4296144 2019-05-09 stsp
5510 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5511 aba9c984 2019-09-08 stsp if (error)
5512 aba9c984 2019-09-08 stsp return error;
5513 aba9c984 2019-09-08 stsp
5514 314a6357 2019-05-13 stsp /*
5515 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5516 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5517 314a6357 2019-05-13 stsp */
5518 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5519 314a6357 2019-05-13 stsp error = get_editor(&editor);
5520 314a6357 2019-05-13 stsp else
5521 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5522 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5523 c4296144 2019-05-09 stsp if (error)
5524 c4296144 2019-05-09 stsp goto done;
5525 c4296144 2019-05-09 stsp
5526 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5527 087fb88c 2019-08-04 stsp if (error)
5528 087fb88c 2019-08-04 stsp goto done;
5529 087fb88c 2019-08-04 stsp
5530 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5531 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5532 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5533 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5534 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5535 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5536 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5537 916f288c 2019-07-30 stsp goto done;
5538 916f288c 2019-07-30 stsp }
5539 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5540 916f288c 2019-07-30 stsp }
5541 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5542 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5543 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5544 e0870e44 2019-05-13 stsp if (error) {
5545 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5546 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5547 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5548 c4296144 2019-05-09 stsp goto done;
5549 e0870e44 2019-05-13 stsp }
5550 c4296144 2019-05-09 stsp
5551 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5552 c4296144 2019-05-09 stsp if (error)
5553 c4296144 2019-05-09 stsp goto done;
5554 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5555 c4296144 2019-05-09 stsp done:
5556 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5557 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5558 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5559 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5560 7266f21f 2019-10-21 stsp error == NULL)
5561 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5562 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5563 c4296144 2019-05-09 stsp if (repo)
5564 c4296144 2019-05-09 stsp got_repo_close(repo);
5565 c4296144 2019-05-09 stsp if (worktree)
5566 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5567 c4296144 2019-05-09 stsp free(cwd);
5568 c4296144 2019-05-09 stsp free(id_str);
5569 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5570 e2ba3d07 2019-05-13 stsp free(editor);
5571 aba9c984 2019-09-08 stsp free(author);
5572 234035bc 2019-06-01 stsp return error;
5573 234035bc 2019-06-01 stsp }
5574 234035bc 2019-06-01 stsp
5575 234035bc 2019-06-01 stsp __dead static void
5576 234035bc 2019-06-01 stsp usage_cherrypick(void)
5577 234035bc 2019-06-01 stsp {
5578 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5579 234035bc 2019-06-01 stsp exit(1);
5580 234035bc 2019-06-01 stsp }
5581 234035bc 2019-06-01 stsp
5582 234035bc 2019-06-01 stsp static const struct got_error *
5583 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5584 234035bc 2019-06-01 stsp {
5585 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5586 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5587 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5588 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5589 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5590 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5591 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5592 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5593 234035bc 2019-06-01 stsp int ch, did_something = 0;
5594 234035bc 2019-06-01 stsp
5595 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5596 234035bc 2019-06-01 stsp switch (ch) {
5597 234035bc 2019-06-01 stsp default:
5598 234035bc 2019-06-01 stsp usage_cherrypick();
5599 234035bc 2019-06-01 stsp /* NOTREACHED */
5600 234035bc 2019-06-01 stsp }
5601 234035bc 2019-06-01 stsp }
5602 234035bc 2019-06-01 stsp
5603 234035bc 2019-06-01 stsp argc -= optind;
5604 234035bc 2019-06-01 stsp argv += optind;
5605 234035bc 2019-06-01 stsp
5606 43012d58 2019-07-14 stsp #ifndef PROFILE
5607 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5608 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5609 43012d58 2019-07-14 stsp err(1, "pledge");
5610 43012d58 2019-07-14 stsp #endif
5611 234035bc 2019-06-01 stsp if (argc != 1)
5612 234035bc 2019-06-01 stsp usage_cherrypick();
5613 234035bc 2019-06-01 stsp
5614 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5615 234035bc 2019-06-01 stsp if (cwd == NULL) {
5616 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5617 234035bc 2019-06-01 stsp goto done;
5618 234035bc 2019-06-01 stsp }
5619 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5620 234035bc 2019-06-01 stsp if (error)
5621 234035bc 2019-06-01 stsp goto done;
5622 234035bc 2019-06-01 stsp
5623 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5624 c9956ddf 2019-09-08 stsp NULL);
5625 234035bc 2019-06-01 stsp if (error != NULL)
5626 234035bc 2019-06-01 stsp goto done;
5627 234035bc 2019-06-01 stsp
5628 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5629 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5630 234035bc 2019-06-01 stsp if (error)
5631 234035bc 2019-06-01 stsp goto done;
5632 234035bc 2019-06-01 stsp
5633 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5634 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5635 234035bc 2019-06-01 stsp if (error != NULL) {
5636 234035bc 2019-06-01 stsp struct got_reference *ref;
5637 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5638 234035bc 2019-06-01 stsp goto done;
5639 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5640 234035bc 2019-06-01 stsp if (error != NULL)
5641 234035bc 2019-06-01 stsp goto done;
5642 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5643 234035bc 2019-06-01 stsp got_ref_close(ref);
5644 234035bc 2019-06-01 stsp if (error != NULL)
5645 234035bc 2019-06-01 stsp goto done;
5646 234035bc 2019-06-01 stsp }
5647 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5648 234035bc 2019-06-01 stsp if (error)
5649 234035bc 2019-06-01 stsp goto done;
5650 234035bc 2019-06-01 stsp
5651 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5652 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5653 234035bc 2019-06-01 stsp if (error != NULL)
5654 234035bc 2019-06-01 stsp goto done;
5655 234035bc 2019-06-01 stsp
5656 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5657 234035bc 2019-06-01 stsp if (error) {
5658 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5659 234035bc 2019-06-01 stsp goto done;
5660 234035bc 2019-06-01 stsp error = NULL;
5661 234035bc 2019-06-01 stsp } else {
5662 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5663 234035bc 2019-06-01 stsp goto done;
5664 234035bc 2019-06-01 stsp }
5665 234035bc 2019-06-01 stsp
5666 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5667 234035bc 2019-06-01 stsp if (error)
5668 234035bc 2019-06-01 stsp goto done;
5669 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5670 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5671 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5672 03415a1a 2019-06-02 stsp NULL);
5673 234035bc 2019-06-01 stsp if (error != NULL)
5674 234035bc 2019-06-01 stsp goto done;
5675 234035bc 2019-06-01 stsp
5676 234035bc 2019-06-01 stsp if (did_something)
5677 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5678 234035bc 2019-06-01 stsp done:
5679 234035bc 2019-06-01 stsp if (commit)
5680 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5681 234035bc 2019-06-01 stsp free(commit_id_str);
5682 234035bc 2019-06-01 stsp if (head_ref)
5683 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5684 234035bc 2019-06-01 stsp if (worktree)
5685 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5686 234035bc 2019-06-01 stsp if (repo)
5687 234035bc 2019-06-01 stsp got_repo_close(repo);
5688 c4296144 2019-05-09 stsp return error;
5689 c4296144 2019-05-09 stsp }
5690 5ef14e63 2019-06-02 stsp
5691 5ef14e63 2019-06-02 stsp __dead static void
5692 5ef14e63 2019-06-02 stsp usage_backout(void)
5693 5ef14e63 2019-06-02 stsp {
5694 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5695 5ef14e63 2019-06-02 stsp exit(1);
5696 5ef14e63 2019-06-02 stsp }
5697 5ef14e63 2019-06-02 stsp
5698 5ef14e63 2019-06-02 stsp static const struct got_error *
5699 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5700 5ef14e63 2019-06-02 stsp {
5701 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5702 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5703 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5704 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5705 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5706 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5707 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5708 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5709 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5710 5ef14e63 2019-06-02 stsp
5711 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5712 5ef14e63 2019-06-02 stsp switch (ch) {
5713 5ef14e63 2019-06-02 stsp default:
5714 5ef14e63 2019-06-02 stsp usage_backout();
5715 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5716 5ef14e63 2019-06-02 stsp }
5717 5ef14e63 2019-06-02 stsp }
5718 5ef14e63 2019-06-02 stsp
5719 5ef14e63 2019-06-02 stsp argc -= optind;
5720 5ef14e63 2019-06-02 stsp argv += optind;
5721 5ef14e63 2019-06-02 stsp
5722 43012d58 2019-07-14 stsp #ifndef PROFILE
5723 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5724 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5725 43012d58 2019-07-14 stsp err(1, "pledge");
5726 43012d58 2019-07-14 stsp #endif
5727 5ef14e63 2019-06-02 stsp if (argc != 1)
5728 5ef14e63 2019-06-02 stsp usage_backout();
5729 5ef14e63 2019-06-02 stsp
5730 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5731 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5732 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5733 5ef14e63 2019-06-02 stsp goto done;
5734 5ef14e63 2019-06-02 stsp }
5735 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5736 5ef14e63 2019-06-02 stsp if (error)
5737 5ef14e63 2019-06-02 stsp goto done;
5738 5ef14e63 2019-06-02 stsp
5739 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5740 c9956ddf 2019-09-08 stsp NULL);
5741 5ef14e63 2019-06-02 stsp if (error != NULL)
5742 5ef14e63 2019-06-02 stsp goto done;
5743 5ef14e63 2019-06-02 stsp
5744 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5745 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5746 5ef14e63 2019-06-02 stsp if (error)
5747 5ef14e63 2019-06-02 stsp goto done;
5748 5ef14e63 2019-06-02 stsp
5749 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5750 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5751 5ef14e63 2019-06-02 stsp if (error != NULL) {
5752 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5753 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5754 5ef14e63 2019-06-02 stsp goto done;
5755 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5756 5ef14e63 2019-06-02 stsp if (error != NULL)
5757 5ef14e63 2019-06-02 stsp goto done;
5758 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5759 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5760 5ef14e63 2019-06-02 stsp if (error != NULL)
5761 5ef14e63 2019-06-02 stsp goto done;
5762 5ef14e63 2019-06-02 stsp }
5763 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5764 5ef14e63 2019-06-02 stsp if (error)
5765 5ef14e63 2019-06-02 stsp goto done;
5766 5ef14e63 2019-06-02 stsp
5767 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5768 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5769 5ef14e63 2019-06-02 stsp if (error != NULL)
5770 5ef14e63 2019-06-02 stsp goto done;
5771 5ef14e63 2019-06-02 stsp
5772 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5773 5ef14e63 2019-06-02 stsp if (error)
5774 5ef14e63 2019-06-02 stsp goto done;
5775 5ef14e63 2019-06-02 stsp
5776 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5777 5ef14e63 2019-06-02 stsp if (error)
5778 5ef14e63 2019-06-02 stsp goto done;
5779 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5780 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5781 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5782 5ef14e63 2019-06-02 stsp goto done;
5783 5ef14e63 2019-06-02 stsp }
5784 5ef14e63 2019-06-02 stsp
5785 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5786 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5787 5ef14e63 2019-06-02 stsp if (error != NULL)
5788 5ef14e63 2019-06-02 stsp goto done;
5789 5ef14e63 2019-06-02 stsp
5790 5ef14e63 2019-06-02 stsp if (did_something)
5791 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5792 5ef14e63 2019-06-02 stsp done:
5793 5ef14e63 2019-06-02 stsp if (commit)
5794 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5795 5ef14e63 2019-06-02 stsp free(commit_id_str);
5796 5ef14e63 2019-06-02 stsp if (head_ref)
5797 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5798 818c7501 2019-07-11 stsp if (worktree)
5799 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5800 818c7501 2019-07-11 stsp if (repo)
5801 818c7501 2019-07-11 stsp got_repo_close(repo);
5802 818c7501 2019-07-11 stsp return error;
5803 818c7501 2019-07-11 stsp }
5804 818c7501 2019-07-11 stsp
5805 818c7501 2019-07-11 stsp __dead static void
5806 818c7501 2019-07-11 stsp usage_rebase(void)
5807 818c7501 2019-07-11 stsp {
5808 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5809 af54c8f8 2019-07-11 stsp getprogname());
5810 818c7501 2019-07-11 stsp exit(1);
5811 0ebf8283 2019-07-24 stsp }
5812 0ebf8283 2019-07-24 stsp
5813 0ebf8283 2019-07-24 stsp void
5814 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5815 0ebf8283 2019-07-24 stsp {
5816 0ebf8283 2019-07-24 stsp char *nl;
5817 0ebf8283 2019-07-24 stsp size_t len;
5818 0ebf8283 2019-07-24 stsp
5819 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5820 0ebf8283 2019-07-24 stsp if (len > limit)
5821 0ebf8283 2019-07-24 stsp len = limit;
5822 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5823 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5824 0ebf8283 2019-07-24 stsp if (nl)
5825 0ebf8283 2019-07-24 stsp *nl = '\0';
5826 0ebf8283 2019-07-24 stsp }
5827 0ebf8283 2019-07-24 stsp
5828 0ebf8283 2019-07-24 stsp static const struct got_error *
5829 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5830 0ebf8283 2019-07-24 stsp {
5831 5943eee2 2019-08-13 stsp const struct got_error *err;
5832 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5833 5943eee2 2019-08-13 stsp const char *s;
5834 0ebf8283 2019-07-24 stsp
5835 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5836 5943eee2 2019-08-13 stsp if (err)
5837 5943eee2 2019-08-13 stsp return err;
5838 0ebf8283 2019-07-24 stsp
5839 5943eee2 2019-08-13 stsp s = logmsg0;
5840 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5841 5943eee2 2019-08-13 stsp s++;
5842 5943eee2 2019-08-13 stsp
5843 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5844 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5845 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5846 5943eee2 2019-08-13 stsp goto done;
5847 5943eee2 2019-08-13 stsp }
5848 0ebf8283 2019-07-24 stsp
5849 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5850 5943eee2 2019-08-13 stsp done:
5851 5943eee2 2019-08-13 stsp free(logmsg0);
5852 a0ea4fc0 2020-02-28 stsp return err;
5853 a0ea4fc0 2020-02-28 stsp }
5854 a0ea4fc0 2020-02-28 stsp
5855 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5856 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5857 a0ea4fc0 2020-02-28 stsp {
5858 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5859 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5860 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5861 a0ea4fc0 2020-02-28 stsp
5862 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5863 a0ea4fc0 2020-02-28 stsp if (err)
5864 a0ea4fc0 2020-02-28 stsp return err;
5865 a0ea4fc0 2020-02-28 stsp
5866 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5867 a0ea4fc0 2020-02-28 stsp if (err)
5868 a0ea4fc0 2020-02-28 stsp goto done;
5869 a0ea4fc0 2020-02-28 stsp
5870 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5871 a0ea4fc0 2020-02-28 stsp
5872 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5873 a0ea4fc0 2020-02-28 stsp if (err)
5874 a0ea4fc0 2020-02-28 stsp goto done;
5875 a0ea4fc0 2020-02-28 stsp
5876 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5877 a0ea4fc0 2020-02-28 stsp done:
5878 a0ea4fc0 2020-02-28 stsp free(id_str);
5879 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5880 a0ea4fc0 2020-02-28 stsp free(logmsg);
5881 5943eee2 2019-08-13 stsp return err;
5882 818c7501 2019-07-11 stsp }
5883 818c7501 2019-07-11 stsp
5884 818c7501 2019-07-11 stsp static const struct got_error *
5885 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5886 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5887 818c7501 2019-07-11 stsp {
5888 818c7501 2019-07-11 stsp const struct got_error *err;
5889 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5890 818c7501 2019-07-11 stsp
5891 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5892 818c7501 2019-07-11 stsp if (err)
5893 818c7501 2019-07-11 stsp goto done;
5894 818c7501 2019-07-11 stsp
5895 ff0d2220 2019-07-11 stsp if (new_id) {
5896 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5897 ff0d2220 2019-07-11 stsp if (err)
5898 ff0d2220 2019-07-11 stsp goto done;
5899 ff0d2220 2019-07-11 stsp }
5900 818c7501 2019-07-11 stsp
5901 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5902 ff0d2220 2019-07-11 stsp if (new_id_str)
5903 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5904 0ebf8283 2019-07-24 stsp
5905 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5906 0ebf8283 2019-07-24 stsp if (err)
5907 0ebf8283 2019-07-24 stsp goto done;
5908 0ebf8283 2019-07-24 stsp
5909 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5910 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5911 818c7501 2019-07-11 stsp done:
5912 818c7501 2019-07-11 stsp free(old_id_str);
5913 818c7501 2019-07-11 stsp free(new_id_str);
5914 272a1371 2020-02-28 stsp free(logmsg);
5915 818c7501 2019-07-11 stsp return err;
5916 818c7501 2019-07-11 stsp }
5917 818c7501 2019-07-11 stsp
5918 1ee397ad 2019-07-12 stsp static const struct got_error *
5919 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5920 818c7501 2019-07-11 stsp {
5921 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5922 818c7501 2019-07-11 stsp
5923 818c7501 2019-07-11 stsp while (path[0] == '/')
5924 818c7501 2019-07-11 stsp path++;
5925 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5926 818c7501 2019-07-11 stsp
5927 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5928 1ee397ad 2019-07-12 stsp return NULL;
5929 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5930 818c7501 2019-07-11 stsp *rebase_status = status;
5931 1ee397ad 2019-07-12 stsp return NULL;
5932 818c7501 2019-07-11 stsp }
5933 818c7501 2019-07-11 stsp
5934 818c7501 2019-07-11 stsp static const struct got_error *
5935 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5936 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5937 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5938 818c7501 2019-07-11 stsp {
5939 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5940 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5941 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5942 818c7501 2019-07-11 stsp }
5943 818c7501 2019-07-11 stsp
5944 818c7501 2019-07-11 stsp static const struct got_error *
5945 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5946 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5947 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5948 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5949 ff0d2220 2019-07-11 stsp {
5950 ff0d2220 2019-07-11 stsp const struct got_error *error;
5951 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5952 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5953 ff0d2220 2019-07-11 stsp
5954 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5955 ff0d2220 2019-07-11 stsp if (error)
5956 ff0d2220 2019-07-11 stsp return error;
5957 ff0d2220 2019-07-11 stsp
5958 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5959 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5960 ff0d2220 2019-07-11 stsp if (error) {
5961 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5962 ff0d2220 2019-07-11 stsp goto done;
5963 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5964 ff0d2220 2019-07-11 stsp } else {
5965 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5966 ff0d2220 2019-07-11 stsp free(new_commit_id);
5967 ff0d2220 2019-07-11 stsp }
5968 ff0d2220 2019-07-11 stsp done:
5969 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5970 ff0d2220 2019-07-11 stsp return error;
5971 64c6d990 2019-07-11 stsp }
5972 64c6d990 2019-07-11 stsp
5973 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5974 64c6d990 2019-07-11 stsp const char *path_prefix;
5975 64c6d990 2019-07-11 stsp size_t len;
5976 8ca9bd68 2019-07-25 stsp int errcode;
5977 64c6d990 2019-07-11 stsp };
5978 64c6d990 2019-07-11 stsp
5979 64c6d990 2019-07-11 stsp static const struct got_error *
5980 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5981 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5982 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5983 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5984 64c6d990 2019-07-11 stsp {
5985 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5986 64c6d990 2019-07-11 stsp
5987 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5988 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5989 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5990 64c6d990 2019-07-11 stsp
5991 64c6d990 2019-07-11 stsp return NULL;
5992 64c6d990 2019-07-11 stsp }
5993 64c6d990 2019-07-11 stsp
5994 64c6d990 2019-07-11 stsp static const struct got_error *
5995 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5996 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5997 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5998 64c6d990 2019-07-11 stsp {
5999 64c6d990 2019-07-11 stsp const struct got_error *err;
6000 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6001 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6002 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6003 64c6d990 2019-07-11 stsp
6004 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6005 64c6d990 2019-07-11 stsp return NULL;
6006 64c6d990 2019-07-11 stsp
6007 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6008 64c6d990 2019-07-11 stsp if (err)
6009 64c6d990 2019-07-11 stsp goto done;
6010 64c6d990 2019-07-11 stsp
6011 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6012 64c6d990 2019-07-11 stsp if (err)
6013 64c6d990 2019-07-11 stsp goto done;
6014 64c6d990 2019-07-11 stsp
6015 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6016 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6017 64c6d990 2019-07-11 stsp if (err)
6018 64c6d990 2019-07-11 stsp goto done;
6019 64c6d990 2019-07-11 stsp
6020 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6021 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6022 64c6d990 2019-07-11 stsp if (err)
6023 64c6d990 2019-07-11 stsp goto done;
6024 64c6d990 2019-07-11 stsp
6025 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6026 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6027 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6028 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6029 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6030 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6031 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6032 64c6d990 2019-07-11 stsp done:
6033 64c6d990 2019-07-11 stsp if (tree1)
6034 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6035 64c6d990 2019-07-11 stsp if (tree2)
6036 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6037 64c6d990 2019-07-11 stsp if (commit)
6038 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6039 64c6d990 2019-07-11 stsp if (parent_commit)
6040 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6041 64c6d990 2019-07-11 stsp return err;
6042 ff0d2220 2019-07-11 stsp }
6043 ff0d2220 2019-07-11 stsp
6044 ff0d2220 2019-07-11 stsp static const struct got_error *
6045 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6046 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6047 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6048 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6049 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6050 af61c510 2019-07-19 stsp {
6051 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6052 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6053 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6054 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6055 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6056 af61c510 2019-07-19 stsp
6057 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6058 af61c510 2019-07-19 stsp if (err)
6059 af61c510 2019-07-19 stsp return err;
6060 af61c510 2019-07-19 stsp
6061 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6062 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6063 af61c510 2019-07-19 stsp if (err)
6064 af61c510 2019-07-19 stsp goto done;
6065 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6066 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6067 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6068 af61c510 2019-07-19 stsp if (err) {
6069 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6070 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6071 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6072 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6073 af61c510 2019-07-19 stsp "been reached?!?");
6074 ee780d5c 2020-01-04 stsp }
6075 ee780d5c 2020-01-04 stsp goto done;
6076 af61c510 2019-07-19 stsp } else {
6077 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6078 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6079 af61c510 2019-07-19 stsp if (err)
6080 af61c510 2019-07-19 stsp goto done;
6081 af61c510 2019-07-19 stsp
6082 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6083 af61c510 2019-07-19 stsp if (err)
6084 af61c510 2019-07-19 stsp goto done;
6085 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6086 af61c510 2019-07-19 stsp commit_id = parent_id;
6087 af61c510 2019-07-19 stsp }
6088 af61c510 2019-07-19 stsp }
6089 af61c510 2019-07-19 stsp done:
6090 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6091 af61c510 2019-07-19 stsp return err;
6092 af61c510 2019-07-19 stsp }
6093 af61c510 2019-07-19 stsp
6094 af61c510 2019-07-19 stsp static const struct got_error *
6095 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6096 818c7501 2019-07-11 stsp {
6097 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6098 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6099 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6100 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6101 818c7501 2019-07-11 stsp char *cwd = NULL;
6102 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6103 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6104 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6105 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6106 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6107 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6108 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6109 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6110 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6111 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6112 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6113 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6114 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6115 818c7501 2019-07-11 stsp
6116 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6117 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6118 818c7501 2019-07-11 stsp
6119 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6120 818c7501 2019-07-11 stsp switch (ch) {
6121 818c7501 2019-07-11 stsp case 'a':
6122 818c7501 2019-07-11 stsp abort_rebase = 1;
6123 818c7501 2019-07-11 stsp break;
6124 818c7501 2019-07-11 stsp case 'c':
6125 818c7501 2019-07-11 stsp continue_rebase = 1;
6126 818c7501 2019-07-11 stsp break;
6127 818c7501 2019-07-11 stsp default:
6128 818c7501 2019-07-11 stsp usage_rebase();
6129 818c7501 2019-07-11 stsp /* NOTREACHED */
6130 818c7501 2019-07-11 stsp }
6131 818c7501 2019-07-11 stsp }
6132 818c7501 2019-07-11 stsp
6133 818c7501 2019-07-11 stsp argc -= optind;
6134 818c7501 2019-07-11 stsp argv += optind;
6135 818c7501 2019-07-11 stsp
6136 43012d58 2019-07-14 stsp #ifndef PROFILE
6137 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6138 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6139 43012d58 2019-07-14 stsp err(1, "pledge");
6140 43012d58 2019-07-14 stsp #endif
6141 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6142 818c7501 2019-07-11 stsp usage_rebase();
6143 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6144 818c7501 2019-07-11 stsp if (argc != 0)
6145 818c7501 2019-07-11 stsp usage_rebase();
6146 818c7501 2019-07-11 stsp } else if (argc != 1)
6147 818c7501 2019-07-11 stsp usage_rebase();
6148 818c7501 2019-07-11 stsp
6149 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6150 818c7501 2019-07-11 stsp if (cwd == NULL) {
6151 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6152 818c7501 2019-07-11 stsp goto done;
6153 818c7501 2019-07-11 stsp }
6154 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6155 818c7501 2019-07-11 stsp if (error)
6156 818c7501 2019-07-11 stsp goto done;
6157 818c7501 2019-07-11 stsp
6158 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6159 c9956ddf 2019-09-08 stsp NULL);
6160 818c7501 2019-07-11 stsp if (error != NULL)
6161 818c7501 2019-07-11 stsp goto done;
6162 818c7501 2019-07-11 stsp
6163 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6164 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6165 7ef62c4e 2020-02-24 stsp if (error)
6166 7ef62c4e 2020-02-24 stsp goto done;
6167 7ef62c4e 2020-02-24 stsp
6168 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6169 7ef62c4e 2020-02-24 stsp worktree);
6170 818c7501 2019-07-11 stsp if (error)
6171 7ef62c4e 2020-02-24 stsp goto done;
6172 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6173 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6174 818c7501 2019-07-11 stsp goto done;
6175 7ef62c4e 2020-02-24 stsp }
6176 818c7501 2019-07-11 stsp
6177 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6178 818c7501 2019-07-11 stsp if (error)
6179 818c7501 2019-07-11 stsp goto done;
6180 818c7501 2019-07-11 stsp
6181 f6794adc 2019-07-23 stsp if (abort_rebase) {
6182 818c7501 2019-07-11 stsp int did_something;
6183 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6184 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6185 f6794adc 2019-07-23 stsp goto done;
6186 f6794adc 2019-07-23 stsp }
6187 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6188 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6189 3e3a69f1 2019-07-25 stsp worktree, repo);
6190 818c7501 2019-07-11 stsp if (error)
6191 818c7501 2019-07-11 stsp goto done;
6192 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6193 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6194 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6195 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6196 818c7501 2019-07-11 stsp if (error)
6197 818c7501 2019-07-11 stsp goto done;
6198 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6199 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6200 818c7501 2019-07-11 stsp }
6201 818c7501 2019-07-11 stsp
6202 818c7501 2019-07-11 stsp if (continue_rebase) {
6203 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6204 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6205 f6794adc 2019-07-23 stsp goto done;
6206 f6794adc 2019-07-23 stsp }
6207 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6208 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6209 3e3a69f1 2019-07-25 stsp worktree, repo);
6210 818c7501 2019-07-11 stsp if (error)
6211 818c7501 2019-07-11 stsp goto done;
6212 818c7501 2019-07-11 stsp
6213 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6214 01757395 2019-07-12 stsp resume_commit_id, repo);
6215 818c7501 2019-07-11 stsp if (error)
6216 818c7501 2019-07-11 stsp goto done;
6217 818c7501 2019-07-11 stsp
6218 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6219 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6220 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6221 818c7501 2019-07-11 stsp goto done;
6222 818c7501 2019-07-11 stsp }
6223 818c7501 2019-07-11 stsp } else {
6224 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6225 818c7501 2019-07-11 stsp if (error != NULL)
6226 818c7501 2019-07-11 stsp goto done;
6227 ff0d2220 2019-07-11 stsp }
6228 818c7501 2019-07-11 stsp
6229 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6230 ff0d2220 2019-07-11 stsp if (error)
6231 ff0d2220 2019-07-11 stsp goto done;
6232 ff0d2220 2019-07-11 stsp
6233 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6234 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6235 a51a74b3 2019-07-27 stsp
6236 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6237 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6238 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6239 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6240 818c7501 2019-07-11 stsp if (error)
6241 818c7501 2019-07-11 stsp goto done;
6242 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6243 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6244 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6245 818c7501 2019-07-11 stsp "with work tree's branch");
6246 818c7501 2019-07-11 stsp goto done;
6247 818c7501 2019-07-11 stsp }
6248 818c7501 2019-07-11 stsp
6249 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6250 a51a74b3 2019-07-27 stsp if (error) {
6251 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6252 a51a74b3 2019-07-27 stsp goto done;
6253 a51a74b3 2019-07-27 stsp error = NULL;
6254 a51a74b3 2019-07-27 stsp } else {
6255 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6256 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6257 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6258 a51a74b3 2019-07-27 stsp goto done;
6259 a51a74b3 2019-07-27 stsp }
6260 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6261 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6262 818c7501 2019-07-11 stsp if (error)
6263 818c7501 2019-07-11 stsp goto done;
6264 818c7501 2019-07-11 stsp }
6265 818c7501 2019-07-11 stsp
6266 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6267 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6268 818c7501 2019-07-11 stsp if (error)
6269 818c7501 2019-07-11 stsp goto done;
6270 818c7501 2019-07-11 stsp
6271 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6272 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6273 fc66b545 2019-08-12 stsp if (pid == NULL) {
6274 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6275 fc66b545 2019-08-12 stsp int did_something;
6276 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6277 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6278 fc66b545 2019-08-12 stsp &did_something);
6279 fc66b545 2019-08-12 stsp if (error)
6280 fc66b545 2019-08-12 stsp goto done;
6281 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6282 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6283 fc66b545 2019-08-12 stsp }
6284 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6285 fc66b545 2019-08-12 stsp goto done;
6286 fc66b545 2019-08-12 stsp }
6287 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6288 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6289 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6290 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6291 818c7501 2019-07-11 stsp commit = NULL;
6292 818c7501 2019-07-11 stsp if (error)
6293 818c7501 2019-07-11 stsp goto done;
6294 64c6d990 2019-07-11 stsp
6295 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6296 38b0338b 2019-11-29 stsp if (continue_rebase) {
6297 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6298 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6299 38b0338b 2019-11-29 stsp goto done;
6300 38b0338b 2019-11-29 stsp } else {
6301 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6302 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6303 38b0338b 2019-11-29 stsp char *id_str;
6304 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6305 38b0338b 2019-11-29 stsp new_base_branch);
6306 38b0338b 2019-11-29 stsp if (error)
6307 38b0338b 2019-11-29 stsp goto done;
6308 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6309 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6310 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6311 38b0338b 2019-11-29 stsp free(id_str);
6312 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6313 38b0338b 2019-11-29 stsp new_head_commit_id);
6314 38b0338b 2019-11-29 stsp if (error)
6315 38b0338b 2019-11-29 stsp goto done;
6316 38b0338b 2019-11-29 stsp }
6317 818c7501 2019-07-11 stsp }
6318 818c7501 2019-07-11 stsp
6319 818c7501 2019-07-11 stsp pid = NULL;
6320 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6321 818c7501 2019-07-11 stsp commit_id = qid->id;
6322 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6323 818c7501 2019-07-11 stsp pid = qid;
6324 818c7501 2019-07-11 stsp
6325 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6326 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6327 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6328 818c7501 2019-07-11 stsp if (error)
6329 818c7501 2019-07-11 stsp goto done;
6330 818c7501 2019-07-11 stsp
6331 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6332 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6333 a0ea4fc0 2020-02-28 stsp if (error)
6334 a0ea4fc0 2020-02-28 stsp goto done;
6335 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6336 818c7501 2019-07-11 stsp break;
6337 01757395 2019-07-12 stsp }
6338 818c7501 2019-07-11 stsp
6339 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6340 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6341 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6342 818c7501 2019-07-11 stsp if (error)
6343 818c7501 2019-07-11 stsp goto done;
6344 818c7501 2019-07-11 stsp }
6345 818c7501 2019-07-11 stsp
6346 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6347 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6348 818c7501 2019-07-11 stsp if (error)
6349 818c7501 2019-07-11 stsp goto done;
6350 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6351 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6352 818c7501 2019-07-11 stsp } else
6353 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6354 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6355 818c7501 2019-07-11 stsp done:
6356 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6357 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6358 818c7501 2019-07-11 stsp free(resume_commit_id);
6359 818c7501 2019-07-11 stsp free(yca_id);
6360 818c7501 2019-07-11 stsp if (commit)
6361 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6362 818c7501 2019-07-11 stsp if (branch)
6363 818c7501 2019-07-11 stsp got_ref_close(branch);
6364 818c7501 2019-07-11 stsp if (new_base_branch)
6365 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6366 818c7501 2019-07-11 stsp if (tmp_branch)
6367 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6368 5ef14e63 2019-06-02 stsp if (worktree)
6369 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6370 5ef14e63 2019-06-02 stsp if (repo)
6371 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6372 5ef14e63 2019-06-02 stsp return error;
6373 0ebf8283 2019-07-24 stsp }
6374 0ebf8283 2019-07-24 stsp
6375 0ebf8283 2019-07-24 stsp __dead static void
6376 0ebf8283 2019-07-24 stsp usage_histedit(void)
6377 0ebf8283 2019-07-24 stsp {
6378 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6379 0ebf8283 2019-07-24 stsp getprogname());
6380 0ebf8283 2019-07-24 stsp exit(1);
6381 0ebf8283 2019-07-24 stsp }
6382 0ebf8283 2019-07-24 stsp
6383 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6384 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6385 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6386 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6387 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6388 0ebf8283 2019-07-24 stsp
6389 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6390 0ebf8283 2019-07-24 stsp unsigned char code;
6391 0ebf8283 2019-07-24 stsp const char *name;
6392 0ebf8283 2019-07-24 stsp const char *desc;
6393 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6394 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6395 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6396 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6397 82997472 2020-01-29 stsp "be used" },
6398 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6399 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6400 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6401 0ebf8283 2019-07-24 stsp };
6402 0ebf8283 2019-07-24 stsp
6403 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6404 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6405 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6406 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6407 0ebf8283 2019-07-24 stsp char *logmsg;
6408 0ebf8283 2019-07-24 stsp };
6409 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6410 0ebf8283 2019-07-24 stsp
6411 0ebf8283 2019-07-24 stsp static const struct got_error *
6412 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6413 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6414 0ebf8283 2019-07-24 stsp {
6415 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6416 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6417 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6418 8138f3e1 2019-08-11 stsp int n;
6419 0ebf8283 2019-07-24 stsp
6420 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6421 0ebf8283 2019-07-24 stsp if (err)
6422 0ebf8283 2019-07-24 stsp goto done;
6423 0ebf8283 2019-07-24 stsp
6424 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6425 0ebf8283 2019-07-24 stsp if (err)
6426 0ebf8283 2019-07-24 stsp goto done;
6427 0ebf8283 2019-07-24 stsp
6428 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6429 0ebf8283 2019-07-24 stsp if (err)
6430 0ebf8283 2019-07-24 stsp goto done;
6431 0ebf8283 2019-07-24 stsp
6432 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6433 0ebf8283 2019-07-24 stsp if (n < 0)
6434 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6435 0ebf8283 2019-07-24 stsp done:
6436 0ebf8283 2019-07-24 stsp if (commit)
6437 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6438 0ebf8283 2019-07-24 stsp free(id_str);
6439 0ebf8283 2019-07-24 stsp free(logmsg);
6440 0ebf8283 2019-07-24 stsp return err;
6441 0ebf8283 2019-07-24 stsp }
6442 0ebf8283 2019-07-24 stsp
6443 0ebf8283 2019-07-24 stsp static const struct got_error *
6444 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6445 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6446 0ebf8283 2019-07-24 stsp {
6447 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6448 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6449 0ebf8283 2019-07-24 stsp
6450 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6451 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6452 0ebf8283 2019-07-24 stsp
6453 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6454 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6455 0ebf8283 2019-07-24 stsp f, repo);
6456 0ebf8283 2019-07-24 stsp if (err)
6457 0ebf8283 2019-07-24 stsp break;
6458 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6459 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6460 083957f4 2020-02-24 stsp if (n < 0) {
6461 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6462 083957f4 2020-02-24 stsp break;
6463 083957f4 2020-02-24 stsp }
6464 083957f4 2020-02-24 stsp }
6465 0ebf8283 2019-07-24 stsp }
6466 0ebf8283 2019-07-24 stsp
6467 0ebf8283 2019-07-24 stsp return err;
6468 0ebf8283 2019-07-24 stsp }
6469 0ebf8283 2019-07-24 stsp
6470 0ebf8283 2019-07-24 stsp static const struct got_error *
6471 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6472 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6473 0ebf8283 2019-07-24 stsp {
6474 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6475 0ebf8283 2019-07-24 stsp int n, i;
6476 514f2ffe 2020-01-29 stsp char *id_str;
6477 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6478 514f2ffe 2020-01-29 stsp
6479 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6480 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6481 514f2ffe 2020-01-29 stsp if (err)
6482 514f2ffe 2020-01-29 stsp return err;
6483 514f2ffe 2020-01-29 stsp
6484 514f2ffe 2020-01-29 stsp n = fprintf(f,
6485 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6486 514f2ffe 2020-01-29 stsp "# commit %s\n"
6487 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6488 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6489 514f2ffe 2020-01-29 stsp if (n < 0) {
6490 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6491 514f2ffe 2020-01-29 stsp goto done;
6492 514f2ffe 2020-01-29 stsp }
6493 0ebf8283 2019-07-24 stsp
6494 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6495 514f2ffe 2020-01-29 stsp if (n < 0) {
6496 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6497 514f2ffe 2020-01-29 stsp goto done;
6498 514f2ffe 2020-01-29 stsp }
6499 0ebf8283 2019-07-24 stsp
6500 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6501 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6502 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6503 0ebf8283 2019-07-24 stsp cmd->desc);
6504 0ebf8283 2019-07-24 stsp if (n < 0) {
6505 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6506 0ebf8283 2019-07-24 stsp break;
6507 0ebf8283 2019-07-24 stsp }
6508 0ebf8283 2019-07-24 stsp }
6509 514f2ffe 2020-01-29 stsp done:
6510 514f2ffe 2020-01-29 stsp free(id_str);
6511 0ebf8283 2019-07-24 stsp return err;
6512 0ebf8283 2019-07-24 stsp }
6513 0ebf8283 2019-07-24 stsp
6514 0ebf8283 2019-07-24 stsp static const struct got_error *
6515 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6516 0ebf8283 2019-07-24 stsp {
6517 0ebf8283 2019-07-24 stsp static char msg[42];
6518 0ebf8283 2019-07-24 stsp int ret;
6519 0ebf8283 2019-07-24 stsp
6520 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6521 0ebf8283 2019-07-24 stsp lineno);
6522 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6523 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6524 0ebf8283 2019-07-24 stsp
6525 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6526 0ebf8283 2019-07-24 stsp }
6527 0ebf8283 2019-07-24 stsp
6528 0ebf8283 2019-07-24 stsp static const struct got_error *
6529 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6530 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6531 0ebf8283 2019-07-24 stsp {
6532 0ebf8283 2019-07-24 stsp const struct got_error *err;
6533 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6534 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6535 0ebf8283 2019-07-24 stsp
6536 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6537 0ebf8283 2019-07-24 stsp if (err)
6538 0ebf8283 2019-07-24 stsp return err;
6539 0ebf8283 2019-07-24 stsp
6540 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6541 0ebf8283 2019-07-24 stsp if (err)
6542 0ebf8283 2019-07-24 stsp goto done;
6543 0ebf8283 2019-07-24 stsp
6544 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6545 5943eee2 2019-08-13 stsp if (err)
6546 5943eee2 2019-08-13 stsp goto done;
6547 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6548 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6549 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6550 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6551 0ebf8283 2019-07-24 stsp }
6552 0ebf8283 2019-07-24 stsp done:
6553 0ebf8283 2019-07-24 stsp if (folded_commit)
6554 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6555 0ebf8283 2019-07-24 stsp free(id_str);
6556 5943eee2 2019-08-13 stsp free(folded_logmsg);
6557 0ebf8283 2019-07-24 stsp return err;
6558 0ebf8283 2019-07-24 stsp }
6559 0ebf8283 2019-07-24 stsp
6560 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6561 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6562 0ebf8283 2019-07-24 stsp {
6563 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6564 0ebf8283 2019-07-24 stsp
6565 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6566 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6567 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6568 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6569 3f9de99f 2019-07-24 stsp folded = prev;
6570 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6571 0ebf8283 2019-07-24 stsp }
6572 0ebf8283 2019-07-24 stsp
6573 0ebf8283 2019-07-24 stsp return folded;
6574 0ebf8283 2019-07-24 stsp }
6575 0ebf8283 2019-07-24 stsp
6576 0ebf8283 2019-07-24 stsp static const struct got_error *
6577 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6578 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6579 0ebf8283 2019-07-24 stsp {
6580 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6581 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6582 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6583 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6584 0ebf8283 2019-07-24 stsp int fd;
6585 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6586 0ebf8283 2019-07-24 stsp
6587 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6588 0ebf8283 2019-07-24 stsp if (err)
6589 0ebf8283 2019-07-24 stsp return err;
6590 0ebf8283 2019-07-24 stsp
6591 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6592 0ebf8283 2019-07-24 stsp if (folded) {
6593 0ebf8283 2019-07-24 stsp while (folded != hle) {
6594 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6595 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6596 3f9de99f 2019-07-24 stsp continue;
6597 3f9de99f 2019-07-24 stsp }
6598 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6599 0ebf8283 2019-07-24 stsp logmsg, repo);
6600 0ebf8283 2019-07-24 stsp if (err)
6601 0ebf8283 2019-07-24 stsp goto done;
6602 0ebf8283 2019-07-24 stsp free(logmsg);
6603 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6604 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6605 0ebf8283 2019-07-24 stsp }
6606 0ebf8283 2019-07-24 stsp }
6607 0ebf8283 2019-07-24 stsp
6608 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6609 0ebf8283 2019-07-24 stsp if (err)
6610 0ebf8283 2019-07-24 stsp goto done;
6611 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6612 5943eee2 2019-08-13 stsp if (err)
6613 5943eee2 2019-08-13 stsp goto done;
6614 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6615 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6616 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6617 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6618 0ebf8283 2019-07-24 stsp goto done;
6619 0ebf8283 2019-07-24 stsp }
6620 0ebf8283 2019-07-24 stsp free(logmsg);
6621 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6622 0ebf8283 2019-07-24 stsp
6623 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6624 0ebf8283 2019-07-24 stsp if (err)
6625 0ebf8283 2019-07-24 stsp goto done;
6626 0ebf8283 2019-07-24 stsp
6627 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6628 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6629 0ebf8283 2019-07-24 stsp if (err)
6630 0ebf8283 2019-07-24 stsp goto done;
6631 0ebf8283 2019-07-24 stsp
6632 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6633 0ebf8283 2019-07-24 stsp close(fd);
6634 0ebf8283 2019-07-24 stsp
6635 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6636 0ebf8283 2019-07-24 stsp if (err)
6637 0ebf8283 2019-07-24 stsp goto done;
6638 0ebf8283 2019-07-24 stsp
6639 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6640 0ebf8283 2019-07-24 stsp if (err) {
6641 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6642 0ebf8283 2019-07-24 stsp goto done;
6643 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6644 0ebf8283 2019-07-24 stsp }
6645 0ebf8283 2019-07-24 stsp done:
6646 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6647 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6648 0ebf8283 2019-07-24 stsp free(logmsg_path);
6649 0ebf8283 2019-07-24 stsp free(logmsg);
6650 5943eee2 2019-08-13 stsp free(orig_logmsg);
6651 0ebf8283 2019-07-24 stsp free(editor);
6652 0ebf8283 2019-07-24 stsp if (commit)
6653 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6654 0ebf8283 2019-07-24 stsp return err;
6655 5ef14e63 2019-06-02 stsp }
6656 0ebf8283 2019-07-24 stsp
6657 0ebf8283 2019-07-24 stsp static const struct got_error *
6658 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6659 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6660 0ebf8283 2019-07-24 stsp {
6661 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6662 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6663 0ebf8283 2019-07-24 stsp size_t size;
6664 0ebf8283 2019-07-24 stsp ssize_t len;
6665 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6666 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6667 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6668 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6669 0ebf8283 2019-07-24 stsp
6670 0ebf8283 2019-07-24 stsp for (;;) {
6671 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6672 0ebf8283 2019-07-24 stsp if (len == -1) {
6673 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6674 0ebf8283 2019-07-24 stsp if (feof(f))
6675 0ebf8283 2019-07-24 stsp break;
6676 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6677 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6678 0ebf8283 2019-07-24 stsp break;
6679 0ebf8283 2019-07-24 stsp }
6680 0ebf8283 2019-07-24 stsp lineno++;
6681 0ebf8283 2019-07-24 stsp p = line;
6682 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6683 0ebf8283 2019-07-24 stsp p++;
6684 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6685 0ebf8283 2019-07-24 stsp free(line);
6686 0ebf8283 2019-07-24 stsp line = NULL;
6687 0ebf8283 2019-07-24 stsp continue;
6688 0ebf8283 2019-07-24 stsp }
6689 0ebf8283 2019-07-24 stsp cmd = NULL;
6690 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6691 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6692 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6693 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6694 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6695 0ebf8283 2019-07-24 stsp break;
6696 0ebf8283 2019-07-24 stsp }
6697 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6698 0ebf8283 2019-07-24 stsp p++;
6699 0ebf8283 2019-07-24 stsp break;
6700 0ebf8283 2019-07-24 stsp }
6701 0ebf8283 2019-07-24 stsp }
6702 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6703 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6704 0ebf8283 2019-07-24 stsp break;
6705 0ebf8283 2019-07-24 stsp }
6706 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6707 0ebf8283 2019-07-24 stsp p++;
6708 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6709 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6710 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6711 0ebf8283 2019-07-24 stsp break;
6712 0ebf8283 2019-07-24 stsp }
6713 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6714 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6715 0ebf8283 2019-07-24 stsp if (err)
6716 0ebf8283 2019-07-24 stsp break;
6717 0ebf8283 2019-07-24 stsp } else {
6718 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6719 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6720 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6721 0ebf8283 2019-07-24 stsp break;
6722 0ebf8283 2019-07-24 stsp }
6723 0ebf8283 2019-07-24 stsp }
6724 0ebf8283 2019-07-24 stsp free(line);
6725 0ebf8283 2019-07-24 stsp line = NULL;
6726 0ebf8283 2019-07-24 stsp continue;
6727 0ebf8283 2019-07-24 stsp } else {
6728 0ebf8283 2019-07-24 stsp end = p;
6729 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6730 0ebf8283 2019-07-24 stsp end++;
6731 0ebf8283 2019-07-24 stsp *end = '\0';
6732 0ebf8283 2019-07-24 stsp
6733 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6734 0ebf8283 2019-07-24 stsp if (err) {
6735 0ebf8283 2019-07-24 stsp /* override error code */
6736 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6737 0ebf8283 2019-07-24 stsp break;
6738 0ebf8283 2019-07-24 stsp }
6739 0ebf8283 2019-07-24 stsp }
6740 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6741 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6742 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6743 0ebf8283 2019-07-24 stsp break;
6744 0ebf8283 2019-07-24 stsp }
6745 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6746 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6747 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6748 0ebf8283 2019-07-24 stsp commit_id = NULL;
6749 0ebf8283 2019-07-24 stsp free(line);
6750 0ebf8283 2019-07-24 stsp line = NULL;
6751 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6752 0ebf8283 2019-07-24 stsp }
6753 0ebf8283 2019-07-24 stsp
6754 0ebf8283 2019-07-24 stsp free(line);
6755 0ebf8283 2019-07-24 stsp free(commit_id);
6756 0ebf8283 2019-07-24 stsp return err;
6757 0ebf8283 2019-07-24 stsp }
6758 0ebf8283 2019-07-24 stsp
6759 0ebf8283 2019-07-24 stsp static const struct got_error *
6760 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6761 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6762 bfce7f83 2019-07-27 stsp {
6763 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6764 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6765 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6766 5b87815e 2020-03-05 stsp static char msg[92];
6767 bfce7f83 2019-07-27 stsp char *id_str;
6768 bfce7f83 2019-07-27 stsp
6769 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6770 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6771 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6772 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6773 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6774 5b87815e 2020-03-05 stsp
6775 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6776 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6777 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6778 5b87815e 2020-03-05 stsp if (hle == hle2)
6779 5b87815e 2020-03-05 stsp continue;
6780 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6781 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6782 5b87815e 2020-03-05 stsp continue;
6783 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6784 5b87815e 2020-03-05 stsp if (err)
6785 5b87815e 2020-03-05 stsp return err;
6786 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6787 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6788 5b87815e 2020-03-05 stsp free(id_str);
6789 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6790 5b87815e 2020-03-05 stsp }
6791 5b87815e 2020-03-05 stsp }
6792 bfce7f83 2019-07-27 stsp
6793 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6794 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6795 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6796 bfce7f83 2019-07-27 stsp break;
6797 bfce7f83 2019-07-27 stsp }
6798 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6799 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6800 bfce7f83 2019-07-27 stsp if (err)
6801 bfce7f83 2019-07-27 stsp return err;
6802 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6803 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6804 bfce7f83 2019-07-27 stsp free(id_str);
6805 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6806 bfce7f83 2019-07-27 stsp }
6807 bfce7f83 2019-07-27 stsp }
6808 bfce7f83 2019-07-27 stsp
6809 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6810 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6811 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6812 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6813 bfce7f83 2019-07-27 stsp
6814 bfce7f83 2019-07-27 stsp return NULL;
6815 bfce7f83 2019-07-27 stsp }
6816 bfce7f83 2019-07-27 stsp
6817 bfce7f83 2019-07-27 stsp static const struct got_error *
6818 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6819 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6820 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6821 0ebf8283 2019-07-24 stsp {
6822 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6823 0ebf8283 2019-07-24 stsp char *editor;
6824 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6825 0ebf8283 2019-07-24 stsp
6826 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6827 0ebf8283 2019-07-24 stsp if (err)
6828 0ebf8283 2019-07-24 stsp return err;
6829 0ebf8283 2019-07-24 stsp
6830 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6831 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6832 0ebf8283 2019-07-24 stsp goto done;
6833 0ebf8283 2019-07-24 stsp }
6834 0ebf8283 2019-07-24 stsp
6835 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6836 0ebf8283 2019-07-24 stsp if (f == NULL) {
6837 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6838 0ebf8283 2019-07-24 stsp goto done;
6839 0ebf8283 2019-07-24 stsp }
6840 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6841 bfce7f83 2019-07-27 stsp if (err)
6842 bfce7f83 2019-07-27 stsp goto done;
6843 bfce7f83 2019-07-27 stsp
6844 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6845 0ebf8283 2019-07-24 stsp done:
6846 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6847 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6848 0ebf8283 2019-07-24 stsp free(editor);
6849 0ebf8283 2019-07-24 stsp return err;
6850 0ebf8283 2019-07-24 stsp }
6851 0ebf8283 2019-07-24 stsp
6852 0ebf8283 2019-07-24 stsp static const struct got_error *
6853 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6854 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6855 514f2ffe 2020-01-29 stsp struct got_repository *);
6856 0ebf8283 2019-07-24 stsp
6857 0ebf8283 2019-07-24 stsp static const struct got_error *
6858 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6859 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6860 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6861 0ebf8283 2019-07-24 stsp {
6862 0ebf8283 2019-07-24 stsp const struct got_error *err;
6863 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6864 0ebf8283 2019-07-24 stsp char *path = NULL;
6865 0ebf8283 2019-07-24 stsp
6866 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6867 0ebf8283 2019-07-24 stsp if (err)
6868 0ebf8283 2019-07-24 stsp return err;
6869 0ebf8283 2019-07-24 stsp
6870 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6871 0ebf8283 2019-07-24 stsp if (err)
6872 0ebf8283 2019-07-24 stsp goto done;
6873 0ebf8283 2019-07-24 stsp
6874 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6875 0ebf8283 2019-07-24 stsp if (err)
6876 0ebf8283 2019-07-24 stsp goto done;
6877 0ebf8283 2019-07-24 stsp
6878 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6879 083957f4 2020-02-24 stsp rewind(f);
6880 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6881 083957f4 2020-02-24 stsp } else {
6882 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6883 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6884 0ebf8283 2019-07-24 stsp goto done;
6885 083957f4 2020-02-24 stsp }
6886 083957f4 2020-02-24 stsp f = NULL;
6887 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6888 083957f4 2020-02-24 stsp if (err) {
6889 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6890 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6891 083957f4 2020-02-24 stsp goto done;
6892 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6893 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6894 083957f4 2020-02-24 stsp }
6895 0ebf8283 2019-07-24 stsp }
6896 0ebf8283 2019-07-24 stsp done:
6897 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6898 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6899 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6900 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6901 0ebf8283 2019-07-24 stsp free(path);
6902 0ebf8283 2019-07-24 stsp return err;
6903 0ebf8283 2019-07-24 stsp }
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp static const struct got_error *
6906 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6907 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6908 0ebf8283 2019-07-24 stsp {
6909 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6910 0ebf8283 2019-07-24 stsp char *path = NULL;
6911 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6912 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6913 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6914 0ebf8283 2019-07-24 stsp
6915 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6916 0ebf8283 2019-07-24 stsp if (err)
6917 0ebf8283 2019-07-24 stsp return err;
6918 0ebf8283 2019-07-24 stsp
6919 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6920 0ebf8283 2019-07-24 stsp if (f == NULL) {
6921 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6922 0ebf8283 2019-07-24 stsp goto done;
6923 0ebf8283 2019-07-24 stsp }
6924 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6925 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6926 0ebf8283 2019-07-24 stsp repo);
6927 0ebf8283 2019-07-24 stsp if (err)
6928 0ebf8283 2019-07-24 stsp break;
6929 0ebf8283 2019-07-24 stsp
6930 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6931 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6932 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6933 0ebf8283 2019-07-24 stsp if (n < 0) {
6934 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6935 0ebf8283 2019-07-24 stsp break;
6936 0ebf8283 2019-07-24 stsp }
6937 0ebf8283 2019-07-24 stsp }
6938 0ebf8283 2019-07-24 stsp }
6939 0ebf8283 2019-07-24 stsp done:
6940 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6941 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6942 0ebf8283 2019-07-24 stsp free(path);
6943 0ebf8283 2019-07-24 stsp if (commit)
6944 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6945 0ebf8283 2019-07-24 stsp return err;
6946 0ebf8283 2019-07-24 stsp }
6947 0ebf8283 2019-07-24 stsp
6948 bfce7f83 2019-07-27 stsp void
6949 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6950 bfce7f83 2019-07-27 stsp {
6951 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6952 bfce7f83 2019-07-27 stsp
6953 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6954 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6955 bfce7f83 2019-07-27 stsp free(hle);
6956 bfce7f83 2019-07-27 stsp }
6957 bfce7f83 2019-07-27 stsp }
6958 bfce7f83 2019-07-27 stsp
6959 0ebf8283 2019-07-24 stsp static const struct got_error *
6960 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6961 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6962 0ebf8283 2019-07-24 stsp {
6963 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6964 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6965 0ebf8283 2019-07-24 stsp
6966 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6967 0ebf8283 2019-07-24 stsp if (f == NULL) {
6968 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6969 0ebf8283 2019-07-24 stsp goto done;
6970 0ebf8283 2019-07-24 stsp }
6971 0ebf8283 2019-07-24 stsp
6972 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6973 0ebf8283 2019-07-24 stsp done:
6974 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6975 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6976 0ebf8283 2019-07-24 stsp return err;
6977 0ebf8283 2019-07-24 stsp }
6978 0ebf8283 2019-07-24 stsp
6979 0ebf8283 2019-07-24 stsp static const struct got_error *
6980 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6981 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6982 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6983 0ebf8283 2019-07-24 stsp {
6984 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6985 0ebf8283 2019-07-24 stsp int resp = ' ';
6986 0ebf8283 2019-07-24 stsp
6987 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6988 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6989 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6990 0ebf8283 2019-07-24 stsp resp = getchar();
6991 a61a4414 2019-08-07 stsp if (resp == '\n')
6992 a61a4414 2019-08-07 stsp resp = getchar();
6993 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6994 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6995 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6996 bfce7f83 2019-07-27 stsp repo);
6997 426ebf2e 2019-07-25 stsp if (err) {
6998 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6999 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7000 426ebf2e 2019-07-25 stsp break;
7001 bfce7f83 2019-07-27 stsp prev_err = err;
7002 426ebf2e 2019-07-25 stsp resp = ' ';
7003 426ebf2e 2019-07-25 stsp continue;
7004 426ebf2e 2019-07-25 stsp }
7005 bfce7f83 2019-07-27 stsp break;
7006 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7007 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7008 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7009 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7010 426ebf2e 2019-07-25 stsp if (err) {
7011 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7012 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7013 426ebf2e 2019-07-25 stsp break;
7014 bfce7f83 2019-07-27 stsp prev_err = err;
7015 426ebf2e 2019-07-25 stsp resp = ' ';
7016 426ebf2e 2019-07-25 stsp continue;
7017 426ebf2e 2019-07-25 stsp }
7018 bfce7f83 2019-07-27 stsp break;
7019 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7020 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7021 0ebf8283 2019-07-24 stsp break;
7022 426ebf2e 2019-07-25 stsp } else
7023 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7024 0ebf8283 2019-07-24 stsp }
7025 0ebf8283 2019-07-24 stsp
7026 0ebf8283 2019-07-24 stsp return err;
7027 0ebf8283 2019-07-24 stsp }
7028 0ebf8283 2019-07-24 stsp
7029 0ebf8283 2019-07-24 stsp static const struct got_error *
7030 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7031 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7032 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7033 0ebf8283 2019-07-24 stsp {
7034 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7035 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7036 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7037 3e3a69f1 2019-07-25 stsp branch, repo);
7038 0ebf8283 2019-07-24 stsp }
7039 0ebf8283 2019-07-24 stsp
7040 0ebf8283 2019-07-24 stsp static const struct got_error *
7041 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7042 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7043 0ebf8283 2019-07-24 stsp {
7044 0ebf8283 2019-07-24 stsp const struct got_error *err;
7045 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7048 0ebf8283 2019-07-24 stsp if (err)
7049 0ebf8283 2019-07-24 stsp goto done;
7050 0ebf8283 2019-07-24 stsp
7051 0ebf8283 2019-07-24 stsp if (new_id) {
7052 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7053 0ebf8283 2019-07-24 stsp if (err)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp }
7056 0ebf8283 2019-07-24 stsp
7057 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7058 0ebf8283 2019-07-24 stsp if (new_id_str)
7059 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7060 0ebf8283 2019-07-24 stsp
7061 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7062 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7063 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7064 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7065 0ebf8283 2019-07-24 stsp goto done;
7066 0ebf8283 2019-07-24 stsp }
7067 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7068 0ebf8283 2019-07-24 stsp } else {
7069 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7070 0ebf8283 2019-07-24 stsp if (err)
7071 0ebf8283 2019-07-24 stsp goto done;
7072 0ebf8283 2019-07-24 stsp }
7073 0ebf8283 2019-07-24 stsp
7074 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7075 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7076 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7077 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7078 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7079 0ebf8283 2019-07-24 stsp break;
7080 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7081 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7082 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7083 0ebf8283 2019-07-24 stsp logmsg);
7084 0ebf8283 2019-07-24 stsp break;
7085 0ebf8283 2019-07-24 stsp default:
7086 0ebf8283 2019-07-24 stsp break;
7087 0ebf8283 2019-07-24 stsp }
7088 0ebf8283 2019-07-24 stsp done:
7089 0ebf8283 2019-07-24 stsp free(old_id_str);
7090 0ebf8283 2019-07-24 stsp free(new_id_str);
7091 0ebf8283 2019-07-24 stsp return err;
7092 0ebf8283 2019-07-24 stsp }
7093 0ebf8283 2019-07-24 stsp
7094 0ebf8283 2019-07-24 stsp static const struct got_error *
7095 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7096 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7097 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7098 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7099 0ebf8283 2019-07-24 stsp {
7100 0ebf8283 2019-07-24 stsp const struct got_error *err;
7101 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7102 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7103 0ebf8283 2019-07-24 stsp
7104 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7105 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7106 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7107 0ebf8283 2019-07-24 stsp if (err)
7108 0ebf8283 2019-07-24 stsp return err;
7109 0ebf8283 2019-07-24 stsp }
7110 0ebf8283 2019-07-24 stsp
7111 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7112 0ebf8283 2019-07-24 stsp if (err)
7113 0ebf8283 2019-07-24 stsp return err;
7114 0ebf8283 2019-07-24 stsp
7115 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7116 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7117 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7118 0ebf8283 2019-07-24 stsp if (err) {
7119 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7120 0ebf8283 2019-07-24 stsp goto done;
7121 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7122 0ebf8283 2019-07-24 stsp } else {
7123 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7124 0ebf8283 2019-07-24 stsp free(new_commit_id);
7125 0ebf8283 2019-07-24 stsp }
7126 0ebf8283 2019-07-24 stsp done:
7127 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7128 0ebf8283 2019-07-24 stsp return err;
7129 0ebf8283 2019-07-24 stsp }
7130 0ebf8283 2019-07-24 stsp
7131 0ebf8283 2019-07-24 stsp static const struct got_error *
7132 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7133 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7134 0ebf8283 2019-07-24 stsp {
7135 0ebf8283 2019-07-24 stsp const struct got_error *error;
7136 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7137 0ebf8283 2019-07-24 stsp
7138 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7139 0ebf8283 2019-07-24 stsp repo);
7140 0ebf8283 2019-07-24 stsp if (error)
7141 0ebf8283 2019-07-24 stsp return error;
7142 0ebf8283 2019-07-24 stsp
7143 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7144 0ebf8283 2019-07-24 stsp if (error)
7145 0ebf8283 2019-07-24 stsp return error;
7146 0ebf8283 2019-07-24 stsp
7147 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7148 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7149 0ebf8283 2019-07-24 stsp return error;
7150 ab20a43a 2020-01-29 stsp }
7151 ab20a43a 2020-01-29 stsp
7152 ab20a43a 2020-01-29 stsp static const struct got_error *
7153 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7154 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7155 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7156 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7157 ab20a43a 2020-01-29 stsp {
7158 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7159 ab20a43a 2020-01-29 stsp
7160 ab20a43a 2020-01-29 stsp switch (status) {
7161 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7162 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7163 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7164 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7165 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7166 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7167 ab20a43a 2020-01-29 stsp default:
7168 ab20a43a 2020-01-29 stsp break;
7169 ab20a43a 2020-01-29 stsp }
7170 ab20a43a 2020-01-29 stsp
7171 ab20a43a 2020-01-29 stsp switch (staged_status) {
7172 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7173 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7174 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7175 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7176 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7177 ab20a43a 2020-01-29 stsp default:
7178 ab20a43a 2020-01-29 stsp break;
7179 ab20a43a 2020-01-29 stsp }
7180 ab20a43a 2020-01-29 stsp
7181 ab20a43a 2020-01-29 stsp return NULL;
7182 0ebf8283 2019-07-24 stsp }
7183 0ebf8283 2019-07-24 stsp
7184 0ebf8283 2019-07-24 stsp static const struct got_error *
7185 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7186 0ebf8283 2019-07-24 stsp {
7187 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7188 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7189 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7190 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7191 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7192 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7193 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7194 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7195 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7196 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7197 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7198 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7199 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7200 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7201 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7202 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7203 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7204 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7205 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7206 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7207 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7208 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7209 0ebf8283 2019-07-24 stsp
7210 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7211 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7212 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7213 0ebf8283 2019-07-24 stsp
7214 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7215 0ebf8283 2019-07-24 stsp switch (ch) {
7216 0ebf8283 2019-07-24 stsp case 'a':
7217 0ebf8283 2019-07-24 stsp abort_edit = 1;
7218 0ebf8283 2019-07-24 stsp break;
7219 0ebf8283 2019-07-24 stsp case 'c':
7220 0ebf8283 2019-07-24 stsp continue_edit = 1;
7221 0ebf8283 2019-07-24 stsp break;
7222 0ebf8283 2019-07-24 stsp case 'F':
7223 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7224 0ebf8283 2019-07-24 stsp break;
7225 083957f4 2020-02-24 stsp case 'm':
7226 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7227 083957f4 2020-02-24 stsp break;
7228 0ebf8283 2019-07-24 stsp default:
7229 0ebf8283 2019-07-24 stsp usage_histedit();
7230 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7231 0ebf8283 2019-07-24 stsp }
7232 0ebf8283 2019-07-24 stsp }
7233 0ebf8283 2019-07-24 stsp
7234 0ebf8283 2019-07-24 stsp argc -= optind;
7235 0ebf8283 2019-07-24 stsp argv += optind;
7236 0ebf8283 2019-07-24 stsp
7237 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7238 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7239 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7240 0ebf8283 2019-07-24 stsp err(1, "pledge");
7241 0ebf8283 2019-07-24 stsp #endif
7242 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7243 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7244 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7245 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7246 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7247 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7248 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7249 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7250 0ebf8283 2019-07-24 stsp if (argc != 0)
7251 0ebf8283 2019-07-24 stsp usage_histedit();
7252 0ebf8283 2019-07-24 stsp
7253 0ebf8283 2019-07-24 stsp /*
7254 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7255 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7256 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7257 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7258 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7259 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7260 0ebf8283 2019-07-24 stsp */
7261 0ebf8283 2019-07-24 stsp
7262 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7263 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7264 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7265 0ebf8283 2019-07-24 stsp goto done;
7266 0ebf8283 2019-07-24 stsp }
7267 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7268 0ebf8283 2019-07-24 stsp if (error)
7269 0ebf8283 2019-07-24 stsp goto done;
7270 0ebf8283 2019-07-24 stsp
7271 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7272 c9956ddf 2019-09-08 stsp NULL);
7273 0ebf8283 2019-07-24 stsp if (error != NULL)
7274 0ebf8283 2019-07-24 stsp goto done;
7275 0ebf8283 2019-07-24 stsp
7276 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7277 0ebf8283 2019-07-24 stsp if (error)
7278 0ebf8283 2019-07-24 stsp goto done;
7279 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7280 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7281 0ebf8283 2019-07-24 stsp goto done;
7282 0ebf8283 2019-07-24 stsp }
7283 0ebf8283 2019-07-24 stsp
7284 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7285 0ebf8283 2019-07-24 stsp if (error)
7286 0ebf8283 2019-07-24 stsp goto done;
7287 0ebf8283 2019-07-24 stsp
7288 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7289 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7290 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7291 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7292 083957f4 2020-02-24 stsp "before the -m option can be used");
7293 083957f4 2020-02-24 stsp goto done;
7294 083957f4 2020-02-24 stsp }
7295 083957f4 2020-02-24 stsp
7296 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7297 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7298 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7299 3e3a69f1 2019-07-25 stsp worktree, repo);
7300 0ebf8283 2019-07-24 stsp if (error)
7301 0ebf8283 2019-07-24 stsp goto done;
7302 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7303 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7304 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7305 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7306 0ebf8283 2019-07-24 stsp if (error)
7307 0ebf8283 2019-07-24 stsp goto done;
7308 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7309 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7310 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7311 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7312 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7313 0ebf8283 2019-07-24 stsp goto done;
7314 0ebf8283 2019-07-24 stsp }
7315 0ebf8283 2019-07-24 stsp
7316 0ebf8283 2019-07-24 stsp if (continue_edit) {
7317 0ebf8283 2019-07-24 stsp char *path;
7318 0ebf8283 2019-07-24 stsp
7319 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7320 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7321 0ebf8283 2019-07-24 stsp goto done;
7322 0ebf8283 2019-07-24 stsp }
7323 0ebf8283 2019-07-24 stsp
7324 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7325 0ebf8283 2019-07-24 stsp if (error)
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7329 0ebf8283 2019-07-24 stsp free(path);
7330 0ebf8283 2019-07-24 stsp if (error)
7331 0ebf8283 2019-07-24 stsp goto done;
7332 0ebf8283 2019-07-24 stsp
7333 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7334 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7335 3e3a69f1 2019-07-25 stsp worktree, repo);
7336 0ebf8283 2019-07-24 stsp if (error)
7337 0ebf8283 2019-07-24 stsp goto done;
7338 0ebf8283 2019-07-24 stsp
7339 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7340 0ebf8283 2019-07-24 stsp if (error)
7341 0ebf8283 2019-07-24 stsp goto done;
7342 0ebf8283 2019-07-24 stsp
7343 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7344 0ebf8283 2019-07-24 stsp head_commit_id);
7345 0ebf8283 2019-07-24 stsp if (error)
7346 0ebf8283 2019-07-24 stsp goto done;
7347 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7348 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7349 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7350 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7351 3aac7cf7 2019-07-25 stsp goto done;
7352 3aac7cf7 2019-07-25 stsp }
7353 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7354 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7355 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7356 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7357 0ebf8283 2019-07-24 stsp commit = NULL;
7358 0ebf8283 2019-07-24 stsp if (error)
7359 0ebf8283 2019-07-24 stsp goto done;
7360 0ebf8283 2019-07-24 stsp } else {
7361 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7362 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7363 0ebf8283 2019-07-24 stsp goto done;
7364 0ebf8283 2019-07-24 stsp }
7365 0ebf8283 2019-07-24 stsp
7366 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7367 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7368 0ebf8283 2019-07-24 stsp if (error != NULL)
7369 0ebf8283 2019-07-24 stsp goto done;
7370 0ebf8283 2019-07-24 stsp
7371 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7372 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7373 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7374 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7375 c7d20a3f 2019-07-30 stsp goto done;
7376 c7d20a3f 2019-07-30 stsp }
7377 c7d20a3f 2019-07-30 stsp
7378 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7379 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7380 bfce7f83 2019-07-27 stsp branch = NULL;
7381 0ebf8283 2019-07-24 stsp if (error)
7382 0ebf8283 2019-07-24 stsp goto done;
7383 0ebf8283 2019-07-24 stsp
7384 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7385 0ebf8283 2019-07-24 stsp head_commit_id);
7386 0ebf8283 2019-07-24 stsp if (error)
7387 0ebf8283 2019-07-24 stsp goto done;
7388 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7389 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7390 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7391 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7392 3aac7cf7 2019-07-25 stsp goto done;
7393 3aac7cf7 2019-07-25 stsp }
7394 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7395 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7396 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7397 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7398 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7399 0ebf8283 2019-07-24 stsp commit = NULL;
7400 0ebf8283 2019-07-24 stsp if (error)
7401 0ebf8283 2019-07-24 stsp goto done;
7402 0ebf8283 2019-07-24 stsp
7403 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7404 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7405 c5996fff 2020-01-29 stsp goto done;
7406 c5996fff 2020-01-29 stsp }
7407 c5996fff 2020-01-29 stsp
7408 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7409 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7410 bfce7f83 2019-07-27 stsp if (error)
7411 bfce7f83 2019-07-27 stsp goto done;
7412 bfce7f83 2019-07-27 stsp
7413 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7414 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7415 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7416 6ba2bea2 2019-07-27 stsp if (error) {
7417 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7418 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7419 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7420 0ebf8283 2019-07-24 stsp goto done;
7421 6ba2bea2 2019-07-27 stsp }
7422 0ebf8283 2019-07-24 stsp } else {
7423 514f2ffe 2020-01-29 stsp const char *branch_name;
7424 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7425 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7426 514f2ffe 2020-01-29 stsp branch_name += 11;
7427 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7428 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7429 6ba2bea2 2019-07-27 stsp if (error) {
7430 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7431 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7432 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7433 0ebf8283 2019-07-24 stsp goto done;
7434 6ba2bea2 2019-07-27 stsp }
7435 0ebf8283 2019-07-24 stsp
7436 0ebf8283 2019-07-24 stsp }
7437 0ebf8283 2019-07-24 stsp
7438 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7439 0ebf8283 2019-07-24 stsp repo);
7440 6ba2bea2 2019-07-27 stsp if (error) {
7441 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7442 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7443 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7444 0ebf8283 2019-07-24 stsp goto done;
7445 6ba2bea2 2019-07-27 stsp }
7446 0ebf8283 2019-07-24 stsp
7447 0ebf8283 2019-07-24 stsp }
7448 0ebf8283 2019-07-24 stsp
7449 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7450 4ec14e60 2019-08-28 hiltjo if (error)
7451 0ebf8283 2019-07-24 stsp goto done;
7452 0ebf8283 2019-07-24 stsp
7453 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7454 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7455 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7456 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7457 0ebf8283 2019-07-24 stsp continue;
7458 0ebf8283 2019-07-24 stsp
7459 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7460 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7461 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7462 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7463 0ebf8283 2019-07-24 stsp repo);
7464 ab20a43a 2020-01-29 stsp if (error)
7465 ab20a43a 2020-01-29 stsp goto done;
7466 0ebf8283 2019-07-24 stsp } else {
7467 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7468 ab20a43a 2020-01-29 stsp int have_changes = 0;
7469 ab20a43a 2020-01-29 stsp
7470 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7471 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7472 ab20a43a 2020-01-29 stsp if (error)
7473 ab20a43a 2020-01-29 stsp goto done;
7474 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7475 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7476 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7477 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7478 ab20a43a 2020-01-29 stsp if (error) {
7479 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7480 ab20a43a 2020-01-29 stsp goto done;
7481 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7482 ab20a43a 2020-01-29 stsp goto done;
7483 ab20a43a 2020-01-29 stsp }
7484 ab20a43a 2020-01-29 stsp if (have_changes) {
7485 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7486 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7487 ab20a43a 2020-01-29 stsp if (error)
7488 ab20a43a 2020-01-29 stsp goto done;
7489 ab20a43a 2020-01-29 stsp } else {
7490 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7491 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7492 ab20a43a 2020-01-29 stsp if (error)
7493 ab20a43a 2020-01-29 stsp goto done;
7494 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7495 ab20a43a 2020-01-29 stsp hle, NULL);
7496 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7497 ab20a43a 2020-01-29 stsp commit = NULL;
7498 ab20a43a 2020-01-29 stsp if (error)
7499 ab20a43a 2020-01-29 stsp goto done;
7500 ab20a43a 2020-01-29 stsp }
7501 0ebf8283 2019-07-24 stsp }
7502 0ebf8283 2019-07-24 stsp continue;
7503 0ebf8283 2019-07-24 stsp }
7504 0ebf8283 2019-07-24 stsp
7505 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7506 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7507 0ebf8283 2019-07-24 stsp if (error)
7508 0ebf8283 2019-07-24 stsp goto done;
7509 0ebf8283 2019-07-24 stsp continue;
7510 0ebf8283 2019-07-24 stsp }
7511 0ebf8283 2019-07-24 stsp
7512 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7513 0ebf8283 2019-07-24 stsp hle->commit_id);
7514 0ebf8283 2019-07-24 stsp if (error)
7515 0ebf8283 2019-07-24 stsp goto done;
7516 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7517 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7518 0ebf8283 2019-07-24 stsp
7519 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7520 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7521 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7522 0ebf8283 2019-07-24 stsp if (error)
7523 0ebf8283 2019-07-24 stsp goto done;
7524 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7525 0ebf8283 2019-07-24 stsp commit = NULL;
7526 0ebf8283 2019-07-24 stsp
7527 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7528 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7529 a0ea4fc0 2020-02-28 stsp repo);
7530 a0ea4fc0 2020-02-28 stsp if (error)
7531 a0ea4fc0 2020-02-28 stsp goto done;
7532 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7533 0ebf8283 2019-07-24 stsp break;
7534 0ebf8283 2019-07-24 stsp }
7535 0ebf8283 2019-07-24 stsp
7536 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7537 0ebf8283 2019-07-24 stsp char *id_str;
7538 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7539 0ebf8283 2019-07-24 stsp if (error)
7540 0ebf8283 2019-07-24 stsp goto done;
7541 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7542 0ebf8283 2019-07-24 stsp id_str);
7543 0ebf8283 2019-07-24 stsp free(id_str);
7544 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7545 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7546 3e3a69f1 2019-07-25 stsp fileindex);
7547 0ebf8283 2019-07-24 stsp goto done;
7548 0ebf8283 2019-07-24 stsp }
7549 0ebf8283 2019-07-24 stsp
7550 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7551 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7552 0ebf8283 2019-07-24 stsp if (error)
7553 0ebf8283 2019-07-24 stsp goto done;
7554 0ebf8283 2019-07-24 stsp continue;
7555 0ebf8283 2019-07-24 stsp }
7556 0ebf8283 2019-07-24 stsp
7557 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7558 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7559 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7560 0ebf8283 2019-07-24 stsp if (error)
7561 0ebf8283 2019-07-24 stsp goto done;
7562 0ebf8283 2019-07-24 stsp }
7563 0ebf8283 2019-07-24 stsp
7564 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7565 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7566 0ebf8283 2019-07-24 stsp if (error)
7567 0ebf8283 2019-07-24 stsp goto done;
7568 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7569 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7570 0ebf8283 2019-07-24 stsp } else
7571 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7572 3e3a69f1 2019-07-25 stsp branch, repo);
7573 0ebf8283 2019-07-24 stsp done:
7574 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7575 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7576 0ebf8283 2019-07-24 stsp free(head_commit_id);
7577 0ebf8283 2019-07-24 stsp free(base_commit_id);
7578 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7579 0ebf8283 2019-07-24 stsp if (commit)
7580 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7581 0ebf8283 2019-07-24 stsp if (branch)
7582 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7583 0ebf8283 2019-07-24 stsp if (tmp_branch)
7584 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7585 0ebf8283 2019-07-24 stsp if (worktree)
7586 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7587 2822a352 2019-10-15 stsp if (repo)
7588 2822a352 2019-10-15 stsp got_repo_close(repo);
7589 2822a352 2019-10-15 stsp return error;
7590 2822a352 2019-10-15 stsp }
7591 2822a352 2019-10-15 stsp
7592 2822a352 2019-10-15 stsp __dead static void
7593 2822a352 2019-10-15 stsp usage_integrate(void)
7594 2822a352 2019-10-15 stsp {
7595 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7596 2822a352 2019-10-15 stsp exit(1);
7597 2822a352 2019-10-15 stsp }
7598 2822a352 2019-10-15 stsp
7599 2822a352 2019-10-15 stsp static const struct got_error *
7600 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7601 2822a352 2019-10-15 stsp {
7602 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7603 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7604 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7605 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7606 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7607 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7608 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7609 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7610 2822a352 2019-10-15 stsp int ch, did_something = 0;
7611 2822a352 2019-10-15 stsp
7612 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7613 2822a352 2019-10-15 stsp switch (ch) {
7614 2822a352 2019-10-15 stsp default:
7615 2822a352 2019-10-15 stsp usage_integrate();
7616 2822a352 2019-10-15 stsp /* NOTREACHED */
7617 2822a352 2019-10-15 stsp }
7618 2822a352 2019-10-15 stsp }
7619 2822a352 2019-10-15 stsp
7620 2822a352 2019-10-15 stsp argc -= optind;
7621 2822a352 2019-10-15 stsp argv += optind;
7622 2822a352 2019-10-15 stsp
7623 2822a352 2019-10-15 stsp if (argc != 1)
7624 2822a352 2019-10-15 stsp usage_integrate();
7625 2822a352 2019-10-15 stsp branch_arg = argv[0];
7626 2822a352 2019-10-15 stsp
7627 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7628 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7629 2822a352 2019-10-15 stsp err(1, "pledge");
7630 2822a352 2019-10-15 stsp
7631 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7632 2822a352 2019-10-15 stsp if (cwd == NULL) {
7633 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7634 2822a352 2019-10-15 stsp goto done;
7635 2822a352 2019-10-15 stsp }
7636 2822a352 2019-10-15 stsp
7637 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7638 2822a352 2019-10-15 stsp if (error)
7639 2822a352 2019-10-15 stsp goto done;
7640 2822a352 2019-10-15 stsp
7641 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7642 2822a352 2019-10-15 stsp if (error)
7643 2822a352 2019-10-15 stsp goto done;
7644 2822a352 2019-10-15 stsp
7645 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7646 2822a352 2019-10-15 stsp NULL);
7647 2822a352 2019-10-15 stsp if (error != NULL)
7648 2822a352 2019-10-15 stsp goto done;
7649 2822a352 2019-10-15 stsp
7650 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7651 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7652 2822a352 2019-10-15 stsp if (error)
7653 2822a352 2019-10-15 stsp goto done;
7654 2822a352 2019-10-15 stsp
7655 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7656 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7657 2822a352 2019-10-15 stsp goto done;
7658 2822a352 2019-10-15 stsp }
7659 2822a352 2019-10-15 stsp
7660 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7661 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7662 2822a352 2019-10-15 stsp if (error)
7663 2822a352 2019-10-15 stsp goto done;
7664 2822a352 2019-10-15 stsp
7665 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7666 2822a352 2019-10-15 stsp if (refname == NULL) {
7667 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7668 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7669 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7670 2822a352 2019-10-15 stsp goto done;
7671 2822a352 2019-10-15 stsp }
7672 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7673 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7674 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7675 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7676 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7677 2822a352 2019-10-15 stsp goto done;
7678 2822a352 2019-10-15 stsp }
7679 2822a352 2019-10-15 stsp
7680 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7681 2822a352 2019-10-15 stsp if (error)
7682 2822a352 2019-10-15 stsp goto done;
7683 2822a352 2019-10-15 stsp
7684 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7685 2822a352 2019-10-15 stsp if (error)
7686 2822a352 2019-10-15 stsp goto done;
7687 2822a352 2019-10-15 stsp
7688 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7689 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7690 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7691 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7692 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7693 2822a352 2019-10-15 stsp goto done;
7694 2822a352 2019-10-15 stsp }
7695 2822a352 2019-10-15 stsp
7696 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7697 2822a352 2019-10-15 stsp if (error) {
7698 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7699 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7700 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7701 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7702 2822a352 2019-10-15 stsp goto done;
7703 2822a352 2019-10-15 stsp }
7704 2822a352 2019-10-15 stsp
7705 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7706 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7707 2822a352 2019-10-15 stsp check_cancelled, NULL);
7708 2822a352 2019-10-15 stsp if (error)
7709 2822a352 2019-10-15 stsp goto done;
7710 2822a352 2019-10-15 stsp
7711 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7712 2822a352 2019-10-15 stsp done:
7713 715dc77e 2019-08-03 stsp if (repo)
7714 715dc77e 2019-08-03 stsp got_repo_close(repo);
7715 2822a352 2019-10-15 stsp if (worktree)
7716 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7717 2822a352 2019-10-15 stsp free(cwd);
7718 2822a352 2019-10-15 stsp free(base_commit_id);
7719 2822a352 2019-10-15 stsp free(commit_id);
7720 2822a352 2019-10-15 stsp free(refname);
7721 2822a352 2019-10-15 stsp free(base_refname);
7722 715dc77e 2019-08-03 stsp return error;
7723 715dc77e 2019-08-03 stsp }
7724 715dc77e 2019-08-03 stsp
7725 715dc77e 2019-08-03 stsp __dead static void
7726 715dc77e 2019-08-03 stsp usage_stage(void)
7727 715dc77e 2019-08-03 stsp {
7728 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7729 f3055044 2019-08-07 stsp "[file-path ...]\n",
7730 715dc77e 2019-08-03 stsp getprogname());
7731 715dc77e 2019-08-03 stsp exit(1);
7732 715dc77e 2019-08-03 stsp }
7733 715dc77e 2019-08-03 stsp
7734 715dc77e 2019-08-03 stsp static const struct got_error *
7735 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7736 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7737 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7738 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7739 408b4ebc 2019-08-03 stsp {
7740 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7741 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7742 408b4ebc 2019-08-03 stsp
7743 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7744 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7745 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7746 408b4ebc 2019-08-03 stsp return NULL;
7747 408b4ebc 2019-08-03 stsp
7748 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7749 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7750 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7751 408b4ebc 2019-08-03 stsp else
7752 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7753 408b4ebc 2019-08-03 stsp if (err)
7754 408b4ebc 2019-08-03 stsp return err;
7755 408b4ebc 2019-08-03 stsp
7756 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7757 408b4ebc 2019-08-03 stsp free(id_str);
7758 dc424a06 2019-08-07 stsp return NULL;
7759 dc424a06 2019-08-07 stsp }
7760 2e1f37b0 2019-08-08 stsp
7761 dc424a06 2019-08-07 stsp static const struct got_error *
7762 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7763 715dc77e 2019-08-03 stsp {
7764 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7765 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7766 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7767 715dc77e 2019-08-03 stsp char *cwd = NULL;
7768 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7769 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7770 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7771 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7772 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7773 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7774 715dc77e 2019-08-03 stsp
7775 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7776 715dc77e 2019-08-03 stsp
7777 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7778 715dc77e 2019-08-03 stsp switch (ch) {
7779 408b4ebc 2019-08-03 stsp case 'l':
7780 408b4ebc 2019-08-03 stsp list_stage = 1;
7781 408b4ebc 2019-08-03 stsp break;
7782 dc424a06 2019-08-07 stsp case 'p':
7783 dc424a06 2019-08-07 stsp pflag = 1;
7784 dc424a06 2019-08-07 stsp break;
7785 dc424a06 2019-08-07 stsp case 'F':
7786 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7787 dc424a06 2019-08-07 stsp break;
7788 715dc77e 2019-08-03 stsp default:
7789 715dc77e 2019-08-03 stsp usage_stage();
7790 715dc77e 2019-08-03 stsp /* NOTREACHED */
7791 715dc77e 2019-08-03 stsp }
7792 715dc77e 2019-08-03 stsp }
7793 715dc77e 2019-08-03 stsp
7794 715dc77e 2019-08-03 stsp argc -= optind;
7795 715dc77e 2019-08-03 stsp argv += optind;
7796 715dc77e 2019-08-03 stsp
7797 715dc77e 2019-08-03 stsp #ifndef PROFILE
7798 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7799 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7800 715dc77e 2019-08-03 stsp err(1, "pledge");
7801 715dc77e 2019-08-03 stsp #endif
7802 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7803 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7804 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7805 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7806 715dc77e 2019-08-03 stsp
7807 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7808 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7809 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7810 715dc77e 2019-08-03 stsp goto done;
7811 715dc77e 2019-08-03 stsp }
7812 715dc77e 2019-08-03 stsp
7813 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7814 715dc77e 2019-08-03 stsp if (error)
7815 715dc77e 2019-08-03 stsp goto done;
7816 715dc77e 2019-08-03 stsp
7817 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7818 c9956ddf 2019-09-08 stsp NULL);
7819 715dc77e 2019-08-03 stsp if (error != NULL)
7820 715dc77e 2019-08-03 stsp goto done;
7821 715dc77e 2019-08-03 stsp
7822 dc424a06 2019-08-07 stsp if (patch_script_path) {
7823 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7824 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7825 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7826 dc424a06 2019-08-07 stsp patch_script_path);
7827 dc424a06 2019-08-07 stsp goto done;
7828 dc424a06 2019-08-07 stsp }
7829 dc424a06 2019-08-07 stsp }
7830 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7831 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7832 715dc77e 2019-08-03 stsp if (error)
7833 715dc77e 2019-08-03 stsp goto done;
7834 715dc77e 2019-08-03 stsp
7835 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7836 6d022e97 2019-08-04 stsp if (error)
7837 6d022e97 2019-08-04 stsp goto done;
7838 715dc77e 2019-08-03 stsp
7839 6d022e97 2019-08-04 stsp if (list_stage)
7840 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7841 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7842 2e1f37b0 2019-08-08 stsp else {
7843 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7844 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7845 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7846 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7847 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7848 2e1f37b0 2019-08-08 stsp }
7849 ad493afc 2019-08-03 stsp done:
7850 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7851 2e1f37b0 2019-08-08 stsp error == NULL)
7852 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7853 ad493afc 2019-08-03 stsp if (repo)
7854 ad493afc 2019-08-03 stsp got_repo_close(repo);
7855 ad493afc 2019-08-03 stsp if (worktree)
7856 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7857 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7858 ad493afc 2019-08-03 stsp free((char *)pe->path);
7859 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7860 ad493afc 2019-08-03 stsp free(cwd);
7861 ad493afc 2019-08-03 stsp return error;
7862 ad493afc 2019-08-03 stsp }
7863 ad493afc 2019-08-03 stsp
7864 ad493afc 2019-08-03 stsp __dead static void
7865 ad493afc 2019-08-03 stsp usage_unstage(void)
7866 ad493afc 2019-08-03 stsp {
7867 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7868 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7869 ad493afc 2019-08-03 stsp getprogname());
7870 ad493afc 2019-08-03 stsp exit(1);
7871 ad493afc 2019-08-03 stsp }
7872 ad493afc 2019-08-03 stsp
7873 ad493afc 2019-08-03 stsp
7874 ad493afc 2019-08-03 stsp static const struct got_error *
7875 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7876 ad493afc 2019-08-03 stsp {
7877 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7878 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7879 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7880 ad493afc 2019-08-03 stsp char *cwd = NULL;
7881 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7882 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7883 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7884 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7885 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7886 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7887 ad493afc 2019-08-03 stsp
7888 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7889 ad493afc 2019-08-03 stsp
7890 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7891 ad493afc 2019-08-03 stsp switch (ch) {
7892 2e1f37b0 2019-08-08 stsp case 'p':
7893 2e1f37b0 2019-08-08 stsp pflag = 1;
7894 2e1f37b0 2019-08-08 stsp break;
7895 2e1f37b0 2019-08-08 stsp case 'F':
7896 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7897 2e1f37b0 2019-08-08 stsp break;
7898 ad493afc 2019-08-03 stsp default:
7899 ad493afc 2019-08-03 stsp usage_unstage();
7900 ad493afc 2019-08-03 stsp /* NOTREACHED */
7901 ad493afc 2019-08-03 stsp }
7902 ad493afc 2019-08-03 stsp }
7903 ad493afc 2019-08-03 stsp
7904 ad493afc 2019-08-03 stsp argc -= optind;
7905 ad493afc 2019-08-03 stsp argv += optind;
7906 ad493afc 2019-08-03 stsp
7907 ad493afc 2019-08-03 stsp #ifndef PROFILE
7908 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7909 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7910 ad493afc 2019-08-03 stsp err(1, "pledge");
7911 ad493afc 2019-08-03 stsp #endif
7912 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7913 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7914 2e1f37b0 2019-08-08 stsp
7915 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7916 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7917 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7918 ad493afc 2019-08-03 stsp goto done;
7919 ad493afc 2019-08-03 stsp }
7920 ad493afc 2019-08-03 stsp
7921 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7922 ad493afc 2019-08-03 stsp if (error)
7923 ad493afc 2019-08-03 stsp goto done;
7924 ad493afc 2019-08-03 stsp
7925 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7926 c9956ddf 2019-09-08 stsp NULL);
7927 ad493afc 2019-08-03 stsp if (error != NULL)
7928 ad493afc 2019-08-03 stsp goto done;
7929 ad493afc 2019-08-03 stsp
7930 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7931 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7932 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7933 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7934 2e1f37b0 2019-08-08 stsp patch_script_path);
7935 2e1f37b0 2019-08-08 stsp goto done;
7936 2e1f37b0 2019-08-08 stsp }
7937 2e1f37b0 2019-08-08 stsp }
7938 2e1f37b0 2019-08-08 stsp
7939 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7940 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7941 ad493afc 2019-08-03 stsp if (error)
7942 ad493afc 2019-08-03 stsp goto done;
7943 ad493afc 2019-08-03 stsp
7944 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7945 ad493afc 2019-08-03 stsp if (error)
7946 ad493afc 2019-08-03 stsp goto done;
7947 ad493afc 2019-08-03 stsp
7948 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7949 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7950 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7951 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7952 715dc77e 2019-08-03 stsp done:
7953 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7954 2e1f37b0 2019-08-08 stsp error == NULL)
7955 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7956 0ebf8283 2019-07-24 stsp if (repo)
7957 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7958 715dc77e 2019-08-03 stsp if (worktree)
7959 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7960 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7961 715dc77e 2019-08-03 stsp free((char *)pe->path);
7962 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7963 715dc77e 2019-08-03 stsp free(cwd);
7964 01073a5d 2019-08-22 stsp return error;
7965 01073a5d 2019-08-22 stsp }
7966 01073a5d 2019-08-22 stsp
7967 01073a5d 2019-08-22 stsp __dead static void
7968 01073a5d 2019-08-22 stsp usage_cat(void)
7969 01073a5d 2019-08-22 stsp {
7970 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7971 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7972 01073a5d 2019-08-22 stsp exit(1);
7973 01073a5d 2019-08-22 stsp }
7974 01073a5d 2019-08-22 stsp
7975 01073a5d 2019-08-22 stsp static const struct got_error *
7976 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7977 01073a5d 2019-08-22 stsp {
7978 01073a5d 2019-08-22 stsp const struct got_error *err;
7979 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7980 01073a5d 2019-08-22 stsp
7981 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7982 01073a5d 2019-08-22 stsp if (err)
7983 01073a5d 2019-08-22 stsp return err;
7984 01073a5d 2019-08-22 stsp
7985 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7986 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7987 01073a5d 2019-08-22 stsp return err;
7988 01073a5d 2019-08-22 stsp }
7989 01073a5d 2019-08-22 stsp
7990 01073a5d 2019-08-22 stsp static const struct got_error *
7991 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7992 01073a5d 2019-08-22 stsp {
7993 01073a5d 2019-08-22 stsp const struct got_error *err;
7994 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7995 56e0773d 2019-11-28 stsp int nentries, i;
7996 01073a5d 2019-08-22 stsp
7997 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7998 01073a5d 2019-08-22 stsp if (err)
7999 01073a5d 2019-08-22 stsp return err;
8000 01073a5d 2019-08-22 stsp
8001 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8002 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8003 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8004 01073a5d 2019-08-22 stsp char *id_str;
8005 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8006 01073a5d 2019-08-22 stsp break;
8007 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8008 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8009 01073a5d 2019-08-22 stsp if (err)
8010 01073a5d 2019-08-22 stsp break;
8011 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8012 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8013 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8014 01073a5d 2019-08-22 stsp free(id_str);
8015 01073a5d 2019-08-22 stsp }
8016 01073a5d 2019-08-22 stsp
8017 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8018 01073a5d 2019-08-22 stsp return err;
8019 01073a5d 2019-08-22 stsp }
8020 01073a5d 2019-08-22 stsp
8021 01073a5d 2019-08-22 stsp static const struct got_error *
8022 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8023 01073a5d 2019-08-22 stsp {
8024 01073a5d 2019-08-22 stsp const struct got_error *err;
8025 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8026 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8027 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8028 01073a5d 2019-08-22 stsp char *id_str = NULL;
8029 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8030 01073a5d 2019-08-22 stsp
8031 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8032 01073a5d 2019-08-22 stsp if (err)
8033 01073a5d 2019-08-22 stsp return err;
8034 01073a5d 2019-08-22 stsp
8035 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8036 01073a5d 2019-08-22 stsp if (err)
8037 01073a5d 2019-08-22 stsp goto done;
8038 01073a5d 2019-08-22 stsp
8039 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8040 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8041 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8042 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8043 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8044 01073a5d 2019-08-22 stsp char *pid_str;
8045 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8046 01073a5d 2019-08-22 stsp if (err)
8047 01073a5d 2019-08-22 stsp goto done;
8048 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8049 01073a5d 2019-08-22 stsp free(pid_str);
8050 01073a5d 2019-08-22 stsp }
8051 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8052 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8053 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8054 01073a5d 2019-08-22 stsp
8055 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8056 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8057 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8058 01073a5d 2019-08-22 stsp
8059 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8060 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8061 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8062 01073a5d 2019-08-22 stsp done:
8063 01073a5d 2019-08-22 stsp free(id_str);
8064 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8065 01073a5d 2019-08-22 stsp return err;
8066 01073a5d 2019-08-22 stsp }
8067 01073a5d 2019-08-22 stsp
8068 01073a5d 2019-08-22 stsp static const struct got_error *
8069 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8070 01073a5d 2019-08-22 stsp {
8071 01073a5d 2019-08-22 stsp const struct got_error *err;
8072 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8073 01073a5d 2019-08-22 stsp char *id_str = NULL;
8074 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8075 01073a5d 2019-08-22 stsp
8076 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8077 01073a5d 2019-08-22 stsp if (err)
8078 01073a5d 2019-08-22 stsp return err;
8079 01073a5d 2019-08-22 stsp
8080 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8081 01073a5d 2019-08-22 stsp if (err)
8082 01073a5d 2019-08-22 stsp goto done;
8083 01073a5d 2019-08-22 stsp
8084 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8085 e15d5241 2019-08-22 stsp
8086 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8087 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8088 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8089 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8090 01073a5d 2019-08-22 stsp break;
8091 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8092 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8093 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8094 01073a5d 2019-08-22 stsp break;
8095 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8096 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8097 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8098 01073a5d 2019-08-22 stsp break;
8099 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8100 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8101 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8102 01073a5d 2019-08-22 stsp break;
8103 01073a5d 2019-08-22 stsp default:
8104 01073a5d 2019-08-22 stsp break;
8105 01073a5d 2019-08-22 stsp }
8106 01073a5d 2019-08-22 stsp
8107 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8108 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8109 e15d5241 2019-08-22 stsp
8110 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8111 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8112 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8113 01073a5d 2019-08-22 stsp
8114 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8115 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8116 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8117 01073a5d 2019-08-22 stsp done:
8118 01073a5d 2019-08-22 stsp free(id_str);
8119 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8120 01073a5d 2019-08-22 stsp return err;
8121 01073a5d 2019-08-22 stsp }
8122 01073a5d 2019-08-22 stsp
8123 01073a5d 2019-08-22 stsp static const struct got_error *
8124 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8125 01073a5d 2019-08-22 stsp {
8126 01073a5d 2019-08-22 stsp const struct got_error *error;
8127 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8128 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8129 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8130 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8131 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8132 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8133 01073a5d 2019-08-22 stsp
8134 01073a5d 2019-08-22 stsp #ifndef PROFILE
8135 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8136 01073a5d 2019-08-22 stsp NULL) == -1)
8137 01073a5d 2019-08-22 stsp err(1, "pledge");
8138 01073a5d 2019-08-22 stsp #endif
8139 01073a5d 2019-08-22 stsp
8140 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8141 01073a5d 2019-08-22 stsp switch (ch) {
8142 896e9b6f 2019-08-26 stsp case 'c':
8143 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8144 896e9b6f 2019-08-26 stsp break;
8145 01073a5d 2019-08-22 stsp case 'r':
8146 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8147 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8148 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8149 9ba1d308 2019-10-21 stsp optarg);
8150 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8151 01073a5d 2019-08-22 stsp break;
8152 896e9b6f 2019-08-26 stsp case 'P':
8153 896e9b6f 2019-08-26 stsp force_path = 1;
8154 896e9b6f 2019-08-26 stsp break;
8155 01073a5d 2019-08-22 stsp default:
8156 01073a5d 2019-08-22 stsp usage_cat();
8157 01073a5d 2019-08-22 stsp /* NOTREACHED */
8158 01073a5d 2019-08-22 stsp }
8159 01073a5d 2019-08-22 stsp }
8160 01073a5d 2019-08-22 stsp
8161 01073a5d 2019-08-22 stsp argc -= optind;
8162 01073a5d 2019-08-22 stsp argv += optind;
8163 01073a5d 2019-08-22 stsp
8164 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8165 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8166 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8167 01073a5d 2019-08-22 stsp goto done;
8168 01073a5d 2019-08-22 stsp }
8169 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8170 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8171 01073a5d 2019-08-22 stsp goto done;
8172 01073a5d 2019-08-22 stsp if (worktree) {
8173 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8174 01073a5d 2019-08-22 stsp repo_path = strdup(
8175 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8176 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8177 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8178 01073a5d 2019-08-22 stsp goto done;
8179 01073a5d 2019-08-22 stsp }
8180 01073a5d 2019-08-22 stsp }
8181 01073a5d 2019-08-22 stsp }
8182 01073a5d 2019-08-22 stsp
8183 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8184 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8185 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8186 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8187 01073a5d 2019-08-22 stsp }
8188 01073a5d 2019-08-22 stsp
8189 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8190 01073a5d 2019-08-22 stsp free(repo_path);
8191 01073a5d 2019-08-22 stsp if (error != NULL)
8192 01073a5d 2019-08-22 stsp goto done;
8193 01073a5d 2019-08-22 stsp
8194 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8195 896e9b6f 2019-08-26 stsp if (error)
8196 896e9b6f 2019-08-26 stsp goto done;
8197 896e9b6f 2019-08-26 stsp
8198 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8199 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8200 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8201 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8202 01073a5d 2019-08-22 stsp if (error)
8203 01073a5d 2019-08-22 stsp goto done;
8204 01073a5d 2019-08-22 stsp
8205 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8206 896e9b6f 2019-08-26 stsp if (force_path) {
8207 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8208 896e9b6f 2019-08-26 stsp argv[i]);
8209 896e9b6f 2019-08-26 stsp if (error)
8210 896e9b6f 2019-08-26 stsp break;
8211 896e9b6f 2019-08-26 stsp } else {
8212 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8213 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8214 896e9b6f 2019-08-26 stsp if (error) {
8215 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8216 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8217 896e9b6f 2019-08-26 stsp break;
8218 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8219 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8220 896e9b6f 2019-08-26 stsp if (error)
8221 896e9b6f 2019-08-26 stsp break;
8222 896e9b6f 2019-08-26 stsp }
8223 896e9b6f 2019-08-26 stsp }
8224 01073a5d 2019-08-22 stsp
8225 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8226 01073a5d 2019-08-22 stsp if (error)
8227 01073a5d 2019-08-22 stsp break;
8228 01073a5d 2019-08-22 stsp
8229 01073a5d 2019-08-22 stsp switch (obj_type) {
8230 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8231 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8232 01073a5d 2019-08-22 stsp break;
8233 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8234 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8235 01073a5d 2019-08-22 stsp break;
8236 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8237 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8238 01073a5d 2019-08-22 stsp break;
8239 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8240 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8241 01073a5d 2019-08-22 stsp break;
8242 01073a5d 2019-08-22 stsp default:
8243 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8244 01073a5d 2019-08-22 stsp break;
8245 01073a5d 2019-08-22 stsp }
8246 01073a5d 2019-08-22 stsp if (error)
8247 01073a5d 2019-08-22 stsp break;
8248 01073a5d 2019-08-22 stsp free(label);
8249 01073a5d 2019-08-22 stsp label = NULL;
8250 01073a5d 2019-08-22 stsp free(id);
8251 01073a5d 2019-08-22 stsp id = NULL;
8252 01073a5d 2019-08-22 stsp }
8253 01073a5d 2019-08-22 stsp done:
8254 01073a5d 2019-08-22 stsp free(label);
8255 01073a5d 2019-08-22 stsp free(id);
8256 896e9b6f 2019-08-26 stsp free(commit_id);
8257 01073a5d 2019-08-22 stsp if (worktree)
8258 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8259 01073a5d 2019-08-22 stsp if (repo) {
8260 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8261 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8262 01073a5d 2019-08-22 stsp if (error == NULL)
8263 01073a5d 2019-08-22 stsp error = repo_error;
8264 01073a5d 2019-08-22 stsp }
8265 0ebf8283 2019-07-24 stsp return error;
8266 0ebf8283 2019-07-24 stsp }