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 469dd726 2020-03-20 stsp fprintf(stderr, "usage: %s clone [-m] [-q] [-v] repository-url "
815 8a8b05ce 2020-03-19 stsp "[target-directory]\n", getprogname());
816 93658fb9 2020-03-18 stsp exit(1);
817 93658fb9 2020-03-18 stsp }
818 892ac3b6 2020-03-18 stsp
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
824 892ac3b6 2020-03-18 stsp };
825 93658fb9 2020-03-18 stsp
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
829 531c3985 2020-03-18 stsp {
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
834 b2409d58 2020-03-18 stsp
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
837 68999b92 2020-03-18 stsp
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
842 b2409d58 2020-03-18 stsp }
843 b2409d58 2020-03-18 stsp
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
852 892ac3b6 2020-03-18 stsp }
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
859 892ac3b6 2020-03-18 stsp }
860 61cc1a7a 2020-03-18 stsp }
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
869 892ac3b6 2020-03-18 stsp }
870 61cc1a7a 2020-03-18 stsp }
871 b2409d58 2020-03-18 stsp
872 d2cdc636 2020-03-18 stsp }
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
883 892ac3b6 2020-03-18 stsp
884 531c3985 2020-03-18 stsp return NULL;
885 531c3985 2020-03-18 stsp }
886 531c3985 2020-03-18 stsp
887 531c3985 2020-03-18 stsp static const struct got_error *
888 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
889 93658fb9 2020-03-18 stsp {
890 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
891 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
892 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
893 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
894 bb64b798 2020-03-18 stsp const char *repo_path;
895 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
896 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
897 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
898 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
899 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
900 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
901 b46f3e71 2020-03-18 stsp char *git_url = NULL;
902 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
903 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
904 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
905 b46f3e71 2020-03-18 stsp ssize_t n;
906 469dd726 2020-03-20 stsp int verbosity = 0, mirror_references = 0;
907 93658fb9 2020-03-18 stsp
908 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
909 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
910 d9b4d0c0 2020-03-18 stsp
911 469dd726 2020-03-20 stsp while ((ch = getopt(argc, argv, "mvq")) != -1) {
912 93658fb9 2020-03-18 stsp switch (ch) {
913 469dd726 2020-03-20 stsp case 'm':
914 469dd726 2020-03-20 stsp mirror_references = 1;
915 469dd726 2020-03-20 stsp break;
916 68999b92 2020-03-18 stsp case 'v':
917 68999b92 2020-03-18 stsp if (verbosity < 0)
918 68999b92 2020-03-18 stsp verbosity = 0;
919 68999b92 2020-03-18 stsp else if (verbosity < 3)
920 68999b92 2020-03-18 stsp verbosity++;
921 68999b92 2020-03-18 stsp break;
922 68999b92 2020-03-18 stsp case 'q':
923 68999b92 2020-03-18 stsp verbosity = -1;
924 68999b92 2020-03-18 stsp break;
925 93658fb9 2020-03-18 stsp default:
926 93658fb9 2020-03-18 stsp usage_clone();
927 93658fb9 2020-03-18 stsp break;
928 93658fb9 2020-03-18 stsp }
929 93658fb9 2020-03-18 stsp }
930 93658fb9 2020-03-18 stsp argc -= optind;
931 93658fb9 2020-03-18 stsp argv += optind;
932 39c64a6a 2020-03-18 stsp
933 93658fb9 2020-03-18 stsp uri = argv[0];
934 9df6f38b 2020-03-18 stsp
935 9df6f38b 2020-03-18 stsp if (argc == 1)
936 93658fb9 2020-03-18 stsp dirname = NULL;
937 9df6f38b 2020-03-18 stsp else if (argc == 2)
938 93658fb9 2020-03-18 stsp dirname = argv[1];
939 93658fb9 2020-03-18 stsp else
940 93658fb9 2020-03-18 stsp usage_clone();
941 09838ffc 2020-03-18 stsp
942 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
943 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
944 39c64a6a 2020-03-18 stsp if (error)
945 09838ffc 2020-03-18 stsp goto done;
946 09838ffc 2020-03-18 stsp
947 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
948 b46f3e71 2020-03-18 stsp #ifndef PROFILE
949 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
950 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
951 39c64a6a 2020-03-18 stsp err(1, "pledge");
952 b46f3e71 2020-03-18 stsp #endif
953 b46f3e71 2020-03-18 stsp git_url = strdup(argv[0]);
954 b46f3e71 2020-03-18 stsp if (git_url == NULL) {
955 b46f3e71 2020-03-18 stsp error = got_error_from_errno("strdup");
956 b46f3e71 2020-03-18 stsp goto done;
957 b46f3e71 2020-03-18 stsp }
958 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
959 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
960 b46f3e71 2020-03-18 stsp #ifndef PROFILE
961 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
962 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
963 39c64a6a 2020-03-18 stsp err(1, "pledge");
964 b46f3e71 2020-03-18 stsp #endif
965 b46f3e71 2020-03-18 stsp if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
966 b46f3e71 2020-03-18 stsp error = got_error_from_errno("asprintf");
967 b46f3e71 2020-03-18 stsp goto done;
968 b46f3e71 2020-03-18 stsp }
969 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
970 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
971 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
972 39c64a6a 2020-03-18 stsp goto done;
973 39c64a6a 2020-03-18 stsp } else {
974 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
975 39c64a6a 2020-03-18 stsp goto done;
976 39c64a6a 2020-03-18 stsp }
977 bb64b798 2020-03-18 stsp if (dirname == NULL) {
978 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
979 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
980 bb64b798 2020-03-18 stsp goto done;
981 bb64b798 2020-03-18 stsp }
982 bb64b798 2020-03-18 stsp repo_path = default_destdir;
983 bb64b798 2020-03-18 stsp } else
984 bb64b798 2020-03-18 stsp repo_path = dirname;
985 bb64b798 2020-03-18 stsp
986 39c64a6a 2020-03-18 stsp error = got_path_mkdir(repo_path);
987 39c64a6a 2020-03-18 stsp if (error)
988 bb64b798 2020-03-18 stsp goto done;
989 bb64b798 2020-03-18 stsp
990 39c64a6a 2020-03-18 stsp error = got_repo_init(repo_path);
991 39c64a6a 2020-03-18 stsp if (error)
992 bb64b798 2020-03-18 stsp goto done;
993 bb64b798 2020-03-18 stsp
994 39c64a6a 2020-03-18 stsp error = got_repo_open(&repo, repo_path, NULL);
995 39c64a6a 2020-03-18 stsp if (error)
996 294dfefd 2020-03-18 stsp goto done;
997 294dfefd 2020-03-18 stsp
998 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
999 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1000 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1001 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1002 ee448f5f 2020-03-18 stsp goto done;
1003 ee448f5f 2020-03-18 stsp }
1004 ee448f5f 2020-03-18 stsp }
1005 ee448f5f 2020-03-18 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1006 ee448f5f 2020-03-18 stsp if (error)
1007 ee448f5f 2020-03-18 stsp goto done;
1008 ee448f5f 2020-03-18 stsp
1009 68999b92 2020-03-18 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1010 68999b92 2020-03-18 stsp verbosity);
1011 39c64a6a 2020-03-18 stsp if (error)
1012 bb64b798 2020-03-18 stsp goto done;
1013 294dfefd 2020-03-18 stsp
1014 68999b92 2020-03-18 stsp if (verbosity >= 0)
1015 68999b92 2020-03-18 stsp printf("Connected to %s:%s\n", host, port);
1016 4aa547db 2020-03-19 stsp
1017 4aa547db 2020-03-19 stsp /* Create a config file git-fetch(1) can understand. */
1018 4aa547db 2020-03-19 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1019 4aa547db 2020-03-19 stsp if (gitconfig_path == NULL) {
1020 4aa547db 2020-03-19 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1021 4aa547db 2020-03-19 stsp goto done;
1022 4aa547db 2020-03-19 stsp }
1023 4aa547db 2020-03-19 stsp gitconfig_file = fopen(gitconfig_path, "a");
1024 4aa547db 2020-03-19 stsp if (gitconfig_file == NULL) {
1025 4aa547db 2020-03-19 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1026 4aa547db 2020-03-19 stsp goto done;
1027 4aa547db 2020-03-19 stsp }
1028 469dd726 2020-03-20 stsp if (mirror_references) {
1029 469dd726 2020-03-20 stsp if (asprintf(&gitconfig,
1030 469dd726 2020-03-20 stsp "[remote \"%s\"]\n"
1031 469dd726 2020-03-20 stsp "\turl = %s\n"
1032 469dd726 2020-03-20 stsp "\tmirror = true\n",
1033 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1034 469dd726 2020-03-20 stsp error = got_error_from_errno("asprintf");
1035 469dd726 2020-03-20 stsp goto done;
1036 469dd726 2020-03-20 stsp }
1037 469dd726 2020-03-20 stsp } else {
1038 469dd726 2020-03-20 stsp if (asprintf(&gitconfig,
1039 469dd726 2020-03-20 stsp "[remote \"%s\"]\n"
1040 469dd726 2020-03-20 stsp "\turl = %s\n"
1041 469dd726 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1042 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1043 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1044 469dd726 2020-03-20 stsp error = got_error_from_errno("asprintf");
1045 469dd726 2020-03-20 stsp goto done;
1046 469dd726 2020-03-20 stsp }
1047 4aa547db 2020-03-19 stsp }
1048 4aa547db 2020-03-19 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1049 4aa547db 2020-03-19 stsp if (n != strlen(gitconfig)) {
1050 4aa547db 2020-03-19 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1051 4aa547db 2020-03-19 stsp goto done;
1052 4aa547db 2020-03-19 stsp }
1053 bb64b798 2020-03-18 stsp
1054 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1055 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1056 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1057 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1058 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1059 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1060 469dd726 2020-03-20 stsp fetchfd, repo, fetch_progress, &fpa);
1061 39c64a6a 2020-03-18 stsp if (error)
1062 d9b4d0c0 2020-03-18 stsp goto done;
1063 d9b4d0c0 2020-03-18 stsp
1064 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1065 39c64a6a 2020-03-18 stsp if (error)
1066 d9b4d0c0 2020-03-18 stsp goto done;
1067 68999b92 2020-03-18 stsp if (verbosity >= 0)
1068 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1069 d9b4d0c0 2020-03-18 stsp free(id_str);
1070 d9b4d0c0 2020-03-18 stsp
1071 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1072 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1073 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1074 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1075 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1076 7ebc0570 2020-03-18 stsp char *remote_refname;
1077 668a20f6 2020-03-18 stsp
1078 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1079 39c64a6a 2020-03-18 stsp if (error)
1080 d9b4d0c0 2020-03-18 stsp goto done;
1081 7ebc0570 2020-03-18 stsp error = got_ref_write(ref, repo);
1082 7ebc0570 2020-03-18 stsp got_ref_close(ref);
1083 7ebc0570 2020-03-18 stsp if (error)
1084 7ebc0570 2020-03-18 stsp goto done;
1085 d9b4d0c0 2020-03-18 stsp
1086 469dd726 2020-03-20 stsp if (mirror_references)
1087 469dd726 2020-03-20 stsp continue;
1088 469dd726 2020-03-20 stsp
1089 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1090 7ebc0570 2020-03-18 stsp continue;
1091 7ebc0570 2020-03-18 stsp
1092 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1093 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1094 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1095 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1096 7ebc0570 2020-03-18 stsp goto done;
1097 7ebc0570 2020-03-18 stsp }
1098 7ebc0570 2020-03-18 stsp error = got_ref_alloc(&ref, remote_refname, id);
1099 39c64a6a 2020-03-18 stsp if (error)
1100 d9b4d0c0 2020-03-18 stsp goto done;
1101 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1102 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1103 39c64a6a 2020-03-18 stsp if (error)
1104 d9b4d0c0 2020-03-18 stsp goto done;
1105 d9b4d0c0 2020-03-18 stsp }
1106 d9b4d0c0 2020-03-18 stsp
1107 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1108 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1109 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1110 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1111 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1112 d9b4d0c0 2020-03-18 stsp
1113 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1114 d9b4d0c0 2020-03-18 stsp continue;
1115 d9b4d0c0 2020-03-18 stsp
1116 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1117 39c64a6a 2020-03-18 stsp if (error) {
1118 39c64a6a 2020-03-18 stsp if (error->code == GOT_ERR_NOT_REF)
1119 d9b4d0c0 2020-03-18 stsp continue;
1120 d9b4d0c0 2020-03-18 stsp goto done;
1121 d9b4d0c0 2020-03-18 stsp }
1122 d9b4d0c0 2020-03-18 stsp
1123 39c64a6a 2020-03-18 stsp error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1124 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1125 39c64a6a 2020-03-18 stsp if (error)
1126 d9b4d0c0 2020-03-18 stsp goto done;
1127 d9b4d0c0 2020-03-18 stsp
1128 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1129 68999b92 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1130 68999b92 2020-03-18 stsp got_ref_get_symref_target(symref));
1131 d9b4d0c0 2020-03-18 stsp
1132 39c64a6a 2020-03-18 stsp error = got_ref_write(symref, repo);
1133 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1134 d9b4d0c0 2020-03-18 stsp break;
1135 b46f3e71 2020-03-18 stsp }
1136 d715f13e 2020-03-19 stsp
1137 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1138 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1139 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1140 09838ffc 2020-03-18 stsp done:
1141 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1142 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1143 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1144 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1145 bb64b798 2020-03-18 stsp if (repo)
1146 bb64b798 2020-03-18 stsp got_repo_close(repo);
1147 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1148 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1149 d9b4d0c0 2020-03-18 stsp free(pe->data);
1150 d9b4d0c0 2020-03-18 stsp }
1151 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1152 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1153 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1154 d9b4d0c0 2020-03-18 stsp free(pe->data);
1155 d9b4d0c0 2020-03-18 stsp }
1156 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1157 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1158 09838ffc 2020-03-18 stsp free(proto);
1159 09838ffc 2020-03-18 stsp free(host);
1160 09838ffc 2020-03-18 stsp free(port);
1161 09838ffc 2020-03-18 stsp free(server_path);
1162 09838ffc 2020-03-18 stsp free(repo_name);
1163 bb64b798 2020-03-18 stsp free(default_destdir);
1164 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1165 b46f3e71 2020-03-18 stsp free(git_url);
1166 39c64a6a 2020-03-18 stsp return error;
1167 7848a0e1 2020-03-19 stsp }
1168 7848a0e1 2020-03-19 stsp
1169 7848a0e1 2020-03-19 stsp static const struct got_error *
1170 7848a0e1 2020-03-19 stsp create_ref(const char *refname, struct got_object_id *id,
1171 7848a0e1 2020-03-19 stsp const char *id_str, struct got_repository *repo)
1172 7848a0e1 2020-03-19 stsp {
1173 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1174 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1175 7848a0e1 2020-03-19 stsp
1176 7848a0e1 2020-03-19 stsp printf("Creating %s: %s\n", refname, id_str);
1177 7848a0e1 2020-03-19 stsp
1178 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&ref, refname, id);
1179 7848a0e1 2020-03-19 stsp if (err)
1180 7848a0e1 2020-03-19 stsp return err;
1181 7848a0e1 2020-03-19 stsp
1182 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1183 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1184 7848a0e1 2020-03-19 stsp return err;
1185 7848a0e1 2020-03-19 stsp }
1186 7848a0e1 2020-03-19 stsp
1187 7848a0e1 2020-03-19 stsp static const struct got_error *
1188 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1189 7848a0e1 2020-03-19 stsp struct got_repository *repo)
1190 7848a0e1 2020-03-19 stsp {
1191 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1192 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1193 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1194 7848a0e1 2020-03-19 stsp
1195 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1196 7848a0e1 2020-03-19 stsp if (err)
1197 7848a0e1 2020-03-19 stsp goto done;
1198 7848a0e1 2020-03-19 stsp
1199 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1200 7848a0e1 2020-03-19 stsp struct got_reference *new_ref;
1201 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1202 7848a0e1 2020-03-19 stsp if (err)
1203 7848a0e1 2020-03-19 stsp goto done;
1204 7848a0e1 2020-03-19 stsp printf("Deleting symbolic reference %s -> %s\n",
1205 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), got_ref_get_symref_target(ref));
1206 7848a0e1 2020-03-19 stsp err = got_ref_delete(ref, repo);
1207 7848a0e1 2020-03-19 stsp if (err)
1208 7848a0e1 2020-03-19 stsp goto done;
1209 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n", got_ref_get_name(ref),
1210 7848a0e1 2020-03-19 stsp new_id_str);
1211 7848a0e1 2020-03-19 stsp err = got_ref_write(new_ref, repo);
1212 7848a0e1 2020-03-19 stsp if (err)
1213 7848a0e1 2020-03-19 stsp goto done;
1214 7848a0e1 2020-03-19 stsp } else {
1215 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1216 7848a0e1 2020-03-19 stsp if (err)
1217 7848a0e1 2020-03-19 stsp goto done;
1218 7848a0e1 2020-03-19 stsp if (got_object_id_cmp(old_id, new_id) != 0) {
1219 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n",
1220 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), new_id_str);
1221 7848a0e1 2020-03-19 stsp err = got_ref_change_ref(ref, new_id);
1222 7848a0e1 2020-03-19 stsp if (err)
1223 7848a0e1 2020-03-19 stsp goto done;
1224 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1225 7848a0e1 2020-03-19 stsp if (err)
1226 7848a0e1 2020-03-19 stsp goto done;
1227 7848a0e1 2020-03-19 stsp }
1228 7848a0e1 2020-03-19 stsp }
1229 7848a0e1 2020-03-19 stsp done:
1230 7848a0e1 2020-03-19 stsp free(old_id);
1231 7848a0e1 2020-03-19 stsp free(new_id_str);
1232 7848a0e1 2020-03-19 stsp return err;
1233 2ab43947 2020-03-18 stsp }
1234 2ab43947 2020-03-18 stsp
1235 2ab43947 2020-03-18 stsp __dead static void
1236 7848a0e1 2020-03-19 stsp usage_fetch(void)
1237 7848a0e1 2020-03-19 stsp {
1238 7848a0e1 2020-03-19 stsp fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1239 7848a0e1 2020-03-19 stsp "[remote-repository-name]\n", getprogname());
1240 7848a0e1 2020-03-19 stsp exit(1);
1241 7848a0e1 2020-03-19 stsp }
1242 7848a0e1 2020-03-19 stsp
1243 7848a0e1 2020-03-19 stsp static const struct got_error *
1244 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1245 7848a0e1 2020-03-19 stsp {
1246 7848a0e1 2020-03-19 stsp const struct got_error *error = NULL;
1247 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1248 7848a0e1 2020-03-19 stsp const char *remote_name;
1249 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1250 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1251 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1252 7848a0e1 2020-03-19 stsp int nremotes;
1253 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1254 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1255 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1256 7848a0e1 2020-03-19 stsp struct got_pathlist_head refs, symrefs;
1257 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1258 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1259 7848a0e1 2020-03-19 stsp int i, ch, fetchfd = -1;
1260 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1261 7848a0e1 2020-03-19 stsp int verbosity = 0;
1262 7848a0e1 2020-03-19 stsp
1263 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1264 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1265 7848a0e1 2020-03-19 stsp
1266 7848a0e1 2020-03-19 stsp while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1267 7848a0e1 2020-03-19 stsp switch (ch) {
1268 7848a0e1 2020-03-19 stsp case 'r':
1269 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1270 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1271 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1272 7848a0e1 2020-03-19 stsp optarg);
1273 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1274 7848a0e1 2020-03-19 stsp break;
1275 7848a0e1 2020-03-19 stsp case 'v':
1276 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1277 7848a0e1 2020-03-19 stsp verbosity = 0;
1278 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1279 7848a0e1 2020-03-19 stsp verbosity++;
1280 7848a0e1 2020-03-19 stsp break;
1281 7848a0e1 2020-03-19 stsp case 'q':
1282 7848a0e1 2020-03-19 stsp verbosity = -1;
1283 7848a0e1 2020-03-19 stsp break;
1284 7848a0e1 2020-03-19 stsp default:
1285 7848a0e1 2020-03-19 stsp usage_fetch();
1286 7848a0e1 2020-03-19 stsp break;
1287 7848a0e1 2020-03-19 stsp }
1288 7848a0e1 2020-03-19 stsp }
1289 7848a0e1 2020-03-19 stsp argc -= optind;
1290 7848a0e1 2020-03-19 stsp argv += optind;
1291 7848a0e1 2020-03-19 stsp
1292 7848a0e1 2020-03-19 stsp if (argc == 0)
1293 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1294 7848a0e1 2020-03-19 stsp else if (argc == 1)
1295 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1296 7848a0e1 2020-03-19 stsp else
1297 7848a0e1 2020-03-19 stsp usage_fetch();
1298 7848a0e1 2020-03-19 stsp
1299 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1300 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1301 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1302 7848a0e1 2020-03-19 stsp goto done;
1303 7848a0e1 2020-03-19 stsp }
1304 7848a0e1 2020-03-19 stsp
1305 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1306 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1307 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1308 7848a0e1 2020-03-19 stsp goto done;
1309 7848a0e1 2020-03-19 stsp else
1310 7848a0e1 2020-03-19 stsp error = NULL;
1311 7848a0e1 2020-03-19 stsp if (worktree) {
1312 7848a0e1 2020-03-19 stsp repo_path =
1313 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1314 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1315 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1316 7848a0e1 2020-03-19 stsp if (error)
1317 7848a0e1 2020-03-19 stsp goto done;
1318 7848a0e1 2020-03-19 stsp } else {
1319 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1320 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1321 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1322 7848a0e1 2020-03-19 stsp goto done;
1323 7848a0e1 2020-03-19 stsp }
1324 7848a0e1 2020-03-19 stsp }
1325 7848a0e1 2020-03-19 stsp }
1326 7848a0e1 2020-03-19 stsp
1327 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1328 7848a0e1 2020-03-19 stsp if (error)
1329 7848a0e1 2020-03-19 stsp goto done;
1330 7848a0e1 2020-03-19 stsp
1331 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1332 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1333 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1334 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1335 7848a0e1 2020-03-19 stsp break;
1336 7848a0e1 2020-03-19 stsp }
1337 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1338 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1339 7848a0e1 2020-03-19 stsp goto done;
1340 7848a0e1 2020-03-19 stsp }
1341 7848a0e1 2020-03-19 stsp
1342 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1343 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1344 7848a0e1 2020-03-19 stsp if (error)
1345 7848a0e1 2020-03-19 stsp goto done;
1346 7848a0e1 2020-03-19 stsp
1347 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 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 dns inet 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, "git+ssh") == 0 ||
1354 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1355 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1356 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1357 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1358 7848a0e1 2020-03-19 stsp err(1, "pledge");
1359 7848a0e1 2020-03-19 stsp #endif
1360 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1361 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1362 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1363 7848a0e1 2020-03-19 stsp goto done;
1364 7848a0e1 2020-03-19 stsp } else {
1365 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
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 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1370 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1371 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1372 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1373 7848a0e1 2020-03-19 stsp goto done;
1374 7848a0e1 2020-03-19 stsp }
1375 7848a0e1 2020-03-19 stsp }
1376 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1377 7848a0e1 2020-03-19 stsp if (error)
1378 7848a0e1 2020-03-19 stsp goto done;
1379 7848a0e1 2020-03-19 stsp
1380 7848a0e1 2020-03-19 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1381 7848a0e1 2020-03-19 stsp verbosity);
1382 7848a0e1 2020-03-19 stsp if (error)
1383 7848a0e1 2020-03-19 stsp goto done;
1384 7848a0e1 2020-03-19 stsp
1385 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1386 7848a0e1 2020-03-19 stsp printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1387 7848a0e1 2020-03-19 stsp
1388 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1389 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1390 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1391 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1392 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1393 469dd726 2020-03-20 stsp remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
1394 7848a0e1 2020-03-19 stsp if (error)
1395 7848a0e1 2020-03-19 stsp goto done;
1396 7848a0e1 2020-03-19 stsp
1397 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1398 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1399 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1400 7848a0e1 2020-03-19 stsp goto done;
1401 7848a0e1 2020-03-19 stsp }
1402 7848a0e1 2020-03-19 stsp
1403 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1404 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1405 984065c8 2020-03-19 stsp if (error)
1406 984065c8 2020-03-19 stsp goto done;
1407 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1408 984065c8 2020-03-19 stsp free(id_str);
1409 984065c8 2020-03-19 stsp id_str = NULL;
1410 984065c8 2020-03-19 stsp }
1411 7848a0e1 2020-03-19 stsp
1412 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1413 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1414 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1415 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1416 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1417 7848a0e1 2020-03-19 stsp char *remote_refname;
1418 7848a0e1 2020-03-19 stsp
1419 7848a0e1 2020-03-19 stsp error = got_object_id_str(&id_str, id);
1420 7848a0e1 2020-03-19 stsp if (error)
1421 7848a0e1 2020-03-19 stsp goto done;
1422 7848a0e1 2020-03-19 stsp
1423 7848a0e1 2020-03-19 stsp if (strncmp("refs/tags/", refname, 10) == 0) {
1424 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, refname, 0);
1425 7848a0e1 2020-03-19 stsp if (error) {
1426 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1427 7848a0e1 2020-03-19 stsp goto done;
1428 7848a0e1 2020-03-19 stsp error = create_ref(refname, id, id_str, repo);
1429 7848a0e1 2020-03-19 stsp if (error)
1430 7848a0e1 2020-03-19 stsp goto done;
1431 7848a0e1 2020-03-19 stsp } else {
1432 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1433 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1434 7848a0e1 2020-03-19 stsp if (error)
1435 7848a0e1 2020-03-19 stsp goto done;
1436 7848a0e1 2020-03-19 stsp }
1437 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1438 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1439 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1440 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1441 7848a0e1 2020-03-19 stsp goto done;
1442 7848a0e1 2020-03-19 stsp }
1443 7848a0e1 2020-03-19 stsp
1444 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, remote_refname, 0);
1445 7848a0e1 2020-03-19 stsp if (error) {
1446 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1447 7848a0e1 2020-03-19 stsp goto done;
1448 e5083482 2020-03-20 stsp error = create_ref(remote_refname, id, id_str,
1449 e5083482 2020-03-20 stsp repo);
1450 7848a0e1 2020-03-19 stsp if (error)
1451 7848a0e1 2020-03-19 stsp goto done;
1452 7848a0e1 2020-03-19 stsp } else {
1453 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1454 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1455 7848a0e1 2020-03-19 stsp if (error)
1456 7848a0e1 2020-03-19 stsp goto done;
1457 7848a0e1 2020-03-19 stsp }
1458 7848a0e1 2020-03-19 stsp }
1459 7848a0e1 2020-03-19 stsp free(id_str);
1460 7848a0e1 2020-03-19 stsp id_str = NULL;
1461 7848a0e1 2020-03-19 stsp }
1462 7848a0e1 2020-03-19 stsp done:
1463 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1464 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1465 7848a0e1 2020-03-19 stsp if (repo)
1466 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1467 7848a0e1 2020-03-19 stsp if (worktree)
1468 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1469 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1470 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1471 7848a0e1 2020-03-19 stsp free(pe->data);
1472 7848a0e1 2020-03-19 stsp }
1473 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1474 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1475 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1476 7848a0e1 2020-03-19 stsp free(pe->data);
1477 7848a0e1 2020-03-19 stsp }
1478 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1479 7848a0e1 2020-03-19 stsp free(id_str);
1480 7848a0e1 2020-03-19 stsp free(cwd);
1481 7848a0e1 2020-03-19 stsp free(repo_path);
1482 7848a0e1 2020-03-19 stsp free(pack_hash);
1483 7848a0e1 2020-03-19 stsp free(proto);
1484 7848a0e1 2020-03-19 stsp free(host);
1485 7848a0e1 2020-03-19 stsp free(port);
1486 7848a0e1 2020-03-19 stsp free(server_path);
1487 7848a0e1 2020-03-19 stsp free(repo_name);
1488 7848a0e1 2020-03-19 stsp return error;
1489 7848a0e1 2020-03-19 stsp }
1490 7848a0e1 2020-03-19 stsp
1491 7848a0e1 2020-03-19 stsp
1492 7848a0e1 2020-03-19 stsp __dead static void
1493 2ab43947 2020-03-18 stsp usage_checkout(void)
1494 2ab43947 2020-03-18 stsp {
1495 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1496 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1497 2ab43947 2020-03-18 stsp exit(1);
1498 2ab43947 2020-03-18 stsp }
1499 2ab43947 2020-03-18 stsp
1500 2ab43947 2020-03-18 stsp static void
1501 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1502 2ab43947 2020-03-18 stsp {
1503 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1504 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1505 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1506 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1507 2ab43947 2020-03-18 stsp getprogname());
1508 2ab43947 2020-03-18 stsp }
1509 2ab43947 2020-03-18 stsp
1510 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1511 2ab43947 2020-03-18 stsp const char *worktree_path;
1512 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1513 2ab43947 2020-03-18 stsp };
1514 2ab43947 2020-03-18 stsp
1515 2ab43947 2020-03-18 stsp static const struct got_error *
1516 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1517 2ab43947 2020-03-18 stsp {
1518 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1519 2ab43947 2020-03-18 stsp
1520 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1521 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1522 2ab43947 2020-03-18 stsp return NULL;
1523 2ab43947 2020-03-18 stsp
1524 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1525 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1526 2ab43947 2020-03-18 stsp return NULL;
1527 2ab43947 2020-03-18 stsp }
1528 2ab43947 2020-03-18 stsp
1529 2ab43947 2020-03-18 stsp while (path[0] == '/')
1530 2ab43947 2020-03-18 stsp path++;
1531 2ab43947 2020-03-18 stsp
1532 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1533 2ab43947 2020-03-18 stsp return NULL;
1534 2ab43947 2020-03-18 stsp }
1535 2ab43947 2020-03-18 stsp
1536 2ab43947 2020-03-18 stsp static const struct got_error *
1537 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1538 2ab43947 2020-03-18 stsp {
1539 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1540 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1541 2ab43947 2020-03-18 stsp return NULL;
1542 2ab43947 2020-03-18 stsp }
1543 2ab43947 2020-03-18 stsp
1544 2ab43947 2020-03-18 stsp static const struct got_error *
1545 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1546 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1547 2ab43947 2020-03-18 stsp struct got_repository *repo)
1548 2ab43947 2020-03-18 stsp {
1549 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1550 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1551 2ab43947 2020-03-18 stsp
1552 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1553 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1554 2ab43947 2020-03-18 stsp if (err)
1555 2ab43947 2020-03-18 stsp return err;
1556 2ab43947 2020-03-18 stsp
1557 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1558 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1559 2ab43947 2020-03-18 stsp
1560 2ab43947 2020-03-18 stsp /*
1561 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1562 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1563 2ab43947 2020-03-18 stsp *
1564 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1565 2ab43947 2020-03-18 stsp *
1566 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1567 2ab43947 2020-03-18 stsp * \ /
1568 2ab43947 2020-03-18 stsp * C E
1569 2ab43947 2020-03-18 stsp * \ /
1570 2ab43947 2020-03-18 stsp * B (yca)
1571 2ab43947 2020-03-18 stsp * |
1572 2ab43947 2020-03-18 stsp * A
1573 2ab43947 2020-03-18 stsp *
1574 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1575 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1576 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1577 2ab43947 2020-03-18 stsp */
1578 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1579 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1580 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1581 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1582 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1583 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1584 2ab43947 2020-03-18 stsp
1585 2ab43947 2020-03-18 stsp free(yca_id);
1586 2ab43947 2020-03-18 stsp return NULL;
1587 2ab43947 2020-03-18 stsp }
1588 2ab43947 2020-03-18 stsp
1589 2ab43947 2020-03-18 stsp static const struct got_error *
1590 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1591 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1592 2ab43947 2020-03-18 stsp struct got_repository *repo)
1593 2ab43947 2020-03-18 stsp {
1594 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1595 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1596 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1597 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1598 2ab43947 2020-03-18 stsp
1599 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1600 2ab43947 2020-03-18 stsp if (err)
1601 2ab43947 2020-03-18 stsp goto done;
1602 2ab43947 2020-03-18 stsp
1603 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1604 2ab43947 2020-03-18 stsp is_same_branch = 1;
1605 2ab43947 2020-03-18 stsp goto done;
1606 2ab43947 2020-03-18 stsp }
1607 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1608 2ab43947 2020-03-18 stsp is_same_branch = 1;
1609 2ab43947 2020-03-18 stsp goto done;
1610 2ab43947 2020-03-18 stsp }
1611 2ab43947 2020-03-18 stsp
1612 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1613 2ab43947 2020-03-18 stsp if (err)
1614 2ab43947 2020-03-18 stsp goto done;
1615 2ab43947 2020-03-18 stsp
1616 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1617 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1618 2ab43947 2020-03-18 stsp if (err)
1619 2ab43947 2020-03-18 stsp goto done;
1620 2ab43947 2020-03-18 stsp
1621 2ab43947 2020-03-18 stsp for (;;) {
1622 2ab43947 2020-03-18 stsp struct got_object_id *id;
1623 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1624 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1625 2ab43947 2020-03-18 stsp if (err) {
1626 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1627 2ab43947 2020-03-18 stsp err = NULL;
1628 2ab43947 2020-03-18 stsp break;
1629 2ab43947 2020-03-18 stsp }
1630 2ab43947 2020-03-18 stsp
1631 2ab43947 2020-03-18 stsp if (id) {
1632 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1633 2ab43947 2020-03-18 stsp break;
1634 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1635 2ab43947 2020-03-18 stsp is_same_branch = 1;
1636 2ab43947 2020-03-18 stsp break;
1637 2ab43947 2020-03-18 stsp }
1638 2ab43947 2020-03-18 stsp }
1639 2ab43947 2020-03-18 stsp }
1640 2ab43947 2020-03-18 stsp done:
1641 2ab43947 2020-03-18 stsp if (graph)
1642 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1643 2ab43947 2020-03-18 stsp free(head_commit_id);
1644 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1645 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1646 2ab43947 2020-03-18 stsp return err;
1647 a367ff0f 2019-05-14 stsp }
1648 8069f636 2019-01-12 stsp
1649 4ed7e80c 2018-05-20 stsp static const struct got_error *
1650 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1651 4b6c9460 2020-03-05 stsp {
1652 4b6c9460 2020-03-05 stsp static char msg[512];
1653 4b6c9460 2020-03-05 stsp const char *branch_name;
1654 4b6c9460 2020-03-05 stsp
1655 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1656 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1657 4b6c9460 2020-03-05 stsp else
1658 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1659 4b6c9460 2020-03-05 stsp
1660 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1661 4b6c9460 2020-03-05 stsp branch_name += 11;
1662 4b6c9460 2020-03-05 stsp
1663 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1664 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1665 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1666 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1667 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1668 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1669 4b6c9460 2020-03-05 stsp
1670 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1671 4b6c9460 2020-03-05 stsp }
1672 4b6c9460 2020-03-05 stsp
1673 4b6c9460 2020-03-05 stsp static const struct got_error *
1674 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1675 c09a553d 2018-03-12 stsp {
1676 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1677 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1678 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1679 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1680 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1681 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1682 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1683 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1684 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1685 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1686 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1687 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1688 f2ea84fa 2019-07-27 stsp
1689 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1690 c09a553d 2018-03-12 stsp
1691 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1692 0bb8a95e 2018-03-12 stsp switch (ch) {
1693 08573d5b 2019-05-14 stsp case 'b':
1694 08573d5b 2019-05-14 stsp branch_name = optarg;
1695 08573d5b 2019-05-14 stsp break;
1696 8069f636 2019-01-12 stsp case 'c':
1697 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1698 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1699 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1700 bb51a5b4 2020-01-13 stsp break;
1701 bb51a5b4 2020-01-13 stsp case 'E':
1702 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1703 8069f636 2019-01-12 stsp break;
1704 0bb8a95e 2018-03-12 stsp case 'p':
1705 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1706 0bb8a95e 2018-03-12 stsp break;
1707 0bb8a95e 2018-03-12 stsp default:
1708 2deda0b9 2019-03-07 stsp usage_checkout();
1709 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1710 0bb8a95e 2018-03-12 stsp }
1711 0bb8a95e 2018-03-12 stsp }
1712 0bb8a95e 2018-03-12 stsp
1713 0bb8a95e 2018-03-12 stsp argc -= optind;
1714 0bb8a95e 2018-03-12 stsp argv += optind;
1715 0bb8a95e 2018-03-12 stsp
1716 6715a751 2018-03-16 stsp #ifndef PROFILE
1717 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1718 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1719 c09a553d 2018-03-12 stsp err(1, "pledge");
1720 6715a751 2018-03-16 stsp #endif
1721 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1722 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1723 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1724 76089277 2018-04-01 stsp if (repo_path == NULL)
1725 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1726 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1727 76089277 2018-04-01 stsp if (cwd == NULL) {
1728 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1729 76089277 2018-04-01 stsp goto done;
1730 76089277 2018-04-01 stsp }
1731 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1732 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1733 230a42bd 2019-05-11 jcs if (base == NULL) {
1734 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1735 230a42bd 2019-05-11 jcs path_prefix);
1736 230a42bd 2019-05-11 jcs goto done;
1737 230a42bd 2019-05-11 jcs }
1738 230a42bd 2019-05-11 jcs } else {
1739 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1740 230a42bd 2019-05-11 jcs if (base == NULL) {
1741 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1742 230a42bd 2019-05-11 jcs repo_path);
1743 230a42bd 2019-05-11 jcs goto done;
1744 230a42bd 2019-05-11 jcs }
1745 76089277 2018-04-01 stsp }
1746 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1747 c09a553d 2018-03-12 stsp if (dotgit)
1748 c09a553d 2018-03-12 stsp *dotgit = '\0';
1749 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1750 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1751 c09a553d 2018-03-12 stsp free(cwd);
1752 76089277 2018-04-01 stsp goto done;
1753 c09a553d 2018-03-12 stsp }
1754 c09a553d 2018-03-12 stsp free(cwd);
1755 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1756 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1757 76089277 2018-04-01 stsp if (repo_path == NULL) {
1758 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1759 76089277 2018-04-01 stsp goto done;
1760 76089277 2018-04-01 stsp }
1761 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1762 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1763 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1764 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1765 b4b3a7dd 2019-07-22 stsp argv[1]);
1766 b4b3a7dd 2019-07-22 stsp goto done;
1767 b4b3a7dd 2019-07-22 stsp }
1768 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1769 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1770 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1771 b4b3a7dd 2019-07-22 stsp goto done;
1772 b4b3a7dd 2019-07-22 stsp }
1773 76089277 2018-04-01 stsp }
1774 c09a553d 2018-03-12 stsp } else
1775 c09a553d 2018-03-12 stsp usage_checkout();
1776 c09a553d 2018-03-12 stsp
1777 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1778 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1779 13bfb272 2019-05-10 jcs
1780 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1781 c09a553d 2018-03-12 stsp if (error != NULL)
1782 c02c541e 2019-03-29 stsp goto done;
1783 c02c541e 2019-03-29 stsp
1784 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1785 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1786 c530dc23 2019-07-23 stsp if (error) {
1787 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1788 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1789 c530dc23 2019-07-23 stsp goto done;
1790 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1791 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1792 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1793 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1794 c530dc23 2019-07-23 stsp goto done;
1795 c530dc23 2019-07-23 stsp }
1796 c530dc23 2019-07-23 stsp }
1797 c530dc23 2019-07-23 stsp
1798 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1799 c02c541e 2019-03-29 stsp if (error)
1800 c09a553d 2018-03-12 stsp goto done;
1801 8069f636 2019-01-12 stsp
1802 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1803 c09a553d 2018-03-12 stsp if (error != NULL)
1804 c09a553d 2018-03-12 stsp goto done;
1805 c09a553d 2018-03-12 stsp
1806 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1807 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1808 c09a553d 2018-03-12 stsp goto done;
1809 c09a553d 2018-03-12 stsp
1810 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1811 c09a553d 2018-03-12 stsp if (error != NULL)
1812 c09a553d 2018-03-12 stsp goto done;
1813 c09a553d 2018-03-12 stsp
1814 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1815 e5dc7198 2018-12-29 stsp path_prefix);
1816 e5dc7198 2018-12-29 stsp if (error != NULL)
1817 e5dc7198 2018-12-29 stsp goto done;
1818 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1819 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1820 49520a32 2018-12-29 stsp goto done;
1821 49520a32 2018-12-29 stsp }
1822 49520a32 2018-12-29 stsp
1823 8069f636 2019-01-12 stsp if (commit_id_str) {
1824 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1825 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1826 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1827 30837e32 2019-07-25 stsp if (error)
1828 8069f636 2019-01-12 stsp goto done;
1829 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1830 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1831 8069f636 2019-01-12 stsp if (error != NULL) {
1832 8069f636 2019-01-12 stsp free(commit_id);
1833 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1834 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1835 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1836 4b6c9460 2020-03-05 stsp }
1837 8069f636 2019-01-12 stsp goto done;
1838 8069f636 2019-01-12 stsp }
1839 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1840 4b6c9460 2020-03-05 stsp if (error) {
1841 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1842 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1843 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1844 4b6c9460 2020-03-05 stsp }
1845 45d344f6 2019-05-14 stsp goto done;
1846 4b6c9460 2020-03-05 stsp }
1847 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1848 8069f636 2019-01-12 stsp commit_id);
1849 8069f636 2019-01-12 stsp free(commit_id);
1850 8069f636 2019-01-12 stsp if (error)
1851 8069f636 2019-01-12 stsp goto done;
1852 8069f636 2019-01-12 stsp }
1853 8069f636 2019-01-12 stsp
1854 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1855 f2ea84fa 2019-07-27 stsp if (error)
1856 f2ea84fa 2019-07-27 stsp goto done;
1857 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1858 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1859 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1860 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1861 c09a553d 2018-03-12 stsp if (error != NULL)
1862 c09a553d 2018-03-12 stsp goto done;
1863 c09a553d 2018-03-12 stsp
1864 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1865 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1866 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1867 c09a553d 2018-03-12 stsp done:
1868 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1869 8069f636 2019-01-12 stsp free(commit_id_str);
1870 76089277 2018-04-01 stsp free(repo_path);
1871 507dc3bb 2018-12-29 stsp free(worktree_path);
1872 507dc3bb 2018-12-29 stsp return error;
1873 507dc3bb 2018-12-29 stsp }
1874 507dc3bb 2018-12-29 stsp
1875 507dc3bb 2018-12-29 stsp __dead static void
1876 507dc3bb 2018-12-29 stsp usage_update(void)
1877 507dc3bb 2018-12-29 stsp {
1878 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1879 507dc3bb 2018-12-29 stsp getprogname());
1880 507dc3bb 2018-12-29 stsp exit(1);
1881 507dc3bb 2018-12-29 stsp }
1882 507dc3bb 2018-12-29 stsp
1883 1ee397ad 2019-07-12 stsp static const struct got_error *
1884 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1885 507dc3bb 2018-12-29 stsp {
1886 784955db 2019-01-12 stsp int *did_something = arg;
1887 784955db 2019-01-12 stsp
1888 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1889 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1890 1ee397ad 2019-07-12 stsp return NULL;
1891 507dc3bb 2018-12-29 stsp
1892 1545c615 2019-02-10 stsp *did_something = 1;
1893 a484d721 2019-06-10 stsp
1894 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1895 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1896 1ee397ad 2019-07-12 stsp return NULL;
1897 a484d721 2019-06-10 stsp
1898 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1899 507dc3bb 2018-12-29 stsp path++;
1900 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1901 1ee397ad 2019-07-12 stsp return NULL;
1902 be7061eb 2018-12-30 stsp }
1903 be7061eb 2018-12-30 stsp
1904 be7061eb 2018-12-30 stsp static const struct got_error *
1905 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1906 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1907 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1908 a1fb16d8 2019-05-24 stsp {
1909 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1910 a1fb16d8 2019-05-24 stsp char *base_id_str;
1911 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1912 a1fb16d8 2019-05-24 stsp
1913 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1914 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1915 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1916 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1917 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1918 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1919 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1920 a1fb16d8 2019-05-24 stsp }
1921 a1fb16d8 2019-05-24 stsp
1922 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1923 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1924 a1fb16d8 2019-05-24 stsp if (err) {
1925 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1926 a1fb16d8 2019-05-24 stsp return err;
1927 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1928 a1fb16d8 2019-05-24 stsp }
1929 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1930 a1fb16d8 2019-05-24 stsp return NULL;
1931 a1fb16d8 2019-05-24 stsp
1932 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1933 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1934 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1935 a1fb16d8 2019-05-24 stsp if (err)
1936 a1fb16d8 2019-05-24 stsp return err;
1937 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1938 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1939 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1940 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1941 0ebf8283 2019-07-24 stsp return NULL;
1942 0ebf8283 2019-07-24 stsp }
1943 0ebf8283 2019-07-24 stsp
1944 0ebf8283 2019-07-24 stsp static const struct got_error *
1945 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1946 0ebf8283 2019-07-24 stsp {
1947 0ebf8283 2019-07-24 stsp const struct got_error *err;
1948 0ebf8283 2019-07-24 stsp int in_progress;
1949 0ebf8283 2019-07-24 stsp
1950 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1951 0ebf8283 2019-07-24 stsp if (err)
1952 0ebf8283 2019-07-24 stsp return err;
1953 0ebf8283 2019-07-24 stsp if (in_progress)
1954 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1955 0ebf8283 2019-07-24 stsp
1956 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1957 0ebf8283 2019-07-24 stsp if (err)
1958 0ebf8283 2019-07-24 stsp return err;
1959 0ebf8283 2019-07-24 stsp if (in_progress)
1960 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1961 0ebf8283 2019-07-24 stsp
1962 a1fb16d8 2019-05-24 stsp return NULL;
1963 a5edda0a 2019-07-27 stsp }
1964 a5edda0a 2019-07-27 stsp
1965 a5edda0a 2019-07-27 stsp static const struct got_error *
1966 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1967 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1968 a5edda0a 2019-07-27 stsp {
1969 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1970 a5edda0a 2019-07-27 stsp char *path;
1971 a5edda0a 2019-07-27 stsp int i;
1972 a5edda0a 2019-07-27 stsp
1973 a5edda0a 2019-07-27 stsp if (argc == 0) {
1974 a5edda0a 2019-07-27 stsp path = strdup("");
1975 a5edda0a 2019-07-27 stsp if (path == NULL)
1976 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1977 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1978 a5edda0a 2019-07-27 stsp }
1979 a5edda0a 2019-07-27 stsp
1980 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1981 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1982 a5edda0a 2019-07-27 stsp if (err)
1983 a5edda0a 2019-07-27 stsp break;
1984 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1985 a5edda0a 2019-07-27 stsp if (err) {
1986 a5edda0a 2019-07-27 stsp free(path);
1987 a5edda0a 2019-07-27 stsp break;
1988 a5edda0a 2019-07-27 stsp }
1989 a5edda0a 2019-07-27 stsp }
1990 a5edda0a 2019-07-27 stsp
1991 a5edda0a 2019-07-27 stsp return err;
1992 a1fb16d8 2019-05-24 stsp }
1993 a1fb16d8 2019-05-24 stsp
1994 a1fb16d8 2019-05-24 stsp static const struct got_error *
1995 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1996 507dc3bb 2018-12-29 stsp {
1997 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1998 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1999 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2000 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2001 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2002 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2003 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2004 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2005 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2006 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2007 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2008 507dc3bb 2018-12-29 stsp
2009 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2010 f2ea84fa 2019-07-27 stsp
2011 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2012 507dc3bb 2018-12-29 stsp switch (ch) {
2013 024e9686 2019-05-14 stsp case 'b':
2014 024e9686 2019-05-14 stsp branch_name = optarg;
2015 024e9686 2019-05-14 stsp break;
2016 507dc3bb 2018-12-29 stsp case 'c':
2017 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2018 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2019 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2020 507dc3bb 2018-12-29 stsp break;
2021 507dc3bb 2018-12-29 stsp default:
2022 2deda0b9 2019-03-07 stsp usage_update();
2023 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2024 507dc3bb 2018-12-29 stsp }
2025 507dc3bb 2018-12-29 stsp }
2026 507dc3bb 2018-12-29 stsp
2027 507dc3bb 2018-12-29 stsp argc -= optind;
2028 507dc3bb 2018-12-29 stsp argv += optind;
2029 507dc3bb 2018-12-29 stsp
2030 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2031 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2032 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2033 507dc3bb 2018-12-29 stsp err(1, "pledge");
2034 507dc3bb 2018-12-29 stsp #endif
2035 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2036 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2037 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2038 c4cdcb68 2019-04-03 stsp goto done;
2039 c4cdcb68 2019-04-03 stsp }
2040 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2041 7d5807f4 2019-07-11 stsp if (error)
2042 7d5807f4 2019-07-11 stsp goto done;
2043 7d5807f4 2019-07-11 stsp
2044 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2045 f2ea84fa 2019-07-27 stsp if (error)
2046 f2ea84fa 2019-07-27 stsp goto done;
2047 507dc3bb 2018-12-29 stsp
2048 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2049 c9956ddf 2019-09-08 stsp NULL);
2050 507dc3bb 2018-12-29 stsp if (error != NULL)
2051 507dc3bb 2018-12-29 stsp goto done;
2052 507dc3bb 2018-12-29 stsp
2053 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2054 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2055 087fb88c 2019-08-04 stsp if (error)
2056 087fb88c 2019-08-04 stsp goto done;
2057 087fb88c 2019-08-04 stsp
2058 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2059 0266afb7 2019-01-04 stsp if (error)
2060 0266afb7 2019-01-04 stsp goto done;
2061 0266afb7 2019-01-04 stsp
2062 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2063 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2064 024e9686 2019-05-14 stsp if (error != NULL)
2065 024e9686 2019-05-14 stsp goto done;
2066 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2067 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2068 9c4b8182 2019-01-02 stsp if (error != NULL)
2069 9c4b8182 2019-01-02 stsp goto done;
2070 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2071 507dc3bb 2018-12-29 stsp if (error != NULL)
2072 507dc3bb 2018-12-29 stsp goto done;
2073 507dc3bb 2018-12-29 stsp } else {
2074 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2075 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2076 04f57cb3 2019-07-25 stsp free(commit_id_str);
2077 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2078 30837e32 2019-07-25 stsp if (error)
2079 a297e751 2019-07-11 stsp goto done;
2080 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2081 a297e751 2019-07-11 stsp if (error)
2082 507dc3bb 2018-12-29 stsp goto done;
2083 507dc3bb 2018-12-29 stsp }
2084 35c965b2 2018-12-29 stsp
2085 a1fb16d8 2019-05-24 stsp if (branch_name) {
2086 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2087 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2088 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2089 f2ea84fa 2019-07-27 stsp continue;
2090 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2091 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2092 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2093 024e9686 2019-05-14 stsp goto done;
2094 024e9686 2019-05-14 stsp }
2095 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2096 024e9686 2019-05-14 stsp if (error)
2097 024e9686 2019-05-14 stsp goto done;
2098 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2099 3aef623b 2019-10-15 stsp repo);
2100 a367ff0f 2019-05-14 stsp free(head_commit_id);
2101 024e9686 2019-05-14 stsp if (error != NULL)
2102 024e9686 2019-05-14 stsp goto done;
2103 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2104 a367ff0f 2019-05-14 stsp if (error)
2105 a367ff0f 2019-05-14 stsp goto done;
2106 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2107 024e9686 2019-05-14 stsp if (error)
2108 024e9686 2019-05-14 stsp goto done;
2109 024e9686 2019-05-14 stsp } else {
2110 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2111 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2112 a367ff0f 2019-05-14 stsp if (error != NULL) {
2113 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2114 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2115 024e9686 2019-05-14 stsp goto done;
2116 a367ff0f 2019-05-14 stsp }
2117 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2118 a367ff0f 2019-05-14 stsp if (error)
2119 a367ff0f 2019-05-14 stsp goto done;
2120 024e9686 2019-05-14 stsp }
2121 507dc3bb 2018-12-29 stsp
2122 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2123 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2124 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2125 507dc3bb 2018-12-29 stsp commit_id);
2126 507dc3bb 2018-12-29 stsp if (error)
2127 507dc3bb 2018-12-29 stsp goto done;
2128 507dc3bb 2018-12-29 stsp }
2129 507dc3bb 2018-12-29 stsp
2130 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2131 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2132 507dc3bb 2018-12-29 stsp if (error != NULL)
2133 507dc3bb 2018-12-29 stsp goto done;
2134 9c4b8182 2019-01-02 stsp
2135 784955db 2019-01-12 stsp if (did_something)
2136 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2137 784955db 2019-01-12 stsp else
2138 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2139 507dc3bb 2018-12-29 stsp done:
2140 c09a553d 2018-03-12 stsp free(worktree_path);
2141 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2142 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2143 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2144 507dc3bb 2018-12-29 stsp free(commit_id);
2145 9c4b8182 2019-01-02 stsp free(commit_id_str);
2146 c09a553d 2018-03-12 stsp return error;
2147 c09a553d 2018-03-12 stsp }
2148 c09a553d 2018-03-12 stsp
2149 f42b1b34 2018-03-12 stsp static const struct got_error *
2150 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2151 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2152 63035f9f 2019-10-06 stsp struct got_repository *repo)
2153 5c860e29 2018-03-12 stsp {
2154 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2155 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2156 79109fed 2018-03-27 stsp
2157 44392932 2019-08-25 stsp if (blob_id1) {
2158 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2159 44392932 2019-08-25 stsp if (err)
2160 44392932 2019-08-25 stsp goto done;
2161 44392932 2019-08-25 stsp }
2162 79109fed 2018-03-27 stsp
2163 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2164 44392932 2019-08-25 stsp if (err)
2165 44392932 2019-08-25 stsp goto done;
2166 79109fed 2018-03-27 stsp
2167 44392932 2019-08-25 stsp while (path[0] == '/')
2168 44392932 2019-08-25 stsp path++;
2169 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2170 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2171 44392932 2019-08-25 stsp done:
2172 44392932 2019-08-25 stsp if (blob1)
2173 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2174 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2175 44392932 2019-08-25 stsp return err;
2176 44392932 2019-08-25 stsp }
2177 44392932 2019-08-25 stsp
2178 44392932 2019-08-25 stsp static const struct got_error *
2179 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2180 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2181 63035f9f 2019-10-06 stsp struct got_repository *repo)
2182 44392932 2019-08-25 stsp {
2183 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2184 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2185 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2186 44392932 2019-08-25 stsp
2187 44392932 2019-08-25 stsp if (tree_id1) {
2188 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2189 79109fed 2018-03-27 stsp if (err)
2190 44392932 2019-08-25 stsp goto done;
2191 79109fed 2018-03-27 stsp }
2192 79109fed 2018-03-27 stsp
2193 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2194 0f2b3dca 2018-12-22 stsp if (err)
2195 0f2b3dca 2018-12-22 stsp goto done;
2196 0f2b3dca 2018-12-22 stsp
2197 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2198 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2199 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2200 44392932 2019-08-25 stsp while (path[0] == '/')
2201 44392932 2019-08-25 stsp path++;
2202 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2203 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2204 0f2b3dca 2018-12-22 stsp done:
2205 79109fed 2018-03-27 stsp if (tree1)
2206 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2207 366e0a5f 2019-10-10 stsp if (tree2)
2208 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2209 44392932 2019-08-25 stsp return err;
2210 44392932 2019-08-25 stsp }
2211 44392932 2019-08-25 stsp
2212 44392932 2019-08-25 stsp static const struct got_error *
2213 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2214 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2215 44392932 2019-08-25 stsp {
2216 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2217 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2218 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2219 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2220 44392932 2019-08-25 stsp struct got_object_qid *qid;
2221 44392932 2019-08-25 stsp
2222 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2223 44392932 2019-08-25 stsp if (qid != NULL) {
2224 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2225 44392932 2019-08-25 stsp qid->id);
2226 44392932 2019-08-25 stsp if (err)
2227 44392932 2019-08-25 stsp return err;
2228 44392932 2019-08-25 stsp }
2229 44392932 2019-08-25 stsp
2230 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2231 44392932 2019-08-25 stsp int obj_type;
2232 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2233 44392932 2019-08-25 stsp if (err)
2234 44392932 2019-08-25 stsp goto done;
2235 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2236 44392932 2019-08-25 stsp if (err) {
2237 44392932 2019-08-25 stsp free(obj_id2);
2238 44392932 2019-08-25 stsp goto done;
2239 44392932 2019-08-25 stsp }
2240 44392932 2019-08-25 stsp if (pcommit) {
2241 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2242 44392932 2019-08-25 stsp qid->id, path);
2243 44392932 2019-08-25 stsp if (err) {
2244 44392932 2019-08-25 stsp free(obj_id2);
2245 44392932 2019-08-25 stsp goto done;
2246 44392932 2019-08-25 stsp }
2247 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2248 44392932 2019-08-25 stsp if (err) {
2249 44392932 2019-08-25 stsp free(obj_id2);
2250 44392932 2019-08-25 stsp goto done;
2251 44392932 2019-08-25 stsp }
2252 44392932 2019-08-25 stsp }
2253 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2254 44392932 2019-08-25 stsp if (err) {
2255 44392932 2019-08-25 stsp free(obj_id2);
2256 44392932 2019-08-25 stsp goto done;
2257 44392932 2019-08-25 stsp }
2258 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2259 44392932 2019-08-25 stsp switch (obj_type) {
2260 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2261 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2262 63035f9f 2019-10-06 stsp 0, repo);
2263 44392932 2019-08-25 stsp break;
2264 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2265 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2266 63035f9f 2019-10-06 stsp 0, repo);
2267 44392932 2019-08-25 stsp break;
2268 44392932 2019-08-25 stsp default:
2269 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2270 44392932 2019-08-25 stsp break;
2271 44392932 2019-08-25 stsp }
2272 44392932 2019-08-25 stsp free(obj_id1);
2273 44392932 2019-08-25 stsp free(obj_id2);
2274 44392932 2019-08-25 stsp } else {
2275 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2276 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2277 44392932 2019-08-25 stsp if (err)
2278 44392932 2019-08-25 stsp goto done;
2279 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2280 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2281 44392932 2019-08-25 stsp if (err)
2282 44392932 2019-08-25 stsp goto done;
2283 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2284 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2285 44392932 2019-08-25 stsp }
2286 44392932 2019-08-25 stsp done:
2287 0f2b3dca 2018-12-22 stsp free(id_str1);
2288 0f2b3dca 2018-12-22 stsp free(id_str2);
2289 44392932 2019-08-25 stsp if (pcommit)
2290 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2291 79109fed 2018-03-27 stsp return err;
2292 79109fed 2018-03-27 stsp }
2293 79109fed 2018-03-27 stsp
2294 4bb494d5 2018-06-16 stsp static char *
2295 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2296 6c281f94 2018-06-11 stsp {
2297 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2298 09867e48 2019-08-13 stsp char *p, *s;
2299 09867e48 2019-08-13 stsp
2300 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2301 09867e48 2019-08-13 stsp if (tm == NULL)
2302 09867e48 2019-08-13 stsp return NULL;
2303 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2304 09867e48 2019-08-13 stsp if (s == NULL)
2305 09867e48 2019-08-13 stsp return NULL;
2306 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2307 6c281f94 2018-06-11 stsp if (p)
2308 6c281f94 2018-06-11 stsp *p = '\0';
2309 6c281f94 2018-06-11 stsp return s;
2310 6c281f94 2018-06-11 stsp }
2311 dc424a06 2019-08-07 stsp
2312 6841bf13 2019-11-29 kn static const struct got_error *
2313 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2314 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2315 6841bf13 2019-11-29 kn {
2316 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2317 6841bf13 2019-11-29 kn regmatch_t regmatch;
2318 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2319 6841bf13 2019-11-29 kn
2320 6841bf13 2019-11-29 kn *have_match = 0;
2321 6841bf13 2019-11-29 kn
2322 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2323 6841bf13 2019-11-29 kn if (err)
2324 6841bf13 2019-11-29 kn return err;
2325 6841bf13 2019-11-29 kn
2326 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2327 6841bf13 2019-11-29 kn if (err)
2328 6841bf13 2019-11-29 kn goto done;
2329 6841bf13 2019-11-29 kn
2330 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2331 6841bf13 2019-11-29 kn *have_match = 1;
2332 6841bf13 2019-11-29 kn done:
2333 6841bf13 2019-11-29 kn free(id_str);
2334 6841bf13 2019-11-29 kn free(logmsg);
2335 6841bf13 2019-11-29 kn return err;
2336 6841bf13 2019-11-29 kn }
2337 6841bf13 2019-11-29 kn
2338 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2339 6c281f94 2018-06-11 stsp
2340 79109fed 2018-03-27 stsp static const struct got_error *
2341 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2342 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2343 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2344 79109fed 2018-03-27 stsp {
2345 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2346 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2347 6c281f94 2018-06-11 stsp char datebuf[26];
2348 45d799e2 2018-12-23 stsp time_t committer_time;
2349 45d799e2 2018-12-23 stsp const char *author, *committer;
2350 199a4027 2019-02-02 stsp char *refs_str = NULL;
2351 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2352 5c860e29 2018-03-12 stsp
2353 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2354 199a4027 2019-02-02 stsp char *s;
2355 199a4027 2019-02-02 stsp const char *name;
2356 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2357 a436ad14 2019-08-13 stsp int cmp;
2358 a436ad14 2019-08-13 stsp
2359 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2360 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2361 d9498b20 2019-02-05 stsp continue;
2362 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2363 199a4027 2019-02-02 stsp name += 5;
2364 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2365 7143d404 2019-03-12 stsp continue;
2366 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2367 e34f9ed6 2019-02-02 stsp name += 6;
2368 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2369 141c2bff 2019-02-04 stsp name += 8;
2370 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2371 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2372 5d844a1e 2019-08-13 stsp if (err) {
2373 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2374 5d844a1e 2019-08-13 stsp return err;
2375 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2376 5d844a1e 2019-08-13 stsp err = NULL;
2377 5d844a1e 2019-08-13 stsp tag = NULL;
2378 5d844a1e 2019-08-13 stsp }
2379 a436ad14 2019-08-13 stsp }
2380 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2381 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2382 a436ad14 2019-08-13 stsp if (tag)
2383 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2384 a436ad14 2019-08-13 stsp if (cmp != 0)
2385 a436ad14 2019-08-13 stsp continue;
2386 199a4027 2019-02-02 stsp s = refs_str;
2387 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2388 199a4027 2019-02-02 stsp name) == -1) {
2389 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2390 199a4027 2019-02-02 stsp free(s);
2391 34ca4898 2019-08-13 stsp return err;
2392 199a4027 2019-02-02 stsp }
2393 199a4027 2019-02-02 stsp free(s);
2394 199a4027 2019-02-02 stsp }
2395 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2396 8bf5b3c9 2018-03-17 stsp if (err)
2397 8bf5b3c9 2018-03-17 stsp return err;
2398 788c352e 2018-06-16 stsp
2399 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2400 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2401 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2402 832c249c 2018-06-10 stsp free(id_str);
2403 d3d493d7 2019-02-21 stsp id_str = NULL;
2404 d3d493d7 2019-02-21 stsp free(refs_str);
2405 d3d493d7 2019-02-21 stsp refs_str = NULL;
2406 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2407 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2408 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2409 09867e48 2019-08-13 stsp if (datestr)
2410 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2411 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2412 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2413 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2414 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2415 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2416 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2417 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2418 3fe1abad 2018-06-10 stsp int n = 1;
2419 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2420 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2421 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2422 a0603db2 2018-06-10 stsp if (err)
2423 a0603db2 2018-06-10 stsp return err;
2424 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2425 a0603db2 2018-06-10 stsp free(id_str);
2426 a0603db2 2018-06-10 stsp }
2427 a0603db2 2018-06-10 stsp }
2428 832c249c 2018-06-10 stsp
2429 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2430 5943eee2 2019-08-13 stsp if (err)
2431 5943eee2 2019-08-13 stsp return err;
2432 8bf5b3c9 2018-03-17 stsp
2433 621015ac 2018-07-23 stsp logmsg = logmsg0;
2434 832c249c 2018-06-10 stsp do {
2435 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2436 832c249c 2018-06-10 stsp if (line)
2437 832c249c 2018-06-10 stsp printf(" %s\n", line);
2438 832c249c 2018-06-10 stsp } while (line);
2439 621015ac 2018-07-23 stsp free(logmsg0);
2440 832c249c 2018-06-10 stsp
2441 971751ac 2018-03-27 stsp if (show_patch) {
2442 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2443 971751ac 2018-03-27 stsp if (err == 0)
2444 971751ac 2018-03-27 stsp printf("\n");
2445 971751ac 2018-03-27 stsp }
2446 07862c20 2018-09-15 stsp
2447 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2448 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2449 07862c20 2018-09-15 stsp return err;
2450 f42b1b34 2018-03-12 stsp }
2451 5c860e29 2018-03-12 stsp
2452 f42b1b34 2018-03-12 stsp static const struct got_error *
2453 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2454 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2455 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2456 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2457 f42b1b34 2018-03-12 stsp {
2458 f42b1b34 2018-03-12 stsp const struct got_error *err;
2459 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2460 6841bf13 2019-11-29 kn regex_t regex;
2461 6841bf13 2019-11-29 kn int have_match;
2462 372ccdbb 2018-06-10 stsp
2463 6841bf13 2019-11-29 kn if (search_pattern &&
2464 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2465 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2466 6841bf13 2019-11-29 kn
2467 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2468 f42b1b34 2018-03-12 stsp if (err)
2469 f42b1b34 2018-03-12 stsp return err;
2470 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2471 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2472 372ccdbb 2018-06-10 stsp if (err)
2473 fcc85cad 2018-07-22 stsp goto done;
2474 656b1f76 2019-05-11 jcs for (;;) {
2475 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2476 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2477 8bf5b3c9 2018-03-17 stsp
2478 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2479 84453469 2018-11-11 stsp break;
2480 84453469 2018-11-11 stsp
2481 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2482 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2483 372ccdbb 2018-06-10 stsp if (err) {
2484 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2485 9ba79e04 2018-06-11 stsp err = NULL;
2486 ee780d5c 2020-01-04 stsp break;
2487 7e665116 2018-04-02 stsp }
2488 b43fbaa0 2018-06-11 stsp if (id == NULL)
2489 7e665116 2018-04-02 stsp break;
2490 b43fbaa0 2018-06-11 stsp
2491 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2492 b43fbaa0 2018-06-11 stsp if (err)
2493 fcc85cad 2018-07-22 stsp break;
2494 6841bf13 2019-11-29 kn
2495 6841bf13 2019-11-29 kn if (search_pattern) {
2496 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2497 6841bf13 2019-11-29 kn if (err) {
2498 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2499 6841bf13 2019-11-29 kn break;
2500 6841bf13 2019-11-29 kn }
2501 6841bf13 2019-11-29 kn if (have_match == 0) {
2502 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2503 6841bf13 2019-11-29 kn continue;
2504 6841bf13 2019-11-29 kn }
2505 6841bf13 2019-11-29 kn }
2506 6841bf13 2019-11-29 kn
2507 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2508 44392932 2019-08-25 stsp diff_context, refs);
2509 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2510 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2511 7e665116 2018-04-02 stsp break;
2512 31cedeaf 2018-09-15 stsp }
2513 fcc85cad 2018-07-22 stsp done:
2514 6841bf13 2019-11-29 kn if (search_pattern)
2515 6841bf13 2019-11-29 kn regfree(&regex);
2516 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2517 f42b1b34 2018-03-12 stsp return err;
2518 f42b1b34 2018-03-12 stsp }
2519 5c860e29 2018-03-12 stsp
2520 4ed7e80c 2018-05-20 stsp __dead static void
2521 6f3d1eb0 2018-03-12 stsp usage_log(void)
2522 6f3d1eb0 2018-03-12 stsp {
2523 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2524 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2525 6f3d1eb0 2018-03-12 stsp exit(1);
2526 b1ebc001 2019-08-13 stsp }
2527 b1ebc001 2019-08-13 stsp
2528 b1ebc001 2019-08-13 stsp static int
2529 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2530 b1ebc001 2019-08-13 stsp {
2531 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2532 b1ebc001 2019-08-13 stsp long long n;
2533 b1ebc001 2019-08-13 stsp const char *errstr;
2534 b1ebc001 2019-08-13 stsp
2535 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2536 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2537 b1ebc001 2019-08-13 stsp return 0;
2538 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2539 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2540 b1ebc001 2019-08-13 stsp return 0;
2541 b1ebc001 2019-08-13 stsp return n;
2542 6f3d1eb0 2018-03-12 stsp }
2543 6f3d1eb0 2018-03-12 stsp
2544 4ed7e80c 2018-05-20 stsp static const struct got_error *
2545 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2546 f42b1b34 2018-03-12 stsp {
2547 f42b1b34 2018-03-12 stsp const struct got_error *error;
2548 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2549 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2550 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2551 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2552 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2553 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2554 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2555 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2556 64a96a6d 2018-04-01 stsp const char *errstr;
2557 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2558 1b3893a2 2019-03-18 stsp
2559 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2560 5c860e29 2018-03-12 stsp
2561 6715a751 2018-03-16 stsp #ifndef PROFILE
2562 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2563 6098196c 2019-01-04 stsp NULL)
2564 ad242220 2018-09-08 stsp == -1)
2565 f42b1b34 2018-03-12 stsp err(1, "pledge");
2566 6715a751 2018-03-16 stsp #endif
2567 79109fed 2018-03-27 stsp
2568 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2569 b1ebc001 2019-08-13 stsp
2570 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2571 79109fed 2018-03-27 stsp switch (ch) {
2572 79109fed 2018-03-27 stsp case 'p':
2573 79109fed 2018-03-27 stsp show_patch = 1;
2574 d142fc45 2018-04-01 stsp break;
2575 d142fc45 2018-04-01 stsp case 'c':
2576 d142fc45 2018-04-01 stsp start_commit = optarg;
2577 64a96a6d 2018-04-01 stsp break;
2578 c0cc5c62 2018-10-18 stsp case 'C':
2579 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2580 4a8520aa 2018-10-18 stsp &errstr);
2581 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2582 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2583 c0cc5c62 2018-10-18 stsp break;
2584 64a96a6d 2018-04-01 stsp case 'l':
2585 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2586 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2587 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2588 cc54c501 2019-07-15 stsp break;
2589 48c8c60d 2020-01-27 stsp case 'b':
2590 48c8c60d 2020-01-27 stsp log_branches = 1;
2591 a0603db2 2018-06-10 stsp break;
2592 04ca23f4 2018-07-16 stsp case 'r':
2593 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2594 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2595 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2596 9ba1d308 2019-10-21 stsp optarg);
2597 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2598 04ca23f4 2018-07-16 stsp break;
2599 6841bf13 2019-11-29 kn case 's':
2600 6841bf13 2019-11-29 kn search_pattern = optarg;
2601 6841bf13 2019-11-29 kn break;
2602 79109fed 2018-03-27 stsp default:
2603 2deda0b9 2019-03-07 stsp usage_log();
2604 79109fed 2018-03-27 stsp /* NOTREACHED */
2605 79109fed 2018-03-27 stsp }
2606 79109fed 2018-03-27 stsp }
2607 79109fed 2018-03-27 stsp
2608 79109fed 2018-03-27 stsp argc -= optind;
2609 79109fed 2018-03-27 stsp argv += optind;
2610 f42b1b34 2018-03-12 stsp
2611 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2612 dc1edbfa 2019-11-29 kn diff_context = 3;
2613 dc1edbfa 2019-11-29 kn else if (!show_patch)
2614 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2615 dc1edbfa 2019-11-29 kn
2616 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2617 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2618 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2619 04ca23f4 2018-07-16 stsp goto done;
2620 04ca23f4 2018-07-16 stsp }
2621 cffc0aa4 2019-02-05 stsp
2622 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2623 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2624 cffc0aa4 2019-02-05 stsp goto done;
2625 cffc0aa4 2019-02-05 stsp error = NULL;
2626 cffc0aa4 2019-02-05 stsp
2627 e7301579 2019-03-18 stsp if (argc == 0) {
2628 cbd1af7a 2019-03-18 stsp path = strdup("");
2629 e7301579 2019-03-18 stsp if (path == NULL) {
2630 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2631 cbd1af7a 2019-03-18 stsp goto done;
2632 e7301579 2019-03-18 stsp }
2633 e7301579 2019-03-18 stsp } else if (argc == 1) {
2634 e7301579 2019-03-18 stsp if (worktree) {
2635 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2636 e7301579 2019-03-18 stsp argv[0]);
2637 e7301579 2019-03-18 stsp if (error)
2638 e7301579 2019-03-18 stsp goto done;
2639 e7301579 2019-03-18 stsp } else {
2640 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2641 e7301579 2019-03-18 stsp if (path == NULL) {
2642 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2643 e7301579 2019-03-18 stsp goto done;
2644 e7301579 2019-03-18 stsp }
2645 e7301579 2019-03-18 stsp }
2646 cbd1af7a 2019-03-18 stsp } else
2647 cbd1af7a 2019-03-18 stsp usage_log();
2648 cbd1af7a 2019-03-18 stsp
2649 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2650 5486daa2 2019-05-11 stsp repo_path = worktree ?
2651 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2652 5486daa2 2019-05-11 stsp }
2653 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2654 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2655 cffc0aa4 2019-02-05 stsp goto done;
2656 04ca23f4 2018-07-16 stsp }
2657 6098196c 2019-01-04 stsp
2658 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2659 d7d4f210 2018-03-12 stsp if (error != NULL)
2660 04ca23f4 2018-07-16 stsp goto done;
2661 f42b1b34 2018-03-12 stsp
2662 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2663 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2664 c02c541e 2019-03-29 stsp if (error)
2665 c02c541e 2019-03-29 stsp goto done;
2666 c02c541e 2019-03-29 stsp
2667 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2668 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2669 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2670 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2671 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2672 3235492e 2018-04-01 stsp if (error != NULL)
2673 3235492e 2018-04-01 stsp return error;
2674 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2675 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2676 3235492e 2018-04-01 stsp if (error != NULL)
2677 3235492e 2018-04-01 stsp return error;
2678 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2679 3235492e 2018-04-01 stsp } else {
2680 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2681 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2682 3235492e 2018-04-01 stsp if (error == NULL) {
2683 1caad61b 2019-02-01 stsp int obj_type;
2684 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2685 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2686 d1f2edc9 2018-06-13 stsp if (error != NULL)
2687 1caad61b 2019-02-01 stsp goto done;
2688 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2689 1caad61b 2019-02-01 stsp if (error != NULL)
2690 1caad61b 2019-02-01 stsp goto done;
2691 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2692 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2693 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2694 1caad61b 2019-02-01 stsp if (error != NULL)
2695 1caad61b 2019-02-01 stsp goto done;
2696 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2697 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2698 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2699 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2700 1caad61b 2019-02-01 stsp goto done;
2701 1caad61b 2019-02-01 stsp }
2702 1caad61b 2019-02-01 stsp free(id);
2703 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2704 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2705 1caad61b 2019-02-01 stsp if (id == NULL)
2706 638f9024 2019-05-13 stsp error = got_error_from_errno(
2707 230a42bd 2019-05-11 jcs "got_object_id_dup");
2708 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2709 1caad61b 2019-02-01 stsp if (error)
2710 1caad61b 2019-02-01 stsp goto done;
2711 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2712 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2713 1caad61b 2019-02-01 stsp goto done;
2714 1caad61b 2019-02-01 stsp }
2715 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2716 d1f2edc9 2018-06-13 stsp if (error != NULL)
2717 1caad61b 2019-02-01 stsp goto done;
2718 d1f2edc9 2018-06-13 stsp }
2719 15a94983 2018-12-23 stsp if (commit == NULL) {
2720 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2721 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2722 d1f2edc9 2018-06-13 stsp if (error != NULL)
2723 d1f2edc9 2018-06-13 stsp return error;
2724 3235492e 2018-04-01 stsp }
2725 3235492e 2018-04-01 stsp }
2726 d7d4f210 2018-03-12 stsp if (error != NULL)
2727 04ca23f4 2018-07-16 stsp goto done;
2728 04ca23f4 2018-07-16 stsp
2729 deeabeae 2019-08-27 stsp if (worktree) {
2730 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2731 deeabeae 2019-08-27 stsp char *p;
2732 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2733 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2734 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2735 deeabeae 2019-08-27 stsp goto done;
2736 deeabeae 2019-08-27 stsp }
2737 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2738 deeabeae 2019-08-27 stsp free(p);
2739 deeabeae 2019-08-27 stsp } else
2740 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2741 04ca23f4 2018-07-16 stsp if (error != NULL)
2742 04ca23f4 2018-07-16 stsp goto done;
2743 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2744 04ca23f4 2018-07-16 stsp free(path);
2745 04ca23f4 2018-07-16 stsp path = in_repo_path;
2746 04ca23f4 2018-07-16 stsp }
2747 199a4027 2019-02-02 stsp
2748 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2749 199a4027 2019-02-02 stsp if (error)
2750 199a4027 2019-02-02 stsp goto done;
2751 04ca23f4 2018-07-16 stsp
2752 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2753 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2754 04ca23f4 2018-07-16 stsp done:
2755 04ca23f4 2018-07-16 stsp free(path);
2756 04ca23f4 2018-07-16 stsp free(repo_path);
2757 04ca23f4 2018-07-16 stsp free(cwd);
2758 f42b1b34 2018-03-12 stsp free(id);
2759 cffc0aa4 2019-02-05 stsp if (worktree)
2760 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2761 ad242220 2018-09-08 stsp if (repo) {
2762 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2763 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2764 ad242220 2018-09-08 stsp if (error == NULL)
2765 ad242220 2018-09-08 stsp error = repo_error;
2766 ad242220 2018-09-08 stsp }
2767 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2768 8bf5b3c9 2018-03-17 stsp return error;
2769 5c860e29 2018-03-12 stsp }
2770 5c860e29 2018-03-12 stsp
2771 4ed7e80c 2018-05-20 stsp __dead static void
2772 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2773 3f8b7d6a 2018-04-01 stsp {
2774 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2775 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2776 3f8b7d6a 2018-04-01 stsp exit(1);
2777 b00d56cd 2018-04-01 stsp }
2778 b00d56cd 2018-04-01 stsp
2779 b72f483a 2019-02-05 stsp struct print_diff_arg {
2780 b72f483a 2019-02-05 stsp struct got_repository *repo;
2781 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2782 b72f483a 2019-02-05 stsp int diff_context;
2783 3fc0c068 2019-02-10 stsp const char *id_str;
2784 3fc0c068 2019-02-10 stsp int header_shown;
2785 98eaaa12 2019-08-03 stsp int diff_staged;
2786 63035f9f 2019-10-06 stsp int ignore_whitespace;
2787 b72f483a 2019-02-05 stsp };
2788 b72f483a 2019-02-05 stsp
2789 4ed7e80c 2018-05-20 stsp static const struct got_error *
2790 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2791 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2792 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2793 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2794 b72f483a 2019-02-05 stsp {
2795 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2796 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2797 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2798 12463d8b 2019-12-13 stsp int fd = -1;
2799 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2800 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2801 b72f483a 2019-02-05 stsp struct stat sb;
2802 b72f483a 2019-02-05 stsp
2803 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2804 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2805 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2806 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2807 98eaaa12 2019-08-03 stsp return NULL;
2808 98eaaa12 2019-08-03 stsp } else {
2809 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2810 98eaaa12 2019-08-03 stsp return NULL;
2811 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2812 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2813 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2814 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2815 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2816 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2817 98eaaa12 2019-08-03 stsp return NULL;
2818 98eaaa12 2019-08-03 stsp }
2819 3fc0c068 2019-02-10 stsp
2820 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2821 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2822 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2823 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2824 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2825 3fc0c068 2019-02-10 stsp }
2826 b72f483a 2019-02-05 stsp
2827 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2828 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2829 98eaaa12 2019-08-03 stsp switch (staged_status) {
2830 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2831 98eaaa12 2019-08-03 stsp label1 = path;
2832 98eaaa12 2019-08-03 stsp label2 = path;
2833 98eaaa12 2019-08-03 stsp break;
2834 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2835 98eaaa12 2019-08-03 stsp label2 = path;
2836 98eaaa12 2019-08-03 stsp break;
2837 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2838 98eaaa12 2019-08-03 stsp label1 = path;
2839 98eaaa12 2019-08-03 stsp break;
2840 98eaaa12 2019-08-03 stsp default:
2841 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2842 98eaaa12 2019-08-03 stsp }
2843 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2844 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2845 63035f9f 2019-10-06 stsp a->repo, stdout);
2846 98eaaa12 2019-08-03 stsp }
2847 98eaaa12 2019-08-03 stsp
2848 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2849 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2850 4ce46740 2019-08-08 stsp char *id_str;
2851 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2852 408b4ebc 2019-08-03 stsp 8192);
2853 4ce46740 2019-08-08 stsp if (err)
2854 4ce46740 2019-08-08 stsp goto done;
2855 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2856 4ce46740 2019-08-08 stsp if (err)
2857 4ce46740 2019-08-08 stsp goto done;
2858 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2859 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2860 4ce46740 2019-08-08 stsp free(id_str);
2861 4ce46740 2019-08-08 stsp goto done;
2862 4ce46740 2019-08-08 stsp }
2863 4ce46740 2019-08-08 stsp free(id_str);
2864 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2865 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2866 4ce46740 2019-08-08 stsp if (err)
2867 4ce46740 2019-08-08 stsp goto done;
2868 4ce46740 2019-08-08 stsp }
2869 d00136be 2019-03-26 stsp
2870 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2871 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2872 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2873 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2874 2ec1f75b 2019-03-26 stsp goto done;
2875 2ec1f75b 2019-03-26 stsp }
2876 b72f483a 2019-02-05 stsp
2877 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2878 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2879 12463d8b 2019-12-13 stsp if (fd == -1) {
2880 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2881 12463d8b 2019-12-13 stsp goto done;
2882 12463d8b 2019-12-13 stsp }
2883 12463d8b 2019-12-13 stsp } else {
2884 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2885 12463d8b 2019-12-13 stsp if (fd == -1) {
2886 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2887 12463d8b 2019-12-13 stsp goto done;
2888 12463d8b 2019-12-13 stsp }
2889 12463d8b 2019-12-13 stsp }
2890 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2891 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2892 2ec1f75b 2019-03-26 stsp goto done;
2893 2ec1f75b 2019-03-26 stsp }
2894 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2895 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2896 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2897 2ec1f75b 2019-03-26 stsp goto done;
2898 2ec1f75b 2019-03-26 stsp }
2899 12463d8b 2019-12-13 stsp fd = -1;
2900 2ec1f75b 2019-03-26 stsp } else
2901 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2902 b72f483a 2019-02-05 stsp
2903 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2904 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2905 b72f483a 2019-02-05 stsp done:
2906 b72f483a 2019-02-05 stsp if (blob1)
2907 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2908 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2909 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2910 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2911 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2912 b72f483a 2019-02-05 stsp free(abspath);
2913 927df6b7 2019-02-10 stsp return err;
2914 927df6b7 2019-02-10 stsp }
2915 927df6b7 2019-02-10 stsp
2916 927df6b7 2019-02-10 stsp static const struct got_error *
2917 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2918 b00d56cd 2018-04-01 stsp {
2919 b00d56cd 2018-04-01 stsp const struct got_error *error;
2920 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2921 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2922 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2923 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2924 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2925 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2926 15a94983 2018-12-23 stsp int type1, type2;
2927 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2928 c0cc5c62 2018-10-18 stsp const char *errstr;
2929 927df6b7 2019-02-10 stsp char *path = NULL;
2930 b00d56cd 2018-04-01 stsp
2931 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2932 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2933 25eccc22 2019-01-04 stsp NULL) == -1)
2934 b00d56cd 2018-04-01 stsp err(1, "pledge");
2935 b00d56cd 2018-04-01 stsp #endif
2936 b00d56cd 2018-04-01 stsp
2937 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2938 b00d56cd 2018-04-01 stsp switch (ch) {
2939 c0cc5c62 2018-10-18 stsp case 'C':
2940 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2941 dfcab68b 2019-11-29 kn &errstr);
2942 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2943 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2944 c0cc5c62 2018-10-18 stsp break;
2945 b72f483a 2019-02-05 stsp case 'r':
2946 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2947 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2948 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2949 9ba1d308 2019-10-21 stsp optarg);
2950 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2951 b72f483a 2019-02-05 stsp break;
2952 98eaaa12 2019-08-03 stsp case 's':
2953 98eaaa12 2019-08-03 stsp diff_staged = 1;
2954 63035f9f 2019-10-06 stsp break;
2955 63035f9f 2019-10-06 stsp case 'w':
2956 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2957 98eaaa12 2019-08-03 stsp break;
2958 b00d56cd 2018-04-01 stsp default:
2959 2deda0b9 2019-03-07 stsp usage_diff();
2960 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2961 b00d56cd 2018-04-01 stsp }
2962 b00d56cd 2018-04-01 stsp }
2963 b00d56cd 2018-04-01 stsp
2964 b00d56cd 2018-04-01 stsp argc -= optind;
2965 b00d56cd 2018-04-01 stsp argv += optind;
2966 b00d56cd 2018-04-01 stsp
2967 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2968 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2969 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2970 b72f483a 2019-02-05 stsp goto done;
2971 b72f483a 2019-02-05 stsp }
2972 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2973 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2974 927df6b7 2019-02-10 stsp goto done;
2975 927df6b7 2019-02-10 stsp if (argc <= 1) {
2976 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2977 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2978 927df6b7 2019-02-10 stsp goto done;
2979 927df6b7 2019-02-10 stsp }
2980 b72f483a 2019-02-05 stsp if (repo_path)
2981 b72f483a 2019-02-05 stsp errx(1,
2982 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2983 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2984 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2985 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2986 927df6b7 2019-02-10 stsp goto done;
2987 927df6b7 2019-02-10 stsp }
2988 927df6b7 2019-02-10 stsp if (argc == 1) {
2989 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2990 cbd1af7a 2019-03-18 stsp argv[0]);
2991 927df6b7 2019-02-10 stsp if (error)
2992 927df6b7 2019-02-10 stsp goto done;
2993 927df6b7 2019-02-10 stsp } else {
2994 927df6b7 2019-02-10 stsp path = strdup("");
2995 927df6b7 2019-02-10 stsp if (path == NULL) {
2996 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2997 927df6b7 2019-02-10 stsp goto done;
2998 927df6b7 2019-02-10 stsp }
2999 927df6b7 2019-02-10 stsp }
3000 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3001 98eaaa12 2019-08-03 stsp if (diff_staged)
3002 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3003 98eaaa12 2019-08-03 stsp "objects in repository");
3004 15a94983 2018-12-23 stsp id_str1 = argv[0];
3005 15a94983 2018-12-23 stsp id_str2 = argv[1];
3006 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
3007 30db809c 2019-06-05 stsp repo_path =
3008 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
3009 30db809c 2019-06-05 stsp if (repo_path == NULL) {
3010 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
3011 30db809c 2019-06-05 stsp goto done;
3012 30db809c 2019-06-05 stsp }
3013 30db809c 2019-06-05 stsp }
3014 b00d56cd 2018-04-01 stsp } else
3015 b00d56cd 2018-04-01 stsp usage_diff();
3016 25eccc22 2019-01-04 stsp
3017 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
3018 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
3019 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3020 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3021 b72f483a 2019-02-05 stsp }
3022 b00d56cd 2018-04-01 stsp
3023 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3024 76089277 2018-04-01 stsp free(repo_path);
3025 b00d56cd 2018-04-01 stsp if (error != NULL)
3026 b00d56cd 2018-04-01 stsp goto done;
3027 b00d56cd 2018-04-01 stsp
3028 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3029 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3030 c02c541e 2019-03-29 stsp if (error)
3031 c02c541e 2019-03-29 stsp goto done;
3032 c02c541e 2019-03-29 stsp
3033 30db809c 2019-06-05 stsp if (argc <= 1) {
3034 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3035 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3036 b72f483a 2019-02-05 stsp char *id_str;
3037 72ea6654 2019-07-27 stsp
3038 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3039 72ea6654 2019-07-27 stsp
3040 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3041 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3042 b72f483a 2019-02-05 stsp if (error)
3043 b72f483a 2019-02-05 stsp goto done;
3044 b72f483a 2019-02-05 stsp arg.repo = repo;
3045 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3046 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3047 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3048 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3049 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3050 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3051 b72f483a 2019-02-05 stsp
3052 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3053 72ea6654 2019-07-27 stsp if (error)
3054 72ea6654 2019-07-27 stsp goto done;
3055 72ea6654 2019-07-27 stsp
3056 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3057 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3058 b72f483a 2019-02-05 stsp free(id_str);
3059 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3060 b72f483a 2019-02-05 stsp goto done;
3061 b72f483a 2019-02-05 stsp }
3062 b72f483a 2019-02-05 stsp
3063 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3064 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3065 0adb7196 2019-08-11 stsp if (error)
3066 0adb7196 2019-08-11 stsp goto done;
3067 b00d56cd 2018-04-01 stsp
3068 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3069 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3070 0adb7196 2019-08-11 stsp if (error)
3071 0adb7196 2019-08-11 stsp goto done;
3072 b00d56cd 2018-04-01 stsp
3073 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3074 15a94983 2018-12-23 stsp if (error)
3075 15a94983 2018-12-23 stsp goto done;
3076 15a94983 2018-12-23 stsp
3077 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3078 15a94983 2018-12-23 stsp if (error)
3079 15a94983 2018-12-23 stsp goto done;
3080 15a94983 2018-12-23 stsp
3081 15a94983 2018-12-23 stsp if (type1 != type2) {
3082 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3083 b00d56cd 2018-04-01 stsp goto done;
3084 b00d56cd 2018-04-01 stsp }
3085 b00d56cd 2018-04-01 stsp
3086 15a94983 2018-12-23 stsp switch (type1) {
3087 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3088 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3089 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3090 b00d56cd 2018-04-01 stsp break;
3091 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3092 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3093 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3094 b00d56cd 2018-04-01 stsp break;
3095 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3096 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3097 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3098 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3099 b00d56cd 2018-04-01 stsp break;
3100 b00d56cd 2018-04-01 stsp default:
3101 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3102 b00d56cd 2018-04-01 stsp }
3103 b00d56cd 2018-04-01 stsp done:
3104 5e70831e 2019-05-28 stsp free(label1);
3105 5e70831e 2019-05-28 stsp free(label2);
3106 15a94983 2018-12-23 stsp free(id1);
3107 15a94983 2018-12-23 stsp free(id2);
3108 927df6b7 2019-02-10 stsp free(path);
3109 b72f483a 2019-02-05 stsp if (worktree)
3110 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3111 ad242220 2018-09-08 stsp if (repo) {
3112 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3113 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3114 ad242220 2018-09-08 stsp if (error == NULL)
3115 ad242220 2018-09-08 stsp error = repo_error;
3116 ad242220 2018-09-08 stsp }
3117 b00d56cd 2018-04-01 stsp return error;
3118 404c43c4 2018-06-21 stsp }
3119 404c43c4 2018-06-21 stsp
3120 404c43c4 2018-06-21 stsp __dead static void
3121 404c43c4 2018-06-21 stsp usage_blame(void)
3122 404c43c4 2018-06-21 stsp {
3123 9270e621 2019-02-05 stsp fprintf(stderr,
3124 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3125 404c43c4 2018-06-21 stsp getprogname());
3126 404c43c4 2018-06-21 stsp exit(1);
3127 b00d56cd 2018-04-01 stsp }
3128 b00d56cd 2018-04-01 stsp
3129 28315671 2019-08-14 stsp struct blame_line {
3130 28315671 2019-08-14 stsp int annotated;
3131 28315671 2019-08-14 stsp char *id_str;
3132 82f6abb8 2019-08-14 stsp char *committer;
3133 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3134 28315671 2019-08-14 stsp };
3135 28315671 2019-08-14 stsp
3136 28315671 2019-08-14 stsp struct blame_cb_args {
3137 28315671 2019-08-14 stsp struct blame_line *lines;
3138 28315671 2019-08-14 stsp int nlines;
3139 7ef28ff8 2019-08-14 stsp int nlines_prec;
3140 28315671 2019-08-14 stsp int lineno_cur;
3141 28315671 2019-08-14 stsp off_t *line_offsets;
3142 28315671 2019-08-14 stsp FILE *f;
3143 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3144 28315671 2019-08-14 stsp };
3145 28315671 2019-08-14 stsp
3146 404c43c4 2018-06-21 stsp static const struct got_error *
3147 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3148 28315671 2019-08-14 stsp {
3149 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3150 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3151 28315671 2019-08-14 stsp struct blame_line *bline;
3152 82f6abb8 2019-08-14 stsp char *line = NULL;
3153 28315671 2019-08-14 stsp size_t linesize = 0;
3154 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3155 28315671 2019-08-14 stsp off_t offset;
3156 bcb49d15 2019-08-14 stsp struct tm tm;
3157 bcb49d15 2019-08-14 stsp time_t committer_time;
3158 28315671 2019-08-14 stsp
3159 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3160 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3161 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3162 28315671 2019-08-14 stsp
3163 28315671 2019-08-14 stsp if (sigint_received)
3164 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3165 28315671 2019-08-14 stsp
3166 2839f8b9 2019-08-18 stsp if (lineno == -1)
3167 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3168 2839f8b9 2019-08-18 stsp
3169 28315671 2019-08-14 stsp /* Annotate this line. */
3170 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3171 28315671 2019-08-14 stsp if (bline->annotated)
3172 28315671 2019-08-14 stsp return NULL;
3173 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3174 28315671 2019-08-14 stsp if (err)
3175 28315671 2019-08-14 stsp return err;
3176 82f6abb8 2019-08-14 stsp
3177 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3178 82f6abb8 2019-08-14 stsp if (err)
3179 82f6abb8 2019-08-14 stsp goto done;
3180 82f6abb8 2019-08-14 stsp
3181 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3182 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3183 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3184 bcb49d15 2019-08-14 stsp goto done;
3185 bcb49d15 2019-08-14 stsp }
3186 bcb49d15 2019-08-14 stsp
3187 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3188 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3189 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3190 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3191 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3192 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3193 82f6abb8 2019-08-14 stsp goto done;
3194 82f6abb8 2019-08-14 stsp }
3195 28315671 2019-08-14 stsp bline->annotated = 1;
3196 28315671 2019-08-14 stsp
3197 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3198 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3199 28315671 2019-08-14 stsp if (!bline->annotated)
3200 82f6abb8 2019-08-14 stsp goto done;
3201 28315671 2019-08-14 stsp
3202 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3203 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3204 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3205 82f6abb8 2019-08-14 stsp goto done;
3206 82f6abb8 2019-08-14 stsp }
3207 28315671 2019-08-14 stsp
3208 28315671 2019-08-14 stsp while (bline->annotated) {
3209 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3210 82f6abb8 2019-08-14 stsp size_t len;
3211 82f6abb8 2019-08-14 stsp
3212 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3213 28315671 2019-08-14 stsp if (ferror(a->f))
3214 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3215 28315671 2019-08-14 stsp break;
3216 28315671 2019-08-14 stsp }
3217 82f6abb8 2019-08-14 stsp
3218 82f6abb8 2019-08-14 stsp committer = bline->committer;
3219 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3220 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3221 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3222 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3223 82f6abb8 2019-08-14 stsp if (at)
3224 82f6abb8 2019-08-14 stsp *at = '\0';
3225 82f6abb8 2019-08-14 stsp len = strlen(committer);
3226 82f6abb8 2019-08-14 stsp if (len >= 9)
3227 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3228 28315671 2019-08-14 stsp
3229 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3230 28315671 2019-08-14 stsp if (nl)
3231 28315671 2019-08-14 stsp *nl = '\0';
3232 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3233 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3234 28315671 2019-08-14 stsp
3235 28315671 2019-08-14 stsp a->lineno_cur++;
3236 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3237 28315671 2019-08-14 stsp }
3238 82f6abb8 2019-08-14 stsp done:
3239 82f6abb8 2019-08-14 stsp if (commit)
3240 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3241 28315671 2019-08-14 stsp free(line);
3242 28315671 2019-08-14 stsp return err;
3243 28315671 2019-08-14 stsp }
3244 28315671 2019-08-14 stsp
3245 28315671 2019-08-14 stsp static const struct got_error *
3246 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3247 404c43c4 2018-06-21 stsp {
3248 404c43c4 2018-06-21 stsp const struct got_error *error;
3249 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3250 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3251 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3252 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3253 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3254 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3255 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3256 28315671 2019-08-14 stsp struct blame_cb_args bca;
3257 28315671 2019-08-14 stsp int ch, obj_type, i;
3258 b02560ec 2019-08-19 stsp size_t filesize;
3259 90f3c347 2019-08-19 stsp
3260 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3261 404c43c4 2018-06-21 stsp
3262 404c43c4 2018-06-21 stsp #ifndef PROFILE
3263 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3264 36e2fb66 2019-01-04 stsp NULL) == -1)
3265 404c43c4 2018-06-21 stsp err(1, "pledge");
3266 404c43c4 2018-06-21 stsp #endif
3267 404c43c4 2018-06-21 stsp
3268 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3269 404c43c4 2018-06-21 stsp switch (ch) {
3270 404c43c4 2018-06-21 stsp case 'c':
3271 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3272 404c43c4 2018-06-21 stsp break;
3273 66bea077 2018-08-02 stsp case 'r':
3274 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3275 66bea077 2018-08-02 stsp if (repo_path == NULL)
3276 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3277 9ba1d308 2019-10-21 stsp optarg);
3278 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3279 66bea077 2018-08-02 stsp break;
3280 404c43c4 2018-06-21 stsp default:
3281 2deda0b9 2019-03-07 stsp usage_blame();
3282 404c43c4 2018-06-21 stsp /* NOTREACHED */
3283 404c43c4 2018-06-21 stsp }
3284 404c43c4 2018-06-21 stsp }
3285 404c43c4 2018-06-21 stsp
3286 404c43c4 2018-06-21 stsp argc -= optind;
3287 404c43c4 2018-06-21 stsp argv += optind;
3288 404c43c4 2018-06-21 stsp
3289 a39318fd 2018-08-02 stsp if (argc == 1)
3290 404c43c4 2018-06-21 stsp path = argv[0];
3291 a39318fd 2018-08-02 stsp else
3292 404c43c4 2018-06-21 stsp usage_blame();
3293 404c43c4 2018-06-21 stsp
3294 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3295 66bea077 2018-08-02 stsp if (cwd == NULL) {
3296 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3297 66bea077 2018-08-02 stsp goto done;
3298 66bea077 2018-08-02 stsp }
3299 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3300 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3301 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3302 66bea077 2018-08-02 stsp goto done;
3303 0c06baac 2019-02-05 stsp else
3304 0c06baac 2019-02-05 stsp error = NULL;
3305 0c06baac 2019-02-05 stsp if (worktree) {
3306 0c06baac 2019-02-05 stsp repo_path =
3307 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3308 0c06baac 2019-02-05 stsp if (repo_path == NULL)
3309 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3310 0c06baac 2019-02-05 stsp if (error)
3311 0c06baac 2019-02-05 stsp goto done;
3312 0c06baac 2019-02-05 stsp } else {
3313 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3314 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3315 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3316 0c06baac 2019-02-05 stsp goto done;
3317 0c06baac 2019-02-05 stsp }
3318 66bea077 2018-08-02 stsp }
3319 66bea077 2018-08-02 stsp }
3320 36e2fb66 2019-01-04 stsp
3321 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3322 c02c541e 2019-03-29 stsp if (error != NULL)
3323 36e2fb66 2019-01-04 stsp goto done;
3324 66bea077 2018-08-02 stsp
3325 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3326 c02c541e 2019-03-29 stsp if (error)
3327 404c43c4 2018-06-21 stsp goto done;
3328 404c43c4 2018-06-21 stsp
3329 0c06baac 2019-02-05 stsp if (worktree) {
3330 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3331 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3332 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3333 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3334 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3335 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3336 6efaaa2d 2019-02-05 stsp path) == -1) {
3337 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3338 0c06baac 2019-02-05 stsp goto done;
3339 0c06baac 2019-02-05 stsp }
3340 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3341 0c06baac 2019-02-05 stsp free(p);
3342 0c06baac 2019-02-05 stsp } else {
3343 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3344 0c06baac 2019-02-05 stsp }
3345 0c06baac 2019-02-05 stsp if (error)
3346 66bea077 2018-08-02 stsp goto done;
3347 66bea077 2018-08-02 stsp
3348 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3349 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3350 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3351 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3352 404c43c4 2018-06-21 stsp if (error != NULL)
3353 66bea077 2018-08-02 stsp goto done;
3354 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3355 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3356 404c43c4 2018-06-21 stsp if (error != NULL)
3357 66bea077 2018-08-02 stsp goto done;
3358 404c43c4 2018-06-21 stsp } else {
3359 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3360 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3361 30837e32 2019-07-25 stsp if (error)
3362 66bea077 2018-08-02 stsp goto done;
3363 404c43c4 2018-06-21 stsp }
3364 404c43c4 2018-06-21 stsp
3365 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3366 28315671 2019-08-14 stsp if (error)
3367 28315671 2019-08-14 stsp goto done;
3368 28315671 2019-08-14 stsp
3369 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3370 28315671 2019-08-14 stsp if (error)
3371 28315671 2019-08-14 stsp goto done;
3372 28315671 2019-08-14 stsp
3373 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3374 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3375 28315671 2019-08-14 stsp goto done;
3376 28315671 2019-08-14 stsp }
3377 28315671 2019-08-14 stsp
3378 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3379 28315671 2019-08-14 stsp if (error)
3380 28315671 2019-08-14 stsp goto done;
3381 28315671 2019-08-14 stsp bca.f = got_opentemp();
3382 28315671 2019-08-14 stsp if (bca.f == NULL) {
3383 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3384 28315671 2019-08-14 stsp goto done;
3385 28315671 2019-08-14 stsp }
3386 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3387 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3388 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3389 28315671 2019-08-14 stsp goto done;
3390 28315671 2019-08-14 stsp
3391 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3392 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3393 b02560ec 2019-08-19 stsp bca.nlines--;
3394 b02560ec 2019-08-19 stsp
3395 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3396 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3397 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3398 28315671 2019-08-14 stsp goto done;
3399 28315671 2019-08-14 stsp }
3400 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3401 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3402 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3403 7ef28ff8 2019-08-14 stsp while (i > 0) {
3404 7ef28ff8 2019-08-14 stsp i /= 10;
3405 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3406 7ef28ff8 2019-08-14 stsp }
3407 82f6abb8 2019-08-14 stsp bca.repo = repo;
3408 7ef28ff8 2019-08-14 stsp
3409 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3410 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3411 404c43c4 2018-06-21 stsp done:
3412 66bea077 2018-08-02 stsp free(in_repo_path);
3413 66bea077 2018-08-02 stsp free(repo_path);
3414 66bea077 2018-08-02 stsp free(cwd);
3415 404c43c4 2018-06-21 stsp free(commit_id);
3416 28315671 2019-08-14 stsp free(obj_id);
3417 28315671 2019-08-14 stsp if (blob)
3418 28315671 2019-08-14 stsp got_object_blob_close(blob);
3419 0c06baac 2019-02-05 stsp if (worktree)
3420 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3421 ad242220 2018-09-08 stsp if (repo) {
3422 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3423 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3424 ad242220 2018-09-08 stsp if (error == NULL)
3425 ad242220 2018-09-08 stsp error = repo_error;
3426 ad242220 2018-09-08 stsp }
3427 3affba96 2019-09-22 stsp if (bca.lines) {
3428 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3429 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3430 3affba96 2019-09-22 stsp free(bline->id_str);
3431 3affba96 2019-09-22 stsp free(bline->committer);
3432 3affba96 2019-09-22 stsp }
3433 3affba96 2019-09-22 stsp free(bca.lines);
3434 28315671 2019-08-14 stsp }
3435 28315671 2019-08-14 stsp free(bca.line_offsets);
3436 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3437 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3438 404c43c4 2018-06-21 stsp return error;
3439 5de5890b 2018-10-18 stsp }
3440 5de5890b 2018-10-18 stsp
3441 5de5890b 2018-10-18 stsp __dead static void
3442 5de5890b 2018-10-18 stsp usage_tree(void)
3443 5de5890b 2018-10-18 stsp {
3444 c1669e2e 2019-01-09 stsp fprintf(stderr,
3445 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3446 5de5890b 2018-10-18 stsp getprogname());
3447 5de5890b 2018-10-18 stsp exit(1);
3448 5de5890b 2018-10-18 stsp }
3449 5de5890b 2018-10-18 stsp
3450 c1669e2e 2019-01-09 stsp static void
3451 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3452 c1669e2e 2019-01-09 stsp const char *root_path)
3453 c1669e2e 2019-01-09 stsp {
3454 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3455 848d6979 2019-08-12 stsp const char *modestr = "";
3456 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3457 5de5890b 2018-10-18 stsp
3458 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3459 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3460 c1669e2e 2019-01-09 stsp path++;
3461 c1669e2e 2019-01-09 stsp
3462 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3463 63c5ca5d 2019-08-24 stsp modestr = "$";
3464 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3465 848d6979 2019-08-12 stsp modestr = "@";
3466 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3467 848d6979 2019-08-12 stsp modestr = "/";
3468 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3469 848d6979 2019-08-12 stsp modestr = "*";
3470 848d6979 2019-08-12 stsp
3471 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3472 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3473 c1669e2e 2019-01-09 stsp }
3474 c1669e2e 2019-01-09 stsp
3475 5de5890b 2018-10-18 stsp static const struct got_error *
3476 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3477 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3478 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3479 5de5890b 2018-10-18 stsp {
3480 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3481 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3482 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3483 56e0773d 2019-11-28 stsp int nentries, i;
3484 5de5890b 2018-10-18 stsp
3485 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3486 5de5890b 2018-10-18 stsp if (err)
3487 5de5890b 2018-10-18 stsp goto done;
3488 5de5890b 2018-10-18 stsp
3489 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3490 5de5890b 2018-10-18 stsp if (err)
3491 5de5890b 2018-10-18 stsp goto done;
3492 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3493 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3494 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3495 5de5890b 2018-10-18 stsp char *id = NULL;
3496 84453469 2018-11-11 stsp
3497 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3498 84453469 2018-11-11 stsp break;
3499 84453469 2018-11-11 stsp
3500 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3501 5de5890b 2018-10-18 stsp if (show_ids) {
3502 5de5890b 2018-10-18 stsp char *id_str;
3503 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3504 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3505 5de5890b 2018-10-18 stsp if (err)
3506 5de5890b 2018-10-18 stsp goto done;
3507 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3509 5de5890b 2018-10-18 stsp free(id_str);
3510 5de5890b 2018-10-18 stsp goto done;
3511 5de5890b 2018-10-18 stsp }
3512 5de5890b 2018-10-18 stsp free(id_str);
3513 5de5890b 2018-10-18 stsp }
3514 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3515 5de5890b 2018-10-18 stsp free(id);
3516 c1669e2e 2019-01-09 stsp
3517 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3518 c1669e2e 2019-01-09 stsp char *child_path;
3519 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3520 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3521 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3522 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3523 c1669e2e 2019-01-09 stsp goto done;
3524 c1669e2e 2019-01-09 stsp }
3525 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3526 c1669e2e 2019-01-09 stsp root_path, repo);
3527 c1669e2e 2019-01-09 stsp free(child_path);
3528 c1669e2e 2019-01-09 stsp if (err)
3529 c1669e2e 2019-01-09 stsp goto done;
3530 c1669e2e 2019-01-09 stsp }
3531 5de5890b 2018-10-18 stsp }
3532 5de5890b 2018-10-18 stsp done:
3533 5de5890b 2018-10-18 stsp if (tree)
3534 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3535 5de5890b 2018-10-18 stsp free(tree_id);
3536 5de5890b 2018-10-18 stsp return err;
3537 404c43c4 2018-06-21 stsp }
3538 404c43c4 2018-06-21 stsp
3539 5de5890b 2018-10-18 stsp static const struct got_error *
3540 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3541 5de5890b 2018-10-18 stsp {
3542 5de5890b 2018-10-18 stsp const struct got_error *error;
3543 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3544 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3545 7a2c19d6 2019-02-05 stsp const char *path;
3546 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3547 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3548 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3549 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3550 5de5890b 2018-10-18 stsp int ch;
3551 5de5890b 2018-10-18 stsp
3552 5de5890b 2018-10-18 stsp #ifndef PROFILE
3553 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3554 0f8d269b 2019-01-04 stsp NULL) == -1)
3555 5de5890b 2018-10-18 stsp err(1, "pledge");
3556 5de5890b 2018-10-18 stsp #endif
3557 5de5890b 2018-10-18 stsp
3558 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3559 5de5890b 2018-10-18 stsp switch (ch) {
3560 5de5890b 2018-10-18 stsp case 'c':
3561 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3562 5de5890b 2018-10-18 stsp break;
3563 5de5890b 2018-10-18 stsp case 'r':
3564 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3565 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3566 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3567 9ba1d308 2019-10-21 stsp optarg);
3568 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3569 5de5890b 2018-10-18 stsp break;
3570 5de5890b 2018-10-18 stsp case 'i':
3571 5de5890b 2018-10-18 stsp show_ids = 1;
3572 5de5890b 2018-10-18 stsp break;
3573 c1669e2e 2019-01-09 stsp case 'R':
3574 c1669e2e 2019-01-09 stsp recurse = 1;
3575 c1669e2e 2019-01-09 stsp break;
3576 5de5890b 2018-10-18 stsp default:
3577 2deda0b9 2019-03-07 stsp usage_tree();
3578 5de5890b 2018-10-18 stsp /* NOTREACHED */
3579 5de5890b 2018-10-18 stsp }
3580 5de5890b 2018-10-18 stsp }
3581 5de5890b 2018-10-18 stsp
3582 5de5890b 2018-10-18 stsp argc -= optind;
3583 5de5890b 2018-10-18 stsp argv += optind;
3584 5de5890b 2018-10-18 stsp
3585 5de5890b 2018-10-18 stsp if (argc == 1)
3586 5de5890b 2018-10-18 stsp path = argv[0];
3587 5de5890b 2018-10-18 stsp else if (argc > 1)
3588 5de5890b 2018-10-18 stsp usage_tree();
3589 5de5890b 2018-10-18 stsp else
3590 7a2c19d6 2019-02-05 stsp path = NULL;
3591 7a2c19d6 2019-02-05 stsp
3592 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3593 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3594 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3595 9bf7a39b 2019-02-05 stsp goto done;
3596 9bf7a39b 2019-02-05 stsp }
3597 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3598 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3599 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3600 7a2c19d6 2019-02-05 stsp goto done;
3601 7a2c19d6 2019-02-05 stsp else
3602 7a2c19d6 2019-02-05 stsp error = NULL;
3603 7a2c19d6 2019-02-05 stsp if (worktree) {
3604 7a2c19d6 2019-02-05 stsp repo_path =
3605 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3606 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3607 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3608 7a2c19d6 2019-02-05 stsp if (error)
3609 7a2c19d6 2019-02-05 stsp goto done;
3610 7a2c19d6 2019-02-05 stsp } else {
3611 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3612 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3613 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3614 7a2c19d6 2019-02-05 stsp goto done;
3615 7a2c19d6 2019-02-05 stsp }
3616 5de5890b 2018-10-18 stsp }
3617 5de5890b 2018-10-18 stsp }
3618 5de5890b 2018-10-18 stsp
3619 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3620 5de5890b 2018-10-18 stsp if (error != NULL)
3621 c02c541e 2019-03-29 stsp goto done;
3622 c02c541e 2019-03-29 stsp
3623 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3624 c02c541e 2019-03-29 stsp if (error)
3625 5de5890b 2018-10-18 stsp goto done;
3626 5de5890b 2018-10-18 stsp
3627 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3628 9bf7a39b 2019-02-05 stsp if (worktree) {
3629 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3630 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3631 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3632 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3633 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3634 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3635 9bf7a39b 2019-02-05 stsp goto done;
3636 9bf7a39b 2019-02-05 stsp }
3637 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3638 9bf7a39b 2019-02-05 stsp free(p);
3639 9bf7a39b 2019-02-05 stsp if (error)
3640 9bf7a39b 2019-02-05 stsp goto done;
3641 9bf7a39b 2019-02-05 stsp } else
3642 9bf7a39b 2019-02-05 stsp path = "/";
3643 9bf7a39b 2019-02-05 stsp }
3644 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3645 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3646 9bf7a39b 2019-02-05 stsp if (error != NULL)
3647 9bf7a39b 2019-02-05 stsp goto done;
3648 9bf7a39b 2019-02-05 stsp }
3649 5de5890b 2018-10-18 stsp
3650 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3651 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3652 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3653 5de5890b 2018-10-18 stsp if (error != NULL)
3654 5de5890b 2018-10-18 stsp goto done;
3655 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3656 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3657 5de5890b 2018-10-18 stsp if (error != NULL)
3658 5de5890b 2018-10-18 stsp goto done;
3659 5de5890b 2018-10-18 stsp } else {
3660 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3661 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3662 30837e32 2019-07-25 stsp if (error)
3663 5de5890b 2018-10-18 stsp goto done;
3664 5de5890b 2018-10-18 stsp }
3665 5de5890b 2018-10-18 stsp
3666 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3667 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3668 5de5890b 2018-10-18 stsp done:
3669 5de5890b 2018-10-18 stsp free(in_repo_path);
3670 5de5890b 2018-10-18 stsp free(repo_path);
3671 5de5890b 2018-10-18 stsp free(cwd);
3672 5de5890b 2018-10-18 stsp free(commit_id);
3673 7a2c19d6 2019-02-05 stsp if (worktree)
3674 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3675 5de5890b 2018-10-18 stsp if (repo) {
3676 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3677 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3678 5de5890b 2018-10-18 stsp if (error == NULL)
3679 5de5890b 2018-10-18 stsp error = repo_error;
3680 5de5890b 2018-10-18 stsp }
3681 5de5890b 2018-10-18 stsp return error;
3682 5de5890b 2018-10-18 stsp }
3683 5de5890b 2018-10-18 stsp
3684 6bad629b 2019-02-04 stsp __dead static void
3685 6bad629b 2019-02-04 stsp usage_status(void)
3686 6bad629b 2019-02-04 stsp {
3687 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3688 6bad629b 2019-02-04 stsp exit(1);
3689 6bad629b 2019-02-04 stsp }
3690 5c860e29 2018-03-12 stsp
3691 b72f483a 2019-02-05 stsp static const struct got_error *
3692 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3693 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3694 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3695 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3696 6bad629b 2019-02-04 stsp {
3697 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3698 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3699 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3700 b72f483a 2019-02-05 stsp return NULL;
3701 6bad629b 2019-02-04 stsp }
3702 5c860e29 2018-03-12 stsp
3703 6bad629b 2019-02-04 stsp static const struct got_error *
3704 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3705 6bad629b 2019-02-04 stsp {
3706 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3707 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3708 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3709 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3710 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3711 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3712 a5edda0a 2019-07-27 stsp int ch;
3713 5c860e29 2018-03-12 stsp
3714 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3715 72ea6654 2019-07-27 stsp
3716 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3717 6bad629b 2019-02-04 stsp switch (ch) {
3718 5c860e29 2018-03-12 stsp default:
3719 2deda0b9 2019-03-07 stsp usage_status();
3720 6bad629b 2019-02-04 stsp /* NOTREACHED */
3721 5c860e29 2018-03-12 stsp }
3722 5c860e29 2018-03-12 stsp }
3723 5c860e29 2018-03-12 stsp
3724 6bad629b 2019-02-04 stsp argc -= optind;
3725 6bad629b 2019-02-04 stsp argv += optind;
3726 5c860e29 2018-03-12 stsp
3727 6bad629b 2019-02-04 stsp #ifndef PROFILE
3728 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3729 6bad629b 2019-02-04 stsp NULL) == -1)
3730 6bad629b 2019-02-04 stsp err(1, "pledge");
3731 f42b1b34 2018-03-12 stsp #endif
3732 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3733 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3734 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3735 927df6b7 2019-02-10 stsp goto done;
3736 927df6b7 2019-02-10 stsp }
3737 927df6b7 2019-02-10 stsp
3738 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3739 927df6b7 2019-02-10 stsp if (error != NULL)
3740 a5edda0a 2019-07-27 stsp goto done;
3741 6bad629b 2019-02-04 stsp
3742 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3743 c9956ddf 2019-09-08 stsp NULL);
3744 6bad629b 2019-02-04 stsp if (error != NULL)
3745 6bad629b 2019-02-04 stsp goto done;
3746 6bad629b 2019-02-04 stsp
3747 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3748 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3749 087fb88c 2019-08-04 stsp if (error)
3750 087fb88c 2019-08-04 stsp goto done;
3751 087fb88c 2019-08-04 stsp
3752 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3753 6bad629b 2019-02-04 stsp if (error)
3754 6bad629b 2019-02-04 stsp goto done;
3755 6bad629b 2019-02-04 stsp
3756 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3757 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3758 6bad629b 2019-02-04 stsp done:
3759 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3760 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3761 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3762 927df6b7 2019-02-10 stsp free(cwd);
3763 d0eebce4 2019-03-11 stsp return error;
3764 d0eebce4 2019-03-11 stsp }
3765 d0eebce4 2019-03-11 stsp
3766 d0eebce4 2019-03-11 stsp __dead static void
3767 d0eebce4 2019-03-11 stsp usage_ref(void)
3768 d0eebce4 2019-03-11 stsp {
3769 d0eebce4 2019-03-11 stsp fprintf(stderr,
3770 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3771 d0eebce4 2019-03-11 stsp getprogname());
3772 d0eebce4 2019-03-11 stsp exit(1);
3773 d0eebce4 2019-03-11 stsp }
3774 d0eebce4 2019-03-11 stsp
3775 d0eebce4 2019-03-11 stsp static const struct got_error *
3776 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3777 d0eebce4 2019-03-11 stsp {
3778 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3779 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3780 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3781 d0eebce4 2019-03-11 stsp
3782 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3783 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3784 d0eebce4 2019-03-11 stsp if (err)
3785 d0eebce4 2019-03-11 stsp return err;
3786 d0eebce4 2019-03-11 stsp
3787 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3788 d0eebce4 2019-03-11 stsp char *refstr;
3789 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3790 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3791 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3792 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3793 d0eebce4 2019-03-11 stsp free(refstr);
3794 d0eebce4 2019-03-11 stsp }
3795 d0eebce4 2019-03-11 stsp
3796 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3797 d0eebce4 2019-03-11 stsp return NULL;
3798 d0eebce4 2019-03-11 stsp }
3799 d0eebce4 2019-03-11 stsp
3800 d0eebce4 2019-03-11 stsp static const struct got_error *
3801 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3802 d0eebce4 2019-03-11 stsp {
3803 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3804 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3805 d0eebce4 2019-03-11 stsp
3806 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3807 d0eebce4 2019-03-11 stsp if (err)
3808 d0eebce4 2019-03-11 stsp return err;
3809 d0eebce4 2019-03-11 stsp
3810 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3811 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3812 d0eebce4 2019-03-11 stsp return err;
3813 d0eebce4 2019-03-11 stsp }
3814 d0eebce4 2019-03-11 stsp
3815 d0eebce4 2019-03-11 stsp static const struct got_error *
3816 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3817 d0eebce4 2019-03-11 stsp {
3818 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3819 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3820 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3821 d1644381 2019-07-14 stsp
3822 d1644381 2019-07-14 stsp /*
3823 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3824 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3825 d1644381 2019-07-14 stsp * an unintended typo.
3826 d1644381 2019-07-14 stsp */
3827 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3828 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3829 d0eebce4 2019-03-11 stsp
3830 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3831 dd88155e 2019-06-29 stsp repo);
3832 d83d9d5c 2019-05-13 stsp if (err) {
3833 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3834 d0eebce4 2019-03-11 stsp
3835 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3836 d83d9d5c 2019-05-13 stsp return err;
3837 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3838 d83d9d5c 2019-05-13 stsp if (err)
3839 d83d9d5c 2019-05-13 stsp return err;
3840 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3841 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3842 d83d9d5c 2019-05-13 stsp if (err)
3843 d83d9d5c 2019-05-13 stsp return err;
3844 d83d9d5c 2019-05-13 stsp }
3845 d83d9d5c 2019-05-13 stsp
3846 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3847 d0eebce4 2019-03-11 stsp if (err)
3848 d0eebce4 2019-03-11 stsp goto done;
3849 d0eebce4 2019-03-11 stsp
3850 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3851 d0eebce4 2019-03-11 stsp done:
3852 d0eebce4 2019-03-11 stsp if (ref)
3853 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3854 d0eebce4 2019-03-11 stsp free(id);
3855 d1c1ae5f 2019-08-12 stsp return err;
3856 d1c1ae5f 2019-08-12 stsp }
3857 d1c1ae5f 2019-08-12 stsp
3858 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3859 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3860 d1c1ae5f 2019-08-12 stsp {
3861 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3862 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3863 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3864 d1c1ae5f 2019-08-12 stsp
3865 d1c1ae5f 2019-08-12 stsp /*
3866 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3867 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3868 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3869 d1c1ae5f 2019-08-12 stsp */
3870 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3871 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3872 d1c1ae5f 2019-08-12 stsp
3873 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3874 d1c1ae5f 2019-08-12 stsp if (err)
3875 d1c1ae5f 2019-08-12 stsp return err;
3876 d1c1ae5f 2019-08-12 stsp
3877 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3878 d1c1ae5f 2019-08-12 stsp if (err)
3879 d1c1ae5f 2019-08-12 stsp goto done;
3880 d1c1ae5f 2019-08-12 stsp
3881 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3882 d1c1ae5f 2019-08-12 stsp done:
3883 d1c1ae5f 2019-08-12 stsp if (target_ref)
3884 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3885 d1c1ae5f 2019-08-12 stsp if (ref)
3886 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3887 d0eebce4 2019-03-11 stsp return err;
3888 d0eebce4 2019-03-11 stsp }
3889 d0eebce4 2019-03-11 stsp
3890 d0eebce4 2019-03-11 stsp static const struct got_error *
3891 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3892 d0eebce4 2019-03-11 stsp {
3893 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3894 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3895 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3896 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3897 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3898 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3899 d0eebce4 2019-03-11 stsp
3900 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3901 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3902 d0eebce4 2019-03-11 stsp switch (ch) {
3903 d0eebce4 2019-03-11 stsp case 'd':
3904 d0eebce4 2019-03-11 stsp delref = optarg;
3905 d0eebce4 2019-03-11 stsp break;
3906 d0eebce4 2019-03-11 stsp case 'r':
3907 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3908 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3909 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3910 9ba1d308 2019-10-21 stsp optarg);
3911 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3912 d0eebce4 2019-03-11 stsp break;
3913 d0eebce4 2019-03-11 stsp case 'l':
3914 d0eebce4 2019-03-11 stsp do_list = 1;
3915 d1c1ae5f 2019-08-12 stsp break;
3916 d1c1ae5f 2019-08-12 stsp case 's':
3917 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3918 d0eebce4 2019-03-11 stsp break;
3919 d0eebce4 2019-03-11 stsp default:
3920 d0eebce4 2019-03-11 stsp usage_ref();
3921 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3922 d0eebce4 2019-03-11 stsp }
3923 d0eebce4 2019-03-11 stsp }
3924 d0eebce4 2019-03-11 stsp
3925 d0eebce4 2019-03-11 stsp if (do_list && delref)
3926 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3927 d0eebce4 2019-03-11 stsp
3928 d0eebce4 2019-03-11 stsp argc -= optind;
3929 d0eebce4 2019-03-11 stsp argv += optind;
3930 d0eebce4 2019-03-11 stsp
3931 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3932 d1c1ae5f 2019-08-12 stsp if (create_symref)
3933 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3934 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3935 d0eebce4 2019-03-11 stsp if (argc > 0)
3936 d0eebce4 2019-03-11 stsp usage_ref();
3937 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3938 d0eebce4 2019-03-11 stsp usage_ref();
3939 d0eebce4 2019-03-11 stsp
3940 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3941 e0b57350 2019-03-12 stsp if (do_list) {
3942 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3943 e0b57350 2019-03-12 stsp NULL) == -1)
3944 e0b57350 2019-03-12 stsp err(1, "pledge");
3945 e0b57350 2019-03-12 stsp } else {
3946 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3947 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3948 e0b57350 2019-03-12 stsp err(1, "pledge");
3949 e0b57350 2019-03-12 stsp }
3950 d0eebce4 2019-03-11 stsp #endif
3951 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3952 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3953 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3954 d0eebce4 2019-03-11 stsp goto done;
3955 d0eebce4 2019-03-11 stsp }
3956 d0eebce4 2019-03-11 stsp
3957 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3958 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3959 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3960 d0eebce4 2019-03-11 stsp goto done;
3961 d0eebce4 2019-03-11 stsp else
3962 d0eebce4 2019-03-11 stsp error = NULL;
3963 d0eebce4 2019-03-11 stsp if (worktree) {
3964 d0eebce4 2019-03-11 stsp repo_path =
3965 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3966 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3967 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3968 d0eebce4 2019-03-11 stsp if (error)
3969 d0eebce4 2019-03-11 stsp goto done;
3970 d0eebce4 2019-03-11 stsp } else {
3971 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3972 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3973 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3974 d0eebce4 2019-03-11 stsp goto done;
3975 d0eebce4 2019-03-11 stsp }
3976 d0eebce4 2019-03-11 stsp }
3977 d0eebce4 2019-03-11 stsp }
3978 d0eebce4 2019-03-11 stsp
3979 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3980 d0eebce4 2019-03-11 stsp if (error != NULL)
3981 d0eebce4 2019-03-11 stsp goto done;
3982 d0eebce4 2019-03-11 stsp
3983 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3984 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3985 c02c541e 2019-03-29 stsp if (error)
3986 c02c541e 2019-03-29 stsp goto done;
3987 c02c541e 2019-03-29 stsp
3988 d0eebce4 2019-03-11 stsp if (do_list)
3989 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3990 d0eebce4 2019-03-11 stsp else if (delref)
3991 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3992 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3993 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3994 d0eebce4 2019-03-11 stsp else
3995 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3996 4e759de4 2019-06-26 stsp done:
3997 4e759de4 2019-06-26 stsp if (repo)
3998 4e759de4 2019-06-26 stsp got_repo_close(repo);
3999 4e759de4 2019-06-26 stsp if (worktree)
4000 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4001 4e759de4 2019-06-26 stsp free(cwd);
4002 4e759de4 2019-06-26 stsp free(repo_path);
4003 4e759de4 2019-06-26 stsp return error;
4004 4e759de4 2019-06-26 stsp }
4005 4e759de4 2019-06-26 stsp
4006 4e759de4 2019-06-26 stsp __dead static void
4007 4e759de4 2019-06-26 stsp usage_branch(void)
4008 4e759de4 2019-06-26 stsp {
4009 4e759de4 2019-06-26 stsp fprintf(stderr,
4010 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4011 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4012 4e759de4 2019-06-26 stsp exit(1);
4013 4e759de4 2019-06-26 stsp }
4014 4e759de4 2019-06-26 stsp
4015 4e759de4 2019-06-26 stsp static const struct got_error *
4016 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4017 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4018 4e99b47e 2019-10-04 stsp {
4019 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4020 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4021 4e99b47e 2019-10-04 stsp char *refstr;
4022 4e99b47e 2019-10-04 stsp
4023 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4024 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4025 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4026 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4027 4e99b47e 2019-10-04 stsp
4028 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4029 4e99b47e 2019-10-04 stsp if (err)
4030 4e99b47e 2019-10-04 stsp return err;
4031 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4032 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4033 4e99b47e 2019-10-04 stsp marker = "* ";
4034 4e99b47e 2019-10-04 stsp else
4035 4e99b47e 2019-10-04 stsp marker = "~ ";
4036 4e99b47e 2019-10-04 stsp free(id);
4037 4e99b47e 2019-10-04 stsp }
4038 4e99b47e 2019-10-04 stsp
4039 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4040 4e99b47e 2019-10-04 stsp refname += 11;
4041 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4042 4e99b47e 2019-10-04 stsp refname += 18;
4043 4e99b47e 2019-10-04 stsp
4044 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4045 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4046 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4047 4e99b47e 2019-10-04 stsp
4048 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4049 4e99b47e 2019-10-04 stsp free(refstr);
4050 4e99b47e 2019-10-04 stsp return NULL;
4051 4e99b47e 2019-10-04 stsp }
4052 4e99b47e 2019-10-04 stsp
4053 4e99b47e 2019-10-04 stsp static const struct got_error *
4054 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4055 ad89fa31 2019-10-04 stsp {
4056 ad89fa31 2019-10-04 stsp const char *refname;
4057 ad89fa31 2019-10-04 stsp
4058 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4059 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4060 ad89fa31 2019-10-04 stsp
4061 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4062 ad89fa31 2019-10-04 stsp
4063 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4064 ad89fa31 2019-10-04 stsp refname += 11;
4065 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4066 ad89fa31 2019-10-04 stsp refname += 18;
4067 ad89fa31 2019-10-04 stsp
4068 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4069 ad89fa31 2019-10-04 stsp
4070 ad89fa31 2019-10-04 stsp return NULL;
4071 ad89fa31 2019-10-04 stsp }
4072 ad89fa31 2019-10-04 stsp
4073 ad89fa31 2019-10-04 stsp static const struct got_error *
4074 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4075 4e759de4 2019-06-26 stsp {
4076 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4077 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4078 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4079 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4080 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4081 4e759de4 2019-06-26 stsp
4082 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4083 ba882ee3 2019-07-11 stsp
4084 4e99b47e 2019-10-04 stsp if (worktree) {
4085 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4086 4e99b47e 2019-10-04 stsp worktree);
4087 4e99b47e 2019-10-04 stsp if (err)
4088 4e99b47e 2019-10-04 stsp return err;
4089 4e99b47e 2019-10-04 stsp
4090 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4091 4e99b47e 2019-10-04 stsp worktree);
4092 4e99b47e 2019-10-04 stsp if (err)
4093 4e99b47e 2019-10-04 stsp return err;
4094 4e99b47e 2019-10-04 stsp
4095 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4096 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4097 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4098 4e99b47e 2019-10-04 stsp if (err)
4099 4e99b47e 2019-10-04 stsp return err;
4100 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4101 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4102 4e99b47e 2019-10-04 stsp }
4103 4e99b47e 2019-10-04 stsp }
4104 4e99b47e 2019-10-04 stsp
4105 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4106 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4107 4e759de4 2019-06-26 stsp if (err)
4108 4e759de4 2019-06-26 stsp return err;
4109 4e759de4 2019-06-26 stsp
4110 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4111 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4112 4e759de4 2019-06-26 stsp
4113 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4114 4e759de4 2019-06-26 stsp return NULL;
4115 4e759de4 2019-06-26 stsp }
4116 4e759de4 2019-06-26 stsp
4117 4e759de4 2019-06-26 stsp static const struct got_error *
4118 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4119 45cd4e47 2019-08-25 stsp const char *branch_name)
4120 4e759de4 2019-06-26 stsp {
4121 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4122 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4123 4e759de4 2019-06-26 stsp char *refname;
4124 4e759de4 2019-06-26 stsp
4125 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4126 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4127 4e759de4 2019-06-26 stsp
4128 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4129 4e759de4 2019-06-26 stsp if (err)
4130 4e759de4 2019-06-26 stsp goto done;
4131 4e759de4 2019-06-26 stsp
4132 45cd4e47 2019-08-25 stsp if (worktree &&
4133 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4134 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4135 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4136 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4137 45cd4e47 2019-08-25 stsp goto done;
4138 45cd4e47 2019-08-25 stsp }
4139 45cd4e47 2019-08-25 stsp
4140 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4141 4e759de4 2019-06-26 stsp done:
4142 45cd4e47 2019-08-25 stsp if (ref)
4143 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4144 4e759de4 2019-06-26 stsp free(refname);
4145 4e759de4 2019-06-26 stsp return err;
4146 4e759de4 2019-06-26 stsp }
4147 4e759de4 2019-06-26 stsp
4148 4e759de4 2019-06-26 stsp static const struct got_error *
4149 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4150 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4151 4e759de4 2019-06-26 stsp {
4152 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4153 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4154 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4155 d3f84d51 2019-07-11 stsp
4156 d3f84d51 2019-07-11 stsp /*
4157 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4158 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4159 d3f84d51 2019-07-11 stsp * an unintended typo.
4160 d3f84d51 2019-07-11 stsp */
4161 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4162 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4163 4e759de4 2019-06-26 stsp
4164 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4165 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4166 4e759de4 2019-06-26 stsp goto done;
4167 4e759de4 2019-06-26 stsp }
4168 4e759de4 2019-06-26 stsp
4169 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4170 4e759de4 2019-06-26 stsp if (err == NULL) {
4171 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4172 4e759de4 2019-06-26 stsp goto done;
4173 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4174 4e759de4 2019-06-26 stsp goto done;
4175 4e759de4 2019-06-26 stsp
4176 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4177 4e759de4 2019-06-26 stsp if (err)
4178 4e759de4 2019-06-26 stsp goto done;
4179 4e759de4 2019-06-26 stsp
4180 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4181 d0eebce4 2019-03-11 stsp done:
4182 4e759de4 2019-06-26 stsp if (ref)
4183 4e759de4 2019-06-26 stsp got_ref_close(ref);
4184 4e759de4 2019-06-26 stsp free(base_refname);
4185 4e759de4 2019-06-26 stsp free(refname);
4186 4e759de4 2019-06-26 stsp return err;
4187 4e759de4 2019-06-26 stsp }
4188 4e759de4 2019-06-26 stsp
4189 4e759de4 2019-06-26 stsp static const struct got_error *
4190 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4191 4e759de4 2019-06-26 stsp {
4192 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4193 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4194 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4195 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4196 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4197 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4198 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4199 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4200 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4201 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4202 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4203 4e759de4 2019-06-26 stsp
4204 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4205 da76fce2 2020-02-24 stsp
4206 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4207 4e759de4 2019-06-26 stsp switch (ch) {
4208 a74f7e83 2019-11-10 stsp case 'c':
4209 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4210 a74f7e83 2019-11-10 stsp break;
4211 4e759de4 2019-06-26 stsp case 'd':
4212 4e759de4 2019-06-26 stsp delref = optarg;
4213 4e759de4 2019-06-26 stsp break;
4214 4e759de4 2019-06-26 stsp case 'r':
4215 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4216 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4217 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4218 9ba1d308 2019-10-21 stsp optarg);
4219 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4220 4e759de4 2019-06-26 stsp break;
4221 4e759de4 2019-06-26 stsp case 'l':
4222 4e759de4 2019-06-26 stsp do_list = 1;
4223 da76fce2 2020-02-24 stsp break;
4224 da76fce2 2020-02-24 stsp case 'n':
4225 da76fce2 2020-02-24 stsp do_update = 0;
4226 4e759de4 2019-06-26 stsp break;
4227 4e759de4 2019-06-26 stsp default:
4228 4e759de4 2019-06-26 stsp usage_branch();
4229 4e759de4 2019-06-26 stsp /* NOTREACHED */
4230 4e759de4 2019-06-26 stsp }
4231 4e759de4 2019-06-26 stsp }
4232 4e759de4 2019-06-26 stsp
4233 4e759de4 2019-06-26 stsp if (do_list && delref)
4234 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4235 4e759de4 2019-06-26 stsp
4236 4e759de4 2019-06-26 stsp argc -= optind;
4237 4e759de4 2019-06-26 stsp argv += optind;
4238 ad89fa31 2019-10-04 stsp
4239 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4240 ad89fa31 2019-10-04 stsp do_show = 1;
4241 4e759de4 2019-06-26 stsp
4242 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4243 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4244 a74f7e83 2019-11-10 stsp
4245 4e759de4 2019-06-26 stsp if (do_list || delref) {
4246 4e759de4 2019-06-26 stsp if (argc > 0)
4247 4e759de4 2019-06-26 stsp usage_branch();
4248 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4249 4e759de4 2019-06-26 stsp usage_branch();
4250 4e759de4 2019-06-26 stsp
4251 4e759de4 2019-06-26 stsp #ifndef PROFILE
4252 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4253 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4254 4e759de4 2019-06-26 stsp NULL) == -1)
4255 4e759de4 2019-06-26 stsp err(1, "pledge");
4256 4e759de4 2019-06-26 stsp } else {
4257 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4258 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4259 4e759de4 2019-06-26 stsp err(1, "pledge");
4260 4e759de4 2019-06-26 stsp }
4261 4e759de4 2019-06-26 stsp #endif
4262 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4263 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4264 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4265 4e759de4 2019-06-26 stsp goto done;
4266 4e759de4 2019-06-26 stsp }
4267 4e759de4 2019-06-26 stsp
4268 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4269 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4270 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4271 4e759de4 2019-06-26 stsp goto done;
4272 4e759de4 2019-06-26 stsp else
4273 4e759de4 2019-06-26 stsp error = NULL;
4274 4e759de4 2019-06-26 stsp if (worktree) {
4275 4e759de4 2019-06-26 stsp repo_path =
4276 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4277 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4278 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4279 4e759de4 2019-06-26 stsp if (error)
4280 4e759de4 2019-06-26 stsp goto done;
4281 4e759de4 2019-06-26 stsp } else {
4282 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4283 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4284 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4285 4e759de4 2019-06-26 stsp goto done;
4286 4e759de4 2019-06-26 stsp }
4287 4e759de4 2019-06-26 stsp }
4288 4e759de4 2019-06-26 stsp }
4289 4e759de4 2019-06-26 stsp
4290 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4291 4e759de4 2019-06-26 stsp if (error != NULL)
4292 4e759de4 2019-06-26 stsp goto done;
4293 4e759de4 2019-06-26 stsp
4294 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4295 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4296 4e759de4 2019-06-26 stsp if (error)
4297 4e759de4 2019-06-26 stsp goto done;
4298 4e759de4 2019-06-26 stsp
4299 ad89fa31 2019-10-04 stsp if (do_show)
4300 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4301 ad89fa31 2019-10-04 stsp else if (do_list)
4302 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4303 4e759de4 2019-06-26 stsp else if (delref)
4304 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4305 4e759de4 2019-06-26 stsp else {
4306 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4307 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4308 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4309 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4310 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4311 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4312 a74f7e83 2019-11-10 stsp if (error)
4313 a74f7e83 2019-11-10 stsp goto done;
4314 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4315 da76fce2 2020-02-24 stsp if (error)
4316 da76fce2 2020-02-24 stsp goto done;
4317 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4318 da76fce2 2020-02-24 stsp int did_something = 0;
4319 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4320 da76fce2 2020-02-24 stsp
4321 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4322 da76fce2 2020-02-24 stsp if (error)
4323 da76fce2 2020-02-24 stsp goto done;
4324 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4325 da76fce2 2020-02-24 stsp worktree);
4326 da76fce2 2020-02-24 stsp if (error)
4327 da76fce2 2020-02-24 stsp goto done;
4328 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4329 da76fce2 2020-02-24 stsp == -1) {
4330 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4331 da76fce2 2020-02-24 stsp goto done;
4332 da76fce2 2020-02-24 stsp }
4333 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4334 da76fce2 2020-02-24 stsp free(branch_refname);
4335 da76fce2 2020-02-24 stsp if (error)
4336 da76fce2 2020-02-24 stsp goto done;
4337 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4338 da76fce2 2020-02-24 stsp repo);
4339 da76fce2 2020-02-24 stsp if (error)
4340 da76fce2 2020-02-24 stsp goto done;
4341 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4342 da76fce2 2020-02-24 stsp commit_id);
4343 da76fce2 2020-02-24 stsp if (error)
4344 da76fce2 2020-02-24 stsp goto done;
4345 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4346 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4347 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4348 da76fce2 2020-02-24 stsp if (error)
4349 da76fce2 2020-02-24 stsp goto done;
4350 da76fce2 2020-02-24 stsp if (did_something)
4351 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4352 da76fce2 2020-02-24 stsp }
4353 4e759de4 2019-06-26 stsp }
4354 4e759de4 2019-06-26 stsp done:
4355 da76fce2 2020-02-24 stsp if (ref)
4356 da76fce2 2020-02-24 stsp got_ref_close(ref);
4357 d0eebce4 2019-03-11 stsp if (repo)
4358 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4359 d0eebce4 2019-03-11 stsp if (worktree)
4360 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4361 d0eebce4 2019-03-11 stsp free(cwd);
4362 d0eebce4 2019-03-11 stsp free(repo_path);
4363 da76fce2 2020-02-24 stsp free(commit_id);
4364 da76fce2 2020-02-24 stsp free(commit_id_str);
4365 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4366 da76fce2 2020-02-24 stsp free((char *)pe->path);
4367 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4368 d00136be 2019-03-26 stsp return error;
4369 d00136be 2019-03-26 stsp }
4370 d00136be 2019-03-26 stsp
4371 8e7bd50a 2019-08-22 stsp
4372 d00136be 2019-03-26 stsp __dead static void
4373 8e7bd50a 2019-08-22 stsp usage_tag(void)
4374 8e7bd50a 2019-08-22 stsp {
4375 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4376 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4377 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4378 8e7bd50a 2019-08-22 stsp exit(1);
4379 b8bad2ba 2019-08-23 stsp }
4380 b8bad2ba 2019-08-23 stsp
4381 b8bad2ba 2019-08-23 stsp #if 0
4382 b8bad2ba 2019-08-23 stsp static const struct got_error *
4383 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4384 b8bad2ba 2019-08-23 stsp {
4385 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4386 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4387 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4388 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4389 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4390 b8bad2ba 2019-08-23 stsp
4391 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4392 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4393 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4394 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4395 b8bad2ba 2019-08-23 stsp if (err)
4396 b8bad2ba 2019-08-23 stsp return err;
4397 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4398 b8bad2ba 2019-08-23 stsp continue;
4399 b8bad2ba 2019-08-23 stsp } else {
4400 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4401 b8bad2ba 2019-08-23 stsp if (err)
4402 b8bad2ba 2019-08-23 stsp break;
4403 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4404 b8bad2ba 2019-08-23 stsp free(re_id);
4405 b8bad2ba 2019-08-23 stsp if (err)
4406 b8bad2ba 2019-08-23 stsp break;
4407 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4408 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4409 b8bad2ba 2019-08-23 stsp }
4410 b8bad2ba 2019-08-23 stsp
4411 b8bad2ba 2019-08-23 stsp while (se) {
4412 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4413 b8bad2ba 2019-08-23 stsp if (err)
4414 b8bad2ba 2019-08-23 stsp break;
4415 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4416 b8bad2ba 2019-08-23 stsp free(se_id);
4417 b8bad2ba 2019-08-23 stsp if (err)
4418 b8bad2ba 2019-08-23 stsp break;
4419 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4420 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4421 b8bad2ba 2019-08-23 stsp
4422 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4423 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4424 b8bad2ba 2019-08-23 stsp if (err)
4425 b8bad2ba 2019-08-23 stsp return err;
4426 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4427 b8bad2ba 2019-08-23 stsp break;
4428 b8bad2ba 2019-08-23 stsp }
4429 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4430 b8bad2ba 2019-08-23 stsp continue;
4431 b8bad2ba 2019-08-23 stsp }
4432 b8bad2ba 2019-08-23 stsp }
4433 b8bad2ba 2019-08-23 stsp done:
4434 b8bad2ba 2019-08-23 stsp return err;
4435 8e7bd50a 2019-08-22 stsp }
4436 b8bad2ba 2019-08-23 stsp #endif
4437 b8bad2ba 2019-08-23 stsp
4438 b8bad2ba 2019-08-23 stsp static const struct got_error *
4439 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4440 8e7bd50a 2019-08-22 stsp {
4441 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4442 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4443 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4444 8e7bd50a 2019-08-22 stsp
4445 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4446 8e7bd50a 2019-08-22 stsp
4447 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4448 8e7bd50a 2019-08-22 stsp if (err)
4449 8e7bd50a 2019-08-22 stsp return err;
4450 8e7bd50a 2019-08-22 stsp
4451 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4452 8e7bd50a 2019-08-22 stsp const char *refname;
4453 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4454 8e7bd50a 2019-08-22 stsp char datebuf[26];
4455 d4efa91b 2020-01-14 stsp const char *tagger;
4456 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4457 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4458 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4459 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4460 8e7bd50a 2019-08-22 stsp
4461 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4462 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4463 8e7bd50a 2019-08-22 stsp continue;
4464 8e7bd50a 2019-08-22 stsp refname += 10;
4465 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4466 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4467 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4468 8e7bd50a 2019-08-22 stsp break;
4469 8e7bd50a 2019-08-22 stsp }
4470 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4471 8e7bd50a 2019-08-22 stsp free(refstr);
4472 8e7bd50a 2019-08-22 stsp
4473 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4474 8e7bd50a 2019-08-22 stsp if (err)
4475 8e7bd50a 2019-08-22 stsp break;
4476 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4477 d4efa91b 2020-01-14 stsp if (err) {
4478 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4479 d4efa91b 2020-01-14 stsp free(id);
4480 d4efa91b 2020-01-14 stsp break;
4481 d4efa91b 2020-01-14 stsp }
4482 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4483 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4484 d4efa91b 2020-01-14 stsp if (err) {
4485 d4efa91b 2020-01-14 stsp free(id);
4486 d4efa91b 2020-01-14 stsp break;
4487 d4efa91b 2020-01-14 stsp }
4488 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4489 d4efa91b 2020-01-14 stsp tagger_time =
4490 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4491 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4492 d4efa91b 2020-01-14 stsp free(id);
4493 d4efa91b 2020-01-14 stsp if (err)
4494 d4efa91b 2020-01-14 stsp break;
4495 d4efa91b 2020-01-14 stsp } else {
4496 d4efa91b 2020-01-14 stsp free(id);
4497 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4498 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4499 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4500 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4501 d4efa91b 2020-01-14 stsp if (err)
4502 d4efa91b 2020-01-14 stsp break;
4503 d4efa91b 2020-01-14 stsp }
4504 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4505 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4506 2417344c 2019-08-23 stsp if (datestr)
4507 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4508 d4efa91b 2020-01-14 stsp if (commit)
4509 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4510 d4efa91b 2020-01-14 stsp else {
4511 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4512 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4513 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4514 d4efa91b 2020-01-14 stsp id_str);
4515 d4efa91b 2020-01-14 stsp break;
4516 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4517 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4518 d4efa91b 2020-01-14 stsp id_str);
4519 d4efa91b 2020-01-14 stsp break;
4520 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4521 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4522 d4efa91b 2020-01-14 stsp id_str);
4523 d4efa91b 2020-01-14 stsp break;
4524 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4525 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4526 d4efa91b 2020-01-14 stsp id_str);
4527 d4efa91b 2020-01-14 stsp break;
4528 d4efa91b 2020-01-14 stsp default:
4529 d4efa91b 2020-01-14 stsp break;
4530 d4efa91b 2020-01-14 stsp }
4531 8e7bd50a 2019-08-22 stsp }
4532 8e7bd50a 2019-08-22 stsp free(id_str);
4533 d4efa91b 2020-01-14 stsp if (commit) {
4534 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4535 d4efa91b 2020-01-14 stsp if (err)
4536 d4efa91b 2020-01-14 stsp break;
4537 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4538 d4efa91b 2020-01-14 stsp } else {
4539 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4540 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4541 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4542 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4543 d4efa91b 2020-01-14 stsp break;
4544 d4efa91b 2020-01-14 stsp }
4545 8e7bd50a 2019-08-22 stsp }
4546 8e7bd50a 2019-08-22 stsp
4547 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4548 8e7bd50a 2019-08-22 stsp do {
4549 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4550 8e7bd50a 2019-08-22 stsp if (line)
4551 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4552 8e7bd50a 2019-08-22 stsp } while (line);
4553 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4554 8e7bd50a 2019-08-22 stsp }
4555 8e7bd50a 2019-08-22 stsp
4556 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4557 8e7bd50a 2019-08-22 stsp return NULL;
4558 8e7bd50a 2019-08-22 stsp }
4559 8e7bd50a 2019-08-22 stsp
4560 8e7bd50a 2019-08-22 stsp static const struct got_error *
4561 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4562 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4563 8e7bd50a 2019-08-22 stsp {
4564 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4565 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4566 f372d5cd 2019-10-21 stsp char *editor = NULL;
4567 8e7bd50a 2019-08-22 stsp int fd = -1;
4568 8e7bd50a 2019-08-22 stsp
4569 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4570 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4571 8e7bd50a 2019-08-22 stsp goto done;
4572 8e7bd50a 2019-08-22 stsp }
4573 8e7bd50a 2019-08-22 stsp
4574 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4575 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4576 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4577 8e7bd50a 2019-08-22 stsp goto done;
4578 8e7bd50a 2019-08-22 stsp }
4579 8e7bd50a 2019-08-22 stsp
4580 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4581 8e7bd50a 2019-08-22 stsp if (err)
4582 8e7bd50a 2019-08-22 stsp goto done;
4583 8e7bd50a 2019-08-22 stsp
4584 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4585 8e7bd50a 2019-08-22 stsp close(fd);
4586 8e7bd50a 2019-08-22 stsp
4587 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4588 8e7bd50a 2019-08-22 stsp if (err)
4589 8e7bd50a 2019-08-22 stsp goto done;
4590 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4591 8e7bd50a 2019-08-22 stsp done:
4592 8e7bd50a 2019-08-22 stsp free(initial_content);
4593 8e7bd50a 2019-08-22 stsp free(template);
4594 8e7bd50a 2019-08-22 stsp free(editor);
4595 8e7bd50a 2019-08-22 stsp
4596 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4597 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4598 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4599 8e7bd50a 2019-08-22 stsp if (err) {
4600 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4601 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4602 8e7bd50a 2019-08-22 stsp }
4603 8e7bd50a 2019-08-22 stsp }
4604 8e7bd50a 2019-08-22 stsp return err;
4605 8e7bd50a 2019-08-22 stsp }
4606 8e7bd50a 2019-08-22 stsp
4607 8e7bd50a 2019-08-22 stsp static const struct got_error *
4608 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4609 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4610 8e7bd50a 2019-08-22 stsp {
4611 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4612 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4613 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4614 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4615 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4616 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4617 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4618 8e7bd50a 2019-08-22 stsp
4619 8e7bd50a 2019-08-22 stsp /*
4620 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4621 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4622 8e7bd50a 2019-08-22 stsp * an unintended typo.
4623 8e7bd50a 2019-08-22 stsp */
4624 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4625 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4626 8e7bd50a 2019-08-22 stsp
4627 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4628 8e7bd50a 2019-08-22 stsp if (err)
4629 8e7bd50a 2019-08-22 stsp return err;
4630 8e7bd50a 2019-08-22 stsp
4631 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4632 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4633 8e7bd50a 2019-08-22 stsp if (err)
4634 8e7bd50a 2019-08-22 stsp goto done;
4635 8e7bd50a 2019-08-22 stsp
4636 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4637 8e7bd50a 2019-08-22 stsp if (err)
4638 8e7bd50a 2019-08-22 stsp goto done;
4639 8e7bd50a 2019-08-22 stsp
4640 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4641 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4642 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4643 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4644 8e7bd50a 2019-08-22 stsp goto done;
4645 8e7bd50a 2019-08-22 stsp }
4646 8e7bd50a 2019-08-22 stsp tag_name += 10;
4647 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4648 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4649 8e7bd50a 2019-08-22 stsp goto done;
4650 8e7bd50a 2019-08-22 stsp }
4651 8e7bd50a 2019-08-22 stsp
4652 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4653 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4654 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4655 8e7bd50a 2019-08-22 stsp goto done;
4656 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4657 8e7bd50a 2019-08-22 stsp goto done;
4658 8e7bd50a 2019-08-22 stsp
4659 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4660 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4661 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4662 f372d5cd 2019-10-21 stsp if (err) {
4663 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4664 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
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
4670 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4671 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4672 f372d5cd 2019-10-21 stsp if (err) {
4673 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4674 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4675 8e7bd50a 2019-08-22 stsp goto done;
4676 f372d5cd 2019-10-21 stsp }
4677 8e7bd50a 2019-08-22 stsp
4678 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4679 f372d5cd 2019-10-21 stsp if (err) {
4680 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4681 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4682 8e7bd50a 2019-08-22 stsp goto done;
4683 f372d5cd 2019-10-21 stsp }
4684 8e7bd50a 2019-08-22 stsp
4685 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4686 f372d5cd 2019-10-21 stsp if (err) {
4687 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4688 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4689 f372d5cd 2019-10-21 stsp goto done;
4690 f372d5cd 2019-10-21 stsp }
4691 8e7bd50a 2019-08-22 stsp
4692 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4693 f372d5cd 2019-10-21 stsp if (err) {
4694 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4695 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4696 f372d5cd 2019-10-21 stsp goto done;
4697 8e7bd50a 2019-08-22 stsp }
4698 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4699 8e7bd50a 2019-08-22 stsp done:
4700 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4701 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4702 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4703 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4704 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4705 f372d5cd 2019-10-21 stsp free(tag_id_str);
4706 8e7bd50a 2019-08-22 stsp if (ref)
4707 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4708 8e7bd50a 2019-08-22 stsp free(commit_id);
4709 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4710 8e7bd50a 2019-08-22 stsp free(refname);
4711 8e7bd50a 2019-08-22 stsp free(tagmsg);
4712 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4713 aba9c984 2019-09-08 stsp free(tagger);
4714 8e7bd50a 2019-08-22 stsp return err;
4715 8e7bd50a 2019-08-22 stsp }
4716 8e7bd50a 2019-08-22 stsp
4717 8e7bd50a 2019-08-22 stsp static const struct got_error *
4718 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4719 8e7bd50a 2019-08-22 stsp {
4720 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4721 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4722 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4723 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4724 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4725 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4726 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4727 8e7bd50a 2019-08-22 stsp
4728 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4729 8e7bd50a 2019-08-22 stsp switch (ch) {
4730 80106605 2020-02-24 stsp case 'c':
4731 80106605 2020-02-24 stsp commit_id_arg = optarg;
4732 80106605 2020-02-24 stsp break;
4733 8e7bd50a 2019-08-22 stsp case 'm':
4734 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4735 8e7bd50a 2019-08-22 stsp break;
4736 8e7bd50a 2019-08-22 stsp case 'r':
4737 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4738 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4739 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4740 9ba1d308 2019-10-21 stsp optarg);
4741 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4742 8e7bd50a 2019-08-22 stsp break;
4743 8e7bd50a 2019-08-22 stsp case 'l':
4744 8e7bd50a 2019-08-22 stsp do_list = 1;
4745 8e7bd50a 2019-08-22 stsp break;
4746 8e7bd50a 2019-08-22 stsp default:
4747 8e7bd50a 2019-08-22 stsp usage_tag();
4748 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4749 8e7bd50a 2019-08-22 stsp }
4750 8e7bd50a 2019-08-22 stsp }
4751 8e7bd50a 2019-08-22 stsp
4752 8e7bd50a 2019-08-22 stsp argc -= optind;
4753 8e7bd50a 2019-08-22 stsp argv += optind;
4754 8e7bd50a 2019-08-22 stsp
4755 8e7bd50a 2019-08-22 stsp if (do_list) {
4756 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4757 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4758 8e7bd50a 2019-08-22 stsp if (tagmsg)
4759 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4760 8e7bd50a 2019-08-22 stsp if (argc > 0)
4761 8e7bd50a 2019-08-22 stsp usage_tag();
4762 80106605 2020-02-24 stsp } else if (argc != 1)
4763 8e7bd50a 2019-08-22 stsp usage_tag();
4764 80106605 2020-02-24 stsp
4765 a2887370 2019-08-23 stsp tag_name = argv[0];
4766 8e7bd50a 2019-08-22 stsp
4767 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4768 8e7bd50a 2019-08-22 stsp if (do_list) {
4769 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4770 8e7bd50a 2019-08-22 stsp NULL) == -1)
4771 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4772 8e7bd50a 2019-08-22 stsp } else {
4773 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4774 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4775 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4776 8e7bd50a 2019-08-22 stsp }
4777 8e7bd50a 2019-08-22 stsp #endif
4778 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4779 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4780 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4781 8e7bd50a 2019-08-22 stsp goto done;
4782 8e7bd50a 2019-08-22 stsp }
4783 8e7bd50a 2019-08-22 stsp
4784 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4785 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4786 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4787 8e7bd50a 2019-08-22 stsp goto done;
4788 8e7bd50a 2019-08-22 stsp else
4789 8e7bd50a 2019-08-22 stsp error = NULL;
4790 8e7bd50a 2019-08-22 stsp if (worktree) {
4791 8e7bd50a 2019-08-22 stsp repo_path =
4792 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4793 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4794 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4795 8e7bd50a 2019-08-22 stsp if (error)
4796 8e7bd50a 2019-08-22 stsp goto done;
4797 8e7bd50a 2019-08-22 stsp } else {
4798 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4799 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4800 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4801 8e7bd50a 2019-08-22 stsp goto done;
4802 8e7bd50a 2019-08-22 stsp }
4803 8e7bd50a 2019-08-22 stsp }
4804 8e7bd50a 2019-08-22 stsp }
4805 8e7bd50a 2019-08-22 stsp
4806 8e7bd50a 2019-08-22 stsp if (do_list) {
4807 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4808 c9956ddf 2019-09-08 stsp if (error != NULL)
4809 c9956ddf 2019-09-08 stsp goto done;
4810 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4811 8e7bd50a 2019-08-22 stsp if (error)
4812 8e7bd50a 2019-08-22 stsp goto done;
4813 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4814 8e7bd50a 2019-08-22 stsp } else {
4815 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4816 c9956ddf 2019-09-08 stsp if (error)
4817 c9956ddf 2019-09-08 stsp goto done;
4818 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4819 c9956ddf 2019-09-08 stsp if (error != NULL)
4820 c9956ddf 2019-09-08 stsp goto done;
4821 c9956ddf 2019-09-08 stsp
4822 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4823 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4824 8e7bd50a 2019-08-22 stsp if (error)
4825 8e7bd50a 2019-08-22 stsp goto done;
4826 8e7bd50a 2019-08-22 stsp }
4827 8e7bd50a 2019-08-22 stsp
4828 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4829 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4830 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4831 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4832 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4833 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4834 8e7bd50a 2019-08-22 stsp if (error)
4835 8e7bd50a 2019-08-22 stsp goto done;
4836 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4837 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4838 8e7bd50a 2019-08-22 stsp if (error)
4839 8e7bd50a 2019-08-22 stsp goto done;
4840 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4841 8e7bd50a 2019-08-22 stsp free(commit_id);
4842 8e7bd50a 2019-08-22 stsp if (error)
4843 8e7bd50a 2019-08-22 stsp goto done;
4844 8e7bd50a 2019-08-22 stsp }
4845 8e7bd50a 2019-08-22 stsp
4846 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4847 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4848 8e7bd50a 2019-08-22 stsp }
4849 8e7bd50a 2019-08-22 stsp done:
4850 8e7bd50a 2019-08-22 stsp if (repo)
4851 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4852 8e7bd50a 2019-08-22 stsp if (worktree)
4853 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4854 8e7bd50a 2019-08-22 stsp free(cwd);
4855 8e7bd50a 2019-08-22 stsp free(repo_path);
4856 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4857 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4858 8e7bd50a 2019-08-22 stsp return error;
4859 8e7bd50a 2019-08-22 stsp }
4860 8e7bd50a 2019-08-22 stsp
4861 8e7bd50a 2019-08-22 stsp __dead static void
4862 d00136be 2019-03-26 stsp usage_add(void)
4863 d00136be 2019-03-26 stsp {
4864 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4865 022fae89 2019-12-06 tracey getprogname());
4866 d00136be 2019-03-26 stsp exit(1);
4867 d00136be 2019-03-26 stsp }
4868 d00136be 2019-03-26 stsp
4869 d00136be 2019-03-26 stsp static const struct got_error *
4870 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4871 4e68cba3 2019-11-23 stsp {
4872 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4873 4e68cba3 2019-11-23 stsp path++;
4874 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4875 4e68cba3 2019-11-23 stsp return NULL;
4876 4e68cba3 2019-11-23 stsp }
4877 4e68cba3 2019-11-23 stsp
4878 4e68cba3 2019-11-23 stsp static const struct got_error *
4879 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4880 d00136be 2019-03-26 stsp {
4881 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4882 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4883 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4884 1dd54920 2019-05-11 stsp char *cwd = NULL;
4885 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4886 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4887 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4888 1dd54920 2019-05-11 stsp
4889 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4890 d00136be 2019-03-26 stsp
4891 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4892 d00136be 2019-03-26 stsp switch (ch) {
4893 022fae89 2019-12-06 tracey case 'I':
4894 022fae89 2019-12-06 tracey no_ignores = 1;
4895 022fae89 2019-12-06 tracey break;
4896 4e68cba3 2019-11-23 stsp case 'R':
4897 4e68cba3 2019-11-23 stsp can_recurse = 1;
4898 4e68cba3 2019-11-23 stsp break;
4899 d00136be 2019-03-26 stsp default:
4900 d00136be 2019-03-26 stsp usage_add();
4901 d00136be 2019-03-26 stsp /* NOTREACHED */
4902 d00136be 2019-03-26 stsp }
4903 d00136be 2019-03-26 stsp }
4904 d00136be 2019-03-26 stsp
4905 d00136be 2019-03-26 stsp argc -= optind;
4906 d00136be 2019-03-26 stsp argv += optind;
4907 d00136be 2019-03-26 stsp
4908 43012d58 2019-07-14 stsp #ifndef PROFILE
4909 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4910 43012d58 2019-07-14 stsp NULL) == -1)
4911 43012d58 2019-07-14 stsp err(1, "pledge");
4912 43012d58 2019-07-14 stsp #endif
4913 723c305c 2019-05-11 jcs if (argc < 1)
4914 d00136be 2019-03-26 stsp usage_add();
4915 d00136be 2019-03-26 stsp
4916 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4917 d00136be 2019-03-26 stsp if (cwd == NULL) {
4918 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4919 d00136be 2019-03-26 stsp goto done;
4920 d00136be 2019-03-26 stsp }
4921 723c305c 2019-05-11 jcs
4922 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4923 d00136be 2019-03-26 stsp if (error)
4924 d00136be 2019-03-26 stsp goto done;
4925 d00136be 2019-03-26 stsp
4926 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4927 c9956ddf 2019-09-08 stsp NULL);
4928 031a5338 2019-03-26 stsp if (error != NULL)
4929 031a5338 2019-03-26 stsp goto done;
4930 031a5338 2019-03-26 stsp
4931 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4932 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4933 d00136be 2019-03-26 stsp if (error)
4934 d00136be 2019-03-26 stsp goto done;
4935 d00136be 2019-03-26 stsp
4936 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4937 6d022e97 2019-08-04 stsp if (error)
4938 022fae89 2019-12-06 tracey goto done;
4939 022fae89 2019-12-06 tracey
4940 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4941 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4942 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4943 6d022e97 2019-08-04 stsp goto done;
4944 723c305c 2019-05-11 jcs
4945 022fae89 2019-12-06 tracey }
4946 022fae89 2019-12-06 tracey
4947 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4948 4e68cba3 2019-11-23 stsp char *ondisk_path;
4949 4e68cba3 2019-11-23 stsp struct stat sb;
4950 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4951 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4952 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4953 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4954 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4955 4e68cba3 2019-11-23 stsp goto done;
4956 4e68cba3 2019-11-23 stsp }
4957 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4958 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4959 4e68cba3 2019-11-23 stsp free(ondisk_path);
4960 4e68cba3 2019-11-23 stsp continue;
4961 4e68cba3 2019-11-23 stsp }
4962 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4963 4e68cba3 2019-11-23 stsp ondisk_path);
4964 4e68cba3 2019-11-23 stsp free(ondisk_path);
4965 4e68cba3 2019-11-23 stsp goto done;
4966 4e68cba3 2019-11-23 stsp }
4967 4e68cba3 2019-11-23 stsp free(ondisk_path);
4968 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4969 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4970 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4971 4e68cba3 2019-11-23 stsp goto done;
4972 4e68cba3 2019-11-23 stsp }
4973 4e68cba3 2019-11-23 stsp }
4974 4e68cba3 2019-11-23 stsp }
4975 022fae89 2019-12-06 tracey
4976 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4977 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4978 d00136be 2019-03-26 stsp done:
4979 031a5338 2019-03-26 stsp if (repo)
4980 031a5338 2019-03-26 stsp got_repo_close(repo);
4981 d00136be 2019-03-26 stsp if (worktree)
4982 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4983 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4984 1dd54920 2019-05-11 stsp free((char *)pe->path);
4985 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4986 2ec1f75b 2019-03-26 stsp free(cwd);
4987 2ec1f75b 2019-03-26 stsp return error;
4988 2ec1f75b 2019-03-26 stsp }
4989 2ec1f75b 2019-03-26 stsp
4990 2ec1f75b 2019-03-26 stsp __dead static void
4991 648e4ef7 2019-07-09 stsp usage_remove(void)
4992 2ec1f75b 2019-03-26 stsp {
4993 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4994 f2a9dc41 2019-12-13 tracey getprogname());
4995 2ec1f75b 2019-03-26 stsp exit(1);
4996 2a06fe5f 2019-08-24 stsp }
4997 2a06fe5f 2019-08-24 stsp
4998 2a06fe5f 2019-08-24 stsp static const struct got_error *
4999 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5000 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5001 2a06fe5f 2019-08-24 stsp {
5002 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5003 f2a9dc41 2019-12-13 tracey path++;
5004 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5005 2a06fe5f 2019-08-24 stsp return NULL;
5006 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5007 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5008 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5009 2a06fe5f 2019-08-24 stsp return NULL;
5010 2ec1f75b 2019-03-26 stsp }
5011 2ec1f75b 2019-03-26 stsp
5012 2ec1f75b 2019-03-26 stsp static const struct got_error *
5013 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5014 2ec1f75b 2019-03-26 stsp {
5015 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5016 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5017 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5018 17ed4618 2019-06-02 stsp char *cwd = NULL;
5019 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5020 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5021 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5022 2ec1f75b 2019-03-26 stsp
5023 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5024 17ed4618 2019-06-02 stsp
5025 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5026 2ec1f75b 2019-03-26 stsp switch (ch) {
5027 2ec1f75b 2019-03-26 stsp case 'f':
5028 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5029 2ec1f75b 2019-03-26 stsp break;
5030 70e3e7f5 2019-12-13 tracey case 'k':
5031 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5032 70e3e7f5 2019-12-13 tracey break;
5033 f2a9dc41 2019-12-13 tracey case 'R':
5034 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5035 f2a9dc41 2019-12-13 tracey break;
5036 2ec1f75b 2019-03-26 stsp default:
5037 f2a9dc41 2019-12-13 tracey usage_remove();
5038 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5039 2ec1f75b 2019-03-26 stsp }
5040 2ec1f75b 2019-03-26 stsp }
5041 2ec1f75b 2019-03-26 stsp
5042 2ec1f75b 2019-03-26 stsp argc -= optind;
5043 2ec1f75b 2019-03-26 stsp argv += optind;
5044 2ec1f75b 2019-03-26 stsp
5045 43012d58 2019-07-14 stsp #ifndef PROFILE
5046 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5047 43012d58 2019-07-14 stsp NULL) == -1)
5048 43012d58 2019-07-14 stsp err(1, "pledge");
5049 43012d58 2019-07-14 stsp #endif
5050 17ed4618 2019-06-02 stsp if (argc < 1)
5051 648e4ef7 2019-07-09 stsp usage_remove();
5052 2ec1f75b 2019-03-26 stsp
5053 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5054 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5055 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5056 2ec1f75b 2019-03-26 stsp goto done;
5057 2ec1f75b 2019-03-26 stsp }
5058 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5059 2ec1f75b 2019-03-26 stsp if (error)
5060 2ec1f75b 2019-03-26 stsp goto done;
5061 2ec1f75b 2019-03-26 stsp
5062 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5063 c9956ddf 2019-09-08 stsp NULL);
5064 2af4a041 2019-05-11 jcs if (error)
5065 2ec1f75b 2019-03-26 stsp goto done;
5066 2ec1f75b 2019-03-26 stsp
5067 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5068 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5069 2ec1f75b 2019-03-26 stsp if (error)
5070 2ec1f75b 2019-03-26 stsp goto done;
5071 2ec1f75b 2019-03-26 stsp
5072 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5073 6d022e97 2019-08-04 stsp if (error)
5074 6d022e97 2019-08-04 stsp goto done;
5075 17ed4618 2019-06-02 stsp
5076 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5077 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5078 f2a9dc41 2019-12-13 tracey struct stat sb;
5079 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5080 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5081 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5082 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5083 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5084 f2a9dc41 2019-12-13 tracey goto done;
5085 f2a9dc41 2019-12-13 tracey }
5086 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5087 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5088 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5089 f2a9dc41 2019-12-13 tracey continue;
5090 f2a9dc41 2019-12-13 tracey }
5091 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5092 f2a9dc41 2019-12-13 tracey ondisk_path);
5093 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5094 f2a9dc41 2019-12-13 tracey goto done;
5095 f2a9dc41 2019-12-13 tracey }
5096 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5097 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5098 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5099 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5100 f2a9dc41 2019-12-13 tracey goto done;
5101 f2a9dc41 2019-12-13 tracey }
5102 f2a9dc41 2019-12-13 tracey }
5103 f2a9dc41 2019-12-13 tracey }
5104 f2a9dc41 2019-12-13 tracey
5105 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5106 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5107 a129376b 2019-03-28 stsp done:
5108 a129376b 2019-03-28 stsp if (repo)
5109 a129376b 2019-03-28 stsp got_repo_close(repo);
5110 a129376b 2019-03-28 stsp if (worktree)
5111 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5112 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5113 17ed4618 2019-06-02 stsp free((char *)pe->path);
5114 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5115 a129376b 2019-03-28 stsp free(cwd);
5116 a129376b 2019-03-28 stsp return error;
5117 a129376b 2019-03-28 stsp }
5118 a129376b 2019-03-28 stsp
5119 a129376b 2019-03-28 stsp __dead static void
5120 a129376b 2019-03-28 stsp usage_revert(void)
5121 a129376b 2019-03-28 stsp {
5122 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5123 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5124 a129376b 2019-03-28 stsp exit(1);
5125 a129376b 2019-03-28 stsp }
5126 a129376b 2019-03-28 stsp
5127 1ee397ad 2019-07-12 stsp static const struct got_error *
5128 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5129 a129376b 2019-03-28 stsp {
5130 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5131 fb9704af 2020-01-27 stsp return NULL;
5132 fb9704af 2020-01-27 stsp
5133 a129376b 2019-03-28 stsp while (path[0] == '/')
5134 a129376b 2019-03-28 stsp path++;
5135 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5136 33aa809d 2019-08-08 stsp return NULL;
5137 33aa809d 2019-08-08 stsp }
5138 33aa809d 2019-08-08 stsp
5139 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5140 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5141 33aa809d 2019-08-08 stsp const char *action;
5142 33aa809d 2019-08-08 stsp };
5143 33aa809d 2019-08-08 stsp
5144 33aa809d 2019-08-08 stsp static const struct got_error *
5145 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5146 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5147 33aa809d 2019-08-08 stsp {
5148 33aa809d 2019-08-08 stsp char *line = NULL;
5149 33aa809d 2019-08-08 stsp size_t linesize = 0;
5150 33aa809d 2019-08-08 stsp ssize_t linelen;
5151 33aa809d 2019-08-08 stsp
5152 33aa809d 2019-08-08 stsp switch (status) {
5153 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5154 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5155 33aa809d 2019-08-08 stsp break;
5156 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5157 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5158 33aa809d 2019-08-08 stsp break;
5159 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5160 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5161 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5162 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5163 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5164 33aa809d 2019-08-08 stsp printf("%s", line);
5165 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5166 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5167 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5168 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5169 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5170 33aa809d 2019-08-08 stsp break;
5171 33aa809d 2019-08-08 stsp default:
5172 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5173 33aa809d 2019-08-08 stsp }
5174 33aa809d 2019-08-08 stsp
5175 33aa809d 2019-08-08 stsp return NULL;
5176 33aa809d 2019-08-08 stsp }
5177 33aa809d 2019-08-08 stsp
5178 33aa809d 2019-08-08 stsp static const struct got_error *
5179 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5180 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5181 33aa809d 2019-08-08 stsp {
5182 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5183 33aa809d 2019-08-08 stsp char *line = NULL;
5184 33aa809d 2019-08-08 stsp size_t linesize = 0;
5185 33aa809d 2019-08-08 stsp ssize_t linelen;
5186 33aa809d 2019-08-08 stsp int resp = ' ';
5187 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5188 33aa809d 2019-08-08 stsp
5189 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5190 33aa809d 2019-08-08 stsp
5191 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5192 33aa809d 2019-08-08 stsp char *nl;
5193 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5194 33aa809d 2019-08-08 stsp a->action);
5195 33aa809d 2019-08-08 stsp if (err)
5196 33aa809d 2019-08-08 stsp return err;
5197 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5198 33aa809d 2019-08-08 stsp if (linelen == -1) {
5199 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5200 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5201 33aa809d 2019-08-08 stsp return NULL;
5202 33aa809d 2019-08-08 stsp }
5203 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5204 33aa809d 2019-08-08 stsp if (nl)
5205 33aa809d 2019-08-08 stsp *nl = '\0';
5206 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5207 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5208 33aa809d 2019-08-08 stsp printf("y\n");
5209 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5210 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5211 33aa809d 2019-08-08 stsp printf("n\n");
5212 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5213 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5214 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5215 33aa809d 2019-08-08 stsp printf("q\n");
5216 33aa809d 2019-08-08 stsp } else
5217 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5218 33aa809d 2019-08-08 stsp free(line);
5219 33aa809d 2019-08-08 stsp return NULL;
5220 33aa809d 2019-08-08 stsp }
5221 33aa809d 2019-08-08 stsp
5222 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5223 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5224 33aa809d 2019-08-08 stsp a->action);
5225 33aa809d 2019-08-08 stsp if (err)
5226 33aa809d 2019-08-08 stsp return err;
5227 33aa809d 2019-08-08 stsp resp = getchar();
5228 33aa809d 2019-08-08 stsp if (resp == '\n')
5229 33aa809d 2019-08-08 stsp resp = getchar();
5230 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5231 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5232 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5233 33aa809d 2019-08-08 stsp resp = ' ';
5234 33aa809d 2019-08-08 stsp }
5235 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5236 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5237 33aa809d 2019-08-08 stsp resp = ' ';
5238 33aa809d 2019-08-08 stsp }
5239 33aa809d 2019-08-08 stsp }
5240 33aa809d 2019-08-08 stsp
5241 33aa809d 2019-08-08 stsp if (resp == 'y')
5242 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5243 33aa809d 2019-08-08 stsp else if (resp == 'n')
5244 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5245 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5246 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5247 33aa809d 2019-08-08 stsp
5248 1ee397ad 2019-07-12 stsp return NULL;
5249 a129376b 2019-03-28 stsp }
5250 a129376b 2019-03-28 stsp
5251 33aa809d 2019-08-08 stsp
5252 a129376b 2019-03-28 stsp static const struct got_error *
5253 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5254 a129376b 2019-03-28 stsp {
5255 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5256 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5257 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5258 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5259 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5260 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5261 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5262 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5263 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5264 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5265 a129376b 2019-03-28 stsp
5266 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5267 e20a8b6f 2019-06-04 stsp
5268 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5269 a129376b 2019-03-28 stsp switch (ch) {
5270 33aa809d 2019-08-08 stsp case 'p':
5271 33aa809d 2019-08-08 stsp pflag = 1;
5272 33aa809d 2019-08-08 stsp break;
5273 33aa809d 2019-08-08 stsp case 'F':
5274 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5275 33aa809d 2019-08-08 stsp break;
5276 0f6d7415 2019-08-08 stsp case 'R':
5277 0f6d7415 2019-08-08 stsp can_recurse = 1;
5278 0f6d7415 2019-08-08 stsp break;
5279 a129376b 2019-03-28 stsp default:
5280 a129376b 2019-03-28 stsp usage_revert();
5281 a129376b 2019-03-28 stsp /* NOTREACHED */
5282 a129376b 2019-03-28 stsp }
5283 a129376b 2019-03-28 stsp }
5284 a129376b 2019-03-28 stsp
5285 a129376b 2019-03-28 stsp argc -= optind;
5286 a129376b 2019-03-28 stsp argv += optind;
5287 a129376b 2019-03-28 stsp
5288 43012d58 2019-07-14 stsp #ifndef PROFILE
5289 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5290 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5291 43012d58 2019-07-14 stsp err(1, "pledge");
5292 43012d58 2019-07-14 stsp #endif
5293 e20a8b6f 2019-06-04 stsp if (argc < 1)
5294 a129376b 2019-03-28 stsp usage_revert();
5295 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5296 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5297 a129376b 2019-03-28 stsp
5298 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5299 a129376b 2019-03-28 stsp if (cwd == NULL) {
5300 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5301 a129376b 2019-03-28 stsp goto done;
5302 a129376b 2019-03-28 stsp }
5303 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5304 2ec1f75b 2019-03-26 stsp if (error)
5305 2ec1f75b 2019-03-26 stsp goto done;
5306 a129376b 2019-03-28 stsp
5307 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5308 c9956ddf 2019-09-08 stsp NULL);
5309 a129376b 2019-03-28 stsp if (error != NULL)
5310 a129376b 2019-03-28 stsp goto done;
5311 a129376b 2019-03-28 stsp
5312 33aa809d 2019-08-08 stsp if (patch_script_path) {
5313 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5314 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5315 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5316 33aa809d 2019-08-08 stsp patch_script_path);
5317 33aa809d 2019-08-08 stsp goto done;
5318 33aa809d 2019-08-08 stsp }
5319 33aa809d 2019-08-08 stsp }
5320 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5321 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5322 a129376b 2019-03-28 stsp if (error)
5323 a129376b 2019-03-28 stsp goto done;
5324 a129376b 2019-03-28 stsp
5325 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5326 6d022e97 2019-08-04 stsp if (error)
5327 6d022e97 2019-08-04 stsp goto done;
5328 00db391e 2019-08-03 stsp
5329 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5330 0f6d7415 2019-08-08 stsp char *ondisk_path;
5331 0f6d7415 2019-08-08 stsp struct stat sb;
5332 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5333 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5334 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5335 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5336 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5337 0f6d7415 2019-08-08 stsp goto done;
5338 0f6d7415 2019-08-08 stsp }
5339 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5340 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5341 0f6d7415 2019-08-08 stsp free(ondisk_path);
5342 0f6d7415 2019-08-08 stsp continue;
5343 0f6d7415 2019-08-08 stsp }
5344 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5345 0f6d7415 2019-08-08 stsp ondisk_path);
5346 0f6d7415 2019-08-08 stsp free(ondisk_path);
5347 0f6d7415 2019-08-08 stsp goto done;
5348 0f6d7415 2019-08-08 stsp }
5349 0f6d7415 2019-08-08 stsp free(ondisk_path);
5350 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5351 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5352 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5353 0f6d7415 2019-08-08 stsp goto done;
5354 0f6d7415 2019-08-08 stsp }
5355 0f6d7415 2019-08-08 stsp }
5356 0f6d7415 2019-08-08 stsp }
5357 0f6d7415 2019-08-08 stsp
5358 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5359 33aa809d 2019-08-08 stsp cpa.action = "revert";
5360 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5361 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5362 2ec1f75b 2019-03-26 stsp done:
5363 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5364 33aa809d 2019-08-08 stsp error == NULL)
5365 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5366 2ec1f75b 2019-03-26 stsp if (repo)
5367 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5368 2ec1f75b 2019-03-26 stsp if (worktree)
5369 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5370 2ec1f75b 2019-03-26 stsp free(path);
5371 d00136be 2019-03-26 stsp free(cwd);
5372 6bad629b 2019-02-04 stsp return error;
5373 c4296144 2019-05-09 stsp }
5374 c4296144 2019-05-09 stsp
5375 c4296144 2019-05-09 stsp __dead static void
5376 c4296144 2019-05-09 stsp usage_commit(void)
5377 c4296144 2019-05-09 stsp {
5378 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5379 5c1e53bc 2019-07-28 stsp getprogname());
5380 c4296144 2019-05-09 stsp exit(1);
5381 33ad4cbe 2019-05-12 jcs }
5382 33ad4cbe 2019-05-12 jcs
5383 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5384 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5385 e2ba3d07 2019-05-13 stsp const char *editor;
5386 e0870e44 2019-05-13 stsp const char *worktree_path;
5387 76d98825 2019-06-03 stsp const char *branch_name;
5388 314a6357 2019-05-13 stsp const char *repo_path;
5389 e0870e44 2019-05-13 stsp char *logmsg_path;
5390 e2ba3d07 2019-05-13 stsp
5391 e2ba3d07 2019-05-13 stsp };
5392 e0870e44 2019-05-13 stsp
5393 33ad4cbe 2019-05-12 jcs static const struct got_error *
5394 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5395 33ad4cbe 2019-05-12 jcs void *arg)
5396 33ad4cbe 2019-05-12 jcs {
5397 76d98825 2019-06-03 stsp char *initial_content = NULL;
5398 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5399 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5400 e0870e44 2019-05-13 stsp char *template = NULL;
5401 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5402 3ce1b845 2019-07-15 stsp int fd;
5403 33ad4cbe 2019-05-12 jcs size_t len;
5404 33ad4cbe 2019-05-12 jcs
5405 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5406 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5407 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5408 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5409 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5410 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5411 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5412 33ad4cbe 2019-05-12 jcs return NULL;
5413 33ad4cbe 2019-05-12 jcs }
5414 33ad4cbe 2019-05-12 jcs
5415 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5416 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5417 76d98825 2019-06-03 stsp
5418 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5419 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5420 76d98825 2019-06-03 stsp a->branch_name) == -1)
5421 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5422 e0870e44 2019-05-13 stsp
5423 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5424 793c30b5 2019-05-13 stsp if (err)
5425 e0870e44 2019-05-13 stsp goto done;
5426 33ad4cbe 2019-05-12 jcs
5427 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5428 33ad4cbe 2019-05-12 jcs
5429 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5430 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5431 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5432 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5433 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5434 33ad4cbe 2019-05-12 jcs }
5435 33ad4cbe 2019-05-12 jcs close(fd);
5436 33ad4cbe 2019-05-12 jcs
5437 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5438 33ad4cbe 2019-05-12 jcs done:
5439 76d98825 2019-06-03 stsp free(initial_content);
5440 e0870e44 2019-05-13 stsp free(template);
5441 314a6357 2019-05-13 stsp
5442 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5443 3ce1b845 2019-07-15 stsp if (err == NULL) {
5444 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5445 3ce1b845 2019-07-15 stsp if (err) {
5446 3ce1b845 2019-07-15 stsp free(*logmsg);
5447 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5448 3ce1b845 2019-07-15 stsp }
5449 3ce1b845 2019-07-15 stsp }
5450 33ad4cbe 2019-05-12 jcs return err;
5451 6bad629b 2019-02-04 stsp }
5452 c4296144 2019-05-09 stsp
5453 c4296144 2019-05-09 stsp static const struct got_error *
5454 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5455 c4296144 2019-05-09 stsp {
5456 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5457 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5458 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5459 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5460 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5461 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5462 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5463 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5464 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5465 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5466 5c1e53bc 2019-07-28 stsp
5467 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5468 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5469 c4296144 2019-05-09 stsp
5470 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5471 c4296144 2019-05-09 stsp switch (ch) {
5472 c4296144 2019-05-09 stsp case 'm':
5473 c4296144 2019-05-09 stsp logmsg = optarg;
5474 c4296144 2019-05-09 stsp break;
5475 c4296144 2019-05-09 stsp default:
5476 c4296144 2019-05-09 stsp usage_commit();
5477 c4296144 2019-05-09 stsp /* NOTREACHED */
5478 c4296144 2019-05-09 stsp }
5479 c4296144 2019-05-09 stsp }
5480 c4296144 2019-05-09 stsp
5481 c4296144 2019-05-09 stsp argc -= optind;
5482 c4296144 2019-05-09 stsp argv += optind;
5483 c4296144 2019-05-09 stsp
5484 43012d58 2019-07-14 stsp #ifndef PROFILE
5485 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5486 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5487 43012d58 2019-07-14 stsp err(1, "pledge");
5488 43012d58 2019-07-14 stsp #endif
5489 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5490 c4296144 2019-05-09 stsp if (cwd == NULL) {
5491 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5492 c4296144 2019-05-09 stsp goto done;
5493 c4296144 2019-05-09 stsp }
5494 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5495 7d5807f4 2019-07-11 stsp if (error)
5496 7d5807f4 2019-07-11 stsp goto done;
5497 7d5807f4 2019-07-11 stsp
5498 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5499 c4296144 2019-05-09 stsp if (error)
5500 a698f62e 2019-07-25 stsp goto done;
5501 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5502 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5503 c4296144 2019-05-09 stsp goto done;
5504 a698f62e 2019-07-25 stsp }
5505 5c1e53bc 2019-07-28 stsp
5506 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5507 916f288c 2019-07-30 stsp worktree);
5508 5c1e53bc 2019-07-28 stsp if (error)
5509 5c1e53bc 2019-07-28 stsp goto done;
5510 c4296144 2019-05-09 stsp
5511 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5512 c9956ddf 2019-09-08 stsp if (error)
5513 c9956ddf 2019-09-08 stsp goto done;
5514 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5515 c9956ddf 2019-09-08 stsp gitconfig_path);
5516 c4296144 2019-05-09 stsp if (error != NULL)
5517 c4296144 2019-05-09 stsp goto done;
5518 c4296144 2019-05-09 stsp
5519 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5520 aba9c984 2019-09-08 stsp if (error)
5521 aba9c984 2019-09-08 stsp return error;
5522 aba9c984 2019-09-08 stsp
5523 314a6357 2019-05-13 stsp /*
5524 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5525 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5526 314a6357 2019-05-13 stsp */
5527 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5528 314a6357 2019-05-13 stsp error = get_editor(&editor);
5529 314a6357 2019-05-13 stsp else
5530 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5531 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5532 c4296144 2019-05-09 stsp if (error)
5533 c4296144 2019-05-09 stsp goto done;
5534 c4296144 2019-05-09 stsp
5535 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5536 087fb88c 2019-08-04 stsp if (error)
5537 087fb88c 2019-08-04 stsp goto done;
5538 087fb88c 2019-08-04 stsp
5539 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5540 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5541 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5542 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5543 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5544 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5545 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5546 916f288c 2019-07-30 stsp goto done;
5547 916f288c 2019-07-30 stsp }
5548 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5549 916f288c 2019-07-30 stsp }
5550 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5551 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5552 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5553 e0870e44 2019-05-13 stsp if (error) {
5554 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5555 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5556 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5557 c4296144 2019-05-09 stsp goto done;
5558 e0870e44 2019-05-13 stsp }
5559 c4296144 2019-05-09 stsp
5560 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5561 c4296144 2019-05-09 stsp if (error)
5562 c4296144 2019-05-09 stsp goto done;
5563 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5564 c4296144 2019-05-09 stsp done:
5565 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5566 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5567 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5568 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5569 7266f21f 2019-10-21 stsp error == NULL)
5570 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5571 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5572 c4296144 2019-05-09 stsp if (repo)
5573 c4296144 2019-05-09 stsp got_repo_close(repo);
5574 c4296144 2019-05-09 stsp if (worktree)
5575 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5576 c4296144 2019-05-09 stsp free(cwd);
5577 c4296144 2019-05-09 stsp free(id_str);
5578 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5579 e2ba3d07 2019-05-13 stsp free(editor);
5580 aba9c984 2019-09-08 stsp free(author);
5581 234035bc 2019-06-01 stsp return error;
5582 234035bc 2019-06-01 stsp }
5583 234035bc 2019-06-01 stsp
5584 234035bc 2019-06-01 stsp __dead static void
5585 234035bc 2019-06-01 stsp usage_cherrypick(void)
5586 234035bc 2019-06-01 stsp {
5587 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5588 234035bc 2019-06-01 stsp exit(1);
5589 234035bc 2019-06-01 stsp }
5590 234035bc 2019-06-01 stsp
5591 234035bc 2019-06-01 stsp static const struct got_error *
5592 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5593 234035bc 2019-06-01 stsp {
5594 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5595 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5596 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5597 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5598 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5599 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5600 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5601 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5602 234035bc 2019-06-01 stsp int ch, did_something = 0;
5603 234035bc 2019-06-01 stsp
5604 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5605 234035bc 2019-06-01 stsp switch (ch) {
5606 234035bc 2019-06-01 stsp default:
5607 234035bc 2019-06-01 stsp usage_cherrypick();
5608 234035bc 2019-06-01 stsp /* NOTREACHED */
5609 234035bc 2019-06-01 stsp }
5610 234035bc 2019-06-01 stsp }
5611 234035bc 2019-06-01 stsp
5612 234035bc 2019-06-01 stsp argc -= optind;
5613 234035bc 2019-06-01 stsp argv += optind;
5614 234035bc 2019-06-01 stsp
5615 43012d58 2019-07-14 stsp #ifndef PROFILE
5616 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5617 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5618 43012d58 2019-07-14 stsp err(1, "pledge");
5619 43012d58 2019-07-14 stsp #endif
5620 234035bc 2019-06-01 stsp if (argc != 1)
5621 234035bc 2019-06-01 stsp usage_cherrypick();
5622 234035bc 2019-06-01 stsp
5623 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5624 234035bc 2019-06-01 stsp if (cwd == NULL) {
5625 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5626 234035bc 2019-06-01 stsp goto done;
5627 234035bc 2019-06-01 stsp }
5628 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5629 234035bc 2019-06-01 stsp if (error)
5630 234035bc 2019-06-01 stsp goto done;
5631 234035bc 2019-06-01 stsp
5632 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5633 c9956ddf 2019-09-08 stsp NULL);
5634 234035bc 2019-06-01 stsp if (error != NULL)
5635 234035bc 2019-06-01 stsp goto done;
5636 234035bc 2019-06-01 stsp
5637 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5638 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5639 234035bc 2019-06-01 stsp if (error)
5640 234035bc 2019-06-01 stsp goto done;
5641 234035bc 2019-06-01 stsp
5642 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5643 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5644 234035bc 2019-06-01 stsp if (error != NULL) {
5645 234035bc 2019-06-01 stsp struct got_reference *ref;
5646 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5647 234035bc 2019-06-01 stsp goto done;
5648 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5649 234035bc 2019-06-01 stsp if (error != NULL)
5650 234035bc 2019-06-01 stsp goto done;
5651 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5652 234035bc 2019-06-01 stsp got_ref_close(ref);
5653 234035bc 2019-06-01 stsp if (error != NULL)
5654 234035bc 2019-06-01 stsp goto done;
5655 234035bc 2019-06-01 stsp }
5656 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5657 234035bc 2019-06-01 stsp if (error)
5658 234035bc 2019-06-01 stsp goto done;
5659 234035bc 2019-06-01 stsp
5660 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5661 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5662 234035bc 2019-06-01 stsp if (error != NULL)
5663 234035bc 2019-06-01 stsp goto done;
5664 234035bc 2019-06-01 stsp
5665 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5666 234035bc 2019-06-01 stsp if (error) {
5667 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5668 234035bc 2019-06-01 stsp goto done;
5669 234035bc 2019-06-01 stsp error = NULL;
5670 234035bc 2019-06-01 stsp } else {
5671 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5672 234035bc 2019-06-01 stsp goto done;
5673 234035bc 2019-06-01 stsp }
5674 234035bc 2019-06-01 stsp
5675 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5676 234035bc 2019-06-01 stsp if (error)
5677 234035bc 2019-06-01 stsp goto done;
5678 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5679 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5680 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5681 03415a1a 2019-06-02 stsp NULL);
5682 234035bc 2019-06-01 stsp if (error != NULL)
5683 234035bc 2019-06-01 stsp goto done;
5684 234035bc 2019-06-01 stsp
5685 234035bc 2019-06-01 stsp if (did_something)
5686 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5687 234035bc 2019-06-01 stsp done:
5688 234035bc 2019-06-01 stsp if (commit)
5689 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5690 234035bc 2019-06-01 stsp free(commit_id_str);
5691 234035bc 2019-06-01 stsp if (head_ref)
5692 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5693 234035bc 2019-06-01 stsp if (worktree)
5694 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5695 234035bc 2019-06-01 stsp if (repo)
5696 234035bc 2019-06-01 stsp got_repo_close(repo);
5697 c4296144 2019-05-09 stsp return error;
5698 c4296144 2019-05-09 stsp }
5699 5ef14e63 2019-06-02 stsp
5700 5ef14e63 2019-06-02 stsp __dead static void
5701 5ef14e63 2019-06-02 stsp usage_backout(void)
5702 5ef14e63 2019-06-02 stsp {
5703 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5704 5ef14e63 2019-06-02 stsp exit(1);
5705 5ef14e63 2019-06-02 stsp }
5706 5ef14e63 2019-06-02 stsp
5707 5ef14e63 2019-06-02 stsp static const struct got_error *
5708 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5709 5ef14e63 2019-06-02 stsp {
5710 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5711 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5712 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5713 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5714 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5715 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5716 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5717 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5718 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5719 5ef14e63 2019-06-02 stsp
5720 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5721 5ef14e63 2019-06-02 stsp switch (ch) {
5722 5ef14e63 2019-06-02 stsp default:
5723 5ef14e63 2019-06-02 stsp usage_backout();
5724 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5725 5ef14e63 2019-06-02 stsp }
5726 5ef14e63 2019-06-02 stsp }
5727 5ef14e63 2019-06-02 stsp
5728 5ef14e63 2019-06-02 stsp argc -= optind;
5729 5ef14e63 2019-06-02 stsp argv += optind;
5730 5ef14e63 2019-06-02 stsp
5731 43012d58 2019-07-14 stsp #ifndef PROFILE
5732 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5733 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5734 43012d58 2019-07-14 stsp err(1, "pledge");
5735 43012d58 2019-07-14 stsp #endif
5736 5ef14e63 2019-06-02 stsp if (argc != 1)
5737 5ef14e63 2019-06-02 stsp usage_backout();
5738 5ef14e63 2019-06-02 stsp
5739 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5740 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5741 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5742 5ef14e63 2019-06-02 stsp goto done;
5743 5ef14e63 2019-06-02 stsp }
5744 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5745 5ef14e63 2019-06-02 stsp if (error)
5746 5ef14e63 2019-06-02 stsp goto done;
5747 5ef14e63 2019-06-02 stsp
5748 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5749 c9956ddf 2019-09-08 stsp NULL);
5750 5ef14e63 2019-06-02 stsp if (error != NULL)
5751 5ef14e63 2019-06-02 stsp goto done;
5752 5ef14e63 2019-06-02 stsp
5753 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5754 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5755 5ef14e63 2019-06-02 stsp if (error)
5756 5ef14e63 2019-06-02 stsp goto done;
5757 5ef14e63 2019-06-02 stsp
5758 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5759 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5760 5ef14e63 2019-06-02 stsp if (error != NULL) {
5761 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5762 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5763 5ef14e63 2019-06-02 stsp goto done;
5764 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5765 5ef14e63 2019-06-02 stsp if (error != NULL)
5766 5ef14e63 2019-06-02 stsp goto done;
5767 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5768 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5769 5ef14e63 2019-06-02 stsp if (error != NULL)
5770 5ef14e63 2019-06-02 stsp goto done;
5771 5ef14e63 2019-06-02 stsp }
5772 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
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_ref_open(&head_ref, repo,
5777 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5778 5ef14e63 2019-06-02 stsp if (error != NULL)
5779 5ef14e63 2019-06-02 stsp goto done;
5780 5ef14e63 2019-06-02 stsp
5781 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5782 5ef14e63 2019-06-02 stsp if (error)
5783 5ef14e63 2019-06-02 stsp goto done;
5784 5ef14e63 2019-06-02 stsp
5785 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5786 5ef14e63 2019-06-02 stsp if (error)
5787 5ef14e63 2019-06-02 stsp goto done;
5788 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5789 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5790 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5791 5ef14e63 2019-06-02 stsp goto done;
5792 5ef14e63 2019-06-02 stsp }
5793 5ef14e63 2019-06-02 stsp
5794 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5795 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5796 5ef14e63 2019-06-02 stsp if (error != NULL)
5797 5ef14e63 2019-06-02 stsp goto done;
5798 5ef14e63 2019-06-02 stsp
5799 5ef14e63 2019-06-02 stsp if (did_something)
5800 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5801 5ef14e63 2019-06-02 stsp done:
5802 5ef14e63 2019-06-02 stsp if (commit)
5803 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5804 5ef14e63 2019-06-02 stsp free(commit_id_str);
5805 5ef14e63 2019-06-02 stsp if (head_ref)
5806 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5807 818c7501 2019-07-11 stsp if (worktree)
5808 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5809 818c7501 2019-07-11 stsp if (repo)
5810 818c7501 2019-07-11 stsp got_repo_close(repo);
5811 818c7501 2019-07-11 stsp return error;
5812 818c7501 2019-07-11 stsp }
5813 818c7501 2019-07-11 stsp
5814 818c7501 2019-07-11 stsp __dead static void
5815 818c7501 2019-07-11 stsp usage_rebase(void)
5816 818c7501 2019-07-11 stsp {
5817 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5818 af54c8f8 2019-07-11 stsp getprogname());
5819 818c7501 2019-07-11 stsp exit(1);
5820 0ebf8283 2019-07-24 stsp }
5821 0ebf8283 2019-07-24 stsp
5822 0ebf8283 2019-07-24 stsp void
5823 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5824 0ebf8283 2019-07-24 stsp {
5825 0ebf8283 2019-07-24 stsp char *nl;
5826 0ebf8283 2019-07-24 stsp size_t len;
5827 0ebf8283 2019-07-24 stsp
5828 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5829 0ebf8283 2019-07-24 stsp if (len > limit)
5830 0ebf8283 2019-07-24 stsp len = limit;
5831 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5832 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5833 0ebf8283 2019-07-24 stsp if (nl)
5834 0ebf8283 2019-07-24 stsp *nl = '\0';
5835 0ebf8283 2019-07-24 stsp }
5836 0ebf8283 2019-07-24 stsp
5837 0ebf8283 2019-07-24 stsp static const struct got_error *
5838 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5839 0ebf8283 2019-07-24 stsp {
5840 5943eee2 2019-08-13 stsp const struct got_error *err;
5841 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5842 5943eee2 2019-08-13 stsp const char *s;
5843 0ebf8283 2019-07-24 stsp
5844 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5845 5943eee2 2019-08-13 stsp if (err)
5846 5943eee2 2019-08-13 stsp return err;
5847 0ebf8283 2019-07-24 stsp
5848 5943eee2 2019-08-13 stsp s = logmsg0;
5849 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5850 5943eee2 2019-08-13 stsp s++;
5851 5943eee2 2019-08-13 stsp
5852 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5853 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5854 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5855 5943eee2 2019-08-13 stsp goto done;
5856 5943eee2 2019-08-13 stsp }
5857 0ebf8283 2019-07-24 stsp
5858 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5859 5943eee2 2019-08-13 stsp done:
5860 5943eee2 2019-08-13 stsp free(logmsg0);
5861 a0ea4fc0 2020-02-28 stsp return err;
5862 a0ea4fc0 2020-02-28 stsp }
5863 a0ea4fc0 2020-02-28 stsp
5864 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5865 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5866 a0ea4fc0 2020-02-28 stsp {
5867 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5868 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5869 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5870 a0ea4fc0 2020-02-28 stsp
5871 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5872 a0ea4fc0 2020-02-28 stsp if (err)
5873 a0ea4fc0 2020-02-28 stsp return err;
5874 a0ea4fc0 2020-02-28 stsp
5875 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5876 a0ea4fc0 2020-02-28 stsp if (err)
5877 a0ea4fc0 2020-02-28 stsp goto done;
5878 a0ea4fc0 2020-02-28 stsp
5879 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5880 a0ea4fc0 2020-02-28 stsp
5881 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5882 a0ea4fc0 2020-02-28 stsp if (err)
5883 a0ea4fc0 2020-02-28 stsp goto done;
5884 a0ea4fc0 2020-02-28 stsp
5885 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5886 a0ea4fc0 2020-02-28 stsp done:
5887 a0ea4fc0 2020-02-28 stsp free(id_str);
5888 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5889 a0ea4fc0 2020-02-28 stsp free(logmsg);
5890 5943eee2 2019-08-13 stsp return err;
5891 818c7501 2019-07-11 stsp }
5892 818c7501 2019-07-11 stsp
5893 818c7501 2019-07-11 stsp static const struct got_error *
5894 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5895 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5896 818c7501 2019-07-11 stsp {
5897 818c7501 2019-07-11 stsp const struct got_error *err;
5898 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5899 818c7501 2019-07-11 stsp
5900 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5901 818c7501 2019-07-11 stsp if (err)
5902 818c7501 2019-07-11 stsp goto done;
5903 818c7501 2019-07-11 stsp
5904 ff0d2220 2019-07-11 stsp if (new_id) {
5905 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5906 ff0d2220 2019-07-11 stsp if (err)
5907 ff0d2220 2019-07-11 stsp goto done;
5908 ff0d2220 2019-07-11 stsp }
5909 818c7501 2019-07-11 stsp
5910 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5911 ff0d2220 2019-07-11 stsp if (new_id_str)
5912 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5913 0ebf8283 2019-07-24 stsp
5914 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5915 0ebf8283 2019-07-24 stsp if (err)
5916 0ebf8283 2019-07-24 stsp goto done;
5917 0ebf8283 2019-07-24 stsp
5918 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5919 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5920 818c7501 2019-07-11 stsp done:
5921 818c7501 2019-07-11 stsp free(old_id_str);
5922 818c7501 2019-07-11 stsp free(new_id_str);
5923 272a1371 2020-02-28 stsp free(logmsg);
5924 818c7501 2019-07-11 stsp return err;
5925 818c7501 2019-07-11 stsp }
5926 818c7501 2019-07-11 stsp
5927 1ee397ad 2019-07-12 stsp static const struct got_error *
5928 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5929 818c7501 2019-07-11 stsp {
5930 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5931 818c7501 2019-07-11 stsp
5932 818c7501 2019-07-11 stsp while (path[0] == '/')
5933 818c7501 2019-07-11 stsp path++;
5934 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5935 818c7501 2019-07-11 stsp
5936 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5937 1ee397ad 2019-07-12 stsp return NULL;
5938 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5939 818c7501 2019-07-11 stsp *rebase_status = status;
5940 1ee397ad 2019-07-12 stsp return NULL;
5941 818c7501 2019-07-11 stsp }
5942 818c7501 2019-07-11 stsp
5943 818c7501 2019-07-11 stsp static const struct got_error *
5944 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5945 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5946 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5947 818c7501 2019-07-11 stsp {
5948 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5949 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5950 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5951 818c7501 2019-07-11 stsp }
5952 818c7501 2019-07-11 stsp
5953 818c7501 2019-07-11 stsp static const struct got_error *
5954 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5955 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5956 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5957 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5958 ff0d2220 2019-07-11 stsp {
5959 ff0d2220 2019-07-11 stsp const struct got_error *error;
5960 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5961 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5962 ff0d2220 2019-07-11 stsp
5963 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5964 ff0d2220 2019-07-11 stsp if (error)
5965 ff0d2220 2019-07-11 stsp return error;
5966 ff0d2220 2019-07-11 stsp
5967 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5968 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5969 ff0d2220 2019-07-11 stsp if (error) {
5970 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5971 ff0d2220 2019-07-11 stsp goto done;
5972 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5973 ff0d2220 2019-07-11 stsp } else {
5974 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5975 ff0d2220 2019-07-11 stsp free(new_commit_id);
5976 ff0d2220 2019-07-11 stsp }
5977 ff0d2220 2019-07-11 stsp done:
5978 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5979 ff0d2220 2019-07-11 stsp return error;
5980 64c6d990 2019-07-11 stsp }
5981 64c6d990 2019-07-11 stsp
5982 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5983 64c6d990 2019-07-11 stsp const char *path_prefix;
5984 64c6d990 2019-07-11 stsp size_t len;
5985 8ca9bd68 2019-07-25 stsp int errcode;
5986 64c6d990 2019-07-11 stsp };
5987 64c6d990 2019-07-11 stsp
5988 64c6d990 2019-07-11 stsp static const struct got_error *
5989 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5990 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5991 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5992 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5993 64c6d990 2019-07-11 stsp {
5994 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5995 64c6d990 2019-07-11 stsp
5996 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5997 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5998 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5999 64c6d990 2019-07-11 stsp
6000 64c6d990 2019-07-11 stsp return NULL;
6001 64c6d990 2019-07-11 stsp }
6002 64c6d990 2019-07-11 stsp
6003 64c6d990 2019-07-11 stsp static const struct got_error *
6004 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6005 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6006 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6007 64c6d990 2019-07-11 stsp {
6008 64c6d990 2019-07-11 stsp const struct got_error *err;
6009 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6010 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6011 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6012 64c6d990 2019-07-11 stsp
6013 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6014 64c6d990 2019-07-11 stsp return NULL;
6015 64c6d990 2019-07-11 stsp
6016 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
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_commit(&parent_commit, repo, parent_id);
6021 64c6d990 2019-07-11 stsp if (err)
6022 64c6d990 2019-07-11 stsp goto done;
6023 64c6d990 2019-07-11 stsp
6024 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6025 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6026 64c6d990 2019-07-11 stsp if (err)
6027 64c6d990 2019-07-11 stsp goto done;
6028 64c6d990 2019-07-11 stsp
6029 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6030 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6031 64c6d990 2019-07-11 stsp if (err)
6032 64c6d990 2019-07-11 stsp goto done;
6033 64c6d990 2019-07-11 stsp
6034 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6035 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6036 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6037 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6038 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6039 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6040 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6041 64c6d990 2019-07-11 stsp done:
6042 64c6d990 2019-07-11 stsp if (tree1)
6043 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6044 64c6d990 2019-07-11 stsp if (tree2)
6045 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6046 64c6d990 2019-07-11 stsp if (commit)
6047 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6048 64c6d990 2019-07-11 stsp if (parent_commit)
6049 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6050 64c6d990 2019-07-11 stsp return err;
6051 ff0d2220 2019-07-11 stsp }
6052 ff0d2220 2019-07-11 stsp
6053 ff0d2220 2019-07-11 stsp static const struct got_error *
6054 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6055 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6056 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6057 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6058 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6059 af61c510 2019-07-19 stsp {
6060 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6061 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6062 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6063 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6064 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6065 af61c510 2019-07-19 stsp
6066 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6067 af61c510 2019-07-19 stsp if (err)
6068 af61c510 2019-07-19 stsp return err;
6069 af61c510 2019-07-19 stsp
6070 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6071 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6072 af61c510 2019-07-19 stsp if (err)
6073 af61c510 2019-07-19 stsp goto done;
6074 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6075 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6076 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6077 af61c510 2019-07-19 stsp if (err) {
6078 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6079 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6080 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6081 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6082 af61c510 2019-07-19 stsp "been reached?!?");
6083 ee780d5c 2020-01-04 stsp }
6084 ee780d5c 2020-01-04 stsp goto done;
6085 af61c510 2019-07-19 stsp } else {
6086 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6087 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6088 af61c510 2019-07-19 stsp if (err)
6089 af61c510 2019-07-19 stsp goto done;
6090 af61c510 2019-07-19 stsp
6091 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6092 af61c510 2019-07-19 stsp if (err)
6093 af61c510 2019-07-19 stsp goto done;
6094 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6095 af61c510 2019-07-19 stsp commit_id = parent_id;
6096 af61c510 2019-07-19 stsp }
6097 af61c510 2019-07-19 stsp }
6098 af61c510 2019-07-19 stsp done:
6099 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6100 af61c510 2019-07-19 stsp return err;
6101 af61c510 2019-07-19 stsp }
6102 af61c510 2019-07-19 stsp
6103 af61c510 2019-07-19 stsp static const struct got_error *
6104 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6105 818c7501 2019-07-11 stsp {
6106 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6107 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6108 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6109 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6110 818c7501 2019-07-11 stsp char *cwd = NULL;
6111 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6112 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6113 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6114 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6115 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6116 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6117 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6118 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6119 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6120 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6121 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6122 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6123 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6124 818c7501 2019-07-11 stsp
6125 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6126 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6127 818c7501 2019-07-11 stsp
6128 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6129 818c7501 2019-07-11 stsp switch (ch) {
6130 818c7501 2019-07-11 stsp case 'a':
6131 818c7501 2019-07-11 stsp abort_rebase = 1;
6132 818c7501 2019-07-11 stsp break;
6133 818c7501 2019-07-11 stsp case 'c':
6134 818c7501 2019-07-11 stsp continue_rebase = 1;
6135 818c7501 2019-07-11 stsp break;
6136 818c7501 2019-07-11 stsp default:
6137 818c7501 2019-07-11 stsp usage_rebase();
6138 818c7501 2019-07-11 stsp /* NOTREACHED */
6139 818c7501 2019-07-11 stsp }
6140 818c7501 2019-07-11 stsp }
6141 818c7501 2019-07-11 stsp
6142 818c7501 2019-07-11 stsp argc -= optind;
6143 818c7501 2019-07-11 stsp argv += optind;
6144 818c7501 2019-07-11 stsp
6145 43012d58 2019-07-14 stsp #ifndef PROFILE
6146 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6147 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6148 43012d58 2019-07-14 stsp err(1, "pledge");
6149 43012d58 2019-07-14 stsp #endif
6150 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6151 818c7501 2019-07-11 stsp usage_rebase();
6152 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6153 818c7501 2019-07-11 stsp if (argc != 0)
6154 818c7501 2019-07-11 stsp usage_rebase();
6155 818c7501 2019-07-11 stsp } else if (argc != 1)
6156 818c7501 2019-07-11 stsp usage_rebase();
6157 818c7501 2019-07-11 stsp
6158 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6159 818c7501 2019-07-11 stsp if (cwd == NULL) {
6160 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6161 818c7501 2019-07-11 stsp goto done;
6162 818c7501 2019-07-11 stsp }
6163 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6164 818c7501 2019-07-11 stsp if (error)
6165 818c7501 2019-07-11 stsp goto done;
6166 818c7501 2019-07-11 stsp
6167 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6168 c9956ddf 2019-09-08 stsp NULL);
6169 818c7501 2019-07-11 stsp if (error != NULL)
6170 818c7501 2019-07-11 stsp goto done;
6171 818c7501 2019-07-11 stsp
6172 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6173 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6174 7ef62c4e 2020-02-24 stsp if (error)
6175 7ef62c4e 2020-02-24 stsp goto done;
6176 7ef62c4e 2020-02-24 stsp
6177 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6178 7ef62c4e 2020-02-24 stsp worktree);
6179 818c7501 2019-07-11 stsp if (error)
6180 7ef62c4e 2020-02-24 stsp goto done;
6181 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6182 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6183 818c7501 2019-07-11 stsp goto done;
6184 7ef62c4e 2020-02-24 stsp }
6185 818c7501 2019-07-11 stsp
6186 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6187 818c7501 2019-07-11 stsp if (error)
6188 818c7501 2019-07-11 stsp goto done;
6189 818c7501 2019-07-11 stsp
6190 f6794adc 2019-07-23 stsp if (abort_rebase) {
6191 818c7501 2019-07-11 stsp int did_something;
6192 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6193 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6194 f6794adc 2019-07-23 stsp goto done;
6195 f6794adc 2019-07-23 stsp }
6196 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6197 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6198 3e3a69f1 2019-07-25 stsp worktree, repo);
6199 818c7501 2019-07-11 stsp if (error)
6200 818c7501 2019-07-11 stsp goto done;
6201 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6202 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6203 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6204 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6205 818c7501 2019-07-11 stsp if (error)
6206 818c7501 2019-07-11 stsp goto done;
6207 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6208 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6209 818c7501 2019-07-11 stsp }
6210 818c7501 2019-07-11 stsp
6211 818c7501 2019-07-11 stsp if (continue_rebase) {
6212 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6213 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6214 f6794adc 2019-07-23 stsp goto done;
6215 f6794adc 2019-07-23 stsp }
6216 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6217 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6218 3e3a69f1 2019-07-25 stsp worktree, repo);
6219 818c7501 2019-07-11 stsp if (error)
6220 818c7501 2019-07-11 stsp goto done;
6221 818c7501 2019-07-11 stsp
6222 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6223 01757395 2019-07-12 stsp resume_commit_id, repo);
6224 818c7501 2019-07-11 stsp if (error)
6225 818c7501 2019-07-11 stsp goto done;
6226 818c7501 2019-07-11 stsp
6227 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6228 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6229 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6230 818c7501 2019-07-11 stsp goto done;
6231 818c7501 2019-07-11 stsp }
6232 818c7501 2019-07-11 stsp } else {
6233 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6234 818c7501 2019-07-11 stsp if (error != NULL)
6235 818c7501 2019-07-11 stsp goto done;
6236 ff0d2220 2019-07-11 stsp }
6237 818c7501 2019-07-11 stsp
6238 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6239 ff0d2220 2019-07-11 stsp if (error)
6240 ff0d2220 2019-07-11 stsp goto done;
6241 ff0d2220 2019-07-11 stsp
6242 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6243 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6244 a51a74b3 2019-07-27 stsp
6245 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6246 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6247 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6248 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6249 818c7501 2019-07-11 stsp if (error)
6250 818c7501 2019-07-11 stsp goto done;
6251 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6252 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6253 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6254 818c7501 2019-07-11 stsp "with work tree's branch");
6255 818c7501 2019-07-11 stsp goto done;
6256 818c7501 2019-07-11 stsp }
6257 818c7501 2019-07-11 stsp
6258 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6259 a51a74b3 2019-07-27 stsp if (error) {
6260 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6261 a51a74b3 2019-07-27 stsp goto done;
6262 a51a74b3 2019-07-27 stsp error = NULL;
6263 a51a74b3 2019-07-27 stsp } else {
6264 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6265 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6266 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6267 a51a74b3 2019-07-27 stsp goto done;
6268 a51a74b3 2019-07-27 stsp }
6269 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6270 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6271 818c7501 2019-07-11 stsp if (error)
6272 818c7501 2019-07-11 stsp goto done;
6273 818c7501 2019-07-11 stsp }
6274 818c7501 2019-07-11 stsp
6275 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6276 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6277 818c7501 2019-07-11 stsp if (error)
6278 818c7501 2019-07-11 stsp goto done;
6279 818c7501 2019-07-11 stsp
6280 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6281 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6282 fc66b545 2019-08-12 stsp if (pid == NULL) {
6283 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6284 fc66b545 2019-08-12 stsp int did_something;
6285 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6286 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6287 fc66b545 2019-08-12 stsp &did_something);
6288 fc66b545 2019-08-12 stsp if (error)
6289 fc66b545 2019-08-12 stsp goto done;
6290 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6291 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6292 fc66b545 2019-08-12 stsp }
6293 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6294 fc66b545 2019-08-12 stsp goto done;
6295 fc66b545 2019-08-12 stsp }
6296 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6297 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6298 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6299 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6300 818c7501 2019-07-11 stsp commit = NULL;
6301 818c7501 2019-07-11 stsp if (error)
6302 818c7501 2019-07-11 stsp goto done;
6303 64c6d990 2019-07-11 stsp
6304 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6305 38b0338b 2019-11-29 stsp if (continue_rebase) {
6306 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6307 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6308 38b0338b 2019-11-29 stsp goto done;
6309 38b0338b 2019-11-29 stsp } else {
6310 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6311 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6312 38b0338b 2019-11-29 stsp char *id_str;
6313 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6314 38b0338b 2019-11-29 stsp new_base_branch);
6315 38b0338b 2019-11-29 stsp if (error)
6316 38b0338b 2019-11-29 stsp goto done;
6317 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6318 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6319 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6320 38b0338b 2019-11-29 stsp free(id_str);
6321 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6322 38b0338b 2019-11-29 stsp new_head_commit_id);
6323 38b0338b 2019-11-29 stsp if (error)
6324 38b0338b 2019-11-29 stsp goto done;
6325 38b0338b 2019-11-29 stsp }
6326 818c7501 2019-07-11 stsp }
6327 818c7501 2019-07-11 stsp
6328 818c7501 2019-07-11 stsp pid = NULL;
6329 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6330 818c7501 2019-07-11 stsp commit_id = qid->id;
6331 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6332 818c7501 2019-07-11 stsp pid = qid;
6333 818c7501 2019-07-11 stsp
6334 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6335 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6336 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6337 818c7501 2019-07-11 stsp if (error)
6338 818c7501 2019-07-11 stsp goto done;
6339 818c7501 2019-07-11 stsp
6340 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6341 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6342 a0ea4fc0 2020-02-28 stsp if (error)
6343 a0ea4fc0 2020-02-28 stsp goto done;
6344 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6345 818c7501 2019-07-11 stsp break;
6346 01757395 2019-07-12 stsp }
6347 818c7501 2019-07-11 stsp
6348 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6349 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6350 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6351 818c7501 2019-07-11 stsp if (error)
6352 818c7501 2019-07-11 stsp goto done;
6353 818c7501 2019-07-11 stsp }
6354 818c7501 2019-07-11 stsp
6355 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6356 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6357 818c7501 2019-07-11 stsp if (error)
6358 818c7501 2019-07-11 stsp goto done;
6359 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6360 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6361 818c7501 2019-07-11 stsp } else
6362 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6363 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6364 818c7501 2019-07-11 stsp done:
6365 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6366 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6367 818c7501 2019-07-11 stsp free(resume_commit_id);
6368 818c7501 2019-07-11 stsp free(yca_id);
6369 818c7501 2019-07-11 stsp if (commit)
6370 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6371 818c7501 2019-07-11 stsp if (branch)
6372 818c7501 2019-07-11 stsp got_ref_close(branch);
6373 818c7501 2019-07-11 stsp if (new_base_branch)
6374 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6375 818c7501 2019-07-11 stsp if (tmp_branch)
6376 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6377 5ef14e63 2019-06-02 stsp if (worktree)
6378 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6379 5ef14e63 2019-06-02 stsp if (repo)
6380 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6381 5ef14e63 2019-06-02 stsp return error;
6382 0ebf8283 2019-07-24 stsp }
6383 0ebf8283 2019-07-24 stsp
6384 0ebf8283 2019-07-24 stsp __dead static void
6385 0ebf8283 2019-07-24 stsp usage_histedit(void)
6386 0ebf8283 2019-07-24 stsp {
6387 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6388 0ebf8283 2019-07-24 stsp getprogname());
6389 0ebf8283 2019-07-24 stsp exit(1);
6390 0ebf8283 2019-07-24 stsp }
6391 0ebf8283 2019-07-24 stsp
6392 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6393 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6394 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6395 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6396 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6397 0ebf8283 2019-07-24 stsp
6398 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6399 0ebf8283 2019-07-24 stsp unsigned char code;
6400 0ebf8283 2019-07-24 stsp const char *name;
6401 0ebf8283 2019-07-24 stsp const char *desc;
6402 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6403 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6404 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6405 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6406 82997472 2020-01-29 stsp "be used" },
6407 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6408 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6409 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6410 0ebf8283 2019-07-24 stsp };
6411 0ebf8283 2019-07-24 stsp
6412 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6413 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6414 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6415 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6416 0ebf8283 2019-07-24 stsp char *logmsg;
6417 0ebf8283 2019-07-24 stsp };
6418 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6419 0ebf8283 2019-07-24 stsp
6420 0ebf8283 2019-07-24 stsp static const struct got_error *
6421 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6422 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6423 0ebf8283 2019-07-24 stsp {
6424 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6425 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6426 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6427 8138f3e1 2019-08-11 stsp int n;
6428 0ebf8283 2019-07-24 stsp
6429 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6430 0ebf8283 2019-07-24 stsp if (err)
6431 0ebf8283 2019-07-24 stsp goto done;
6432 0ebf8283 2019-07-24 stsp
6433 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6434 0ebf8283 2019-07-24 stsp if (err)
6435 0ebf8283 2019-07-24 stsp goto done;
6436 0ebf8283 2019-07-24 stsp
6437 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6438 0ebf8283 2019-07-24 stsp if (err)
6439 0ebf8283 2019-07-24 stsp goto done;
6440 0ebf8283 2019-07-24 stsp
6441 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6442 0ebf8283 2019-07-24 stsp if (n < 0)
6443 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6444 0ebf8283 2019-07-24 stsp done:
6445 0ebf8283 2019-07-24 stsp if (commit)
6446 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6447 0ebf8283 2019-07-24 stsp free(id_str);
6448 0ebf8283 2019-07-24 stsp free(logmsg);
6449 0ebf8283 2019-07-24 stsp return err;
6450 0ebf8283 2019-07-24 stsp }
6451 0ebf8283 2019-07-24 stsp
6452 0ebf8283 2019-07-24 stsp static const struct got_error *
6453 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6454 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6455 0ebf8283 2019-07-24 stsp {
6456 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6457 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6458 0ebf8283 2019-07-24 stsp
6459 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6460 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6461 0ebf8283 2019-07-24 stsp
6462 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6463 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6464 0ebf8283 2019-07-24 stsp f, repo);
6465 0ebf8283 2019-07-24 stsp if (err)
6466 0ebf8283 2019-07-24 stsp break;
6467 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6468 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6469 083957f4 2020-02-24 stsp if (n < 0) {
6470 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6471 083957f4 2020-02-24 stsp break;
6472 083957f4 2020-02-24 stsp }
6473 083957f4 2020-02-24 stsp }
6474 0ebf8283 2019-07-24 stsp }
6475 0ebf8283 2019-07-24 stsp
6476 0ebf8283 2019-07-24 stsp return err;
6477 0ebf8283 2019-07-24 stsp }
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp static const struct got_error *
6480 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6481 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6482 0ebf8283 2019-07-24 stsp {
6483 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6484 0ebf8283 2019-07-24 stsp int n, i;
6485 514f2ffe 2020-01-29 stsp char *id_str;
6486 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6487 514f2ffe 2020-01-29 stsp
6488 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6489 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6490 514f2ffe 2020-01-29 stsp if (err)
6491 514f2ffe 2020-01-29 stsp return err;
6492 514f2ffe 2020-01-29 stsp
6493 514f2ffe 2020-01-29 stsp n = fprintf(f,
6494 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6495 514f2ffe 2020-01-29 stsp "# commit %s\n"
6496 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6497 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6498 514f2ffe 2020-01-29 stsp if (n < 0) {
6499 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6500 514f2ffe 2020-01-29 stsp goto done;
6501 514f2ffe 2020-01-29 stsp }
6502 0ebf8283 2019-07-24 stsp
6503 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6504 514f2ffe 2020-01-29 stsp if (n < 0) {
6505 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6506 514f2ffe 2020-01-29 stsp goto done;
6507 514f2ffe 2020-01-29 stsp }
6508 0ebf8283 2019-07-24 stsp
6509 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6510 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6511 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6512 0ebf8283 2019-07-24 stsp cmd->desc);
6513 0ebf8283 2019-07-24 stsp if (n < 0) {
6514 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6515 0ebf8283 2019-07-24 stsp break;
6516 0ebf8283 2019-07-24 stsp }
6517 0ebf8283 2019-07-24 stsp }
6518 514f2ffe 2020-01-29 stsp done:
6519 514f2ffe 2020-01-29 stsp free(id_str);
6520 0ebf8283 2019-07-24 stsp return err;
6521 0ebf8283 2019-07-24 stsp }
6522 0ebf8283 2019-07-24 stsp
6523 0ebf8283 2019-07-24 stsp static const struct got_error *
6524 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6525 0ebf8283 2019-07-24 stsp {
6526 0ebf8283 2019-07-24 stsp static char msg[42];
6527 0ebf8283 2019-07-24 stsp int ret;
6528 0ebf8283 2019-07-24 stsp
6529 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6530 0ebf8283 2019-07-24 stsp lineno);
6531 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6532 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6533 0ebf8283 2019-07-24 stsp
6534 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6535 0ebf8283 2019-07-24 stsp }
6536 0ebf8283 2019-07-24 stsp
6537 0ebf8283 2019-07-24 stsp static const struct got_error *
6538 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6539 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6540 0ebf8283 2019-07-24 stsp {
6541 0ebf8283 2019-07-24 stsp const struct got_error *err;
6542 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6543 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6544 0ebf8283 2019-07-24 stsp
6545 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6546 0ebf8283 2019-07-24 stsp if (err)
6547 0ebf8283 2019-07-24 stsp return err;
6548 0ebf8283 2019-07-24 stsp
6549 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6550 0ebf8283 2019-07-24 stsp if (err)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp
6553 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6554 5943eee2 2019-08-13 stsp if (err)
6555 5943eee2 2019-08-13 stsp goto done;
6556 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6557 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6558 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6559 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6560 0ebf8283 2019-07-24 stsp }
6561 0ebf8283 2019-07-24 stsp done:
6562 0ebf8283 2019-07-24 stsp if (folded_commit)
6563 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6564 0ebf8283 2019-07-24 stsp free(id_str);
6565 5943eee2 2019-08-13 stsp free(folded_logmsg);
6566 0ebf8283 2019-07-24 stsp return err;
6567 0ebf8283 2019-07-24 stsp }
6568 0ebf8283 2019-07-24 stsp
6569 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6570 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6571 0ebf8283 2019-07-24 stsp {
6572 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6573 0ebf8283 2019-07-24 stsp
6574 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6575 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6576 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6577 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6578 3f9de99f 2019-07-24 stsp folded = prev;
6579 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6580 0ebf8283 2019-07-24 stsp }
6581 0ebf8283 2019-07-24 stsp
6582 0ebf8283 2019-07-24 stsp return folded;
6583 0ebf8283 2019-07-24 stsp }
6584 0ebf8283 2019-07-24 stsp
6585 0ebf8283 2019-07-24 stsp static const struct got_error *
6586 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6587 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6588 0ebf8283 2019-07-24 stsp {
6589 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6590 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6591 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6592 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6593 0ebf8283 2019-07-24 stsp int fd;
6594 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6595 0ebf8283 2019-07-24 stsp
6596 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6597 0ebf8283 2019-07-24 stsp if (err)
6598 0ebf8283 2019-07-24 stsp return err;
6599 0ebf8283 2019-07-24 stsp
6600 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6601 0ebf8283 2019-07-24 stsp if (folded) {
6602 0ebf8283 2019-07-24 stsp while (folded != hle) {
6603 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6604 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6605 3f9de99f 2019-07-24 stsp continue;
6606 3f9de99f 2019-07-24 stsp }
6607 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6608 0ebf8283 2019-07-24 stsp logmsg, repo);
6609 0ebf8283 2019-07-24 stsp if (err)
6610 0ebf8283 2019-07-24 stsp goto done;
6611 0ebf8283 2019-07-24 stsp free(logmsg);
6612 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6613 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6614 0ebf8283 2019-07-24 stsp }
6615 0ebf8283 2019-07-24 stsp }
6616 0ebf8283 2019-07-24 stsp
6617 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6618 0ebf8283 2019-07-24 stsp if (err)
6619 0ebf8283 2019-07-24 stsp goto done;
6620 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6621 5943eee2 2019-08-13 stsp if (err)
6622 5943eee2 2019-08-13 stsp goto done;
6623 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6624 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6625 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6626 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6627 0ebf8283 2019-07-24 stsp goto done;
6628 0ebf8283 2019-07-24 stsp }
6629 0ebf8283 2019-07-24 stsp free(logmsg);
6630 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6631 0ebf8283 2019-07-24 stsp
6632 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6633 0ebf8283 2019-07-24 stsp if (err)
6634 0ebf8283 2019-07-24 stsp goto done;
6635 0ebf8283 2019-07-24 stsp
6636 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6637 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6638 0ebf8283 2019-07-24 stsp if (err)
6639 0ebf8283 2019-07-24 stsp goto done;
6640 0ebf8283 2019-07-24 stsp
6641 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6642 0ebf8283 2019-07-24 stsp close(fd);
6643 0ebf8283 2019-07-24 stsp
6644 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6645 0ebf8283 2019-07-24 stsp if (err)
6646 0ebf8283 2019-07-24 stsp goto done;
6647 0ebf8283 2019-07-24 stsp
6648 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6649 0ebf8283 2019-07-24 stsp if (err) {
6650 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6651 0ebf8283 2019-07-24 stsp goto done;
6652 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6653 0ebf8283 2019-07-24 stsp }
6654 0ebf8283 2019-07-24 stsp done:
6655 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6656 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6657 0ebf8283 2019-07-24 stsp free(logmsg_path);
6658 0ebf8283 2019-07-24 stsp free(logmsg);
6659 5943eee2 2019-08-13 stsp free(orig_logmsg);
6660 0ebf8283 2019-07-24 stsp free(editor);
6661 0ebf8283 2019-07-24 stsp if (commit)
6662 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6663 0ebf8283 2019-07-24 stsp return err;
6664 5ef14e63 2019-06-02 stsp }
6665 0ebf8283 2019-07-24 stsp
6666 0ebf8283 2019-07-24 stsp static const struct got_error *
6667 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6668 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6669 0ebf8283 2019-07-24 stsp {
6670 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6671 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6672 0ebf8283 2019-07-24 stsp size_t size;
6673 0ebf8283 2019-07-24 stsp ssize_t len;
6674 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6675 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6676 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6677 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6678 0ebf8283 2019-07-24 stsp
6679 0ebf8283 2019-07-24 stsp for (;;) {
6680 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6681 0ebf8283 2019-07-24 stsp if (len == -1) {
6682 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6683 0ebf8283 2019-07-24 stsp if (feof(f))
6684 0ebf8283 2019-07-24 stsp break;
6685 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6686 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6687 0ebf8283 2019-07-24 stsp break;
6688 0ebf8283 2019-07-24 stsp }
6689 0ebf8283 2019-07-24 stsp lineno++;
6690 0ebf8283 2019-07-24 stsp p = line;
6691 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6692 0ebf8283 2019-07-24 stsp p++;
6693 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6694 0ebf8283 2019-07-24 stsp free(line);
6695 0ebf8283 2019-07-24 stsp line = NULL;
6696 0ebf8283 2019-07-24 stsp continue;
6697 0ebf8283 2019-07-24 stsp }
6698 0ebf8283 2019-07-24 stsp cmd = NULL;
6699 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6700 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6701 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6702 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6703 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6704 0ebf8283 2019-07-24 stsp break;
6705 0ebf8283 2019-07-24 stsp }
6706 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6707 0ebf8283 2019-07-24 stsp p++;
6708 0ebf8283 2019-07-24 stsp break;
6709 0ebf8283 2019-07-24 stsp }
6710 0ebf8283 2019-07-24 stsp }
6711 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6712 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6713 0ebf8283 2019-07-24 stsp break;
6714 0ebf8283 2019-07-24 stsp }
6715 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6716 0ebf8283 2019-07-24 stsp p++;
6717 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6718 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6719 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6720 0ebf8283 2019-07-24 stsp break;
6721 0ebf8283 2019-07-24 stsp }
6722 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6723 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6724 0ebf8283 2019-07-24 stsp if (err)
6725 0ebf8283 2019-07-24 stsp break;
6726 0ebf8283 2019-07-24 stsp } else {
6727 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6728 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6729 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6730 0ebf8283 2019-07-24 stsp break;
6731 0ebf8283 2019-07-24 stsp }
6732 0ebf8283 2019-07-24 stsp }
6733 0ebf8283 2019-07-24 stsp free(line);
6734 0ebf8283 2019-07-24 stsp line = NULL;
6735 0ebf8283 2019-07-24 stsp continue;
6736 0ebf8283 2019-07-24 stsp } else {
6737 0ebf8283 2019-07-24 stsp end = p;
6738 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6739 0ebf8283 2019-07-24 stsp end++;
6740 0ebf8283 2019-07-24 stsp *end = '\0';
6741 0ebf8283 2019-07-24 stsp
6742 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6743 0ebf8283 2019-07-24 stsp if (err) {
6744 0ebf8283 2019-07-24 stsp /* override error code */
6745 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6746 0ebf8283 2019-07-24 stsp break;
6747 0ebf8283 2019-07-24 stsp }
6748 0ebf8283 2019-07-24 stsp }
6749 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6750 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6751 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6752 0ebf8283 2019-07-24 stsp break;
6753 0ebf8283 2019-07-24 stsp }
6754 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6755 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6756 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6757 0ebf8283 2019-07-24 stsp commit_id = NULL;
6758 0ebf8283 2019-07-24 stsp free(line);
6759 0ebf8283 2019-07-24 stsp line = NULL;
6760 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6761 0ebf8283 2019-07-24 stsp }
6762 0ebf8283 2019-07-24 stsp
6763 0ebf8283 2019-07-24 stsp free(line);
6764 0ebf8283 2019-07-24 stsp free(commit_id);
6765 0ebf8283 2019-07-24 stsp return err;
6766 0ebf8283 2019-07-24 stsp }
6767 0ebf8283 2019-07-24 stsp
6768 0ebf8283 2019-07-24 stsp static const struct got_error *
6769 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6770 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6771 bfce7f83 2019-07-27 stsp {
6772 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6773 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6774 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6775 5b87815e 2020-03-05 stsp static char msg[92];
6776 bfce7f83 2019-07-27 stsp char *id_str;
6777 bfce7f83 2019-07-27 stsp
6778 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6779 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6780 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6781 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6782 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6783 5b87815e 2020-03-05 stsp
6784 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6785 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6786 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6787 5b87815e 2020-03-05 stsp if (hle == hle2)
6788 5b87815e 2020-03-05 stsp continue;
6789 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6790 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6791 5b87815e 2020-03-05 stsp continue;
6792 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6793 5b87815e 2020-03-05 stsp if (err)
6794 5b87815e 2020-03-05 stsp return err;
6795 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6796 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6797 5b87815e 2020-03-05 stsp free(id_str);
6798 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6799 5b87815e 2020-03-05 stsp }
6800 5b87815e 2020-03-05 stsp }
6801 bfce7f83 2019-07-27 stsp
6802 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6803 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6804 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6805 bfce7f83 2019-07-27 stsp break;
6806 bfce7f83 2019-07-27 stsp }
6807 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6808 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6809 bfce7f83 2019-07-27 stsp if (err)
6810 bfce7f83 2019-07-27 stsp return err;
6811 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6812 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6813 bfce7f83 2019-07-27 stsp free(id_str);
6814 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6815 bfce7f83 2019-07-27 stsp }
6816 bfce7f83 2019-07-27 stsp }
6817 bfce7f83 2019-07-27 stsp
6818 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6819 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6820 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6821 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6822 bfce7f83 2019-07-27 stsp
6823 bfce7f83 2019-07-27 stsp return NULL;
6824 bfce7f83 2019-07-27 stsp }
6825 bfce7f83 2019-07-27 stsp
6826 bfce7f83 2019-07-27 stsp static const struct got_error *
6827 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6828 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6829 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6830 0ebf8283 2019-07-24 stsp {
6831 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6832 0ebf8283 2019-07-24 stsp char *editor;
6833 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6834 0ebf8283 2019-07-24 stsp
6835 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6836 0ebf8283 2019-07-24 stsp if (err)
6837 0ebf8283 2019-07-24 stsp return err;
6838 0ebf8283 2019-07-24 stsp
6839 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6840 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6841 0ebf8283 2019-07-24 stsp goto done;
6842 0ebf8283 2019-07-24 stsp }
6843 0ebf8283 2019-07-24 stsp
6844 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6845 0ebf8283 2019-07-24 stsp if (f == NULL) {
6846 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6847 0ebf8283 2019-07-24 stsp goto done;
6848 0ebf8283 2019-07-24 stsp }
6849 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6850 bfce7f83 2019-07-27 stsp if (err)
6851 bfce7f83 2019-07-27 stsp goto done;
6852 bfce7f83 2019-07-27 stsp
6853 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6854 0ebf8283 2019-07-24 stsp done:
6855 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6856 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6857 0ebf8283 2019-07-24 stsp free(editor);
6858 0ebf8283 2019-07-24 stsp return err;
6859 0ebf8283 2019-07-24 stsp }
6860 0ebf8283 2019-07-24 stsp
6861 0ebf8283 2019-07-24 stsp static const struct got_error *
6862 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6863 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6864 514f2ffe 2020-01-29 stsp struct got_repository *);
6865 0ebf8283 2019-07-24 stsp
6866 0ebf8283 2019-07-24 stsp static const struct got_error *
6867 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6868 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6869 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6870 0ebf8283 2019-07-24 stsp {
6871 0ebf8283 2019-07-24 stsp const struct got_error *err;
6872 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6873 0ebf8283 2019-07-24 stsp char *path = NULL;
6874 0ebf8283 2019-07-24 stsp
6875 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6876 0ebf8283 2019-07-24 stsp if (err)
6877 0ebf8283 2019-07-24 stsp return err;
6878 0ebf8283 2019-07-24 stsp
6879 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6880 0ebf8283 2019-07-24 stsp if (err)
6881 0ebf8283 2019-07-24 stsp goto done;
6882 0ebf8283 2019-07-24 stsp
6883 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6884 0ebf8283 2019-07-24 stsp if (err)
6885 0ebf8283 2019-07-24 stsp goto done;
6886 0ebf8283 2019-07-24 stsp
6887 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6888 083957f4 2020-02-24 stsp rewind(f);
6889 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6890 083957f4 2020-02-24 stsp } else {
6891 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6892 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6893 0ebf8283 2019-07-24 stsp goto done;
6894 083957f4 2020-02-24 stsp }
6895 083957f4 2020-02-24 stsp f = NULL;
6896 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6897 083957f4 2020-02-24 stsp if (err) {
6898 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6899 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6900 083957f4 2020-02-24 stsp goto done;
6901 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6902 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6903 083957f4 2020-02-24 stsp }
6904 0ebf8283 2019-07-24 stsp }
6905 0ebf8283 2019-07-24 stsp done:
6906 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6907 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6908 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6909 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6910 0ebf8283 2019-07-24 stsp free(path);
6911 0ebf8283 2019-07-24 stsp return err;
6912 0ebf8283 2019-07-24 stsp }
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp static const struct got_error *
6915 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6916 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6917 0ebf8283 2019-07-24 stsp {
6918 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6919 0ebf8283 2019-07-24 stsp char *path = NULL;
6920 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6921 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6922 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6923 0ebf8283 2019-07-24 stsp
6924 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6925 0ebf8283 2019-07-24 stsp if (err)
6926 0ebf8283 2019-07-24 stsp return err;
6927 0ebf8283 2019-07-24 stsp
6928 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6929 0ebf8283 2019-07-24 stsp if (f == NULL) {
6930 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp }
6933 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6934 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6935 0ebf8283 2019-07-24 stsp repo);
6936 0ebf8283 2019-07-24 stsp if (err)
6937 0ebf8283 2019-07-24 stsp break;
6938 0ebf8283 2019-07-24 stsp
6939 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6940 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6941 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6942 0ebf8283 2019-07-24 stsp if (n < 0) {
6943 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6944 0ebf8283 2019-07-24 stsp break;
6945 0ebf8283 2019-07-24 stsp }
6946 0ebf8283 2019-07-24 stsp }
6947 0ebf8283 2019-07-24 stsp }
6948 0ebf8283 2019-07-24 stsp done:
6949 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6950 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6951 0ebf8283 2019-07-24 stsp free(path);
6952 0ebf8283 2019-07-24 stsp if (commit)
6953 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6954 0ebf8283 2019-07-24 stsp return err;
6955 0ebf8283 2019-07-24 stsp }
6956 0ebf8283 2019-07-24 stsp
6957 bfce7f83 2019-07-27 stsp void
6958 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6959 bfce7f83 2019-07-27 stsp {
6960 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6961 bfce7f83 2019-07-27 stsp
6962 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6963 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6964 bfce7f83 2019-07-27 stsp free(hle);
6965 bfce7f83 2019-07-27 stsp }
6966 bfce7f83 2019-07-27 stsp }
6967 bfce7f83 2019-07-27 stsp
6968 0ebf8283 2019-07-24 stsp static const struct got_error *
6969 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6970 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6971 0ebf8283 2019-07-24 stsp {
6972 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6973 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6976 0ebf8283 2019-07-24 stsp if (f == NULL) {
6977 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6978 0ebf8283 2019-07-24 stsp goto done;
6979 0ebf8283 2019-07-24 stsp }
6980 0ebf8283 2019-07-24 stsp
6981 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6982 0ebf8283 2019-07-24 stsp done:
6983 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6984 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6985 0ebf8283 2019-07-24 stsp return err;
6986 0ebf8283 2019-07-24 stsp }
6987 0ebf8283 2019-07-24 stsp
6988 0ebf8283 2019-07-24 stsp static const struct got_error *
6989 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6990 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6991 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6992 0ebf8283 2019-07-24 stsp {
6993 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6994 0ebf8283 2019-07-24 stsp int resp = ' ';
6995 0ebf8283 2019-07-24 stsp
6996 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6997 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6998 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6999 0ebf8283 2019-07-24 stsp resp = getchar();
7000 a61a4414 2019-08-07 stsp if (resp == '\n')
7001 a61a4414 2019-08-07 stsp resp = getchar();
7002 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7003 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7004 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7005 bfce7f83 2019-07-27 stsp repo);
7006 426ebf2e 2019-07-25 stsp if (err) {
7007 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7008 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7009 426ebf2e 2019-07-25 stsp break;
7010 bfce7f83 2019-07-27 stsp prev_err = err;
7011 426ebf2e 2019-07-25 stsp resp = ' ';
7012 426ebf2e 2019-07-25 stsp continue;
7013 426ebf2e 2019-07-25 stsp }
7014 bfce7f83 2019-07-27 stsp break;
7015 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7016 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7017 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7018 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7019 426ebf2e 2019-07-25 stsp if (err) {
7020 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7021 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7022 426ebf2e 2019-07-25 stsp break;
7023 bfce7f83 2019-07-27 stsp prev_err = err;
7024 426ebf2e 2019-07-25 stsp resp = ' ';
7025 426ebf2e 2019-07-25 stsp continue;
7026 426ebf2e 2019-07-25 stsp }
7027 bfce7f83 2019-07-27 stsp break;
7028 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7029 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7030 0ebf8283 2019-07-24 stsp break;
7031 426ebf2e 2019-07-25 stsp } else
7032 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7033 0ebf8283 2019-07-24 stsp }
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp return err;
7036 0ebf8283 2019-07-24 stsp }
7037 0ebf8283 2019-07-24 stsp
7038 0ebf8283 2019-07-24 stsp static const struct got_error *
7039 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7040 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7041 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7042 0ebf8283 2019-07-24 stsp {
7043 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7044 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7045 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7046 3e3a69f1 2019-07-25 stsp branch, repo);
7047 0ebf8283 2019-07-24 stsp }
7048 0ebf8283 2019-07-24 stsp
7049 0ebf8283 2019-07-24 stsp static const struct got_error *
7050 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7051 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7052 0ebf8283 2019-07-24 stsp {
7053 0ebf8283 2019-07-24 stsp const struct got_error *err;
7054 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7055 0ebf8283 2019-07-24 stsp
7056 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7057 0ebf8283 2019-07-24 stsp if (err)
7058 0ebf8283 2019-07-24 stsp goto done;
7059 0ebf8283 2019-07-24 stsp
7060 0ebf8283 2019-07-24 stsp if (new_id) {
7061 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7062 0ebf8283 2019-07-24 stsp if (err)
7063 0ebf8283 2019-07-24 stsp goto done;
7064 0ebf8283 2019-07-24 stsp }
7065 0ebf8283 2019-07-24 stsp
7066 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7067 0ebf8283 2019-07-24 stsp if (new_id_str)
7068 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7069 0ebf8283 2019-07-24 stsp
7070 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7071 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7072 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7073 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7074 0ebf8283 2019-07-24 stsp goto done;
7075 0ebf8283 2019-07-24 stsp }
7076 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7077 0ebf8283 2019-07-24 stsp } else {
7078 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7079 0ebf8283 2019-07-24 stsp if (err)
7080 0ebf8283 2019-07-24 stsp goto done;
7081 0ebf8283 2019-07-24 stsp }
7082 0ebf8283 2019-07-24 stsp
7083 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7084 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7085 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7086 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7087 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7088 0ebf8283 2019-07-24 stsp break;
7089 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7090 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7091 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7092 0ebf8283 2019-07-24 stsp logmsg);
7093 0ebf8283 2019-07-24 stsp break;
7094 0ebf8283 2019-07-24 stsp default:
7095 0ebf8283 2019-07-24 stsp break;
7096 0ebf8283 2019-07-24 stsp }
7097 0ebf8283 2019-07-24 stsp done:
7098 0ebf8283 2019-07-24 stsp free(old_id_str);
7099 0ebf8283 2019-07-24 stsp free(new_id_str);
7100 0ebf8283 2019-07-24 stsp return err;
7101 0ebf8283 2019-07-24 stsp }
7102 0ebf8283 2019-07-24 stsp
7103 0ebf8283 2019-07-24 stsp static const struct got_error *
7104 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7105 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7106 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7107 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7108 0ebf8283 2019-07-24 stsp {
7109 0ebf8283 2019-07-24 stsp const struct got_error *err;
7110 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7111 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7112 0ebf8283 2019-07-24 stsp
7113 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7114 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7115 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7116 0ebf8283 2019-07-24 stsp if (err)
7117 0ebf8283 2019-07-24 stsp return err;
7118 0ebf8283 2019-07-24 stsp }
7119 0ebf8283 2019-07-24 stsp
7120 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7121 0ebf8283 2019-07-24 stsp if (err)
7122 0ebf8283 2019-07-24 stsp return err;
7123 0ebf8283 2019-07-24 stsp
7124 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7125 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7126 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7127 0ebf8283 2019-07-24 stsp if (err) {
7128 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7129 0ebf8283 2019-07-24 stsp goto done;
7130 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7131 0ebf8283 2019-07-24 stsp } else {
7132 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7133 0ebf8283 2019-07-24 stsp free(new_commit_id);
7134 0ebf8283 2019-07-24 stsp }
7135 0ebf8283 2019-07-24 stsp done:
7136 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7137 0ebf8283 2019-07-24 stsp return err;
7138 0ebf8283 2019-07-24 stsp }
7139 0ebf8283 2019-07-24 stsp
7140 0ebf8283 2019-07-24 stsp static const struct got_error *
7141 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7142 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7143 0ebf8283 2019-07-24 stsp {
7144 0ebf8283 2019-07-24 stsp const struct got_error *error;
7145 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7146 0ebf8283 2019-07-24 stsp
7147 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7148 0ebf8283 2019-07-24 stsp repo);
7149 0ebf8283 2019-07-24 stsp if (error)
7150 0ebf8283 2019-07-24 stsp return error;
7151 0ebf8283 2019-07-24 stsp
7152 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7153 0ebf8283 2019-07-24 stsp if (error)
7154 0ebf8283 2019-07-24 stsp return error;
7155 0ebf8283 2019-07-24 stsp
7156 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7157 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7158 0ebf8283 2019-07-24 stsp return error;
7159 ab20a43a 2020-01-29 stsp }
7160 ab20a43a 2020-01-29 stsp
7161 ab20a43a 2020-01-29 stsp static const struct got_error *
7162 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7163 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7164 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7165 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7166 ab20a43a 2020-01-29 stsp {
7167 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7168 ab20a43a 2020-01-29 stsp
7169 ab20a43a 2020-01-29 stsp switch (status) {
7170 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7171 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7172 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7173 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7174 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7175 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7176 ab20a43a 2020-01-29 stsp default:
7177 ab20a43a 2020-01-29 stsp break;
7178 ab20a43a 2020-01-29 stsp }
7179 ab20a43a 2020-01-29 stsp
7180 ab20a43a 2020-01-29 stsp switch (staged_status) {
7181 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7182 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7183 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7184 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7185 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7186 ab20a43a 2020-01-29 stsp default:
7187 ab20a43a 2020-01-29 stsp break;
7188 ab20a43a 2020-01-29 stsp }
7189 ab20a43a 2020-01-29 stsp
7190 ab20a43a 2020-01-29 stsp return NULL;
7191 0ebf8283 2019-07-24 stsp }
7192 0ebf8283 2019-07-24 stsp
7193 0ebf8283 2019-07-24 stsp static const struct got_error *
7194 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7195 0ebf8283 2019-07-24 stsp {
7196 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7197 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7198 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7199 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7200 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7201 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7202 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7203 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7204 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7205 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7206 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7207 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7208 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7209 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7210 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7211 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7212 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7213 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7214 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7215 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7216 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7217 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7218 0ebf8283 2019-07-24 stsp
7219 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7220 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7221 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7222 0ebf8283 2019-07-24 stsp
7223 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7224 0ebf8283 2019-07-24 stsp switch (ch) {
7225 0ebf8283 2019-07-24 stsp case 'a':
7226 0ebf8283 2019-07-24 stsp abort_edit = 1;
7227 0ebf8283 2019-07-24 stsp break;
7228 0ebf8283 2019-07-24 stsp case 'c':
7229 0ebf8283 2019-07-24 stsp continue_edit = 1;
7230 0ebf8283 2019-07-24 stsp break;
7231 0ebf8283 2019-07-24 stsp case 'F':
7232 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7233 0ebf8283 2019-07-24 stsp break;
7234 083957f4 2020-02-24 stsp case 'm':
7235 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7236 083957f4 2020-02-24 stsp break;
7237 0ebf8283 2019-07-24 stsp default:
7238 0ebf8283 2019-07-24 stsp usage_histedit();
7239 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7240 0ebf8283 2019-07-24 stsp }
7241 0ebf8283 2019-07-24 stsp }
7242 0ebf8283 2019-07-24 stsp
7243 0ebf8283 2019-07-24 stsp argc -= optind;
7244 0ebf8283 2019-07-24 stsp argv += optind;
7245 0ebf8283 2019-07-24 stsp
7246 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7247 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7248 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7249 0ebf8283 2019-07-24 stsp err(1, "pledge");
7250 0ebf8283 2019-07-24 stsp #endif
7251 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7252 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7253 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7254 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7255 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7256 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7257 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7258 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7259 0ebf8283 2019-07-24 stsp if (argc != 0)
7260 0ebf8283 2019-07-24 stsp usage_histedit();
7261 0ebf8283 2019-07-24 stsp
7262 0ebf8283 2019-07-24 stsp /*
7263 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7264 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7265 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7266 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7267 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7268 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7269 0ebf8283 2019-07-24 stsp */
7270 0ebf8283 2019-07-24 stsp
7271 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7272 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7273 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7274 0ebf8283 2019-07-24 stsp goto done;
7275 0ebf8283 2019-07-24 stsp }
7276 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7277 0ebf8283 2019-07-24 stsp if (error)
7278 0ebf8283 2019-07-24 stsp goto done;
7279 0ebf8283 2019-07-24 stsp
7280 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7281 c9956ddf 2019-09-08 stsp NULL);
7282 0ebf8283 2019-07-24 stsp if (error != NULL)
7283 0ebf8283 2019-07-24 stsp goto done;
7284 0ebf8283 2019-07-24 stsp
7285 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7286 0ebf8283 2019-07-24 stsp if (error)
7287 0ebf8283 2019-07-24 stsp goto done;
7288 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7289 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7290 0ebf8283 2019-07-24 stsp goto done;
7291 0ebf8283 2019-07-24 stsp }
7292 0ebf8283 2019-07-24 stsp
7293 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7294 0ebf8283 2019-07-24 stsp if (error)
7295 0ebf8283 2019-07-24 stsp goto done;
7296 0ebf8283 2019-07-24 stsp
7297 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7298 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7299 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7300 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7301 083957f4 2020-02-24 stsp "before the -m option can be used");
7302 083957f4 2020-02-24 stsp goto done;
7303 083957f4 2020-02-24 stsp }
7304 083957f4 2020-02-24 stsp
7305 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7306 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7307 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7308 3e3a69f1 2019-07-25 stsp worktree, repo);
7309 0ebf8283 2019-07-24 stsp if (error)
7310 0ebf8283 2019-07-24 stsp goto done;
7311 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7312 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7313 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7314 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7315 0ebf8283 2019-07-24 stsp if (error)
7316 0ebf8283 2019-07-24 stsp goto done;
7317 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7318 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7319 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7320 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7321 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7322 0ebf8283 2019-07-24 stsp goto done;
7323 0ebf8283 2019-07-24 stsp }
7324 0ebf8283 2019-07-24 stsp
7325 0ebf8283 2019-07-24 stsp if (continue_edit) {
7326 0ebf8283 2019-07-24 stsp char *path;
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7329 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7330 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp }
7332 0ebf8283 2019-07-24 stsp
7333 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7334 0ebf8283 2019-07-24 stsp if (error)
7335 0ebf8283 2019-07-24 stsp goto done;
7336 0ebf8283 2019-07-24 stsp
7337 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7338 0ebf8283 2019-07-24 stsp free(path);
7339 0ebf8283 2019-07-24 stsp if (error)
7340 0ebf8283 2019-07-24 stsp goto done;
7341 0ebf8283 2019-07-24 stsp
7342 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7343 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7344 3e3a69f1 2019-07-25 stsp worktree, repo);
7345 0ebf8283 2019-07-24 stsp if (error)
7346 0ebf8283 2019-07-24 stsp goto done;
7347 0ebf8283 2019-07-24 stsp
7348 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7349 0ebf8283 2019-07-24 stsp if (error)
7350 0ebf8283 2019-07-24 stsp goto done;
7351 0ebf8283 2019-07-24 stsp
7352 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7353 0ebf8283 2019-07-24 stsp head_commit_id);
7354 0ebf8283 2019-07-24 stsp if (error)
7355 0ebf8283 2019-07-24 stsp goto done;
7356 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7357 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7358 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7359 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7360 3aac7cf7 2019-07-25 stsp goto done;
7361 3aac7cf7 2019-07-25 stsp }
7362 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7363 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7364 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7365 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7366 0ebf8283 2019-07-24 stsp commit = NULL;
7367 0ebf8283 2019-07-24 stsp if (error)
7368 0ebf8283 2019-07-24 stsp goto done;
7369 0ebf8283 2019-07-24 stsp } else {
7370 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7371 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7372 0ebf8283 2019-07-24 stsp goto done;
7373 0ebf8283 2019-07-24 stsp }
7374 0ebf8283 2019-07-24 stsp
7375 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7376 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7377 0ebf8283 2019-07-24 stsp if (error != NULL)
7378 0ebf8283 2019-07-24 stsp goto done;
7379 0ebf8283 2019-07-24 stsp
7380 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7381 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7382 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7383 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7384 c7d20a3f 2019-07-30 stsp goto done;
7385 c7d20a3f 2019-07-30 stsp }
7386 c7d20a3f 2019-07-30 stsp
7387 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7388 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7389 bfce7f83 2019-07-27 stsp branch = NULL;
7390 0ebf8283 2019-07-24 stsp if (error)
7391 0ebf8283 2019-07-24 stsp goto done;
7392 0ebf8283 2019-07-24 stsp
7393 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7394 0ebf8283 2019-07-24 stsp head_commit_id);
7395 0ebf8283 2019-07-24 stsp if (error)
7396 0ebf8283 2019-07-24 stsp goto done;
7397 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7398 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7399 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7400 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7401 3aac7cf7 2019-07-25 stsp goto done;
7402 3aac7cf7 2019-07-25 stsp }
7403 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7404 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7405 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7406 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7407 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7408 0ebf8283 2019-07-24 stsp commit = NULL;
7409 0ebf8283 2019-07-24 stsp if (error)
7410 0ebf8283 2019-07-24 stsp goto done;
7411 0ebf8283 2019-07-24 stsp
7412 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7413 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7414 c5996fff 2020-01-29 stsp goto done;
7415 c5996fff 2020-01-29 stsp }
7416 c5996fff 2020-01-29 stsp
7417 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7418 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7419 bfce7f83 2019-07-27 stsp if (error)
7420 bfce7f83 2019-07-27 stsp goto done;
7421 bfce7f83 2019-07-27 stsp
7422 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7423 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7424 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7425 6ba2bea2 2019-07-27 stsp if (error) {
7426 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7427 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7428 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7429 0ebf8283 2019-07-24 stsp goto done;
7430 6ba2bea2 2019-07-27 stsp }
7431 0ebf8283 2019-07-24 stsp } else {
7432 514f2ffe 2020-01-29 stsp const char *branch_name;
7433 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7434 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7435 514f2ffe 2020-01-29 stsp branch_name += 11;
7436 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7437 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7438 6ba2bea2 2019-07-27 stsp if (error) {
7439 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7440 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7441 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7442 0ebf8283 2019-07-24 stsp goto done;
7443 6ba2bea2 2019-07-27 stsp }
7444 0ebf8283 2019-07-24 stsp
7445 0ebf8283 2019-07-24 stsp }
7446 0ebf8283 2019-07-24 stsp
7447 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7448 0ebf8283 2019-07-24 stsp repo);
7449 6ba2bea2 2019-07-27 stsp if (error) {
7450 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7451 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7452 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7453 0ebf8283 2019-07-24 stsp goto done;
7454 6ba2bea2 2019-07-27 stsp }
7455 0ebf8283 2019-07-24 stsp
7456 0ebf8283 2019-07-24 stsp }
7457 0ebf8283 2019-07-24 stsp
7458 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7459 4ec14e60 2019-08-28 hiltjo if (error)
7460 0ebf8283 2019-07-24 stsp goto done;
7461 0ebf8283 2019-07-24 stsp
7462 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7463 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7464 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7465 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7466 0ebf8283 2019-07-24 stsp continue;
7467 0ebf8283 2019-07-24 stsp
7468 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7469 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7470 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7471 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7472 0ebf8283 2019-07-24 stsp repo);
7473 ab20a43a 2020-01-29 stsp if (error)
7474 ab20a43a 2020-01-29 stsp goto done;
7475 0ebf8283 2019-07-24 stsp } else {
7476 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7477 ab20a43a 2020-01-29 stsp int have_changes = 0;
7478 ab20a43a 2020-01-29 stsp
7479 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7480 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7481 ab20a43a 2020-01-29 stsp if (error)
7482 ab20a43a 2020-01-29 stsp goto done;
7483 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7484 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7485 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7486 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7487 ab20a43a 2020-01-29 stsp if (error) {
7488 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7489 ab20a43a 2020-01-29 stsp goto done;
7490 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7491 ab20a43a 2020-01-29 stsp goto done;
7492 ab20a43a 2020-01-29 stsp }
7493 ab20a43a 2020-01-29 stsp if (have_changes) {
7494 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7495 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7496 ab20a43a 2020-01-29 stsp if (error)
7497 ab20a43a 2020-01-29 stsp goto done;
7498 ab20a43a 2020-01-29 stsp } else {
7499 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7500 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7501 ab20a43a 2020-01-29 stsp if (error)
7502 ab20a43a 2020-01-29 stsp goto done;
7503 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7504 ab20a43a 2020-01-29 stsp hle, NULL);
7505 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7506 ab20a43a 2020-01-29 stsp commit = NULL;
7507 ab20a43a 2020-01-29 stsp if (error)
7508 ab20a43a 2020-01-29 stsp goto done;
7509 ab20a43a 2020-01-29 stsp }
7510 0ebf8283 2019-07-24 stsp }
7511 0ebf8283 2019-07-24 stsp continue;
7512 0ebf8283 2019-07-24 stsp }
7513 0ebf8283 2019-07-24 stsp
7514 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7515 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7516 0ebf8283 2019-07-24 stsp if (error)
7517 0ebf8283 2019-07-24 stsp goto done;
7518 0ebf8283 2019-07-24 stsp continue;
7519 0ebf8283 2019-07-24 stsp }
7520 0ebf8283 2019-07-24 stsp
7521 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7522 0ebf8283 2019-07-24 stsp hle->commit_id);
7523 0ebf8283 2019-07-24 stsp if (error)
7524 0ebf8283 2019-07-24 stsp goto done;
7525 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7526 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7527 0ebf8283 2019-07-24 stsp
7528 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7529 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7530 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7531 0ebf8283 2019-07-24 stsp if (error)
7532 0ebf8283 2019-07-24 stsp goto done;
7533 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7534 0ebf8283 2019-07-24 stsp commit = NULL;
7535 0ebf8283 2019-07-24 stsp
7536 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7537 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7538 a0ea4fc0 2020-02-28 stsp repo);
7539 a0ea4fc0 2020-02-28 stsp if (error)
7540 a0ea4fc0 2020-02-28 stsp goto done;
7541 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7542 0ebf8283 2019-07-24 stsp break;
7543 0ebf8283 2019-07-24 stsp }
7544 0ebf8283 2019-07-24 stsp
7545 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7546 0ebf8283 2019-07-24 stsp char *id_str;
7547 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7548 0ebf8283 2019-07-24 stsp if (error)
7549 0ebf8283 2019-07-24 stsp goto done;
7550 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7551 0ebf8283 2019-07-24 stsp id_str);
7552 0ebf8283 2019-07-24 stsp free(id_str);
7553 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7554 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7555 3e3a69f1 2019-07-25 stsp fileindex);
7556 0ebf8283 2019-07-24 stsp goto done;
7557 0ebf8283 2019-07-24 stsp }
7558 0ebf8283 2019-07-24 stsp
7559 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7560 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7561 0ebf8283 2019-07-24 stsp if (error)
7562 0ebf8283 2019-07-24 stsp goto done;
7563 0ebf8283 2019-07-24 stsp continue;
7564 0ebf8283 2019-07-24 stsp }
7565 0ebf8283 2019-07-24 stsp
7566 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7567 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7568 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7569 0ebf8283 2019-07-24 stsp if (error)
7570 0ebf8283 2019-07-24 stsp goto done;
7571 0ebf8283 2019-07-24 stsp }
7572 0ebf8283 2019-07-24 stsp
7573 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7574 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7575 0ebf8283 2019-07-24 stsp if (error)
7576 0ebf8283 2019-07-24 stsp goto done;
7577 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7578 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7579 0ebf8283 2019-07-24 stsp } else
7580 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7581 3e3a69f1 2019-07-25 stsp branch, repo);
7582 0ebf8283 2019-07-24 stsp done:
7583 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7584 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7585 0ebf8283 2019-07-24 stsp free(head_commit_id);
7586 0ebf8283 2019-07-24 stsp free(base_commit_id);
7587 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7588 0ebf8283 2019-07-24 stsp if (commit)
7589 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7590 0ebf8283 2019-07-24 stsp if (branch)
7591 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7592 0ebf8283 2019-07-24 stsp if (tmp_branch)
7593 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7594 0ebf8283 2019-07-24 stsp if (worktree)
7595 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7596 2822a352 2019-10-15 stsp if (repo)
7597 2822a352 2019-10-15 stsp got_repo_close(repo);
7598 2822a352 2019-10-15 stsp return error;
7599 2822a352 2019-10-15 stsp }
7600 2822a352 2019-10-15 stsp
7601 2822a352 2019-10-15 stsp __dead static void
7602 2822a352 2019-10-15 stsp usage_integrate(void)
7603 2822a352 2019-10-15 stsp {
7604 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7605 2822a352 2019-10-15 stsp exit(1);
7606 2822a352 2019-10-15 stsp }
7607 2822a352 2019-10-15 stsp
7608 2822a352 2019-10-15 stsp static const struct got_error *
7609 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7610 2822a352 2019-10-15 stsp {
7611 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7612 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7613 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7614 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7615 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7616 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7617 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7618 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7619 2822a352 2019-10-15 stsp int ch, did_something = 0;
7620 2822a352 2019-10-15 stsp
7621 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7622 2822a352 2019-10-15 stsp switch (ch) {
7623 2822a352 2019-10-15 stsp default:
7624 2822a352 2019-10-15 stsp usage_integrate();
7625 2822a352 2019-10-15 stsp /* NOTREACHED */
7626 2822a352 2019-10-15 stsp }
7627 2822a352 2019-10-15 stsp }
7628 2822a352 2019-10-15 stsp
7629 2822a352 2019-10-15 stsp argc -= optind;
7630 2822a352 2019-10-15 stsp argv += optind;
7631 2822a352 2019-10-15 stsp
7632 2822a352 2019-10-15 stsp if (argc != 1)
7633 2822a352 2019-10-15 stsp usage_integrate();
7634 2822a352 2019-10-15 stsp branch_arg = argv[0];
7635 2822a352 2019-10-15 stsp
7636 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7637 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7638 2822a352 2019-10-15 stsp err(1, "pledge");
7639 2822a352 2019-10-15 stsp
7640 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7641 2822a352 2019-10-15 stsp if (cwd == NULL) {
7642 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7643 2822a352 2019-10-15 stsp goto done;
7644 2822a352 2019-10-15 stsp }
7645 2822a352 2019-10-15 stsp
7646 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7647 2822a352 2019-10-15 stsp if (error)
7648 2822a352 2019-10-15 stsp goto done;
7649 2822a352 2019-10-15 stsp
7650 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7651 2822a352 2019-10-15 stsp if (error)
7652 2822a352 2019-10-15 stsp goto done;
7653 2822a352 2019-10-15 stsp
7654 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7655 2822a352 2019-10-15 stsp NULL);
7656 2822a352 2019-10-15 stsp if (error != NULL)
7657 2822a352 2019-10-15 stsp goto done;
7658 2822a352 2019-10-15 stsp
7659 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7660 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7661 2822a352 2019-10-15 stsp if (error)
7662 2822a352 2019-10-15 stsp goto done;
7663 2822a352 2019-10-15 stsp
7664 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7665 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7666 2822a352 2019-10-15 stsp goto done;
7667 2822a352 2019-10-15 stsp }
7668 2822a352 2019-10-15 stsp
7669 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7670 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7671 2822a352 2019-10-15 stsp if (error)
7672 2822a352 2019-10-15 stsp goto done;
7673 2822a352 2019-10-15 stsp
7674 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7675 2822a352 2019-10-15 stsp if (refname == NULL) {
7676 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7677 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7678 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7679 2822a352 2019-10-15 stsp goto done;
7680 2822a352 2019-10-15 stsp }
7681 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7682 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7683 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7684 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7685 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7686 2822a352 2019-10-15 stsp goto done;
7687 2822a352 2019-10-15 stsp }
7688 2822a352 2019-10-15 stsp
7689 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7690 2822a352 2019-10-15 stsp if (error)
7691 2822a352 2019-10-15 stsp goto done;
7692 2822a352 2019-10-15 stsp
7693 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7694 2822a352 2019-10-15 stsp if (error)
7695 2822a352 2019-10-15 stsp goto done;
7696 2822a352 2019-10-15 stsp
7697 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7698 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7699 2822a352 2019-10-15 stsp "specified branch has already been integrated");
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 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7706 2822a352 2019-10-15 stsp if (error) {
7707 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7708 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7709 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7710 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7711 2822a352 2019-10-15 stsp goto done;
7712 2822a352 2019-10-15 stsp }
7713 2822a352 2019-10-15 stsp
7714 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7715 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7716 2822a352 2019-10-15 stsp check_cancelled, NULL);
7717 2822a352 2019-10-15 stsp if (error)
7718 2822a352 2019-10-15 stsp goto done;
7719 2822a352 2019-10-15 stsp
7720 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7721 2822a352 2019-10-15 stsp done:
7722 715dc77e 2019-08-03 stsp if (repo)
7723 715dc77e 2019-08-03 stsp got_repo_close(repo);
7724 2822a352 2019-10-15 stsp if (worktree)
7725 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7726 2822a352 2019-10-15 stsp free(cwd);
7727 2822a352 2019-10-15 stsp free(base_commit_id);
7728 2822a352 2019-10-15 stsp free(commit_id);
7729 2822a352 2019-10-15 stsp free(refname);
7730 2822a352 2019-10-15 stsp free(base_refname);
7731 715dc77e 2019-08-03 stsp return error;
7732 715dc77e 2019-08-03 stsp }
7733 715dc77e 2019-08-03 stsp
7734 715dc77e 2019-08-03 stsp __dead static void
7735 715dc77e 2019-08-03 stsp usage_stage(void)
7736 715dc77e 2019-08-03 stsp {
7737 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7738 f3055044 2019-08-07 stsp "[file-path ...]\n",
7739 715dc77e 2019-08-03 stsp getprogname());
7740 715dc77e 2019-08-03 stsp exit(1);
7741 715dc77e 2019-08-03 stsp }
7742 715dc77e 2019-08-03 stsp
7743 715dc77e 2019-08-03 stsp static const struct got_error *
7744 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7745 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7746 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7747 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7748 408b4ebc 2019-08-03 stsp {
7749 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7750 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7751 408b4ebc 2019-08-03 stsp
7752 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7753 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7754 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7755 408b4ebc 2019-08-03 stsp return NULL;
7756 408b4ebc 2019-08-03 stsp
7757 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7758 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7759 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7760 408b4ebc 2019-08-03 stsp else
7761 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7762 408b4ebc 2019-08-03 stsp if (err)
7763 408b4ebc 2019-08-03 stsp return err;
7764 408b4ebc 2019-08-03 stsp
7765 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7766 408b4ebc 2019-08-03 stsp free(id_str);
7767 dc424a06 2019-08-07 stsp return NULL;
7768 dc424a06 2019-08-07 stsp }
7769 2e1f37b0 2019-08-08 stsp
7770 dc424a06 2019-08-07 stsp static const struct got_error *
7771 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7772 715dc77e 2019-08-03 stsp {
7773 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7774 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7775 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7776 715dc77e 2019-08-03 stsp char *cwd = NULL;
7777 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7778 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7779 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7780 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7781 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7782 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7783 715dc77e 2019-08-03 stsp
7784 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7785 715dc77e 2019-08-03 stsp
7786 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7787 715dc77e 2019-08-03 stsp switch (ch) {
7788 408b4ebc 2019-08-03 stsp case 'l':
7789 408b4ebc 2019-08-03 stsp list_stage = 1;
7790 408b4ebc 2019-08-03 stsp break;
7791 dc424a06 2019-08-07 stsp case 'p':
7792 dc424a06 2019-08-07 stsp pflag = 1;
7793 dc424a06 2019-08-07 stsp break;
7794 dc424a06 2019-08-07 stsp case 'F':
7795 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7796 dc424a06 2019-08-07 stsp break;
7797 715dc77e 2019-08-03 stsp default:
7798 715dc77e 2019-08-03 stsp usage_stage();
7799 715dc77e 2019-08-03 stsp /* NOTREACHED */
7800 715dc77e 2019-08-03 stsp }
7801 715dc77e 2019-08-03 stsp }
7802 715dc77e 2019-08-03 stsp
7803 715dc77e 2019-08-03 stsp argc -= optind;
7804 715dc77e 2019-08-03 stsp argv += optind;
7805 715dc77e 2019-08-03 stsp
7806 715dc77e 2019-08-03 stsp #ifndef PROFILE
7807 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7808 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7809 715dc77e 2019-08-03 stsp err(1, "pledge");
7810 715dc77e 2019-08-03 stsp #endif
7811 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7812 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7813 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7814 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7815 715dc77e 2019-08-03 stsp
7816 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7817 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7818 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7819 715dc77e 2019-08-03 stsp goto done;
7820 715dc77e 2019-08-03 stsp }
7821 715dc77e 2019-08-03 stsp
7822 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7823 715dc77e 2019-08-03 stsp if (error)
7824 715dc77e 2019-08-03 stsp goto done;
7825 715dc77e 2019-08-03 stsp
7826 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7827 c9956ddf 2019-09-08 stsp NULL);
7828 715dc77e 2019-08-03 stsp if (error != NULL)
7829 715dc77e 2019-08-03 stsp goto done;
7830 715dc77e 2019-08-03 stsp
7831 dc424a06 2019-08-07 stsp if (patch_script_path) {
7832 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7833 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7834 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7835 dc424a06 2019-08-07 stsp patch_script_path);
7836 dc424a06 2019-08-07 stsp goto done;
7837 dc424a06 2019-08-07 stsp }
7838 dc424a06 2019-08-07 stsp }
7839 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7840 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7841 715dc77e 2019-08-03 stsp if (error)
7842 715dc77e 2019-08-03 stsp goto done;
7843 715dc77e 2019-08-03 stsp
7844 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7845 6d022e97 2019-08-04 stsp if (error)
7846 6d022e97 2019-08-04 stsp goto done;
7847 715dc77e 2019-08-03 stsp
7848 6d022e97 2019-08-04 stsp if (list_stage)
7849 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7850 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7851 2e1f37b0 2019-08-08 stsp else {
7852 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7853 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7854 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7855 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7856 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7857 2e1f37b0 2019-08-08 stsp }
7858 ad493afc 2019-08-03 stsp done:
7859 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7860 2e1f37b0 2019-08-08 stsp error == NULL)
7861 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7862 ad493afc 2019-08-03 stsp if (repo)
7863 ad493afc 2019-08-03 stsp got_repo_close(repo);
7864 ad493afc 2019-08-03 stsp if (worktree)
7865 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7866 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7867 ad493afc 2019-08-03 stsp free((char *)pe->path);
7868 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7869 ad493afc 2019-08-03 stsp free(cwd);
7870 ad493afc 2019-08-03 stsp return error;
7871 ad493afc 2019-08-03 stsp }
7872 ad493afc 2019-08-03 stsp
7873 ad493afc 2019-08-03 stsp __dead static void
7874 ad493afc 2019-08-03 stsp usage_unstage(void)
7875 ad493afc 2019-08-03 stsp {
7876 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7877 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7878 ad493afc 2019-08-03 stsp getprogname());
7879 ad493afc 2019-08-03 stsp exit(1);
7880 ad493afc 2019-08-03 stsp }
7881 ad493afc 2019-08-03 stsp
7882 ad493afc 2019-08-03 stsp
7883 ad493afc 2019-08-03 stsp static const struct got_error *
7884 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7885 ad493afc 2019-08-03 stsp {
7886 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7887 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7888 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7889 ad493afc 2019-08-03 stsp char *cwd = NULL;
7890 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7891 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7892 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7893 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7894 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7895 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7896 ad493afc 2019-08-03 stsp
7897 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7898 ad493afc 2019-08-03 stsp
7899 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7900 ad493afc 2019-08-03 stsp switch (ch) {
7901 2e1f37b0 2019-08-08 stsp case 'p':
7902 2e1f37b0 2019-08-08 stsp pflag = 1;
7903 2e1f37b0 2019-08-08 stsp break;
7904 2e1f37b0 2019-08-08 stsp case 'F':
7905 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7906 2e1f37b0 2019-08-08 stsp break;
7907 ad493afc 2019-08-03 stsp default:
7908 ad493afc 2019-08-03 stsp usage_unstage();
7909 ad493afc 2019-08-03 stsp /* NOTREACHED */
7910 ad493afc 2019-08-03 stsp }
7911 ad493afc 2019-08-03 stsp }
7912 ad493afc 2019-08-03 stsp
7913 ad493afc 2019-08-03 stsp argc -= optind;
7914 ad493afc 2019-08-03 stsp argv += optind;
7915 ad493afc 2019-08-03 stsp
7916 ad493afc 2019-08-03 stsp #ifndef PROFILE
7917 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7918 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7919 ad493afc 2019-08-03 stsp err(1, "pledge");
7920 ad493afc 2019-08-03 stsp #endif
7921 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7922 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7923 2e1f37b0 2019-08-08 stsp
7924 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7925 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7926 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7927 ad493afc 2019-08-03 stsp goto done;
7928 ad493afc 2019-08-03 stsp }
7929 ad493afc 2019-08-03 stsp
7930 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7931 ad493afc 2019-08-03 stsp if (error)
7932 ad493afc 2019-08-03 stsp goto done;
7933 ad493afc 2019-08-03 stsp
7934 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7935 c9956ddf 2019-09-08 stsp NULL);
7936 ad493afc 2019-08-03 stsp if (error != NULL)
7937 ad493afc 2019-08-03 stsp goto done;
7938 ad493afc 2019-08-03 stsp
7939 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7940 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7941 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7942 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7943 2e1f37b0 2019-08-08 stsp patch_script_path);
7944 2e1f37b0 2019-08-08 stsp goto done;
7945 2e1f37b0 2019-08-08 stsp }
7946 2e1f37b0 2019-08-08 stsp }
7947 2e1f37b0 2019-08-08 stsp
7948 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7949 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7950 ad493afc 2019-08-03 stsp if (error)
7951 ad493afc 2019-08-03 stsp goto done;
7952 ad493afc 2019-08-03 stsp
7953 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7954 ad493afc 2019-08-03 stsp if (error)
7955 ad493afc 2019-08-03 stsp goto done;
7956 ad493afc 2019-08-03 stsp
7957 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7958 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7959 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7960 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7961 715dc77e 2019-08-03 stsp done:
7962 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7963 2e1f37b0 2019-08-08 stsp error == NULL)
7964 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7965 0ebf8283 2019-07-24 stsp if (repo)
7966 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7967 715dc77e 2019-08-03 stsp if (worktree)
7968 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7969 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7970 715dc77e 2019-08-03 stsp free((char *)pe->path);
7971 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7972 715dc77e 2019-08-03 stsp free(cwd);
7973 01073a5d 2019-08-22 stsp return error;
7974 01073a5d 2019-08-22 stsp }
7975 01073a5d 2019-08-22 stsp
7976 01073a5d 2019-08-22 stsp __dead static void
7977 01073a5d 2019-08-22 stsp usage_cat(void)
7978 01073a5d 2019-08-22 stsp {
7979 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7980 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7981 01073a5d 2019-08-22 stsp exit(1);
7982 01073a5d 2019-08-22 stsp }
7983 01073a5d 2019-08-22 stsp
7984 01073a5d 2019-08-22 stsp static const struct got_error *
7985 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7986 01073a5d 2019-08-22 stsp {
7987 01073a5d 2019-08-22 stsp const struct got_error *err;
7988 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7989 01073a5d 2019-08-22 stsp
7990 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7991 01073a5d 2019-08-22 stsp if (err)
7992 01073a5d 2019-08-22 stsp return err;
7993 01073a5d 2019-08-22 stsp
7994 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7995 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7996 01073a5d 2019-08-22 stsp return err;
7997 01073a5d 2019-08-22 stsp }
7998 01073a5d 2019-08-22 stsp
7999 01073a5d 2019-08-22 stsp static const struct got_error *
8000 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8001 01073a5d 2019-08-22 stsp {
8002 01073a5d 2019-08-22 stsp const struct got_error *err;
8003 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8004 56e0773d 2019-11-28 stsp int nentries, i;
8005 01073a5d 2019-08-22 stsp
8006 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8007 01073a5d 2019-08-22 stsp if (err)
8008 01073a5d 2019-08-22 stsp return err;
8009 01073a5d 2019-08-22 stsp
8010 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8011 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8012 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8013 01073a5d 2019-08-22 stsp char *id_str;
8014 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8015 01073a5d 2019-08-22 stsp break;
8016 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8017 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8018 01073a5d 2019-08-22 stsp if (err)
8019 01073a5d 2019-08-22 stsp break;
8020 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8021 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8022 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8023 01073a5d 2019-08-22 stsp free(id_str);
8024 01073a5d 2019-08-22 stsp }
8025 01073a5d 2019-08-22 stsp
8026 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8027 01073a5d 2019-08-22 stsp return err;
8028 01073a5d 2019-08-22 stsp }
8029 01073a5d 2019-08-22 stsp
8030 01073a5d 2019-08-22 stsp static const struct got_error *
8031 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8032 01073a5d 2019-08-22 stsp {
8033 01073a5d 2019-08-22 stsp const struct got_error *err;
8034 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8035 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8036 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8037 01073a5d 2019-08-22 stsp char *id_str = NULL;
8038 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8039 01073a5d 2019-08-22 stsp
8040 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8041 01073a5d 2019-08-22 stsp if (err)
8042 01073a5d 2019-08-22 stsp return err;
8043 01073a5d 2019-08-22 stsp
8044 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8045 01073a5d 2019-08-22 stsp if (err)
8046 01073a5d 2019-08-22 stsp goto done;
8047 01073a5d 2019-08-22 stsp
8048 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8049 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8050 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8051 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8052 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8053 01073a5d 2019-08-22 stsp char *pid_str;
8054 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8055 01073a5d 2019-08-22 stsp if (err)
8056 01073a5d 2019-08-22 stsp goto done;
8057 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8058 01073a5d 2019-08-22 stsp free(pid_str);
8059 01073a5d 2019-08-22 stsp }
8060 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8061 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8062 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8063 01073a5d 2019-08-22 stsp
8064 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8065 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8066 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8067 01073a5d 2019-08-22 stsp
8068 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8069 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8070 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8071 01073a5d 2019-08-22 stsp done:
8072 01073a5d 2019-08-22 stsp free(id_str);
8073 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8074 01073a5d 2019-08-22 stsp return err;
8075 01073a5d 2019-08-22 stsp }
8076 01073a5d 2019-08-22 stsp
8077 01073a5d 2019-08-22 stsp static const struct got_error *
8078 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8079 01073a5d 2019-08-22 stsp {
8080 01073a5d 2019-08-22 stsp const struct got_error *err;
8081 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8082 01073a5d 2019-08-22 stsp char *id_str = NULL;
8083 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8084 01073a5d 2019-08-22 stsp
8085 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8086 01073a5d 2019-08-22 stsp if (err)
8087 01073a5d 2019-08-22 stsp return err;
8088 01073a5d 2019-08-22 stsp
8089 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8090 01073a5d 2019-08-22 stsp if (err)
8091 01073a5d 2019-08-22 stsp goto done;
8092 01073a5d 2019-08-22 stsp
8093 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8094 e15d5241 2019-08-22 stsp
8095 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8096 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8097 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8098 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8099 01073a5d 2019-08-22 stsp break;
8100 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8101 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8102 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8103 01073a5d 2019-08-22 stsp break;
8104 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8105 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8106 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8107 01073a5d 2019-08-22 stsp break;
8108 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8109 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8110 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8111 01073a5d 2019-08-22 stsp break;
8112 01073a5d 2019-08-22 stsp default:
8113 01073a5d 2019-08-22 stsp break;
8114 01073a5d 2019-08-22 stsp }
8115 01073a5d 2019-08-22 stsp
8116 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8117 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8118 e15d5241 2019-08-22 stsp
8119 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8120 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8121 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8122 01073a5d 2019-08-22 stsp
8123 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8124 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8125 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8126 01073a5d 2019-08-22 stsp done:
8127 01073a5d 2019-08-22 stsp free(id_str);
8128 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8129 01073a5d 2019-08-22 stsp return err;
8130 01073a5d 2019-08-22 stsp }
8131 01073a5d 2019-08-22 stsp
8132 01073a5d 2019-08-22 stsp static const struct got_error *
8133 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8134 01073a5d 2019-08-22 stsp {
8135 01073a5d 2019-08-22 stsp const struct got_error *error;
8136 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8137 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8138 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8139 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8140 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8141 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8142 01073a5d 2019-08-22 stsp
8143 01073a5d 2019-08-22 stsp #ifndef PROFILE
8144 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8145 01073a5d 2019-08-22 stsp NULL) == -1)
8146 01073a5d 2019-08-22 stsp err(1, "pledge");
8147 01073a5d 2019-08-22 stsp #endif
8148 01073a5d 2019-08-22 stsp
8149 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8150 01073a5d 2019-08-22 stsp switch (ch) {
8151 896e9b6f 2019-08-26 stsp case 'c':
8152 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8153 896e9b6f 2019-08-26 stsp break;
8154 01073a5d 2019-08-22 stsp case 'r':
8155 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8156 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8157 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8158 9ba1d308 2019-10-21 stsp optarg);
8159 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8160 01073a5d 2019-08-22 stsp break;
8161 896e9b6f 2019-08-26 stsp case 'P':
8162 896e9b6f 2019-08-26 stsp force_path = 1;
8163 896e9b6f 2019-08-26 stsp break;
8164 01073a5d 2019-08-22 stsp default:
8165 01073a5d 2019-08-22 stsp usage_cat();
8166 01073a5d 2019-08-22 stsp /* NOTREACHED */
8167 01073a5d 2019-08-22 stsp }
8168 01073a5d 2019-08-22 stsp }
8169 01073a5d 2019-08-22 stsp
8170 01073a5d 2019-08-22 stsp argc -= optind;
8171 01073a5d 2019-08-22 stsp argv += optind;
8172 01073a5d 2019-08-22 stsp
8173 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8174 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8175 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8176 01073a5d 2019-08-22 stsp goto done;
8177 01073a5d 2019-08-22 stsp }
8178 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8179 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8180 01073a5d 2019-08-22 stsp goto done;
8181 01073a5d 2019-08-22 stsp if (worktree) {
8182 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8183 01073a5d 2019-08-22 stsp repo_path = strdup(
8184 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8185 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8186 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8187 01073a5d 2019-08-22 stsp goto done;
8188 01073a5d 2019-08-22 stsp }
8189 01073a5d 2019-08-22 stsp }
8190 01073a5d 2019-08-22 stsp }
8191 01073a5d 2019-08-22 stsp
8192 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8193 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8194 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8195 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8196 01073a5d 2019-08-22 stsp }
8197 01073a5d 2019-08-22 stsp
8198 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8199 01073a5d 2019-08-22 stsp free(repo_path);
8200 01073a5d 2019-08-22 stsp if (error != NULL)
8201 01073a5d 2019-08-22 stsp goto done;
8202 01073a5d 2019-08-22 stsp
8203 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8204 896e9b6f 2019-08-26 stsp if (error)
8205 896e9b6f 2019-08-26 stsp goto done;
8206 896e9b6f 2019-08-26 stsp
8207 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8208 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8209 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8210 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8211 01073a5d 2019-08-22 stsp if (error)
8212 01073a5d 2019-08-22 stsp goto done;
8213 01073a5d 2019-08-22 stsp
8214 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8215 896e9b6f 2019-08-26 stsp if (force_path) {
8216 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8217 896e9b6f 2019-08-26 stsp argv[i]);
8218 896e9b6f 2019-08-26 stsp if (error)
8219 896e9b6f 2019-08-26 stsp break;
8220 896e9b6f 2019-08-26 stsp } else {
8221 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8222 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8223 896e9b6f 2019-08-26 stsp if (error) {
8224 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8225 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8226 896e9b6f 2019-08-26 stsp break;
8227 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8228 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8229 896e9b6f 2019-08-26 stsp if (error)
8230 896e9b6f 2019-08-26 stsp break;
8231 896e9b6f 2019-08-26 stsp }
8232 896e9b6f 2019-08-26 stsp }
8233 01073a5d 2019-08-22 stsp
8234 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8235 01073a5d 2019-08-22 stsp if (error)
8236 01073a5d 2019-08-22 stsp break;
8237 01073a5d 2019-08-22 stsp
8238 01073a5d 2019-08-22 stsp switch (obj_type) {
8239 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8240 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8241 01073a5d 2019-08-22 stsp break;
8242 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8243 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8244 01073a5d 2019-08-22 stsp break;
8245 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8246 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8247 01073a5d 2019-08-22 stsp break;
8248 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8249 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8250 01073a5d 2019-08-22 stsp break;
8251 01073a5d 2019-08-22 stsp default:
8252 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8253 01073a5d 2019-08-22 stsp break;
8254 01073a5d 2019-08-22 stsp }
8255 01073a5d 2019-08-22 stsp if (error)
8256 01073a5d 2019-08-22 stsp break;
8257 01073a5d 2019-08-22 stsp free(label);
8258 01073a5d 2019-08-22 stsp label = NULL;
8259 01073a5d 2019-08-22 stsp free(id);
8260 01073a5d 2019-08-22 stsp id = NULL;
8261 01073a5d 2019-08-22 stsp }
8262 01073a5d 2019-08-22 stsp done:
8263 01073a5d 2019-08-22 stsp free(label);
8264 01073a5d 2019-08-22 stsp free(id);
8265 896e9b6f 2019-08-26 stsp free(commit_id);
8266 01073a5d 2019-08-22 stsp if (worktree)
8267 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8268 01073a5d 2019-08-22 stsp if (repo) {
8269 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8270 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8271 01073a5d 2019-08-22 stsp if (error == NULL)
8272 01073a5d 2019-08-22 stsp error = repo_error;
8273 01073a5d 2019-08-22 stsp }
8274 0ebf8283 2019-07-24 stsp return error;
8275 0ebf8283 2019-07-24 stsp }