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 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
90 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
93 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
94 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
95 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
96 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
97 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
98 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
99 d00136be 2019-03-26 stsp __dead static void usage_add(void);
100 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
101 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
102 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
103 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
104 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
105 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
106 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
107 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
108 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
109 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
110 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
111 5c860e29 2018-03-12 stsp
112 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
113 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
114 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
115 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
116 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
118 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
119 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
120 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
122 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
123 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
124 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
125 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
126 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
127 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
128 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
129 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
130 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
131 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
132 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
133 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
134 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
135 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
136 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
137 5c860e29 2018-03-12 stsp
138 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
139 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
140 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
141 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
142 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
143 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
144 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
145 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
146 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
147 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
148 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
149 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
150 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
151 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
152 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
153 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
154 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
155 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
156 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
158 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
159 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
160 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
161 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
162 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
163 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
164 5c860e29 2018-03-12 stsp };
165 ce5b7c56 2019-07-09 stsp
166 ce5b7c56 2019-07-09 stsp static void
167 ce5b7c56 2019-07-09 stsp list_commands(void)
168 ce5b7c56 2019-07-09 stsp {
169 ce5b7c56 2019-07-09 stsp int i;
170 ce5b7c56 2019-07-09 stsp
171 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
172 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
173 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
175 ce5b7c56 2019-07-09 stsp }
176 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
177 ce5b7c56 2019-07-09 stsp }
178 5c860e29 2018-03-12 stsp
179 5c860e29 2018-03-12 stsp int
180 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
181 5c860e29 2018-03-12 stsp {
182 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
183 5c860e29 2018-03-12 stsp unsigned int i;
184 5c860e29 2018-03-12 stsp int ch;
185 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
186 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
187 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
188 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
189 83cd27f8 2020-01-13 stsp };
190 5c860e29 2018-03-12 stsp
191 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
192 5c860e29 2018-03-12 stsp
193 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 5c860e29 2018-03-12 stsp switch (ch) {
195 1b6b95a8 2018-03-12 stsp case 'h':
196 1b6b95a8 2018-03-12 stsp hflag = 1;
197 53ccebc2 2019-07-30 stsp break;
198 53ccebc2 2019-07-30 stsp case 'V':
199 53ccebc2 2019-07-30 stsp Vflag = 1;
200 1b6b95a8 2018-03-12 stsp break;
201 5c860e29 2018-03-12 stsp default:
202 ce5b7c56 2019-07-09 stsp usage(hflag);
203 5c860e29 2018-03-12 stsp /* NOTREACHED */
204 5c860e29 2018-03-12 stsp }
205 5c860e29 2018-03-12 stsp }
206 5c860e29 2018-03-12 stsp
207 5c860e29 2018-03-12 stsp argc -= optind;
208 5c860e29 2018-03-12 stsp argv += optind;
209 1e70621d 2018-03-27 stsp optind = 0;
210 53ccebc2 2019-07-30 stsp
211 53ccebc2 2019-07-30 stsp if (Vflag) {
212 53ccebc2 2019-07-30 stsp got_version_print_str();
213 53ccebc2 2019-07-30 stsp return 1;
214 53ccebc2 2019-07-30 stsp }
215 5c860e29 2018-03-12 stsp
216 5c860e29 2018-03-12 stsp if (argc <= 0)
217 ce5b7c56 2019-07-09 stsp usage(hflag);
218 5c860e29 2018-03-12 stsp
219 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
220 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
221 99437157 2018-11-11 stsp
222 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
223 d7d4f210 2018-03-12 stsp const struct got_error *error;
224 d7d4f210 2018-03-12 stsp
225 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
226 5c860e29 2018-03-12 stsp
227 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
229 5c860e29 2018-03-12 stsp continue;
230 5c860e29 2018-03-12 stsp
231 1b6b95a8 2018-03-12 stsp if (hflag)
232 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
233 1b6b95a8 2018-03-12 stsp
234 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
235 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
236 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
237 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
238 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 70015d7a 2019-11-08 stsp !(sigint_received &&
240 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 d7d4f210 2018-03-12 stsp return 1;
243 d7d4f210 2018-03-12 stsp }
244 d7d4f210 2018-03-12 stsp
245 d7d4f210 2018-03-12 stsp return 0;
246 5c860e29 2018-03-12 stsp }
247 5c860e29 2018-03-12 stsp
248 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 ce5b7c56 2019-07-09 stsp list_commands();
250 5c860e29 2018-03-12 stsp return 1;
251 5c860e29 2018-03-12 stsp }
252 5c860e29 2018-03-12 stsp
253 4ed7e80c 2018-05-20 stsp __dead static void
254 ce5b7c56 2019-07-09 stsp usage(int hflag)
255 5c860e29 2018-03-12 stsp {
256 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 53ccebc2 2019-07-30 stsp getprogname());
258 ce5b7c56 2019-07-09 stsp if (hflag)
259 ce5b7c56 2019-07-09 stsp list_commands();
260 5c860e29 2018-03-12 stsp exit(1);
261 5c860e29 2018-03-12 stsp }
262 5c860e29 2018-03-12 stsp
263 0266afb7 2019-01-04 stsp static const struct got_error *
264 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
265 e2ba3d07 2019-05-13 stsp {
266 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
267 e2ba3d07 2019-05-13 stsp const char *editor;
268 8920fa04 2019-08-18 stsp
269 8920fa04 2019-08-18 stsp *abspath = NULL;
270 e2ba3d07 2019-05-13 stsp
271 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
272 e2ba3d07 2019-05-13 stsp if (editor == NULL)
273 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
274 e2ba3d07 2019-05-13 stsp
275 0ee7065d 2019-05-13 stsp if (editor) {
276 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
277 0ee7065d 2019-05-13 stsp if (err)
278 0ee7065d 2019-05-13 stsp return err;
279 0ee7065d 2019-05-13 stsp }
280 e2ba3d07 2019-05-13 stsp
281 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
282 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
283 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
284 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
285 0ee7065d 2019-05-13 stsp }
286 0ee7065d 2019-05-13 stsp
287 e2ba3d07 2019-05-13 stsp return NULL;
288 e2ba3d07 2019-05-13 stsp }
289 e2ba3d07 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp static const struct got_error *
291 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
292 c530dc23 2019-07-23 stsp const char *worktree_path)
293 0266afb7 2019-01-04 stsp {
294 163ce85a 2019-05-13 stsp const struct got_error *err;
295 0266afb7 2019-01-04 stsp
296 37c06ea4 2019-07-15 stsp #ifdef PROFILE
297 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
298 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
299 37c06ea4 2019-07-15 stsp #endif
300 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
302 0266afb7 2019-01-04 stsp
303 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
305 0266afb7 2019-01-04 stsp
306 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 0266afb7 2019-01-04 stsp
309 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
310 163ce85a 2019-05-13 stsp if (err != NULL)
311 163ce85a 2019-05-13 stsp return err;
312 0266afb7 2019-01-04 stsp
313 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
314 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
315 0266afb7 2019-01-04 stsp
316 0266afb7 2019-01-04 stsp return NULL;
317 2c7829a4 2019-06-17 stsp }
318 2c7829a4 2019-06-17 stsp
319 2c7829a4 2019-06-17 stsp __dead static void
320 2c7829a4 2019-06-17 stsp usage_init(void)
321 2c7829a4 2019-06-17 stsp {
322 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
323 2c7829a4 2019-06-17 stsp exit(1);
324 2c7829a4 2019-06-17 stsp }
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp static const struct got_error *
327 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
328 2c7829a4 2019-06-17 stsp {
329 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
330 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
331 2c7829a4 2019-06-17 stsp int ch;
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
334 2c7829a4 2019-06-17 stsp switch (ch) {
335 2c7829a4 2019-06-17 stsp default:
336 2c7829a4 2019-06-17 stsp usage_init();
337 2c7829a4 2019-06-17 stsp /* NOTREACHED */
338 2c7829a4 2019-06-17 stsp }
339 2c7829a4 2019-06-17 stsp }
340 2c7829a4 2019-06-17 stsp
341 2c7829a4 2019-06-17 stsp argc -= optind;
342 2c7829a4 2019-06-17 stsp argv += optind;
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp #ifndef PROFILE
345 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 2c7829a4 2019-06-17 stsp err(1, "pledge");
347 2c7829a4 2019-06-17 stsp #endif
348 2c7829a4 2019-06-17 stsp if (argc != 1)
349 bc20e173 2019-06-17 stsp usage_init();
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
352 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
353 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
354 2c7829a4 2019-06-17 stsp
355 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
356 2c7829a4 2019-06-17 stsp
357 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
358 2c7829a4 2019-06-17 stsp if (error &&
359 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
360 2c7829a4 2019-06-17 stsp goto done;
361 2c7829a4 2019-06-17 stsp
362 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
363 2c7829a4 2019-06-17 stsp if (error)
364 2c7829a4 2019-06-17 stsp goto done;
365 2c7829a4 2019-06-17 stsp
366 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
367 3ce1b845 2019-07-15 stsp done:
368 3ce1b845 2019-07-15 stsp free(repo_path);
369 3ce1b845 2019-07-15 stsp return error;
370 3ce1b845 2019-07-15 stsp }
371 3ce1b845 2019-07-15 stsp
372 3ce1b845 2019-07-15 stsp __dead static void
373 3ce1b845 2019-07-15 stsp usage_import(void)
374 3ce1b845 2019-07-15 stsp {
375 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
377 3ce1b845 2019-07-15 stsp exit(1);
378 3ce1b845 2019-07-15 stsp }
379 3ce1b845 2019-07-15 stsp
380 3ce1b845 2019-07-15 stsp int
381 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
382 3ce1b845 2019-07-15 stsp {
383 3ce1b845 2019-07-15 stsp pid_t pid;
384 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
385 3ce1b845 2019-07-15 stsp int st = -1;
386 3ce1b845 2019-07-15 stsp
387 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
389 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
390 3ce1b845 2019-07-15 stsp
391 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
392 3ce1b845 2019-07-15 stsp case -1:
393 3ce1b845 2019-07-15 stsp goto doneediting;
394 3ce1b845 2019-07-15 stsp case 0:
395 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
396 3ce1b845 2019-07-15 stsp _exit(127);
397 3ce1b845 2019-07-15 stsp }
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
400 3ce1b845 2019-07-15 stsp if (errno != EINTR)
401 3ce1b845 2019-07-15 stsp break;
402 3ce1b845 2019-07-15 stsp
403 3ce1b845 2019-07-15 stsp doneediting:
404 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
406 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
407 3ce1b845 2019-07-15 stsp
408 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
409 3ce1b845 2019-07-15 stsp errno = EINTR;
410 3ce1b845 2019-07-15 stsp return -1;
411 3ce1b845 2019-07-15 stsp }
412 3ce1b845 2019-07-15 stsp
413 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
414 3ce1b845 2019-07-15 stsp }
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp static const struct got_error *
417 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 3ce1b845 2019-07-15 stsp const char *initial_content)
419 3ce1b845 2019-07-15 stsp {
420 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
421 3ce1b845 2019-07-15 stsp char buf[1024];
422 3ce1b845 2019-07-15 stsp struct stat st, st2;
423 3ce1b845 2019-07-15 stsp FILE *fp;
424 3ce1b845 2019-07-15 stsp int content_changed = 0;
425 3ce1b845 2019-07-15 stsp size_t len;
426 3ce1b845 2019-07-15 stsp
427 3ce1b845 2019-07-15 stsp *logmsg = NULL;
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
430 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
443 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
444 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
445 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
446 3ce1b845 2019-07-15 stsp len = 0;
447 3ce1b845 2019-07-15 stsp
448 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
449 3ce1b845 2019-07-15 stsp if (fp == NULL) {
450 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
451 3ce1b845 2019-07-15 stsp goto done;
452 3ce1b845 2019-07-15 stsp }
453 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
454 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
455 3ce1b845 2019-07-15 stsp content_changed = 1;
456 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
458 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp fclose(fp);
461 3ce1b845 2019-07-15 stsp
462 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
464 3ce1b845 2019-07-15 stsp len--;
465 3ce1b845 2019-07-15 stsp }
466 3ce1b845 2019-07-15 stsp
467 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
468 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
470 3ce1b845 2019-07-15 stsp done:
471 3ce1b845 2019-07-15 stsp if (err) {
472 3ce1b845 2019-07-15 stsp free(*logmsg);
473 3ce1b845 2019-07-15 stsp *logmsg = NULL;
474 3ce1b845 2019-07-15 stsp }
475 3ce1b845 2019-07-15 stsp return err;
476 3ce1b845 2019-07-15 stsp }
477 3ce1b845 2019-07-15 stsp
478 3ce1b845 2019-07-15 stsp static const struct got_error *
479 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
481 3ce1b845 2019-07-15 stsp {
482 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
483 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
484 3ce1b845 2019-07-15 stsp int fd;
485 3ce1b845 2019-07-15 stsp
486 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
487 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
488 3ce1b845 2019-07-15 stsp branch_name) == -1)
489 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
490 3ce1b845 2019-07-15 stsp
491 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
492 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
493 3ce1b845 2019-07-15 stsp if (err)
494 3ce1b845 2019-07-15 stsp goto done;
495 3ce1b845 2019-07-15 stsp
496 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
497 3ce1b845 2019-07-15 stsp close(fd);
498 3ce1b845 2019-07-15 stsp
499 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 3ce1b845 2019-07-15 stsp done:
501 3ce1b845 2019-07-15 stsp free(initial_content);
502 3ce1b845 2019-07-15 stsp return err;
503 3ce1b845 2019-07-15 stsp }
504 3ce1b845 2019-07-15 stsp
505 3ce1b845 2019-07-15 stsp static const struct got_error *
506 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
507 3ce1b845 2019-07-15 stsp {
508 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
509 3ce1b845 2019-07-15 stsp return NULL;
510 3ce1b845 2019-07-15 stsp }
511 3ce1b845 2019-07-15 stsp
512 3ce1b845 2019-07-15 stsp static const struct got_error *
513 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
514 84792843 2019-08-09 stsp {
515 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
516 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
517 84792843 2019-08-09 stsp
518 84792843 2019-08-09 stsp *author = NULL;
519 aba9c984 2019-09-08 stsp
520 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
521 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
522 c9956ddf 2019-09-08 stsp if (name && email) {
523 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
524 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
525 aba9c984 2019-09-08 stsp return NULL;
526 aba9c984 2019-09-08 stsp }
527 84792843 2019-08-09 stsp
528 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
529 84792843 2019-08-09 stsp if (got_author == NULL) {
530 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
531 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
532 c9956ddf 2019-09-08 stsp if (name && email) {
533 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
534 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
535 c9956ddf 2019-09-08 stsp return NULL;
536 c9956ddf 2019-09-08 stsp }
537 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
538 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
539 84792843 2019-08-09 stsp }
540 84792843 2019-08-09 stsp
541 aba9c984 2019-09-08 stsp *author = strdup(got_author);
542 aba9c984 2019-09-08 stsp if (*author == NULL)
543 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
544 84792843 2019-08-09 stsp
545 84792843 2019-08-09 stsp /*
546 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
547 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
548 84792843 2019-08-09 stsp */
549 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
550 84792843 2019-08-09 stsp got_author++;
551 aba9c984 2019-09-08 stsp if (*got_author != '<') {
552 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 aba9c984 2019-09-08 stsp goto done;
554 aba9c984 2019-09-08 stsp }
555 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
556 84792843 2019-08-09 stsp got_author++;
557 aba9c984 2019-09-08 stsp if (*got_author != '@') {
558 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 aba9c984 2019-09-08 stsp goto done;
560 aba9c984 2019-09-08 stsp }
561 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
562 84792843 2019-08-09 stsp got_author++;
563 84792843 2019-08-09 stsp if (*got_author != '>')
564 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
565 aba9c984 2019-09-08 stsp done:
566 aba9c984 2019-09-08 stsp if (err) {
567 aba9c984 2019-09-08 stsp free(*author);
568 aba9c984 2019-09-08 stsp *author = NULL;
569 aba9c984 2019-09-08 stsp }
570 aba9c984 2019-09-08 stsp return err;
571 c9956ddf 2019-09-08 stsp }
572 c9956ddf 2019-09-08 stsp
573 c9956ddf 2019-09-08 stsp static const struct got_error *
574 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
575 c9956ddf 2019-09-08 stsp {
576 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
577 c9956ddf 2019-09-08 stsp
578 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
579 c9956ddf 2019-09-08 stsp if (homedir) {
580 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
582 c9956ddf 2019-09-08 stsp
583 c9956ddf 2019-09-08 stsp }
584 c9956ddf 2019-09-08 stsp return NULL;
585 84792843 2019-08-09 stsp }
586 84792843 2019-08-09 stsp
587 84792843 2019-08-09 stsp static const struct got_error *
588 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
589 3ce1b845 2019-07-15 stsp {
590 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
591 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
594 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
596 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
598 3ce1b845 2019-07-15 stsp int ch;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
600 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
601 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
602 3ce1b845 2019-07-15 stsp
603 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
604 3ce1b845 2019-07-15 stsp
605 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 3ce1b845 2019-07-15 stsp switch (ch) {
607 3ce1b845 2019-07-15 stsp case 'b':
608 3ce1b845 2019-07-15 stsp branch_name = optarg;
609 3ce1b845 2019-07-15 stsp break;
610 3ce1b845 2019-07-15 stsp case 'm':
611 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
612 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
613 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
614 3ce1b845 2019-07-15 stsp goto done;
615 3ce1b845 2019-07-15 stsp }
616 3ce1b845 2019-07-15 stsp break;
617 3ce1b845 2019-07-15 stsp case 'r':
618 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
619 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
620 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
621 9ba1d308 2019-10-21 stsp optarg);
622 3ce1b845 2019-07-15 stsp goto done;
623 3ce1b845 2019-07-15 stsp }
624 3ce1b845 2019-07-15 stsp break;
625 3ce1b845 2019-07-15 stsp case 'I':
626 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
627 3ce1b845 2019-07-15 stsp break;
628 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
629 3ce1b845 2019-07-15 stsp NULL);
630 3ce1b845 2019-07-15 stsp if (error)
631 3ce1b845 2019-07-15 stsp goto done;
632 3ce1b845 2019-07-15 stsp break;
633 3ce1b845 2019-07-15 stsp default:
634 b2b65d18 2019-08-22 stsp usage_import();
635 3ce1b845 2019-07-15 stsp /* NOTREACHED */
636 3ce1b845 2019-07-15 stsp }
637 3ce1b845 2019-07-15 stsp }
638 3ce1b845 2019-07-15 stsp
639 3ce1b845 2019-07-15 stsp argc -= optind;
640 3ce1b845 2019-07-15 stsp argv += optind;
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp #ifndef PROFILE
643 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 aba9c984 2019-09-08 stsp "unveil",
645 3ce1b845 2019-07-15 stsp NULL) == -1)
646 3ce1b845 2019-07-15 stsp err(1, "pledge");
647 3ce1b845 2019-07-15 stsp #endif
648 3ce1b845 2019-07-15 stsp if (argc != 1)
649 3ce1b845 2019-07-15 stsp usage_import();
650 2c7829a4 2019-06-17 stsp
651 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
652 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
653 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
654 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
655 3ce1b845 2019-07-15 stsp }
656 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
657 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
658 c9956ddf 2019-09-08 stsp if (error)
659 c9956ddf 2019-09-08 stsp goto done;
660 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
661 3ce1b845 2019-07-15 stsp if (error)
662 3ce1b845 2019-07-15 stsp goto done;
663 aba9c984 2019-09-08 stsp
664 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
665 aba9c984 2019-09-08 stsp if (error)
666 aba9c984 2019-09-08 stsp return error;
667 e560b7e0 2019-11-28 stsp
668 e560b7e0 2019-11-28 stsp /*
669 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
670 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
671 e560b7e0 2019-11-28 stsp * an unintended typo.
672 e560b7e0 2019-11-28 stsp */
673 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
674 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 3ce1b845 2019-07-15 stsp
676 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
678 3ce1b845 2019-07-15 stsp goto done;
679 3ce1b845 2019-07-15 stsp }
680 3ce1b845 2019-07-15 stsp
681 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
682 3ce1b845 2019-07-15 stsp if (error) {
683 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
684 3ce1b845 2019-07-15 stsp goto done;
685 3ce1b845 2019-07-15 stsp } else {
686 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 3ce1b845 2019-07-15 stsp "import target branch already exists");
688 3ce1b845 2019-07-15 stsp goto done;
689 3ce1b845 2019-07-15 stsp }
690 3ce1b845 2019-07-15 stsp
691 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
692 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
693 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
694 3ce1b845 2019-07-15 stsp goto done;
695 3ce1b845 2019-07-15 stsp }
696 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
697 3ce1b845 2019-07-15 stsp
698 3ce1b845 2019-07-15 stsp /*
699 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
700 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
701 3ce1b845 2019-07-15 stsp */
702 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
703 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
704 3ce1b845 2019-07-15 stsp if (error)
705 3ce1b845 2019-07-15 stsp goto done;
706 8e158b01 2019-09-22 stsp free(logmsg);
707 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 ef293bdd 2019-10-21 stsp path_dir, refname);
709 ef293bdd 2019-10-21 stsp if (error) {
710 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
712 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
713 3ce1b845 2019-07-15 stsp goto done;
714 ef293bdd 2019-10-21 stsp }
715 3ce1b845 2019-07-15 stsp }
716 3ce1b845 2019-07-15 stsp
717 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
718 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
719 ef293bdd 2019-10-21 stsp if (logmsg_path)
720 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
721 3ce1b845 2019-07-15 stsp goto done;
722 ef293bdd 2019-10-21 stsp }
723 3ce1b845 2019-07-15 stsp
724 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 ef293bdd 2019-10-21 stsp if (error) {
726 ef293bdd 2019-10-21 stsp if (logmsg_path)
727 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
728 ef293bdd 2019-10-21 stsp goto done;
729 ef293bdd 2019-10-21 stsp }
730 ef293bdd 2019-10-21 stsp
731 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
733 ef293bdd 2019-10-21 stsp if (error) {
734 ef293bdd 2019-10-21 stsp if (logmsg_path)
735 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
736 3ce1b845 2019-07-15 stsp goto done;
737 ef293bdd 2019-10-21 stsp }
738 3ce1b845 2019-07-15 stsp
739 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 ef293bdd 2019-10-21 stsp if (error) {
741 ef293bdd 2019-10-21 stsp if (logmsg_path)
742 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
743 3ce1b845 2019-07-15 stsp goto done;
744 ef293bdd 2019-10-21 stsp }
745 3ce1b845 2019-07-15 stsp
746 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
747 ef293bdd 2019-10-21 stsp if (error) {
748 ef293bdd 2019-10-21 stsp if (logmsg_path)
749 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
750 3ce1b845 2019-07-15 stsp goto done;
751 ef293bdd 2019-10-21 stsp }
752 3ce1b845 2019-07-15 stsp
753 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
754 ef293bdd 2019-10-21 stsp if (error) {
755 ef293bdd 2019-10-21 stsp if (logmsg_path)
756 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
757 3ce1b845 2019-07-15 stsp goto done;
758 ef293bdd 2019-10-21 stsp }
759 3ce1b845 2019-07-15 stsp
760 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 3ce1b845 2019-07-15 stsp if (error) {
762 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
763 ef293bdd 2019-10-21 stsp if (logmsg_path)
764 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
765 3ce1b845 2019-07-15 stsp goto done;
766 ef293bdd 2019-10-21 stsp }
767 3ce1b845 2019-07-15 stsp
768 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 3ce1b845 2019-07-15 stsp branch_ref);
770 ef293bdd 2019-10-21 stsp if (error) {
771 ef293bdd 2019-10-21 stsp if (logmsg_path)
772 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
773 3ce1b845 2019-07-15 stsp goto done;
774 ef293bdd 2019-10-21 stsp }
775 3ce1b845 2019-07-15 stsp
776 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
777 ef293bdd 2019-10-21 stsp if (error) {
778 ef293bdd 2019-10-21 stsp if (logmsg_path)
779 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
780 3ce1b845 2019-07-15 stsp goto done;
781 ef293bdd 2019-10-21 stsp }
782 3ce1b845 2019-07-15 stsp }
783 3ce1b845 2019-07-15 stsp
784 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
785 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
786 2c7829a4 2019-06-17 stsp done:
787 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
788 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
789 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
790 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
792 8e158b01 2019-09-22 stsp free(logmsg);
793 ef293bdd 2019-10-21 stsp free(logmsg_path);
794 2c7829a4 2019-06-17 stsp free(repo_path);
795 3ce1b845 2019-07-15 stsp free(editor);
796 3ce1b845 2019-07-15 stsp free(refname);
797 3ce1b845 2019-07-15 stsp free(new_commit_id);
798 3ce1b845 2019-07-15 stsp free(id_str);
799 aba9c984 2019-09-08 stsp free(author);
800 c9956ddf 2019-09-08 stsp free(gitconfig_path);
801 3ce1b845 2019-07-15 stsp if (branch_ref)
802 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
803 3ce1b845 2019-07-15 stsp if (head_ref)
804 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
805 2c7829a4 2019-06-17 stsp return error;
806 93658fb9 2020-03-18 stsp }
807 93658fb9 2020-03-18 stsp
808 93658fb9 2020-03-18 stsp __dead static void
809 93658fb9 2020-03-18 stsp usage_clone(void)
810 93658fb9 2020-03-18 stsp {
811 68999b92 2020-03-18 stsp fprintf(stderr, "usage: %s clone [-q] [-v] repository-url [target-directory]\n",
812 9df6f38b 2020-03-18 stsp getprogname());
813 93658fb9 2020-03-18 stsp exit(1);
814 93658fb9 2020-03-18 stsp }
815 892ac3b6 2020-03-18 stsp
816 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
817 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
818 892ac3b6 2020-03-18 stsp int last_p_indexed;
819 892ac3b6 2020-03-18 stsp int last_p_resolved;
820 68999b92 2020-03-18 stsp int verbosity;
821 892ac3b6 2020-03-18 stsp };
822 93658fb9 2020-03-18 stsp
823 93658fb9 2020-03-18 stsp static const struct got_error *
824 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
825 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
826 531c3985 2020-03-18 stsp {
827 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
828 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
829 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
830 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
831 b2409d58 2020-03-18 stsp
832 68999b92 2020-03-18 stsp if (a->verbosity < 0)
833 68999b92 2020-03-18 stsp return NULL;
834 68999b92 2020-03-18 stsp
835 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
836 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
837 892ac3b6 2020-03-18 stsp fflush(stdout);
838 b2409d58 2020-03-18 stsp }
839 b2409d58 2020-03-18 stsp
840 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
841 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
842 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
843 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
844 892ac3b6 2020-03-18 stsp print_size = 1;
845 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
846 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
847 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
848 892ac3b6 2020-03-18 stsp }
849 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
850 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
851 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
852 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
853 892ac3b6 2020-03-18 stsp print_indexed = 1;
854 892ac3b6 2020-03-18 stsp print_size = 1;
855 892ac3b6 2020-03-18 stsp }
856 61cc1a7a 2020-03-18 stsp }
857 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
858 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
859 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
860 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
861 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
862 892ac3b6 2020-03-18 stsp print_resolved = 1;
863 892ac3b6 2020-03-18 stsp print_indexed = 1;
864 892ac3b6 2020-03-18 stsp print_size = 1;
865 892ac3b6 2020-03-18 stsp }
866 61cc1a7a 2020-03-18 stsp }
867 b2409d58 2020-03-18 stsp
868 d2cdc636 2020-03-18 stsp }
869 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
870 892ac3b6 2020-03-18 stsp printf("\r");
871 892ac3b6 2020-03-18 stsp if (print_size)
872 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
873 68999b92 2020-03-18 stsp if (print_indexed) {
874 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
875 68999b92 2020-03-18 stsp if (a->verbosity > 0)
876 68999b92 2020-03-18 stsp printf(" (%d/%d)", nobj_indexed, nobj_total);
877 68999b92 2020-03-18 stsp }
878 68999b92 2020-03-18 stsp if (print_resolved) {
879 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
880 68999b92 2020-03-18 stsp if (a->verbosity > 0)
881 68999b92 2020-03-18 stsp printf(" (%d/%d)", nobj_resolved,
882 68999b92 2020-03-18 stsp nobj_total - nobj_loose);
883 68999b92 2020-03-18 stsp }
884 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
885 892ac3b6 2020-03-18 stsp fflush(stdout);
886 892ac3b6 2020-03-18 stsp
887 892ac3b6 2020-03-18 stsp if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
888 892ac3b6 2020-03-18 stsp nobj_resolved == nobj_total - nobj_loose)
889 892ac3b6 2020-03-18 stsp printf("\nWriting pack index...\n");
890 892ac3b6 2020-03-18 stsp
891 531c3985 2020-03-18 stsp return NULL;
892 531c3985 2020-03-18 stsp }
893 531c3985 2020-03-18 stsp
894 531c3985 2020-03-18 stsp static const struct got_error *
895 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
896 93658fb9 2020-03-18 stsp {
897 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
898 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
899 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
900 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
901 bb64b798 2020-03-18 stsp const char *repo_path;
902 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
903 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
904 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
905 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
906 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
907 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
908 b46f3e71 2020-03-18 stsp char *git_url = NULL;
909 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
910 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
911 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
912 b46f3e71 2020-03-18 stsp ssize_t n;
913 68999b92 2020-03-18 stsp int verbosity = 0;
914 93658fb9 2020-03-18 stsp
915 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
916 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
917 d9b4d0c0 2020-03-18 stsp
918 68999b92 2020-03-18 stsp while ((ch = getopt(argc, argv, "vq")) != -1) {
919 93658fb9 2020-03-18 stsp switch (ch) {
920 68999b92 2020-03-18 stsp case 'v':
921 68999b92 2020-03-18 stsp if (verbosity < 0)
922 68999b92 2020-03-18 stsp verbosity = 0;
923 68999b92 2020-03-18 stsp else if (verbosity < 3)
924 68999b92 2020-03-18 stsp verbosity++;
925 68999b92 2020-03-18 stsp break;
926 68999b92 2020-03-18 stsp case 'q':
927 68999b92 2020-03-18 stsp verbosity = -1;
928 68999b92 2020-03-18 stsp break;
929 93658fb9 2020-03-18 stsp default:
930 93658fb9 2020-03-18 stsp usage_clone();
931 93658fb9 2020-03-18 stsp break;
932 93658fb9 2020-03-18 stsp }
933 93658fb9 2020-03-18 stsp }
934 93658fb9 2020-03-18 stsp argc -= optind;
935 93658fb9 2020-03-18 stsp argv += optind;
936 39c64a6a 2020-03-18 stsp
937 93658fb9 2020-03-18 stsp uri = argv[0];
938 9df6f38b 2020-03-18 stsp
939 9df6f38b 2020-03-18 stsp if (argc == 1)
940 93658fb9 2020-03-18 stsp dirname = NULL;
941 9df6f38b 2020-03-18 stsp else if (argc == 2)
942 93658fb9 2020-03-18 stsp dirname = argv[1];
943 93658fb9 2020-03-18 stsp else
944 93658fb9 2020-03-18 stsp usage_clone();
945 09838ffc 2020-03-18 stsp
946 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
948 39c64a6a 2020-03-18 stsp if (error)
949 09838ffc 2020-03-18 stsp goto done;
950 09838ffc 2020-03-18 stsp
951 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
952 b46f3e71 2020-03-18 stsp #ifndef PROFILE
953 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
954 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
955 39c64a6a 2020-03-18 stsp err(1, "pledge");
956 b46f3e71 2020-03-18 stsp #endif
957 b46f3e71 2020-03-18 stsp git_url = strdup(argv[0]);
958 b46f3e71 2020-03-18 stsp if (git_url == NULL) {
959 b46f3e71 2020-03-18 stsp error = got_error_from_errno("strdup");
960 b46f3e71 2020-03-18 stsp goto done;
961 b46f3e71 2020-03-18 stsp }
962 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
963 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
964 b46f3e71 2020-03-18 stsp #ifndef PROFILE
965 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
966 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
967 39c64a6a 2020-03-18 stsp err(1, "pledge");
968 b46f3e71 2020-03-18 stsp #endif
969 b46f3e71 2020-03-18 stsp if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
970 b46f3e71 2020-03-18 stsp error = got_error_from_errno("asprintf");
971 b46f3e71 2020-03-18 stsp goto done;
972 b46f3e71 2020-03-18 stsp }
973 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
974 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
975 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
976 39c64a6a 2020-03-18 stsp goto done;
977 39c64a6a 2020-03-18 stsp } else {
978 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
979 39c64a6a 2020-03-18 stsp goto done;
980 39c64a6a 2020-03-18 stsp }
981 bb64b798 2020-03-18 stsp if (dirname == NULL) {
982 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
983 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
984 bb64b798 2020-03-18 stsp goto done;
985 bb64b798 2020-03-18 stsp }
986 bb64b798 2020-03-18 stsp repo_path = default_destdir;
987 bb64b798 2020-03-18 stsp } else
988 bb64b798 2020-03-18 stsp repo_path = dirname;
989 bb64b798 2020-03-18 stsp
990 39c64a6a 2020-03-18 stsp error = got_path_mkdir(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_init(repo_path);
995 39c64a6a 2020-03-18 stsp if (error)
996 bb64b798 2020-03-18 stsp goto done;
997 bb64b798 2020-03-18 stsp
998 39c64a6a 2020-03-18 stsp error = got_repo_open(&repo, repo_path, NULL);
999 39c64a6a 2020-03-18 stsp if (error)
1000 294dfefd 2020-03-18 stsp goto done;
1001 294dfefd 2020-03-18 stsp
1002 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1003 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1004 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1005 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1006 ee448f5f 2020-03-18 stsp goto done;
1007 ee448f5f 2020-03-18 stsp }
1008 ee448f5f 2020-03-18 stsp }
1009 ee448f5f 2020-03-18 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1010 ee448f5f 2020-03-18 stsp if (error)
1011 ee448f5f 2020-03-18 stsp goto done;
1012 ee448f5f 2020-03-18 stsp
1013 68999b92 2020-03-18 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1014 68999b92 2020-03-18 stsp verbosity);
1015 39c64a6a 2020-03-18 stsp if (error)
1016 bb64b798 2020-03-18 stsp goto done;
1017 294dfefd 2020-03-18 stsp
1018 68999b92 2020-03-18 stsp if (verbosity >= 0)
1019 68999b92 2020-03-18 stsp printf("Connected to %s:%s\n", host, port);
1020 bb64b798 2020-03-18 stsp
1021 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1022 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1023 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1024 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1025 39c64a6a 2020-03-18 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1026 892ac3b6 2020-03-18 stsp repo, fetch_progress, &fpa);
1027 39c64a6a 2020-03-18 stsp if (error)
1028 d9b4d0c0 2020-03-18 stsp goto done;
1029 d9b4d0c0 2020-03-18 stsp
1030 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1031 39c64a6a 2020-03-18 stsp if (error)
1032 d9b4d0c0 2020-03-18 stsp goto done;
1033 68999b92 2020-03-18 stsp if (verbosity >= 0)
1034 68999b92 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1035 d9b4d0c0 2020-03-18 stsp free(id_str);
1036 d9b4d0c0 2020-03-18 stsp
1037 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1038 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1039 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1040 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1041 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1042 7ebc0570 2020-03-18 stsp char *remote_refname;
1043 668a20f6 2020-03-18 stsp
1044 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1045 39c64a6a 2020-03-18 stsp if (error)
1046 d9b4d0c0 2020-03-18 stsp goto done;
1047 7ebc0570 2020-03-18 stsp error = got_ref_write(ref, repo);
1048 7ebc0570 2020-03-18 stsp got_ref_close(ref);
1049 7ebc0570 2020-03-18 stsp if (error)
1050 7ebc0570 2020-03-18 stsp goto done;
1051 d9b4d0c0 2020-03-18 stsp
1052 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1053 7ebc0570 2020-03-18 stsp continue;
1054 7ebc0570 2020-03-18 stsp
1055 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1056 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1057 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1058 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1059 7ebc0570 2020-03-18 stsp goto done;
1060 7ebc0570 2020-03-18 stsp }
1061 7ebc0570 2020-03-18 stsp error = got_ref_alloc(&ref, remote_refname, id);
1062 39c64a6a 2020-03-18 stsp if (error)
1063 d9b4d0c0 2020-03-18 stsp goto done;
1064 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1065 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1066 39c64a6a 2020-03-18 stsp if (error)
1067 d9b4d0c0 2020-03-18 stsp goto done;
1068 d9b4d0c0 2020-03-18 stsp }
1069 d9b4d0c0 2020-03-18 stsp
1070 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1071 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1072 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1073 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1074 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1075 d9b4d0c0 2020-03-18 stsp
1076 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1077 d9b4d0c0 2020-03-18 stsp continue;
1078 d9b4d0c0 2020-03-18 stsp
1079 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1080 39c64a6a 2020-03-18 stsp if (error) {
1081 39c64a6a 2020-03-18 stsp if (error->code == GOT_ERR_NOT_REF)
1082 d9b4d0c0 2020-03-18 stsp continue;
1083 d9b4d0c0 2020-03-18 stsp goto done;
1084 d9b4d0c0 2020-03-18 stsp }
1085 d9b4d0c0 2020-03-18 stsp
1086 39c64a6a 2020-03-18 stsp error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1087 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1088 39c64a6a 2020-03-18 stsp if (error)
1089 d9b4d0c0 2020-03-18 stsp goto done;
1090 d9b4d0c0 2020-03-18 stsp
1091 68999b92 2020-03-18 stsp if (verbosity > 1) {
1092 68999b92 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1093 68999b92 2020-03-18 stsp got_ref_get_symref_target(symref));
1094 68999b92 2020-03-18 stsp }
1095 d52aaa3d 2020-03-18 stsp if (verbosity >= 0)
1096 d52aaa3d 2020-03-18 stsp printf("Created cloned repository '%s'\n", repo_path);
1097 d9b4d0c0 2020-03-18 stsp
1098 39c64a6a 2020-03-18 stsp error = got_ref_write(symref, repo);
1099 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1100 d9b4d0c0 2020-03-18 stsp break;
1101 d9b4d0c0 2020-03-18 stsp }
1102 d9b4d0c0 2020-03-18 stsp
1103 b46f3e71 2020-03-18 stsp /* Create a config file so Git can understand this repository. */
1104 b46f3e71 2020-03-18 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 b46f3e71 2020-03-18 stsp if (gitconfig_path == NULL) {
1106 b46f3e71 2020-03-18 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1107 b46f3e71 2020-03-18 stsp goto done;
1108 b46f3e71 2020-03-18 stsp }
1109 b46f3e71 2020-03-18 stsp gitconfig_file = fopen(gitconfig_path, "w");
1110 b46f3e71 2020-03-18 stsp if (gitconfig_file == NULL) {
1111 b46f3e71 2020-03-18 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1112 b46f3e71 2020-03-18 stsp goto done;
1113 b46f3e71 2020-03-18 stsp }
1114 b46f3e71 2020-03-18 stsp if (asprintf(&gitconfig,
1115 b46f3e71 2020-03-18 stsp "[core]\n"
1116 b46f3e71 2020-03-18 stsp "\trepositoryformatversion = 0\n"
1117 b46f3e71 2020-03-18 stsp "\tbare = true\n"
1118 7ebc0570 2020-03-18 stsp "[remote \"%s\"]\n"
1119 b46f3e71 2020-03-18 stsp "\turl = %s\n"
1120 7ebc0570 2020-03-18 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1121 7ebc0570 2020-03-18 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1122 7ebc0570 2020-03-18 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1123 b46f3e71 2020-03-18 stsp error = got_error_from_errno("asprintf");
1124 b46f3e71 2020-03-18 stsp goto done;
1125 b46f3e71 2020-03-18 stsp }
1126 b46f3e71 2020-03-18 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1127 b46f3e71 2020-03-18 stsp if (n != strlen(gitconfig))
1128 b46f3e71 2020-03-18 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1129 09838ffc 2020-03-18 stsp done:
1130 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1131 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1132 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1133 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1134 bb64b798 2020-03-18 stsp if (repo)
1135 bb64b798 2020-03-18 stsp got_repo_close(repo);
1136 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1137 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1138 d9b4d0c0 2020-03-18 stsp free(pe->data);
1139 d9b4d0c0 2020-03-18 stsp }
1140 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1141 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1142 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1143 d9b4d0c0 2020-03-18 stsp free(pe->data);
1144 d9b4d0c0 2020-03-18 stsp }
1145 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1146 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1147 09838ffc 2020-03-18 stsp free(proto);
1148 09838ffc 2020-03-18 stsp free(host);
1149 09838ffc 2020-03-18 stsp free(port);
1150 09838ffc 2020-03-18 stsp free(server_path);
1151 09838ffc 2020-03-18 stsp free(repo_name);
1152 bb64b798 2020-03-18 stsp free(default_destdir);
1153 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1154 b46f3e71 2020-03-18 stsp free(git_url);
1155 39c64a6a 2020-03-18 stsp return error;
1156 2ab43947 2020-03-18 stsp }
1157 2ab43947 2020-03-18 stsp
1158 2ab43947 2020-03-18 stsp __dead static void
1159 2ab43947 2020-03-18 stsp usage_checkout(void)
1160 2ab43947 2020-03-18 stsp {
1161 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1162 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1163 2ab43947 2020-03-18 stsp exit(1);
1164 2ab43947 2020-03-18 stsp }
1165 2ab43947 2020-03-18 stsp
1166 2ab43947 2020-03-18 stsp static void
1167 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1168 2ab43947 2020-03-18 stsp {
1169 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1170 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1171 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1172 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1173 2ab43947 2020-03-18 stsp getprogname());
1174 2ab43947 2020-03-18 stsp }
1175 2ab43947 2020-03-18 stsp
1176 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1177 2ab43947 2020-03-18 stsp const char *worktree_path;
1178 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1179 2ab43947 2020-03-18 stsp };
1180 2ab43947 2020-03-18 stsp
1181 2ab43947 2020-03-18 stsp static const struct got_error *
1182 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1183 2ab43947 2020-03-18 stsp {
1184 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1185 2ab43947 2020-03-18 stsp
1186 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1187 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1188 2ab43947 2020-03-18 stsp return NULL;
1189 2ab43947 2020-03-18 stsp
1190 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1191 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1192 2ab43947 2020-03-18 stsp return NULL;
1193 2ab43947 2020-03-18 stsp }
1194 2ab43947 2020-03-18 stsp
1195 2ab43947 2020-03-18 stsp while (path[0] == '/')
1196 2ab43947 2020-03-18 stsp path++;
1197 2ab43947 2020-03-18 stsp
1198 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1199 2ab43947 2020-03-18 stsp return NULL;
1200 2ab43947 2020-03-18 stsp }
1201 2ab43947 2020-03-18 stsp
1202 2ab43947 2020-03-18 stsp static const struct got_error *
1203 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1204 2ab43947 2020-03-18 stsp {
1205 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1206 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1207 2ab43947 2020-03-18 stsp return NULL;
1208 2ab43947 2020-03-18 stsp }
1209 2ab43947 2020-03-18 stsp
1210 2ab43947 2020-03-18 stsp static const struct got_error *
1211 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1212 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1213 2ab43947 2020-03-18 stsp struct got_repository *repo)
1214 2ab43947 2020-03-18 stsp {
1215 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1216 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1217 2ab43947 2020-03-18 stsp
1218 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1219 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1220 2ab43947 2020-03-18 stsp if (err)
1221 2ab43947 2020-03-18 stsp return err;
1222 2ab43947 2020-03-18 stsp
1223 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1224 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1225 2ab43947 2020-03-18 stsp
1226 2ab43947 2020-03-18 stsp /*
1227 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1228 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1229 2ab43947 2020-03-18 stsp *
1230 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1231 2ab43947 2020-03-18 stsp *
1232 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1233 2ab43947 2020-03-18 stsp * \ /
1234 2ab43947 2020-03-18 stsp * C E
1235 2ab43947 2020-03-18 stsp * \ /
1236 2ab43947 2020-03-18 stsp * B (yca)
1237 2ab43947 2020-03-18 stsp * |
1238 2ab43947 2020-03-18 stsp * A
1239 2ab43947 2020-03-18 stsp *
1240 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1241 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1242 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1243 2ab43947 2020-03-18 stsp */
1244 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1245 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1246 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1247 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1248 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1249 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1250 2ab43947 2020-03-18 stsp
1251 2ab43947 2020-03-18 stsp free(yca_id);
1252 2ab43947 2020-03-18 stsp return NULL;
1253 2ab43947 2020-03-18 stsp }
1254 2ab43947 2020-03-18 stsp
1255 2ab43947 2020-03-18 stsp static const struct got_error *
1256 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1257 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1258 2ab43947 2020-03-18 stsp struct got_repository *repo)
1259 2ab43947 2020-03-18 stsp {
1260 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1261 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1262 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1263 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1264 2ab43947 2020-03-18 stsp
1265 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1266 2ab43947 2020-03-18 stsp if (err)
1267 2ab43947 2020-03-18 stsp goto done;
1268 2ab43947 2020-03-18 stsp
1269 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1270 2ab43947 2020-03-18 stsp is_same_branch = 1;
1271 2ab43947 2020-03-18 stsp goto done;
1272 2ab43947 2020-03-18 stsp }
1273 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1274 2ab43947 2020-03-18 stsp is_same_branch = 1;
1275 2ab43947 2020-03-18 stsp goto done;
1276 2ab43947 2020-03-18 stsp }
1277 2ab43947 2020-03-18 stsp
1278 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1279 2ab43947 2020-03-18 stsp if (err)
1280 2ab43947 2020-03-18 stsp goto done;
1281 2ab43947 2020-03-18 stsp
1282 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1283 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1284 2ab43947 2020-03-18 stsp if (err)
1285 2ab43947 2020-03-18 stsp goto done;
1286 2ab43947 2020-03-18 stsp
1287 2ab43947 2020-03-18 stsp for (;;) {
1288 2ab43947 2020-03-18 stsp struct got_object_id *id;
1289 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1290 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1291 2ab43947 2020-03-18 stsp if (err) {
1292 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1293 2ab43947 2020-03-18 stsp err = NULL;
1294 2ab43947 2020-03-18 stsp break;
1295 2ab43947 2020-03-18 stsp }
1296 2ab43947 2020-03-18 stsp
1297 2ab43947 2020-03-18 stsp if (id) {
1298 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1299 2ab43947 2020-03-18 stsp break;
1300 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1301 2ab43947 2020-03-18 stsp is_same_branch = 1;
1302 2ab43947 2020-03-18 stsp break;
1303 2ab43947 2020-03-18 stsp }
1304 2ab43947 2020-03-18 stsp }
1305 2ab43947 2020-03-18 stsp }
1306 2ab43947 2020-03-18 stsp done:
1307 2ab43947 2020-03-18 stsp if (graph)
1308 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1309 2ab43947 2020-03-18 stsp free(head_commit_id);
1310 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1311 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1312 2ab43947 2020-03-18 stsp return err;
1313 a367ff0f 2019-05-14 stsp }
1314 8069f636 2019-01-12 stsp
1315 4ed7e80c 2018-05-20 stsp static const struct got_error *
1316 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1317 4b6c9460 2020-03-05 stsp {
1318 4b6c9460 2020-03-05 stsp static char msg[512];
1319 4b6c9460 2020-03-05 stsp const char *branch_name;
1320 4b6c9460 2020-03-05 stsp
1321 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1322 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1323 4b6c9460 2020-03-05 stsp else
1324 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1325 4b6c9460 2020-03-05 stsp
1326 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1327 4b6c9460 2020-03-05 stsp branch_name += 11;
1328 4b6c9460 2020-03-05 stsp
1329 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1330 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1331 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1332 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1333 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1334 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1335 4b6c9460 2020-03-05 stsp
1336 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1337 4b6c9460 2020-03-05 stsp }
1338 4b6c9460 2020-03-05 stsp
1339 4b6c9460 2020-03-05 stsp static const struct got_error *
1340 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1341 c09a553d 2018-03-12 stsp {
1342 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1343 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1344 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1345 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1346 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1347 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1348 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1349 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1350 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1351 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1352 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1353 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1354 f2ea84fa 2019-07-27 stsp
1355 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1356 c09a553d 2018-03-12 stsp
1357 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1358 0bb8a95e 2018-03-12 stsp switch (ch) {
1359 08573d5b 2019-05-14 stsp case 'b':
1360 08573d5b 2019-05-14 stsp branch_name = optarg;
1361 08573d5b 2019-05-14 stsp break;
1362 8069f636 2019-01-12 stsp case 'c':
1363 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1364 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1365 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1366 bb51a5b4 2020-01-13 stsp break;
1367 bb51a5b4 2020-01-13 stsp case 'E':
1368 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1369 8069f636 2019-01-12 stsp break;
1370 0bb8a95e 2018-03-12 stsp case 'p':
1371 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1372 0bb8a95e 2018-03-12 stsp break;
1373 0bb8a95e 2018-03-12 stsp default:
1374 2deda0b9 2019-03-07 stsp usage_checkout();
1375 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1376 0bb8a95e 2018-03-12 stsp }
1377 0bb8a95e 2018-03-12 stsp }
1378 0bb8a95e 2018-03-12 stsp
1379 0bb8a95e 2018-03-12 stsp argc -= optind;
1380 0bb8a95e 2018-03-12 stsp argv += optind;
1381 0bb8a95e 2018-03-12 stsp
1382 6715a751 2018-03-16 stsp #ifndef PROFILE
1383 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1384 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1385 c09a553d 2018-03-12 stsp err(1, "pledge");
1386 6715a751 2018-03-16 stsp #endif
1387 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1388 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1389 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1390 76089277 2018-04-01 stsp if (repo_path == NULL)
1391 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1392 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1393 76089277 2018-04-01 stsp if (cwd == NULL) {
1394 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1395 76089277 2018-04-01 stsp goto done;
1396 76089277 2018-04-01 stsp }
1397 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1398 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1399 230a42bd 2019-05-11 jcs if (base == NULL) {
1400 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1401 230a42bd 2019-05-11 jcs path_prefix);
1402 230a42bd 2019-05-11 jcs goto done;
1403 230a42bd 2019-05-11 jcs }
1404 230a42bd 2019-05-11 jcs } else {
1405 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1406 230a42bd 2019-05-11 jcs if (base == NULL) {
1407 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1408 230a42bd 2019-05-11 jcs repo_path);
1409 230a42bd 2019-05-11 jcs goto done;
1410 230a42bd 2019-05-11 jcs }
1411 76089277 2018-04-01 stsp }
1412 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1413 c09a553d 2018-03-12 stsp if (dotgit)
1414 c09a553d 2018-03-12 stsp *dotgit = '\0';
1415 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1416 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1417 c09a553d 2018-03-12 stsp free(cwd);
1418 76089277 2018-04-01 stsp goto done;
1419 c09a553d 2018-03-12 stsp }
1420 c09a553d 2018-03-12 stsp free(cwd);
1421 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1422 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1423 76089277 2018-04-01 stsp if (repo_path == NULL) {
1424 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1425 76089277 2018-04-01 stsp goto done;
1426 76089277 2018-04-01 stsp }
1427 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1428 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1429 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1430 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1431 b4b3a7dd 2019-07-22 stsp argv[1]);
1432 b4b3a7dd 2019-07-22 stsp goto done;
1433 b4b3a7dd 2019-07-22 stsp }
1434 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1435 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1436 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1437 b4b3a7dd 2019-07-22 stsp goto done;
1438 b4b3a7dd 2019-07-22 stsp }
1439 76089277 2018-04-01 stsp }
1440 c09a553d 2018-03-12 stsp } else
1441 c09a553d 2018-03-12 stsp usage_checkout();
1442 c09a553d 2018-03-12 stsp
1443 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1444 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1445 13bfb272 2019-05-10 jcs
1446 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1447 c09a553d 2018-03-12 stsp if (error != NULL)
1448 c02c541e 2019-03-29 stsp goto done;
1449 c02c541e 2019-03-29 stsp
1450 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1451 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1452 c530dc23 2019-07-23 stsp if (error) {
1453 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1454 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1455 c530dc23 2019-07-23 stsp goto done;
1456 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1457 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1458 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1459 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1460 c530dc23 2019-07-23 stsp goto done;
1461 c530dc23 2019-07-23 stsp }
1462 c530dc23 2019-07-23 stsp }
1463 c530dc23 2019-07-23 stsp
1464 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1465 c02c541e 2019-03-29 stsp if (error)
1466 c09a553d 2018-03-12 stsp goto done;
1467 8069f636 2019-01-12 stsp
1468 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1469 c09a553d 2018-03-12 stsp if (error != NULL)
1470 c09a553d 2018-03-12 stsp goto done;
1471 c09a553d 2018-03-12 stsp
1472 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1473 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1474 c09a553d 2018-03-12 stsp goto done;
1475 c09a553d 2018-03-12 stsp
1476 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1477 c09a553d 2018-03-12 stsp if (error != NULL)
1478 c09a553d 2018-03-12 stsp goto done;
1479 c09a553d 2018-03-12 stsp
1480 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1481 e5dc7198 2018-12-29 stsp path_prefix);
1482 e5dc7198 2018-12-29 stsp if (error != NULL)
1483 e5dc7198 2018-12-29 stsp goto done;
1484 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1485 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1486 49520a32 2018-12-29 stsp goto done;
1487 49520a32 2018-12-29 stsp }
1488 49520a32 2018-12-29 stsp
1489 8069f636 2019-01-12 stsp if (commit_id_str) {
1490 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1491 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1492 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1493 30837e32 2019-07-25 stsp if (error)
1494 8069f636 2019-01-12 stsp goto done;
1495 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1496 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1497 8069f636 2019-01-12 stsp if (error != NULL) {
1498 8069f636 2019-01-12 stsp free(commit_id);
1499 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1500 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1501 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1502 4b6c9460 2020-03-05 stsp }
1503 8069f636 2019-01-12 stsp goto done;
1504 8069f636 2019-01-12 stsp }
1505 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1506 4b6c9460 2020-03-05 stsp if (error) {
1507 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1508 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1509 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1510 4b6c9460 2020-03-05 stsp }
1511 45d344f6 2019-05-14 stsp goto done;
1512 4b6c9460 2020-03-05 stsp }
1513 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1514 8069f636 2019-01-12 stsp commit_id);
1515 8069f636 2019-01-12 stsp free(commit_id);
1516 8069f636 2019-01-12 stsp if (error)
1517 8069f636 2019-01-12 stsp goto done;
1518 8069f636 2019-01-12 stsp }
1519 8069f636 2019-01-12 stsp
1520 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1521 f2ea84fa 2019-07-27 stsp if (error)
1522 f2ea84fa 2019-07-27 stsp goto done;
1523 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1524 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1525 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1526 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1527 c09a553d 2018-03-12 stsp if (error != NULL)
1528 c09a553d 2018-03-12 stsp goto done;
1529 c09a553d 2018-03-12 stsp
1530 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1531 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1532 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1533 c09a553d 2018-03-12 stsp done:
1534 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1535 8069f636 2019-01-12 stsp free(commit_id_str);
1536 76089277 2018-04-01 stsp free(repo_path);
1537 507dc3bb 2018-12-29 stsp free(worktree_path);
1538 507dc3bb 2018-12-29 stsp return error;
1539 507dc3bb 2018-12-29 stsp }
1540 507dc3bb 2018-12-29 stsp
1541 507dc3bb 2018-12-29 stsp __dead static void
1542 507dc3bb 2018-12-29 stsp usage_update(void)
1543 507dc3bb 2018-12-29 stsp {
1544 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1545 507dc3bb 2018-12-29 stsp getprogname());
1546 507dc3bb 2018-12-29 stsp exit(1);
1547 507dc3bb 2018-12-29 stsp }
1548 507dc3bb 2018-12-29 stsp
1549 1ee397ad 2019-07-12 stsp static const struct got_error *
1550 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1551 507dc3bb 2018-12-29 stsp {
1552 784955db 2019-01-12 stsp int *did_something = arg;
1553 784955db 2019-01-12 stsp
1554 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1555 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1556 1ee397ad 2019-07-12 stsp return NULL;
1557 507dc3bb 2018-12-29 stsp
1558 1545c615 2019-02-10 stsp *did_something = 1;
1559 a484d721 2019-06-10 stsp
1560 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1561 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1562 1ee397ad 2019-07-12 stsp return NULL;
1563 a484d721 2019-06-10 stsp
1564 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1565 507dc3bb 2018-12-29 stsp path++;
1566 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1567 1ee397ad 2019-07-12 stsp return NULL;
1568 be7061eb 2018-12-30 stsp }
1569 be7061eb 2018-12-30 stsp
1570 be7061eb 2018-12-30 stsp static const struct got_error *
1571 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1572 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1573 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1574 a1fb16d8 2019-05-24 stsp {
1575 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1576 a1fb16d8 2019-05-24 stsp char *base_id_str;
1577 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1578 a1fb16d8 2019-05-24 stsp
1579 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1580 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1581 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1582 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1583 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1584 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1585 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1586 a1fb16d8 2019-05-24 stsp }
1587 a1fb16d8 2019-05-24 stsp
1588 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1589 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1590 a1fb16d8 2019-05-24 stsp if (err) {
1591 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1592 a1fb16d8 2019-05-24 stsp return err;
1593 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1594 a1fb16d8 2019-05-24 stsp }
1595 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1596 a1fb16d8 2019-05-24 stsp return NULL;
1597 a1fb16d8 2019-05-24 stsp
1598 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1599 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1600 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1601 a1fb16d8 2019-05-24 stsp if (err)
1602 a1fb16d8 2019-05-24 stsp return err;
1603 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1604 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1605 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1606 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1607 0ebf8283 2019-07-24 stsp return NULL;
1608 0ebf8283 2019-07-24 stsp }
1609 0ebf8283 2019-07-24 stsp
1610 0ebf8283 2019-07-24 stsp static const struct got_error *
1611 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1612 0ebf8283 2019-07-24 stsp {
1613 0ebf8283 2019-07-24 stsp const struct got_error *err;
1614 0ebf8283 2019-07-24 stsp int in_progress;
1615 0ebf8283 2019-07-24 stsp
1616 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1617 0ebf8283 2019-07-24 stsp if (err)
1618 0ebf8283 2019-07-24 stsp return err;
1619 0ebf8283 2019-07-24 stsp if (in_progress)
1620 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1621 0ebf8283 2019-07-24 stsp
1622 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1623 0ebf8283 2019-07-24 stsp if (err)
1624 0ebf8283 2019-07-24 stsp return err;
1625 0ebf8283 2019-07-24 stsp if (in_progress)
1626 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1627 0ebf8283 2019-07-24 stsp
1628 a1fb16d8 2019-05-24 stsp return NULL;
1629 a5edda0a 2019-07-27 stsp }
1630 a5edda0a 2019-07-27 stsp
1631 a5edda0a 2019-07-27 stsp static const struct got_error *
1632 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1633 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1634 a5edda0a 2019-07-27 stsp {
1635 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1636 a5edda0a 2019-07-27 stsp char *path;
1637 a5edda0a 2019-07-27 stsp int i;
1638 a5edda0a 2019-07-27 stsp
1639 a5edda0a 2019-07-27 stsp if (argc == 0) {
1640 a5edda0a 2019-07-27 stsp path = strdup("");
1641 a5edda0a 2019-07-27 stsp if (path == NULL)
1642 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1643 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1644 a5edda0a 2019-07-27 stsp }
1645 a5edda0a 2019-07-27 stsp
1646 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1647 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1648 a5edda0a 2019-07-27 stsp if (err)
1649 a5edda0a 2019-07-27 stsp break;
1650 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1651 a5edda0a 2019-07-27 stsp if (err) {
1652 a5edda0a 2019-07-27 stsp free(path);
1653 a5edda0a 2019-07-27 stsp break;
1654 a5edda0a 2019-07-27 stsp }
1655 a5edda0a 2019-07-27 stsp }
1656 a5edda0a 2019-07-27 stsp
1657 a5edda0a 2019-07-27 stsp return err;
1658 a1fb16d8 2019-05-24 stsp }
1659 a1fb16d8 2019-05-24 stsp
1660 a1fb16d8 2019-05-24 stsp static const struct got_error *
1661 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1662 507dc3bb 2018-12-29 stsp {
1663 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1664 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1665 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1666 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1667 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1668 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1669 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1670 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1671 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1672 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1673 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1674 507dc3bb 2018-12-29 stsp
1675 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1676 f2ea84fa 2019-07-27 stsp
1677 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1678 507dc3bb 2018-12-29 stsp switch (ch) {
1679 024e9686 2019-05-14 stsp case 'b':
1680 024e9686 2019-05-14 stsp branch_name = optarg;
1681 024e9686 2019-05-14 stsp break;
1682 507dc3bb 2018-12-29 stsp case 'c':
1683 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1684 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1685 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1686 507dc3bb 2018-12-29 stsp break;
1687 507dc3bb 2018-12-29 stsp default:
1688 2deda0b9 2019-03-07 stsp usage_update();
1689 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1690 507dc3bb 2018-12-29 stsp }
1691 507dc3bb 2018-12-29 stsp }
1692 507dc3bb 2018-12-29 stsp
1693 507dc3bb 2018-12-29 stsp argc -= optind;
1694 507dc3bb 2018-12-29 stsp argv += optind;
1695 507dc3bb 2018-12-29 stsp
1696 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1697 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1698 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1699 507dc3bb 2018-12-29 stsp err(1, "pledge");
1700 507dc3bb 2018-12-29 stsp #endif
1701 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1702 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1703 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1704 c4cdcb68 2019-04-03 stsp goto done;
1705 c4cdcb68 2019-04-03 stsp }
1706 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1707 7d5807f4 2019-07-11 stsp if (error)
1708 7d5807f4 2019-07-11 stsp goto done;
1709 7d5807f4 2019-07-11 stsp
1710 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1711 f2ea84fa 2019-07-27 stsp if (error)
1712 f2ea84fa 2019-07-27 stsp goto done;
1713 507dc3bb 2018-12-29 stsp
1714 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1715 c9956ddf 2019-09-08 stsp NULL);
1716 507dc3bb 2018-12-29 stsp if (error != NULL)
1717 507dc3bb 2018-12-29 stsp goto done;
1718 507dc3bb 2018-12-29 stsp
1719 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1720 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1721 087fb88c 2019-08-04 stsp if (error)
1722 087fb88c 2019-08-04 stsp goto done;
1723 087fb88c 2019-08-04 stsp
1724 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1725 0266afb7 2019-01-04 stsp if (error)
1726 0266afb7 2019-01-04 stsp goto done;
1727 0266afb7 2019-01-04 stsp
1728 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1729 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1730 024e9686 2019-05-14 stsp if (error != NULL)
1731 024e9686 2019-05-14 stsp goto done;
1732 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1733 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1734 9c4b8182 2019-01-02 stsp if (error != NULL)
1735 9c4b8182 2019-01-02 stsp goto done;
1736 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1737 507dc3bb 2018-12-29 stsp if (error != NULL)
1738 507dc3bb 2018-12-29 stsp goto done;
1739 507dc3bb 2018-12-29 stsp } else {
1740 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1741 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1742 04f57cb3 2019-07-25 stsp free(commit_id_str);
1743 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1744 30837e32 2019-07-25 stsp if (error)
1745 a297e751 2019-07-11 stsp goto done;
1746 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1747 a297e751 2019-07-11 stsp if (error)
1748 507dc3bb 2018-12-29 stsp goto done;
1749 507dc3bb 2018-12-29 stsp }
1750 35c965b2 2018-12-29 stsp
1751 a1fb16d8 2019-05-24 stsp if (branch_name) {
1752 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1753 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1754 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1755 f2ea84fa 2019-07-27 stsp continue;
1756 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1757 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1758 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1759 024e9686 2019-05-14 stsp goto done;
1760 024e9686 2019-05-14 stsp }
1761 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1762 024e9686 2019-05-14 stsp if (error)
1763 024e9686 2019-05-14 stsp goto done;
1764 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1765 3aef623b 2019-10-15 stsp repo);
1766 a367ff0f 2019-05-14 stsp free(head_commit_id);
1767 024e9686 2019-05-14 stsp if (error != NULL)
1768 024e9686 2019-05-14 stsp goto done;
1769 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1770 a367ff0f 2019-05-14 stsp if (error)
1771 a367ff0f 2019-05-14 stsp goto done;
1772 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1773 024e9686 2019-05-14 stsp if (error)
1774 024e9686 2019-05-14 stsp goto done;
1775 024e9686 2019-05-14 stsp } else {
1776 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1777 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1778 a367ff0f 2019-05-14 stsp if (error != NULL) {
1779 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1780 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1781 024e9686 2019-05-14 stsp goto done;
1782 a367ff0f 2019-05-14 stsp }
1783 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1784 a367ff0f 2019-05-14 stsp if (error)
1785 a367ff0f 2019-05-14 stsp goto done;
1786 024e9686 2019-05-14 stsp }
1787 507dc3bb 2018-12-29 stsp
1788 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1789 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1790 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1791 507dc3bb 2018-12-29 stsp commit_id);
1792 507dc3bb 2018-12-29 stsp if (error)
1793 507dc3bb 2018-12-29 stsp goto done;
1794 507dc3bb 2018-12-29 stsp }
1795 507dc3bb 2018-12-29 stsp
1796 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1797 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1798 507dc3bb 2018-12-29 stsp if (error != NULL)
1799 507dc3bb 2018-12-29 stsp goto done;
1800 9c4b8182 2019-01-02 stsp
1801 784955db 2019-01-12 stsp if (did_something)
1802 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1803 784955db 2019-01-12 stsp else
1804 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1805 507dc3bb 2018-12-29 stsp done:
1806 c09a553d 2018-03-12 stsp free(worktree_path);
1807 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1808 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1809 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1810 507dc3bb 2018-12-29 stsp free(commit_id);
1811 9c4b8182 2019-01-02 stsp free(commit_id_str);
1812 c09a553d 2018-03-12 stsp return error;
1813 c09a553d 2018-03-12 stsp }
1814 c09a553d 2018-03-12 stsp
1815 f42b1b34 2018-03-12 stsp static const struct got_error *
1816 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1817 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1818 63035f9f 2019-10-06 stsp struct got_repository *repo)
1819 5c860e29 2018-03-12 stsp {
1820 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1821 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1822 79109fed 2018-03-27 stsp
1823 44392932 2019-08-25 stsp if (blob_id1) {
1824 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1825 44392932 2019-08-25 stsp if (err)
1826 44392932 2019-08-25 stsp goto done;
1827 44392932 2019-08-25 stsp }
1828 79109fed 2018-03-27 stsp
1829 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1830 44392932 2019-08-25 stsp if (err)
1831 44392932 2019-08-25 stsp goto done;
1832 79109fed 2018-03-27 stsp
1833 44392932 2019-08-25 stsp while (path[0] == '/')
1834 44392932 2019-08-25 stsp path++;
1835 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1836 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1837 44392932 2019-08-25 stsp done:
1838 44392932 2019-08-25 stsp if (blob1)
1839 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1840 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1841 44392932 2019-08-25 stsp return err;
1842 44392932 2019-08-25 stsp }
1843 44392932 2019-08-25 stsp
1844 44392932 2019-08-25 stsp static const struct got_error *
1845 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1846 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1847 63035f9f 2019-10-06 stsp struct got_repository *repo)
1848 44392932 2019-08-25 stsp {
1849 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1850 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1852 44392932 2019-08-25 stsp
1853 44392932 2019-08-25 stsp if (tree_id1) {
1854 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1855 79109fed 2018-03-27 stsp if (err)
1856 44392932 2019-08-25 stsp goto done;
1857 79109fed 2018-03-27 stsp }
1858 79109fed 2018-03-27 stsp
1859 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1860 0f2b3dca 2018-12-22 stsp if (err)
1861 0f2b3dca 2018-12-22 stsp goto done;
1862 0f2b3dca 2018-12-22 stsp
1863 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1864 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1865 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1866 44392932 2019-08-25 stsp while (path[0] == '/')
1867 44392932 2019-08-25 stsp path++;
1868 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1869 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1870 0f2b3dca 2018-12-22 stsp done:
1871 79109fed 2018-03-27 stsp if (tree1)
1872 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1873 366e0a5f 2019-10-10 stsp if (tree2)
1874 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1875 44392932 2019-08-25 stsp return err;
1876 44392932 2019-08-25 stsp }
1877 44392932 2019-08-25 stsp
1878 44392932 2019-08-25 stsp static const struct got_error *
1879 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1880 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1881 44392932 2019-08-25 stsp {
1882 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1883 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1884 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1885 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1886 44392932 2019-08-25 stsp struct got_object_qid *qid;
1887 44392932 2019-08-25 stsp
1888 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1889 44392932 2019-08-25 stsp if (qid != NULL) {
1890 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1891 44392932 2019-08-25 stsp qid->id);
1892 44392932 2019-08-25 stsp if (err)
1893 44392932 2019-08-25 stsp return err;
1894 44392932 2019-08-25 stsp }
1895 44392932 2019-08-25 stsp
1896 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1897 44392932 2019-08-25 stsp int obj_type;
1898 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1899 44392932 2019-08-25 stsp if (err)
1900 44392932 2019-08-25 stsp goto done;
1901 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1902 44392932 2019-08-25 stsp if (err) {
1903 44392932 2019-08-25 stsp free(obj_id2);
1904 44392932 2019-08-25 stsp goto done;
1905 44392932 2019-08-25 stsp }
1906 44392932 2019-08-25 stsp if (pcommit) {
1907 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1908 44392932 2019-08-25 stsp qid->id, path);
1909 44392932 2019-08-25 stsp if (err) {
1910 44392932 2019-08-25 stsp free(obj_id2);
1911 44392932 2019-08-25 stsp goto done;
1912 44392932 2019-08-25 stsp }
1913 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1914 44392932 2019-08-25 stsp if (err) {
1915 44392932 2019-08-25 stsp free(obj_id2);
1916 44392932 2019-08-25 stsp goto done;
1917 44392932 2019-08-25 stsp }
1918 44392932 2019-08-25 stsp }
1919 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1920 44392932 2019-08-25 stsp if (err) {
1921 44392932 2019-08-25 stsp free(obj_id2);
1922 44392932 2019-08-25 stsp goto done;
1923 44392932 2019-08-25 stsp }
1924 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1925 44392932 2019-08-25 stsp switch (obj_type) {
1926 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1927 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1928 63035f9f 2019-10-06 stsp 0, repo);
1929 44392932 2019-08-25 stsp break;
1930 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1931 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1932 63035f9f 2019-10-06 stsp 0, repo);
1933 44392932 2019-08-25 stsp break;
1934 44392932 2019-08-25 stsp default:
1935 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1936 44392932 2019-08-25 stsp break;
1937 44392932 2019-08-25 stsp }
1938 44392932 2019-08-25 stsp free(obj_id1);
1939 44392932 2019-08-25 stsp free(obj_id2);
1940 44392932 2019-08-25 stsp } else {
1941 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1942 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1943 44392932 2019-08-25 stsp if (err)
1944 44392932 2019-08-25 stsp goto done;
1945 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1946 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1947 44392932 2019-08-25 stsp if (err)
1948 44392932 2019-08-25 stsp goto done;
1949 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1950 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1951 44392932 2019-08-25 stsp }
1952 44392932 2019-08-25 stsp done:
1953 0f2b3dca 2018-12-22 stsp free(id_str1);
1954 0f2b3dca 2018-12-22 stsp free(id_str2);
1955 44392932 2019-08-25 stsp if (pcommit)
1956 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1957 79109fed 2018-03-27 stsp return err;
1958 79109fed 2018-03-27 stsp }
1959 79109fed 2018-03-27 stsp
1960 4bb494d5 2018-06-16 stsp static char *
1961 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1962 6c281f94 2018-06-11 stsp {
1963 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1964 09867e48 2019-08-13 stsp char *p, *s;
1965 09867e48 2019-08-13 stsp
1966 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1967 09867e48 2019-08-13 stsp if (tm == NULL)
1968 09867e48 2019-08-13 stsp return NULL;
1969 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1970 09867e48 2019-08-13 stsp if (s == NULL)
1971 09867e48 2019-08-13 stsp return NULL;
1972 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1973 6c281f94 2018-06-11 stsp if (p)
1974 6c281f94 2018-06-11 stsp *p = '\0';
1975 6c281f94 2018-06-11 stsp return s;
1976 6c281f94 2018-06-11 stsp }
1977 dc424a06 2019-08-07 stsp
1978 6841bf13 2019-11-29 kn static const struct got_error *
1979 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1980 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1981 6841bf13 2019-11-29 kn {
1982 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1983 6841bf13 2019-11-29 kn regmatch_t regmatch;
1984 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1985 6841bf13 2019-11-29 kn
1986 6841bf13 2019-11-29 kn *have_match = 0;
1987 6841bf13 2019-11-29 kn
1988 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1989 6841bf13 2019-11-29 kn if (err)
1990 6841bf13 2019-11-29 kn return err;
1991 6841bf13 2019-11-29 kn
1992 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1993 6841bf13 2019-11-29 kn if (err)
1994 6841bf13 2019-11-29 kn goto done;
1995 6841bf13 2019-11-29 kn
1996 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1997 6841bf13 2019-11-29 kn *have_match = 1;
1998 6841bf13 2019-11-29 kn done:
1999 6841bf13 2019-11-29 kn free(id_str);
2000 6841bf13 2019-11-29 kn free(logmsg);
2001 6841bf13 2019-11-29 kn return err;
2002 6841bf13 2019-11-29 kn }
2003 6841bf13 2019-11-29 kn
2004 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2005 6c281f94 2018-06-11 stsp
2006 79109fed 2018-03-27 stsp static const struct got_error *
2007 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2008 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2009 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2010 79109fed 2018-03-27 stsp {
2011 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2012 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2013 6c281f94 2018-06-11 stsp char datebuf[26];
2014 45d799e2 2018-12-23 stsp time_t committer_time;
2015 45d799e2 2018-12-23 stsp const char *author, *committer;
2016 199a4027 2019-02-02 stsp char *refs_str = NULL;
2017 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2018 5c860e29 2018-03-12 stsp
2019 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2020 199a4027 2019-02-02 stsp char *s;
2021 199a4027 2019-02-02 stsp const char *name;
2022 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2023 a436ad14 2019-08-13 stsp int cmp;
2024 a436ad14 2019-08-13 stsp
2025 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2026 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2027 d9498b20 2019-02-05 stsp continue;
2028 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2029 199a4027 2019-02-02 stsp name += 5;
2030 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2031 7143d404 2019-03-12 stsp continue;
2032 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2033 e34f9ed6 2019-02-02 stsp name += 6;
2034 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2035 141c2bff 2019-02-04 stsp name += 8;
2036 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2037 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2038 5d844a1e 2019-08-13 stsp if (err) {
2039 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2040 5d844a1e 2019-08-13 stsp return err;
2041 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2042 5d844a1e 2019-08-13 stsp err = NULL;
2043 5d844a1e 2019-08-13 stsp tag = NULL;
2044 5d844a1e 2019-08-13 stsp }
2045 a436ad14 2019-08-13 stsp }
2046 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2047 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2048 a436ad14 2019-08-13 stsp if (tag)
2049 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2050 a436ad14 2019-08-13 stsp if (cmp != 0)
2051 a436ad14 2019-08-13 stsp continue;
2052 199a4027 2019-02-02 stsp s = refs_str;
2053 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2054 199a4027 2019-02-02 stsp name) == -1) {
2055 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2056 199a4027 2019-02-02 stsp free(s);
2057 34ca4898 2019-08-13 stsp return err;
2058 199a4027 2019-02-02 stsp }
2059 199a4027 2019-02-02 stsp free(s);
2060 199a4027 2019-02-02 stsp }
2061 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2062 8bf5b3c9 2018-03-17 stsp if (err)
2063 8bf5b3c9 2018-03-17 stsp return err;
2064 788c352e 2018-06-16 stsp
2065 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2066 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2067 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2068 832c249c 2018-06-10 stsp free(id_str);
2069 d3d493d7 2019-02-21 stsp id_str = NULL;
2070 d3d493d7 2019-02-21 stsp free(refs_str);
2071 d3d493d7 2019-02-21 stsp refs_str = NULL;
2072 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2073 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2074 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2075 09867e48 2019-08-13 stsp if (datestr)
2076 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2077 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2078 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2079 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2080 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2081 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2082 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2083 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2084 3fe1abad 2018-06-10 stsp int n = 1;
2085 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2086 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2087 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2088 a0603db2 2018-06-10 stsp if (err)
2089 a0603db2 2018-06-10 stsp return err;
2090 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2091 a0603db2 2018-06-10 stsp free(id_str);
2092 a0603db2 2018-06-10 stsp }
2093 a0603db2 2018-06-10 stsp }
2094 832c249c 2018-06-10 stsp
2095 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2096 5943eee2 2019-08-13 stsp if (err)
2097 5943eee2 2019-08-13 stsp return err;
2098 8bf5b3c9 2018-03-17 stsp
2099 621015ac 2018-07-23 stsp logmsg = logmsg0;
2100 832c249c 2018-06-10 stsp do {
2101 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2102 832c249c 2018-06-10 stsp if (line)
2103 832c249c 2018-06-10 stsp printf(" %s\n", line);
2104 832c249c 2018-06-10 stsp } while (line);
2105 621015ac 2018-07-23 stsp free(logmsg0);
2106 832c249c 2018-06-10 stsp
2107 971751ac 2018-03-27 stsp if (show_patch) {
2108 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2109 971751ac 2018-03-27 stsp if (err == 0)
2110 971751ac 2018-03-27 stsp printf("\n");
2111 971751ac 2018-03-27 stsp }
2112 07862c20 2018-09-15 stsp
2113 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2114 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2115 07862c20 2018-09-15 stsp return err;
2116 f42b1b34 2018-03-12 stsp }
2117 5c860e29 2018-03-12 stsp
2118 f42b1b34 2018-03-12 stsp static const struct got_error *
2119 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2120 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2121 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2122 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2123 f42b1b34 2018-03-12 stsp {
2124 f42b1b34 2018-03-12 stsp const struct got_error *err;
2125 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2126 6841bf13 2019-11-29 kn regex_t regex;
2127 6841bf13 2019-11-29 kn int have_match;
2128 372ccdbb 2018-06-10 stsp
2129 6841bf13 2019-11-29 kn if (search_pattern &&
2130 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2131 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2132 6841bf13 2019-11-29 kn
2133 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2134 f42b1b34 2018-03-12 stsp if (err)
2135 f42b1b34 2018-03-12 stsp return err;
2136 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2137 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2138 372ccdbb 2018-06-10 stsp if (err)
2139 fcc85cad 2018-07-22 stsp goto done;
2140 656b1f76 2019-05-11 jcs for (;;) {
2141 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2142 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2143 8bf5b3c9 2018-03-17 stsp
2144 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2145 84453469 2018-11-11 stsp break;
2146 84453469 2018-11-11 stsp
2147 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2148 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2149 372ccdbb 2018-06-10 stsp if (err) {
2150 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2151 9ba79e04 2018-06-11 stsp err = NULL;
2152 ee780d5c 2020-01-04 stsp break;
2153 7e665116 2018-04-02 stsp }
2154 b43fbaa0 2018-06-11 stsp if (id == NULL)
2155 7e665116 2018-04-02 stsp break;
2156 b43fbaa0 2018-06-11 stsp
2157 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2158 b43fbaa0 2018-06-11 stsp if (err)
2159 fcc85cad 2018-07-22 stsp break;
2160 6841bf13 2019-11-29 kn
2161 6841bf13 2019-11-29 kn if (search_pattern) {
2162 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2163 6841bf13 2019-11-29 kn if (err) {
2164 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2165 6841bf13 2019-11-29 kn break;
2166 6841bf13 2019-11-29 kn }
2167 6841bf13 2019-11-29 kn if (have_match == 0) {
2168 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2169 6841bf13 2019-11-29 kn continue;
2170 6841bf13 2019-11-29 kn }
2171 6841bf13 2019-11-29 kn }
2172 6841bf13 2019-11-29 kn
2173 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2174 44392932 2019-08-25 stsp diff_context, refs);
2175 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2176 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2177 7e665116 2018-04-02 stsp break;
2178 31cedeaf 2018-09-15 stsp }
2179 fcc85cad 2018-07-22 stsp done:
2180 6841bf13 2019-11-29 kn if (search_pattern)
2181 6841bf13 2019-11-29 kn regfree(&regex);
2182 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2183 f42b1b34 2018-03-12 stsp return err;
2184 f42b1b34 2018-03-12 stsp }
2185 5c860e29 2018-03-12 stsp
2186 4ed7e80c 2018-05-20 stsp __dead static void
2187 6f3d1eb0 2018-03-12 stsp usage_log(void)
2188 6f3d1eb0 2018-03-12 stsp {
2189 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2190 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2191 6f3d1eb0 2018-03-12 stsp exit(1);
2192 b1ebc001 2019-08-13 stsp }
2193 b1ebc001 2019-08-13 stsp
2194 b1ebc001 2019-08-13 stsp static int
2195 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2196 b1ebc001 2019-08-13 stsp {
2197 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2198 b1ebc001 2019-08-13 stsp long long n;
2199 b1ebc001 2019-08-13 stsp const char *errstr;
2200 b1ebc001 2019-08-13 stsp
2201 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2202 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2203 b1ebc001 2019-08-13 stsp return 0;
2204 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2205 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2206 b1ebc001 2019-08-13 stsp return 0;
2207 b1ebc001 2019-08-13 stsp return n;
2208 6f3d1eb0 2018-03-12 stsp }
2209 6f3d1eb0 2018-03-12 stsp
2210 4ed7e80c 2018-05-20 stsp static const struct got_error *
2211 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2212 f42b1b34 2018-03-12 stsp {
2213 f42b1b34 2018-03-12 stsp const struct got_error *error;
2214 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2215 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2216 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2217 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2218 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2219 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2220 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2221 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2222 64a96a6d 2018-04-01 stsp const char *errstr;
2223 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2224 1b3893a2 2019-03-18 stsp
2225 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2226 5c860e29 2018-03-12 stsp
2227 6715a751 2018-03-16 stsp #ifndef PROFILE
2228 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2229 6098196c 2019-01-04 stsp NULL)
2230 ad242220 2018-09-08 stsp == -1)
2231 f42b1b34 2018-03-12 stsp err(1, "pledge");
2232 6715a751 2018-03-16 stsp #endif
2233 79109fed 2018-03-27 stsp
2234 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2235 b1ebc001 2019-08-13 stsp
2236 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2237 79109fed 2018-03-27 stsp switch (ch) {
2238 79109fed 2018-03-27 stsp case 'p':
2239 79109fed 2018-03-27 stsp show_patch = 1;
2240 d142fc45 2018-04-01 stsp break;
2241 d142fc45 2018-04-01 stsp case 'c':
2242 d142fc45 2018-04-01 stsp start_commit = optarg;
2243 64a96a6d 2018-04-01 stsp break;
2244 c0cc5c62 2018-10-18 stsp case 'C':
2245 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2246 4a8520aa 2018-10-18 stsp &errstr);
2247 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2248 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2249 c0cc5c62 2018-10-18 stsp break;
2250 64a96a6d 2018-04-01 stsp case 'l':
2251 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2252 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2253 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2254 cc54c501 2019-07-15 stsp break;
2255 48c8c60d 2020-01-27 stsp case 'b':
2256 48c8c60d 2020-01-27 stsp log_branches = 1;
2257 a0603db2 2018-06-10 stsp break;
2258 04ca23f4 2018-07-16 stsp case 'r':
2259 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2260 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2261 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2262 9ba1d308 2019-10-21 stsp optarg);
2263 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2264 04ca23f4 2018-07-16 stsp break;
2265 6841bf13 2019-11-29 kn case 's':
2266 6841bf13 2019-11-29 kn search_pattern = optarg;
2267 6841bf13 2019-11-29 kn break;
2268 79109fed 2018-03-27 stsp default:
2269 2deda0b9 2019-03-07 stsp usage_log();
2270 79109fed 2018-03-27 stsp /* NOTREACHED */
2271 79109fed 2018-03-27 stsp }
2272 79109fed 2018-03-27 stsp }
2273 79109fed 2018-03-27 stsp
2274 79109fed 2018-03-27 stsp argc -= optind;
2275 79109fed 2018-03-27 stsp argv += optind;
2276 f42b1b34 2018-03-12 stsp
2277 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2278 dc1edbfa 2019-11-29 kn diff_context = 3;
2279 dc1edbfa 2019-11-29 kn else if (!show_patch)
2280 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2281 dc1edbfa 2019-11-29 kn
2282 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2283 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2284 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2285 04ca23f4 2018-07-16 stsp goto done;
2286 04ca23f4 2018-07-16 stsp }
2287 cffc0aa4 2019-02-05 stsp
2288 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2289 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2290 cffc0aa4 2019-02-05 stsp goto done;
2291 cffc0aa4 2019-02-05 stsp error = NULL;
2292 cffc0aa4 2019-02-05 stsp
2293 e7301579 2019-03-18 stsp if (argc == 0) {
2294 cbd1af7a 2019-03-18 stsp path = strdup("");
2295 e7301579 2019-03-18 stsp if (path == NULL) {
2296 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2297 cbd1af7a 2019-03-18 stsp goto done;
2298 e7301579 2019-03-18 stsp }
2299 e7301579 2019-03-18 stsp } else if (argc == 1) {
2300 e7301579 2019-03-18 stsp if (worktree) {
2301 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2302 e7301579 2019-03-18 stsp argv[0]);
2303 e7301579 2019-03-18 stsp if (error)
2304 e7301579 2019-03-18 stsp goto done;
2305 e7301579 2019-03-18 stsp } else {
2306 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2307 e7301579 2019-03-18 stsp if (path == NULL) {
2308 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2309 e7301579 2019-03-18 stsp goto done;
2310 e7301579 2019-03-18 stsp }
2311 e7301579 2019-03-18 stsp }
2312 cbd1af7a 2019-03-18 stsp } else
2313 cbd1af7a 2019-03-18 stsp usage_log();
2314 cbd1af7a 2019-03-18 stsp
2315 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2316 5486daa2 2019-05-11 stsp repo_path = worktree ?
2317 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2318 5486daa2 2019-05-11 stsp }
2319 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2320 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2321 cffc0aa4 2019-02-05 stsp goto done;
2322 04ca23f4 2018-07-16 stsp }
2323 6098196c 2019-01-04 stsp
2324 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2325 d7d4f210 2018-03-12 stsp if (error != NULL)
2326 04ca23f4 2018-07-16 stsp goto done;
2327 f42b1b34 2018-03-12 stsp
2328 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2329 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2330 c02c541e 2019-03-29 stsp if (error)
2331 c02c541e 2019-03-29 stsp goto done;
2332 c02c541e 2019-03-29 stsp
2333 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2334 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2335 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2336 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2337 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2338 3235492e 2018-04-01 stsp if (error != NULL)
2339 3235492e 2018-04-01 stsp return error;
2340 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2341 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2342 3235492e 2018-04-01 stsp if (error != NULL)
2343 3235492e 2018-04-01 stsp return error;
2344 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2345 3235492e 2018-04-01 stsp } else {
2346 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2347 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2348 3235492e 2018-04-01 stsp if (error == NULL) {
2349 1caad61b 2019-02-01 stsp int obj_type;
2350 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2351 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2352 d1f2edc9 2018-06-13 stsp if (error != NULL)
2353 1caad61b 2019-02-01 stsp goto done;
2354 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2355 1caad61b 2019-02-01 stsp if (error != NULL)
2356 1caad61b 2019-02-01 stsp goto done;
2357 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2358 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2359 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2360 1caad61b 2019-02-01 stsp if (error != NULL)
2361 1caad61b 2019-02-01 stsp goto done;
2362 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2363 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2364 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2365 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2366 1caad61b 2019-02-01 stsp goto done;
2367 1caad61b 2019-02-01 stsp }
2368 1caad61b 2019-02-01 stsp free(id);
2369 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2370 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2371 1caad61b 2019-02-01 stsp if (id == NULL)
2372 638f9024 2019-05-13 stsp error = got_error_from_errno(
2373 230a42bd 2019-05-11 jcs "got_object_id_dup");
2374 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2375 1caad61b 2019-02-01 stsp if (error)
2376 1caad61b 2019-02-01 stsp goto done;
2377 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2378 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2379 1caad61b 2019-02-01 stsp goto done;
2380 1caad61b 2019-02-01 stsp }
2381 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2382 d1f2edc9 2018-06-13 stsp if (error != NULL)
2383 1caad61b 2019-02-01 stsp goto done;
2384 d1f2edc9 2018-06-13 stsp }
2385 15a94983 2018-12-23 stsp if (commit == NULL) {
2386 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2387 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2388 d1f2edc9 2018-06-13 stsp if (error != NULL)
2389 d1f2edc9 2018-06-13 stsp return error;
2390 3235492e 2018-04-01 stsp }
2391 3235492e 2018-04-01 stsp }
2392 d7d4f210 2018-03-12 stsp if (error != NULL)
2393 04ca23f4 2018-07-16 stsp goto done;
2394 04ca23f4 2018-07-16 stsp
2395 deeabeae 2019-08-27 stsp if (worktree) {
2396 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2397 deeabeae 2019-08-27 stsp char *p;
2398 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2399 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2400 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2401 deeabeae 2019-08-27 stsp goto done;
2402 deeabeae 2019-08-27 stsp }
2403 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2404 deeabeae 2019-08-27 stsp free(p);
2405 deeabeae 2019-08-27 stsp } else
2406 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2407 04ca23f4 2018-07-16 stsp if (error != NULL)
2408 04ca23f4 2018-07-16 stsp goto done;
2409 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2410 04ca23f4 2018-07-16 stsp free(path);
2411 04ca23f4 2018-07-16 stsp path = in_repo_path;
2412 04ca23f4 2018-07-16 stsp }
2413 199a4027 2019-02-02 stsp
2414 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2415 199a4027 2019-02-02 stsp if (error)
2416 199a4027 2019-02-02 stsp goto done;
2417 04ca23f4 2018-07-16 stsp
2418 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2419 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2420 04ca23f4 2018-07-16 stsp done:
2421 04ca23f4 2018-07-16 stsp free(path);
2422 04ca23f4 2018-07-16 stsp free(repo_path);
2423 04ca23f4 2018-07-16 stsp free(cwd);
2424 f42b1b34 2018-03-12 stsp free(id);
2425 cffc0aa4 2019-02-05 stsp if (worktree)
2426 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2427 ad242220 2018-09-08 stsp if (repo) {
2428 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2429 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2430 ad242220 2018-09-08 stsp if (error == NULL)
2431 ad242220 2018-09-08 stsp error = repo_error;
2432 ad242220 2018-09-08 stsp }
2433 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2434 8bf5b3c9 2018-03-17 stsp return error;
2435 5c860e29 2018-03-12 stsp }
2436 5c860e29 2018-03-12 stsp
2437 4ed7e80c 2018-05-20 stsp __dead static void
2438 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2439 3f8b7d6a 2018-04-01 stsp {
2440 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2441 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2442 3f8b7d6a 2018-04-01 stsp exit(1);
2443 b00d56cd 2018-04-01 stsp }
2444 b00d56cd 2018-04-01 stsp
2445 b72f483a 2019-02-05 stsp struct print_diff_arg {
2446 b72f483a 2019-02-05 stsp struct got_repository *repo;
2447 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2448 b72f483a 2019-02-05 stsp int diff_context;
2449 3fc0c068 2019-02-10 stsp const char *id_str;
2450 3fc0c068 2019-02-10 stsp int header_shown;
2451 98eaaa12 2019-08-03 stsp int diff_staged;
2452 63035f9f 2019-10-06 stsp int ignore_whitespace;
2453 b72f483a 2019-02-05 stsp };
2454 b72f483a 2019-02-05 stsp
2455 4ed7e80c 2018-05-20 stsp static const struct got_error *
2456 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2457 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2458 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2459 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2460 b72f483a 2019-02-05 stsp {
2461 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2462 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2463 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2464 12463d8b 2019-12-13 stsp int fd = -1;
2465 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2466 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2467 b72f483a 2019-02-05 stsp struct stat sb;
2468 b72f483a 2019-02-05 stsp
2469 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2470 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2471 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2472 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2473 98eaaa12 2019-08-03 stsp return NULL;
2474 98eaaa12 2019-08-03 stsp } else {
2475 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2476 98eaaa12 2019-08-03 stsp return NULL;
2477 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2478 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2479 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2480 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2481 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2482 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2483 98eaaa12 2019-08-03 stsp return NULL;
2484 98eaaa12 2019-08-03 stsp }
2485 3fc0c068 2019-02-10 stsp
2486 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2487 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2488 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2489 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2490 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2491 3fc0c068 2019-02-10 stsp }
2492 b72f483a 2019-02-05 stsp
2493 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2494 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2495 98eaaa12 2019-08-03 stsp switch (staged_status) {
2496 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2497 98eaaa12 2019-08-03 stsp label1 = path;
2498 98eaaa12 2019-08-03 stsp label2 = path;
2499 98eaaa12 2019-08-03 stsp break;
2500 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2501 98eaaa12 2019-08-03 stsp label2 = path;
2502 98eaaa12 2019-08-03 stsp break;
2503 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2504 98eaaa12 2019-08-03 stsp label1 = path;
2505 98eaaa12 2019-08-03 stsp break;
2506 98eaaa12 2019-08-03 stsp default:
2507 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2508 98eaaa12 2019-08-03 stsp }
2509 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2510 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2511 63035f9f 2019-10-06 stsp a->repo, stdout);
2512 98eaaa12 2019-08-03 stsp }
2513 98eaaa12 2019-08-03 stsp
2514 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2515 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2516 4ce46740 2019-08-08 stsp char *id_str;
2517 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2518 408b4ebc 2019-08-03 stsp 8192);
2519 4ce46740 2019-08-08 stsp if (err)
2520 4ce46740 2019-08-08 stsp goto done;
2521 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2522 4ce46740 2019-08-08 stsp if (err)
2523 4ce46740 2019-08-08 stsp goto done;
2524 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2525 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2526 4ce46740 2019-08-08 stsp free(id_str);
2527 4ce46740 2019-08-08 stsp goto done;
2528 4ce46740 2019-08-08 stsp }
2529 4ce46740 2019-08-08 stsp free(id_str);
2530 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2531 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2532 4ce46740 2019-08-08 stsp if (err)
2533 4ce46740 2019-08-08 stsp goto done;
2534 4ce46740 2019-08-08 stsp }
2535 d00136be 2019-03-26 stsp
2536 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2537 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2538 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2539 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2540 2ec1f75b 2019-03-26 stsp goto done;
2541 2ec1f75b 2019-03-26 stsp }
2542 b72f483a 2019-02-05 stsp
2543 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2544 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2545 12463d8b 2019-12-13 stsp if (fd == -1) {
2546 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2547 12463d8b 2019-12-13 stsp goto done;
2548 12463d8b 2019-12-13 stsp }
2549 12463d8b 2019-12-13 stsp } else {
2550 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2551 12463d8b 2019-12-13 stsp if (fd == -1) {
2552 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2553 12463d8b 2019-12-13 stsp goto done;
2554 12463d8b 2019-12-13 stsp }
2555 12463d8b 2019-12-13 stsp }
2556 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2557 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2558 2ec1f75b 2019-03-26 stsp goto done;
2559 2ec1f75b 2019-03-26 stsp }
2560 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2561 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2562 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2563 2ec1f75b 2019-03-26 stsp goto done;
2564 2ec1f75b 2019-03-26 stsp }
2565 12463d8b 2019-12-13 stsp fd = -1;
2566 2ec1f75b 2019-03-26 stsp } else
2567 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2568 b72f483a 2019-02-05 stsp
2569 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2570 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2571 b72f483a 2019-02-05 stsp done:
2572 b72f483a 2019-02-05 stsp if (blob1)
2573 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2574 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2575 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2576 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2577 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2578 b72f483a 2019-02-05 stsp free(abspath);
2579 927df6b7 2019-02-10 stsp return err;
2580 927df6b7 2019-02-10 stsp }
2581 927df6b7 2019-02-10 stsp
2582 927df6b7 2019-02-10 stsp static const struct got_error *
2583 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2584 b00d56cd 2018-04-01 stsp {
2585 b00d56cd 2018-04-01 stsp const struct got_error *error;
2586 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2587 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2588 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2589 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2590 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2591 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2592 15a94983 2018-12-23 stsp int type1, type2;
2593 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2594 c0cc5c62 2018-10-18 stsp const char *errstr;
2595 927df6b7 2019-02-10 stsp char *path = NULL;
2596 b00d56cd 2018-04-01 stsp
2597 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2598 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2599 25eccc22 2019-01-04 stsp NULL) == -1)
2600 b00d56cd 2018-04-01 stsp err(1, "pledge");
2601 b00d56cd 2018-04-01 stsp #endif
2602 b00d56cd 2018-04-01 stsp
2603 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2604 b00d56cd 2018-04-01 stsp switch (ch) {
2605 c0cc5c62 2018-10-18 stsp case 'C':
2606 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2607 dfcab68b 2019-11-29 kn &errstr);
2608 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2609 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2610 c0cc5c62 2018-10-18 stsp break;
2611 b72f483a 2019-02-05 stsp case 'r':
2612 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2613 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2614 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2615 9ba1d308 2019-10-21 stsp optarg);
2616 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2617 b72f483a 2019-02-05 stsp break;
2618 98eaaa12 2019-08-03 stsp case 's':
2619 98eaaa12 2019-08-03 stsp diff_staged = 1;
2620 63035f9f 2019-10-06 stsp break;
2621 63035f9f 2019-10-06 stsp case 'w':
2622 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2623 98eaaa12 2019-08-03 stsp break;
2624 b00d56cd 2018-04-01 stsp default:
2625 2deda0b9 2019-03-07 stsp usage_diff();
2626 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2627 b00d56cd 2018-04-01 stsp }
2628 b00d56cd 2018-04-01 stsp }
2629 b00d56cd 2018-04-01 stsp
2630 b00d56cd 2018-04-01 stsp argc -= optind;
2631 b00d56cd 2018-04-01 stsp argv += optind;
2632 b00d56cd 2018-04-01 stsp
2633 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2634 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2635 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2636 b72f483a 2019-02-05 stsp goto done;
2637 b72f483a 2019-02-05 stsp }
2638 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2639 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2640 927df6b7 2019-02-10 stsp goto done;
2641 927df6b7 2019-02-10 stsp if (argc <= 1) {
2642 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2643 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2644 927df6b7 2019-02-10 stsp goto done;
2645 927df6b7 2019-02-10 stsp }
2646 b72f483a 2019-02-05 stsp if (repo_path)
2647 b72f483a 2019-02-05 stsp errx(1,
2648 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2649 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2650 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2651 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2652 927df6b7 2019-02-10 stsp goto done;
2653 927df6b7 2019-02-10 stsp }
2654 927df6b7 2019-02-10 stsp if (argc == 1) {
2655 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2656 cbd1af7a 2019-03-18 stsp argv[0]);
2657 927df6b7 2019-02-10 stsp if (error)
2658 927df6b7 2019-02-10 stsp goto done;
2659 927df6b7 2019-02-10 stsp } else {
2660 927df6b7 2019-02-10 stsp path = strdup("");
2661 927df6b7 2019-02-10 stsp if (path == NULL) {
2662 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2663 927df6b7 2019-02-10 stsp goto done;
2664 927df6b7 2019-02-10 stsp }
2665 927df6b7 2019-02-10 stsp }
2666 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2667 98eaaa12 2019-08-03 stsp if (diff_staged)
2668 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2669 98eaaa12 2019-08-03 stsp "objects in repository");
2670 15a94983 2018-12-23 stsp id_str1 = argv[0];
2671 15a94983 2018-12-23 stsp id_str2 = argv[1];
2672 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2673 30db809c 2019-06-05 stsp repo_path =
2674 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2675 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2676 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2677 30db809c 2019-06-05 stsp goto done;
2678 30db809c 2019-06-05 stsp }
2679 30db809c 2019-06-05 stsp }
2680 b00d56cd 2018-04-01 stsp } else
2681 b00d56cd 2018-04-01 stsp usage_diff();
2682 25eccc22 2019-01-04 stsp
2683 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2684 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2685 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2686 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2687 b72f483a 2019-02-05 stsp }
2688 b00d56cd 2018-04-01 stsp
2689 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2690 76089277 2018-04-01 stsp free(repo_path);
2691 b00d56cd 2018-04-01 stsp if (error != NULL)
2692 b00d56cd 2018-04-01 stsp goto done;
2693 b00d56cd 2018-04-01 stsp
2694 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2695 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2696 c02c541e 2019-03-29 stsp if (error)
2697 c02c541e 2019-03-29 stsp goto done;
2698 c02c541e 2019-03-29 stsp
2699 30db809c 2019-06-05 stsp if (argc <= 1) {
2700 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2701 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2702 b72f483a 2019-02-05 stsp char *id_str;
2703 72ea6654 2019-07-27 stsp
2704 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2705 72ea6654 2019-07-27 stsp
2706 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2707 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2708 b72f483a 2019-02-05 stsp if (error)
2709 b72f483a 2019-02-05 stsp goto done;
2710 b72f483a 2019-02-05 stsp arg.repo = repo;
2711 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2712 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2713 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2714 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2715 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2716 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2717 b72f483a 2019-02-05 stsp
2718 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2719 72ea6654 2019-07-27 stsp if (error)
2720 72ea6654 2019-07-27 stsp goto done;
2721 72ea6654 2019-07-27 stsp
2722 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2723 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2724 b72f483a 2019-02-05 stsp free(id_str);
2725 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2726 b72f483a 2019-02-05 stsp goto done;
2727 b72f483a 2019-02-05 stsp }
2728 b72f483a 2019-02-05 stsp
2729 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2730 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2731 0adb7196 2019-08-11 stsp if (error)
2732 0adb7196 2019-08-11 stsp goto done;
2733 b00d56cd 2018-04-01 stsp
2734 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2735 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2736 0adb7196 2019-08-11 stsp if (error)
2737 0adb7196 2019-08-11 stsp goto done;
2738 b00d56cd 2018-04-01 stsp
2739 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2740 15a94983 2018-12-23 stsp if (error)
2741 15a94983 2018-12-23 stsp goto done;
2742 15a94983 2018-12-23 stsp
2743 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2744 15a94983 2018-12-23 stsp if (error)
2745 15a94983 2018-12-23 stsp goto done;
2746 15a94983 2018-12-23 stsp
2747 15a94983 2018-12-23 stsp if (type1 != type2) {
2748 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2749 b00d56cd 2018-04-01 stsp goto done;
2750 b00d56cd 2018-04-01 stsp }
2751 b00d56cd 2018-04-01 stsp
2752 15a94983 2018-12-23 stsp switch (type1) {
2753 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2754 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2755 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2756 b00d56cd 2018-04-01 stsp break;
2757 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2758 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2759 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2760 b00d56cd 2018-04-01 stsp break;
2761 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2762 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2763 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2764 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2765 b00d56cd 2018-04-01 stsp break;
2766 b00d56cd 2018-04-01 stsp default:
2767 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2768 b00d56cd 2018-04-01 stsp }
2769 b00d56cd 2018-04-01 stsp done:
2770 5e70831e 2019-05-28 stsp free(label1);
2771 5e70831e 2019-05-28 stsp free(label2);
2772 15a94983 2018-12-23 stsp free(id1);
2773 15a94983 2018-12-23 stsp free(id2);
2774 927df6b7 2019-02-10 stsp free(path);
2775 b72f483a 2019-02-05 stsp if (worktree)
2776 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2777 ad242220 2018-09-08 stsp if (repo) {
2778 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2779 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2780 ad242220 2018-09-08 stsp if (error == NULL)
2781 ad242220 2018-09-08 stsp error = repo_error;
2782 ad242220 2018-09-08 stsp }
2783 b00d56cd 2018-04-01 stsp return error;
2784 404c43c4 2018-06-21 stsp }
2785 404c43c4 2018-06-21 stsp
2786 404c43c4 2018-06-21 stsp __dead static void
2787 404c43c4 2018-06-21 stsp usage_blame(void)
2788 404c43c4 2018-06-21 stsp {
2789 9270e621 2019-02-05 stsp fprintf(stderr,
2790 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2791 404c43c4 2018-06-21 stsp getprogname());
2792 404c43c4 2018-06-21 stsp exit(1);
2793 b00d56cd 2018-04-01 stsp }
2794 b00d56cd 2018-04-01 stsp
2795 28315671 2019-08-14 stsp struct blame_line {
2796 28315671 2019-08-14 stsp int annotated;
2797 28315671 2019-08-14 stsp char *id_str;
2798 82f6abb8 2019-08-14 stsp char *committer;
2799 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2800 28315671 2019-08-14 stsp };
2801 28315671 2019-08-14 stsp
2802 28315671 2019-08-14 stsp struct blame_cb_args {
2803 28315671 2019-08-14 stsp struct blame_line *lines;
2804 28315671 2019-08-14 stsp int nlines;
2805 7ef28ff8 2019-08-14 stsp int nlines_prec;
2806 28315671 2019-08-14 stsp int lineno_cur;
2807 28315671 2019-08-14 stsp off_t *line_offsets;
2808 28315671 2019-08-14 stsp FILE *f;
2809 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2810 28315671 2019-08-14 stsp };
2811 28315671 2019-08-14 stsp
2812 404c43c4 2018-06-21 stsp static const struct got_error *
2813 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2814 28315671 2019-08-14 stsp {
2815 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2816 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2817 28315671 2019-08-14 stsp struct blame_line *bline;
2818 82f6abb8 2019-08-14 stsp char *line = NULL;
2819 28315671 2019-08-14 stsp size_t linesize = 0;
2820 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2821 28315671 2019-08-14 stsp off_t offset;
2822 bcb49d15 2019-08-14 stsp struct tm tm;
2823 bcb49d15 2019-08-14 stsp time_t committer_time;
2824 28315671 2019-08-14 stsp
2825 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2826 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2827 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2828 28315671 2019-08-14 stsp
2829 28315671 2019-08-14 stsp if (sigint_received)
2830 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2831 28315671 2019-08-14 stsp
2832 2839f8b9 2019-08-18 stsp if (lineno == -1)
2833 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2834 2839f8b9 2019-08-18 stsp
2835 28315671 2019-08-14 stsp /* Annotate this line. */
2836 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2837 28315671 2019-08-14 stsp if (bline->annotated)
2838 28315671 2019-08-14 stsp return NULL;
2839 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2840 28315671 2019-08-14 stsp if (err)
2841 28315671 2019-08-14 stsp return err;
2842 82f6abb8 2019-08-14 stsp
2843 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2844 82f6abb8 2019-08-14 stsp if (err)
2845 82f6abb8 2019-08-14 stsp goto done;
2846 82f6abb8 2019-08-14 stsp
2847 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2848 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2849 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2850 bcb49d15 2019-08-14 stsp goto done;
2851 bcb49d15 2019-08-14 stsp }
2852 bcb49d15 2019-08-14 stsp
2853 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2854 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2855 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2856 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2857 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2858 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2859 82f6abb8 2019-08-14 stsp goto done;
2860 82f6abb8 2019-08-14 stsp }
2861 28315671 2019-08-14 stsp bline->annotated = 1;
2862 28315671 2019-08-14 stsp
2863 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2864 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2865 28315671 2019-08-14 stsp if (!bline->annotated)
2866 82f6abb8 2019-08-14 stsp goto done;
2867 28315671 2019-08-14 stsp
2868 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2869 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2870 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2871 82f6abb8 2019-08-14 stsp goto done;
2872 82f6abb8 2019-08-14 stsp }
2873 28315671 2019-08-14 stsp
2874 28315671 2019-08-14 stsp while (bline->annotated) {
2875 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2876 82f6abb8 2019-08-14 stsp size_t len;
2877 82f6abb8 2019-08-14 stsp
2878 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2879 28315671 2019-08-14 stsp if (ferror(a->f))
2880 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2881 28315671 2019-08-14 stsp break;
2882 28315671 2019-08-14 stsp }
2883 82f6abb8 2019-08-14 stsp
2884 82f6abb8 2019-08-14 stsp committer = bline->committer;
2885 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2886 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2887 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2888 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2889 82f6abb8 2019-08-14 stsp if (at)
2890 82f6abb8 2019-08-14 stsp *at = '\0';
2891 82f6abb8 2019-08-14 stsp len = strlen(committer);
2892 82f6abb8 2019-08-14 stsp if (len >= 9)
2893 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2894 28315671 2019-08-14 stsp
2895 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2896 28315671 2019-08-14 stsp if (nl)
2897 28315671 2019-08-14 stsp *nl = '\0';
2898 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2899 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2900 28315671 2019-08-14 stsp
2901 28315671 2019-08-14 stsp a->lineno_cur++;
2902 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2903 28315671 2019-08-14 stsp }
2904 82f6abb8 2019-08-14 stsp done:
2905 82f6abb8 2019-08-14 stsp if (commit)
2906 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2907 28315671 2019-08-14 stsp free(line);
2908 28315671 2019-08-14 stsp return err;
2909 28315671 2019-08-14 stsp }
2910 28315671 2019-08-14 stsp
2911 28315671 2019-08-14 stsp static const struct got_error *
2912 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2913 404c43c4 2018-06-21 stsp {
2914 404c43c4 2018-06-21 stsp const struct got_error *error;
2915 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2916 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2917 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2918 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2919 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2920 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2921 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2922 28315671 2019-08-14 stsp struct blame_cb_args bca;
2923 28315671 2019-08-14 stsp int ch, obj_type, i;
2924 b02560ec 2019-08-19 stsp size_t filesize;
2925 90f3c347 2019-08-19 stsp
2926 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2927 404c43c4 2018-06-21 stsp
2928 404c43c4 2018-06-21 stsp #ifndef PROFILE
2929 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2930 36e2fb66 2019-01-04 stsp NULL) == -1)
2931 404c43c4 2018-06-21 stsp err(1, "pledge");
2932 404c43c4 2018-06-21 stsp #endif
2933 404c43c4 2018-06-21 stsp
2934 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2935 404c43c4 2018-06-21 stsp switch (ch) {
2936 404c43c4 2018-06-21 stsp case 'c':
2937 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2938 404c43c4 2018-06-21 stsp break;
2939 66bea077 2018-08-02 stsp case 'r':
2940 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2941 66bea077 2018-08-02 stsp if (repo_path == NULL)
2942 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2943 9ba1d308 2019-10-21 stsp optarg);
2944 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2945 66bea077 2018-08-02 stsp break;
2946 404c43c4 2018-06-21 stsp default:
2947 2deda0b9 2019-03-07 stsp usage_blame();
2948 404c43c4 2018-06-21 stsp /* NOTREACHED */
2949 404c43c4 2018-06-21 stsp }
2950 404c43c4 2018-06-21 stsp }
2951 404c43c4 2018-06-21 stsp
2952 404c43c4 2018-06-21 stsp argc -= optind;
2953 404c43c4 2018-06-21 stsp argv += optind;
2954 404c43c4 2018-06-21 stsp
2955 a39318fd 2018-08-02 stsp if (argc == 1)
2956 404c43c4 2018-06-21 stsp path = argv[0];
2957 a39318fd 2018-08-02 stsp else
2958 404c43c4 2018-06-21 stsp usage_blame();
2959 404c43c4 2018-06-21 stsp
2960 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2961 66bea077 2018-08-02 stsp if (cwd == NULL) {
2962 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2963 66bea077 2018-08-02 stsp goto done;
2964 66bea077 2018-08-02 stsp }
2965 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2966 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2967 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2968 66bea077 2018-08-02 stsp goto done;
2969 0c06baac 2019-02-05 stsp else
2970 0c06baac 2019-02-05 stsp error = NULL;
2971 0c06baac 2019-02-05 stsp if (worktree) {
2972 0c06baac 2019-02-05 stsp repo_path =
2973 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2974 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2975 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2976 0c06baac 2019-02-05 stsp if (error)
2977 0c06baac 2019-02-05 stsp goto done;
2978 0c06baac 2019-02-05 stsp } else {
2979 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2980 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2981 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2982 0c06baac 2019-02-05 stsp goto done;
2983 0c06baac 2019-02-05 stsp }
2984 66bea077 2018-08-02 stsp }
2985 66bea077 2018-08-02 stsp }
2986 36e2fb66 2019-01-04 stsp
2987 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2988 c02c541e 2019-03-29 stsp if (error != NULL)
2989 36e2fb66 2019-01-04 stsp goto done;
2990 66bea077 2018-08-02 stsp
2991 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2992 c02c541e 2019-03-29 stsp if (error)
2993 404c43c4 2018-06-21 stsp goto done;
2994 404c43c4 2018-06-21 stsp
2995 0c06baac 2019-02-05 stsp if (worktree) {
2996 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2997 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2998 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2999 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3000 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3001 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3002 6efaaa2d 2019-02-05 stsp path) == -1) {
3003 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3004 0c06baac 2019-02-05 stsp goto done;
3005 0c06baac 2019-02-05 stsp }
3006 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3007 0c06baac 2019-02-05 stsp free(p);
3008 0c06baac 2019-02-05 stsp } else {
3009 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3010 0c06baac 2019-02-05 stsp }
3011 0c06baac 2019-02-05 stsp if (error)
3012 66bea077 2018-08-02 stsp goto done;
3013 66bea077 2018-08-02 stsp
3014 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3015 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3016 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3017 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3018 404c43c4 2018-06-21 stsp if (error != NULL)
3019 66bea077 2018-08-02 stsp goto done;
3020 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3021 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3022 404c43c4 2018-06-21 stsp if (error != NULL)
3023 66bea077 2018-08-02 stsp goto done;
3024 404c43c4 2018-06-21 stsp } else {
3025 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3026 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3027 30837e32 2019-07-25 stsp if (error)
3028 66bea077 2018-08-02 stsp goto done;
3029 404c43c4 2018-06-21 stsp }
3030 404c43c4 2018-06-21 stsp
3031 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3032 28315671 2019-08-14 stsp if (error)
3033 28315671 2019-08-14 stsp goto done;
3034 28315671 2019-08-14 stsp
3035 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3036 28315671 2019-08-14 stsp if (error)
3037 28315671 2019-08-14 stsp goto done;
3038 28315671 2019-08-14 stsp
3039 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3040 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3041 28315671 2019-08-14 stsp goto done;
3042 28315671 2019-08-14 stsp }
3043 28315671 2019-08-14 stsp
3044 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3045 28315671 2019-08-14 stsp if (error)
3046 28315671 2019-08-14 stsp goto done;
3047 28315671 2019-08-14 stsp bca.f = got_opentemp();
3048 28315671 2019-08-14 stsp if (bca.f == NULL) {
3049 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3050 28315671 2019-08-14 stsp goto done;
3051 28315671 2019-08-14 stsp }
3052 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3053 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3054 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3055 28315671 2019-08-14 stsp goto done;
3056 28315671 2019-08-14 stsp
3057 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3058 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3059 b02560ec 2019-08-19 stsp bca.nlines--;
3060 b02560ec 2019-08-19 stsp
3061 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3062 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3063 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3064 28315671 2019-08-14 stsp goto done;
3065 28315671 2019-08-14 stsp }
3066 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3067 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3068 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3069 7ef28ff8 2019-08-14 stsp while (i > 0) {
3070 7ef28ff8 2019-08-14 stsp i /= 10;
3071 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3072 7ef28ff8 2019-08-14 stsp }
3073 82f6abb8 2019-08-14 stsp bca.repo = repo;
3074 7ef28ff8 2019-08-14 stsp
3075 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3076 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3077 404c43c4 2018-06-21 stsp done:
3078 66bea077 2018-08-02 stsp free(in_repo_path);
3079 66bea077 2018-08-02 stsp free(repo_path);
3080 66bea077 2018-08-02 stsp free(cwd);
3081 404c43c4 2018-06-21 stsp free(commit_id);
3082 28315671 2019-08-14 stsp free(obj_id);
3083 28315671 2019-08-14 stsp if (blob)
3084 28315671 2019-08-14 stsp got_object_blob_close(blob);
3085 0c06baac 2019-02-05 stsp if (worktree)
3086 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3087 ad242220 2018-09-08 stsp if (repo) {
3088 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3089 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3090 ad242220 2018-09-08 stsp if (error == NULL)
3091 ad242220 2018-09-08 stsp error = repo_error;
3092 ad242220 2018-09-08 stsp }
3093 3affba96 2019-09-22 stsp if (bca.lines) {
3094 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3095 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3096 3affba96 2019-09-22 stsp free(bline->id_str);
3097 3affba96 2019-09-22 stsp free(bline->committer);
3098 3affba96 2019-09-22 stsp }
3099 3affba96 2019-09-22 stsp free(bca.lines);
3100 28315671 2019-08-14 stsp }
3101 28315671 2019-08-14 stsp free(bca.line_offsets);
3102 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3103 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3104 404c43c4 2018-06-21 stsp return error;
3105 5de5890b 2018-10-18 stsp }
3106 5de5890b 2018-10-18 stsp
3107 5de5890b 2018-10-18 stsp __dead static void
3108 5de5890b 2018-10-18 stsp usage_tree(void)
3109 5de5890b 2018-10-18 stsp {
3110 c1669e2e 2019-01-09 stsp fprintf(stderr,
3111 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3112 5de5890b 2018-10-18 stsp getprogname());
3113 5de5890b 2018-10-18 stsp exit(1);
3114 5de5890b 2018-10-18 stsp }
3115 5de5890b 2018-10-18 stsp
3116 c1669e2e 2019-01-09 stsp static void
3117 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3118 c1669e2e 2019-01-09 stsp const char *root_path)
3119 c1669e2e 2019-01-09 stsp {
3120 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3121 848d6979 2019-08-12 stsp const char *modestr = "";
3122 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3123 5de5890b 2018-10-18 stsp
3124 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3125 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3126 c1669e2e 2019-01-09 stsp path++;
3127 c1669e2e 2019-01-09 stsp
3128 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3129 63c5ca5d 2019-08-24 stsp modestr = "$";
3130 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3131 848d6979 2019-08-12 stsp modestr = "@";
3132 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3133 848d6979 2019-08-12 stsp modestr = "/";
3134 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3135 848d6979 2019-08-12 stsp modestr = "*";
3136 848d6979 2019-08-12 stsp
3137 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3138 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3139 c1669e2e 2019-01-09 stsp }
3140 c1669e2e 2019-01-09 stsp
3141 5de5890b 2018-10-18 stsp static const struct got_error *
3142 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3143 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3144 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3145 5de5890b 2018-10-18 stsp {
3146 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3147 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3148 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3149 56e0773d 2019-11-28 stsp int nentries, i;
3150 5de5890b 2018-10-18 stsp
3151 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3152 5de5890b 2018-10-18 stsp if (err)
3153 5de5890b 2018-10-18 stsp goto done;
3154 5de5890b 2018-10-18 stsp
3155 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3156 5de5890b 2018-10-18 stsp if (err)
3157 5de5890b 2018-10-18 stsp goto done;
3158 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3159 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3160 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3161 5de5890b 2018-10-18 stsp char *id = NULL;
3162 84453469 2018-11-11 stsp
3163 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3164 84453469 2018-11-11 stsp break;
3165 84453469 2018-11-11 stsp
3166 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3167 5de5890b 2018-10-18 stsp if (show_ids) {
3168 5de5890b 2018-10-18 stsp char *id_str;
3169 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3170 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3171 5de5890b 2018-10-18 stsp if (err)
3172 5de5890b 2018-10-18 stsp goto done;
3173 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3174 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3175 5de5890b 2018-10-18 stsp free(id_str);
3176 5de5890b 2018-10-18 stsp goto done;
3177 5de5890b 2018-10-18 stsp }
3178 5de5890b 2018-10-18 stsp free(id_str);
3179 5de5890b 2018-10-18 stsp }
3180 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3181 5de5890b 2018-10-18 stsp free(id);
3182 c1669e2e 2019-01-09 stsp
3183 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3184 c1669e2e 2019-01-09 stsp char *child_path;
3185 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3186 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3187 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3188 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3189 c1669e2e 2019-01-09 stsp goto done;
3190 c1669e2e 2019-01-09 stsp }
3191 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3192 c1669e2e 2019-01-09 stsp root_path, repo);
3193 c1669e2e 2019-01-09 stsp free(child_path);
3194 c1669e2e 2019-01-09 stsp if (err)
3195 c1669e2e 2019-01-09 stsp goto done;
3196 c1669e2e 2019-01-09 stsp }
3197 5de5890b 2018-10-18 stsp }
3198 5de5890b 2018-10-18 stsp done:
3199 5de5890b 2018-10-18 stsp if (tree)
3200 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3201 5de5890b 2018-10-18 stsp free(tree_id);
3202 5de5890b 2018-10-18 stsp return err;
3203 404c43c4 2018-06-21 stsp }
3204 404c43c4 2018-06-21 stsp
3205 5de5890b 2018-10-18 stsp static const struct got_error *
3206 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3207 5de5890b 2018-10-18 stsp {
3208 5de5890b 2018-10-18 stsp const struct got_error *error;
3209 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3210 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3211 7a2c19d6 2019-02-05 stsp const char *path;
3212 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3213 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3214 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3215 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3216 5de5890b 2018-10-18 stsp int ch;
3217 5de5890b 2018-10-18 stsp
3218 5de5890b 2018-10-18 stsp #ifndef PROFILE
3219 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3220 0f8d269b 2019-01-04 stsp NULL) == -1)
3221 5de5890b 2018-10-18 stsp err(1, "pledge");
3222 5de5890b 2018-10-18 stsp #endif
3223 5de5890b 2018-10-18 stsp
3224 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3225 5de5890b 2018-10-18 stsp switch (ch) {
3226 5de5890b 2018-10-18 stsp case 'c':
3227 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3228 5de5890b 2018-10-18 stsp break;
3229 5de5890b 2018-10-18 stsp case 'r':
3230 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3231 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3232 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3233 9ba1d308 2019-10-21 stsp optarg);
3234 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3235 5de5890b 2018-10-18 stsp break;
3236 5de5890b 2018-10-18 stsp case 'i':
3237 5de5890b 2018-10-18 stsp show_ids = 1;
3238 5de5890b 2018-10-18 stsp break;
3239 c1669e2e 2019-01-09 stsp case 'R':
3240 c1669e2e 2019-01-09 stsp recurse = 1;
3241 c1669e2e 2019-01-09 stsp break;
3242 5de5890b 2018-10-18 stsp default:
3243 2deda0b9 2019-03-07 stsp usage_tree();
3244 5de5890b 2018-10-18 stsp /* NOTREACHED */
3245 5de5890b 2018-10-18 stsp }
3246 5de5890b 2018-10-18 stsp }
3247 5de5890b 2018-10-18 stsp
3248 5de5890b 2018-10-18 stsp argc -= optind;
3249 5de5890b 2018-10-18 stsp argv += optind;
3250 5de5890b 2018-10-18 stsp
3251 5de5890b 2018-10-18 stsp if (argc == 1)
3252 5de5890b 2018-10-18 stsp path = argv[0];
3253 5de5890b 2018-10-18 stsp else if (argc > 1)
3254 5de5890b 2018-10-18 stsp usage_tree();
3255 5de5890b 2018-10-18 stsp else
3256 7a2c19d6 2019-02-05 stsp path = NULL;
3257 7a2c19d6 2019-02-05 stsp
3258 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3259 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3260 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3261 9bf7a39b 2019-02-05 stsp goto done;
3262 9bf7a39b 2019-02-05 stsp }
3263 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3264 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3265 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3266 7a2c19d6 2019-02-05 stsp goto done;
3267 7a2c19d6 2019-02-05 stsp else
3268 7a2c19d6 2019-02-05 stsp error = NULL;
3269 7a2c19d6 2019-02-05 stsp if (worktree) {
3270 7a2c19d6 2019-02-05 stsp repo_path =
3271 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3272 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3273 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3274 7a2c19d6 2019-02-05 stsp if (error)
3275 7a2c19d6 2019-02-05 stsp goto done;
3276 7a2c19d6 2019-02-05 stsp } else {
3277 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3278 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3279 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3280 7a2c19d6 2019-02-05 stsp goto done;
3281 7a2c19d6 2019-02-05 stsp }
3282 5de5890b 2018-10-18 stsp }
3283 5de5890b 2018-10-18 stsp }
3284 5de5890b 2018-10-18 stsp
3285 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3286 5de5890b 2018-10-18 stsp if (error != NULL)
3287 c02c541e 2019-03-29 stsp goto done;
3288 c02c541e 2019-03-29 stsp
3289 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3290 c02c541e 2019-03-29 stsp if (error)
3291 5de5890b 2018-10-18 stsp goto done;
3292 5de5890b 2018-10-18 stsp
3293 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3294 9bf7a39b 2019-02-05 stsp if (worktree) {
3295 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3296 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3297 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3298 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3299 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3300 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3301 9bf7a39b 2019-02-05 stsp goto done;
3302 9bf7a39b 2019-02-05 stsp }
3303 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3304 9bf7a39b 2019-02-05 stsp free(p);
3305 9bf7a39b 2019-02-05 stsp if (error)
3306 9bf7a39b 2019-02-05 stsp goto done;
3307 9bf7a39b 2019-02-05 stsp } else
3308 9bf7a39b 2019-02-05 stsp path = "/";
3309 9bf7a39b 2019-02-05 stsp }
3310 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3311 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3312 9bf7a39b 2019-02-05 stsp if (error != NULL)
3313 9bf7a39b 2019-02-05 stsp goto done;
3314 9bf7a39b 2019-02-05 stsp }
3315 5de5890b 2018-10-18 stsp
3316 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3317 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3318 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3319 5de5890b 2018-10-18 stsp if (error != NULL)
3320 5de5890b 2018-10-18 stsp goto done;
3321 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3322 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3323 5de5890b 2018-10-18 stsp if (error != NULL)
3324 5de5890b 2018-10-18 stsp goto done;
3325 5de5890b 2018-10-18 stsp } else {
3326 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3327 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3328 30837e32 2019-07-25 stsp if (error)
3329 5de5890b 2018-10-18 stsp goto done;
3330 5de5890b 2018-10-18 stsp }
3331 5de5890b 2018-10-18 stsp
3332 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3333 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3334 5de5890b 2018-10-18 stsp done:
3335 5de5890b 2018-10-18 stsp free(in_repo_path);
3336 5de5890b 2018-10-18 stsp free(repo_path);
3337 5de5890b 2018-10-18 stsp free(cwd);
3338 5de5890b 2018-10-18 stsp free(commit_id);
3339 7a2c19d6 2019-02-05 stsp if (worktree)
3340 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3341 5de5890b 2018-10-18 stsp if (repo) {
3342 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3343 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3344 5de5890b 2018-10-18 stsp if (error == NULL)
3345 5de5890b 2018-10-18 stsp error = repo_error;
3346 5de5890b 2018-10-18 stsp }
3347 5de5890b 2018-10-18 stsp return error;
3348 5de5890b 2018-10-18 stsp }
3349 5de5890b 2018-10-18 stsp
3350 6bad629b 2019-02-04 stsp __dead static void
3351 6bad629b 2019-02-04 stsp usage_status(void)
3352 6bad629b 2019-02-04 stsp {
3353 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3354 6bad629b 2019-02-04 stsp exit(1);
3355 6bad629b 2019-02-04 stsp }
3356 5c860e29 2018-03-12 stsp
3357 b72f483a 2019-02-05 stsp static const struct got_error *
3358 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3359 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3360 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3361 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3362 6bad629b 2019-02-04 stsp {
3363 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3364 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3365 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3366 b72f483a 2019-02-05 stsp return NULL;
3367 6bad629b 2019-02-04 stsp }
3368 5c860e29 2018-03-12 stsp
3369 6bad629b 2019-02-04 stsp static const struct got_error *
3370 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3371 6bad629b 2019-02-04 stsp {
3372 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3373 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3374 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3375 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3376 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3377 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3378 a5edda0a 2019-07-27 stsp int ch;
3379 5c860e29 2018-03-12 stsp
3380 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3381 72ea6654 2019-07-27 stsp
3382 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3383 6bad629b 2019-02-04 stsp switch (ch) {
3384 5c860e29 2018-03-12 stsp default:
3385 2deda0b9 2019-03-07 stsp usage_status();
3386 6bad629b 2019-02-04 stsp /* NOTREACHED */
3387 5c860e29 2018-03-12 stsp }
3388 5c860e29 2018-03-12 stsp }
3389 5c860e29 2018-03-12 stsp
3390 6bad629b 2019-02-04 stsp argc -= optind;
3391 6bad629b 2019-02-04 stsp argv += optind;
3392 5c860e29 2018-03-12 stsp
3393 6bad629b 2019-02-04 stsp #ifndef PROFILE
3394 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3395 6bad629b 2019-02-04 stsp NULL) == -1)
3396 6bad629b 2019-02-04 stsp err(1, "pledge");
3397 f42b1b34 2018-03-12 stsp #endif
3398 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3399 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3400 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3401 927df6b7 2019-02-10 stsp goto done;
3402 927df6b7 2019-02-10 stsp }
3403 927df6b7 2019-02-10 stsp
3404 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3405 927df6b7 2019-02-10 stsp if (error != NULL)
3406 a5edda0a 2019-07-27 stsp goto done;
3407 6bad629b 2019-02-04 stsp
3408 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3409 c9956ddf 2019-09-08 stsp NULL);
3410 6bad629b 2019-02-04 stsp if (error != NULL)
3411 6bad629b 2019-02-04 stsp goto done;
3412 6bad629b 2019-02-04 stsp
3413 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3414 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3415 087fb88c 2019-08-04 stsp if (error)
3416 087fb88c 2019-08-04 stsp goto done;
3417 087fb88c 2019-08-04 stsp
3418 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3419 6bad629b 2019-02-04 stsp if (error)
3420 6bad629b 2019-02-04 stsp goto done;
3421 6bad629b 2019-02-04 stsp
3422 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3423 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3424 6bad629b 2019-02-04 stsp done:
3425 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3426 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3427 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3428 927df6b7 2019-02-10 stsp free(cwd);
3429 d0eebce4 2019-03-11 stsp return error;
3430 d0eebce4 2019-03-11 stsp }
3431 d0eebce4 2019-03-11 stsp
3432 d0eebce4 2019-03-11 stsp __dead static void
3433 d0eebce4 2019-03-11 stsp usage_ref(void)
3434 d0eebce4 2019-03-11 stsp {
3435 d0eebce4 2019-03-11 stsp fprintf(stderr,
3436 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3437 d0eebce4 2019-03-11 stsp getprogname());
3438 d0eebce4 2019-03-11 stsp exit(1);
3439 d0eebce4 2019-03-11 stsp }
3440 d0eebce4 2019-03-11 stsp
3441 d0eebce4 2019-03-11 stsp static const struct got_error *
3442 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3443 d0eebce4 2019-03-11 stsp {
3444 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3445 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3446 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3447 d0eebce4 2019-03-11 stsp
3448 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3449 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3450 d0eebce4 2019-03-11 stsp if (err)
3451 d0eebce4 2019-03-11 stsp return err;
3452 d0eebce4 2019-03-11 stsp
3453 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3454 d0eebce4 2019-03-11 stsp char *refstr;
3455 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3456 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3457 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3458 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3459 d0eebce4 2019-03-11 stsp free(refstr);
3460 d0eebce4 2019-03-11 stsp }
3461 d0eebce4 2019-03-11 stsp
3462 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3463 d0eebce4 2019-03-11 stsp return NULL;
3464 d0eebce4 2019-03-11 stsp }
3465 d0eebce4 2019-03-11 stsp
3466 d0eebce4 2019-03-11 stsp static const struct got_error *
3467 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3468 d0eebce4 2019-03-11 stsp {
3469 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3470 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3471 d0eebce4 2019-03-11 stsp
3472 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3473 d0eebce4 2019-03-11 stsp if (err)
3474 d0eebce4 2019-03-11 stsp return err;
3475 d0eebce4 2019-03-11 stsp
3476 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3477 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3478 d0eebce4 2019-03-11 stsp return err;
3479 d0eebce4 2019-03-11 stsp }
3480 d0eebce4 2019-03-11 stsp
3481 d0eebce4 2019-03-11 stsp static const struct got_error *
3482 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3483 d0eebce4 2019-03-11 stsp {
3484 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3485 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3486 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3487 d1644381 2019-07-14 stsp
3488 d1644381 2019-07-14 stsp /*
3489 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3490 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3491 d1644381 2019-07-14 stsp * an unintended typo.
3492 d1644381 2019-07-14 stsp */
3493 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3494 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3495 d0eebce4 2019-03-11 stsp
3496 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3497 dd88155e 2019-06-29 stsp repo);
3498 d83d9d5c 2019-05-13 stsp if (err) {
3499 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3500 d0eebce4 2019-03-11 stsp
3501 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3502 d83d9d5c 2019-05-13 stsp return err;
3503 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3504 d83d9d5c 2019-05-13 stsp if (err)
3505 d83d9d5c 2019-05-13 stsp return err;
3506 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3507 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3508 d83d9d5c 2019-05-13 stsp if (err)
3509 d83d9d5c 2019-05-13 stsp return err;
3510 d83d9d5c 2019-05-13 stsp }
3511 d83d9d5c 2019-05-13 stsp
3512 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3513 d0eebce4 2019-03-11 stsp if (err)
3514 d0eebce4 2019-03-11 stsp goto done;
3515 d0eebce4 2019-03-11 stsp
3516 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3517 d0eebce4 2019-03-11 stsp done:
3518 d0eebce4 2019-03-11 stsp if (ref)
3519 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3520 d0eebce4 2019-03-11 stsp free(id);
3521 d1c1ae5f 2019-08-12 stsp return err;
3522 d1c1ae5f 2019-08-12 stsp }
3523 d1c1ae5f 2019-08-12 stsp
3524 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3525 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3526 d1c1ae5f 2019-08-12 stsp {
3527 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3528 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3529 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3530 d1c1ae5f 2019-08-12 stsp
3531 d1c1ae5f 2019-08-12 stsp /*
3532 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3533 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3534 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3535 d1c1ae5f 2019-08-12 stsp */
3536 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3537 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3538 d1c1ae5f 2019-08-12 stsp
3539 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3540 d1c1ae5f 2019-08-12 stsp if (err)
3541 d1c1ae5f 2019-08-12 stsp return err;
3542 d1c1ae5f 2019-08-12 stsp
3543 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3544 d1c1ae5f 2019-08-12 stsp if (err)
3545 d1c1ae5f 2019-08-12 stsp goto done;
3546 d1c1ae5f 2019-08-12 stsp
3547 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3548 d1c1ae5f 2019-08-12 stsp done:
3549 d1c1ae5f 2019-08-12 stsp if (target_ref)
3550 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3551 d1c1ae5f 2019-08-12 stsp if (ref)
3552 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3553 d0eebce4 2019-03-11 stsp return err;
3554 d0eebce4 2019-03-11 stsp }
3555 d0eebce4 2019-03-11 stsp
3556 d0eebce4 2019-03-11 stsp static const struct got_error *
3557 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3558 d0eebce4 2019-03-11 stsp {
3559 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3560 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3561 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3562 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3563 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3564 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3565 d0eebce4 2019-03-11 stsp
3566 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3567 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3568 d0eebce4 2019-03-11 stsp switch (ch) {
3569 d0eebce4 2019-03-11 stsp case 'd':
3570 d0eebce4 2019-03-11 stsp delref = optarg;
3571 d0eebce4 2019-03-11 stsp break;
3572 d0eebce4 2019-03-11 stsp case 'r':
3573 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3574 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3575 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3576 9ba1d308 2019-10-21 stsp optarg);
3577 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3578 d0eebce4 2019-03-11 stsp break;
3579 d0eebce4 2019-03-11 stsp case 'l':
3580 d0eebce4 2019-03-11 stsp do_list = 1;
3581 d1c1ae5f 2019-08-12 stsp break;
3582 d1c1ae5f 2019-08-12 stsp case 's':
3583 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3584 d0eebce4 2019-03-11 stsp break;
3585 d0eebce4 2019-03-11 stsp default:
3586 d0eebce4 2019-03-11 stsp usage_ref();
3587 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3588 d0eebce4 2019-03-11 stsp }
3589 d0eebce4 2019-03-11 stsp }
3590 d0eebce4 2019-03-11 stsp
3591 d0eebce4 2019-03-11 stsp if (do_list && delref)
3592 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3593 d0eebce4 2019-03-11 stsp
3594 d0eebce4 2019-03-11 stsp argc -= optind;
3595 d0eebce4 2019-03-11 stsp argv += optind;
3596 d0eebce4 2019-03-11 stsp
3597 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3598 d1c1ae5f 2019-08-12 stsp if (create_symref)
3599 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3600 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3601 d0eebce4 2019-03-11 stsp if (argc > 0)
3602 d0eebce4 2019-03-11 stsp usage_ref();
3603 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3604 d0eebce4 2019-03-11 stsp usage_ref();
3605 d0eebce4 2019-03-11 stsp
3606 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3607 e0b57350 2019-03-12 stsp if (do_list) {
3608 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3609 e0b57350 2019-03-12 stsp NULL) == -1)
3610 e0b57350 2019-03-12 stsp err(1, "pledge");
3611 e0b57350 2019-03-12 stsp } else {
3612 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3613 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3614 e0b57350 2019-03-12 stsp err(1, "pledge");
3615 e0b57350 2019-03-12 stsp }
3616 d0eebce4 2019-03-11 stsp #endif
3617 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3618 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3619 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3620 d0eebce4 2019-03-11 stsp goto done;
3621 d0eebce4 2019-03-11 stsp }
3622 d0eebce4 2019-03-11 stsp
3623 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3624 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3625 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3626 d0eebce4 2019-03-11 stsp goto done;
3627 d0eebce4 2019-03-11 stsp else
3628 d0eebce4 2019-03-11 stsp error = NULL;
3629 d0eebce4 2019-03-11 stsp if (worktree) {
3630 d0eebce4 2019-03-11 stsp repo_path =
3631 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3632 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3633 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3634 d0eebce4 2019-03-11 stsp if (error)
3635 d0eebce4 2019-03-11 stsp goto done;
3636 d0eebce4 2019-03-11 stsp } else {
3637 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3638 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3639 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3640 d0eebce4 2019-03-11 stsp goto done;
3641 d0eebce4 2019-03-11 stsp }
3642 d0eebce4 2019-03-11 stsp }
3643 d0eebce4 2019-03-11 stsp }
3644 d0eebce4 2019-03-11 stsp
3645 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3646 d0eebce4 2019-03-11 stsp if (error != NULL)
3647 d0eebce4 2019-03-11 stsp goto done;
3648 d0eebce4 2019-03-11 stsp
3649 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3650 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3651 c02c541e 2019-03-29 stsp if (error)
3652 c02c541e 2019-03-29 stsp goto done;
3653 c02c541e 2019-03-29 stsp
3654 d0eebce4 2019-03-11 stsp if (do_list)
3655 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3656 d0eebce4 2019-03-11 stsp else if (delref)
3657 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3658 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3659 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3660 d0eebce4 2019-03-11 stsp else
3661 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3662 4e759de4 2019-06-26 stsp done:
3663 4e759de4 2019-06-26 stsp if (repo)
3664 4e759de4 2019-06-26 stsp got_repo_close(repo);
3665 4e759de4 2019-06-26 stsp if (worktree)
3666 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3667 4e759de4 2019-06-26 stsp free(cwd);
3668 4e759de4 2019-06-26 stsp free(repo_path);
3669 4e759de4 2019-06-26 stsp return error;
3670 4e759de4 2019-06-26 stsp }
3671 4e759de4 2019-06-26 stsp
3672 4e759de4 2019-06-26 stsp __dead static void
3673 4e759de4 2019-06-26 stsp usage_branch(void)
3674 4e759de4 2019-06-26 stsp {
3675 4e759de4 2019-06-26 stsp fprintf(stderr,
3676 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3677 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3678 4e759de4 2019-06-26 stsp exit(1);
3679 4e759de4 2019-06-26 stsp }
3680 4e759de4 2019-06-26 stsp
3681 4e759de4 2019-06-26 stsp static const struct got_error *
3682 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3683 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3684 4e99b47e 2019-10-04 stsp {
3685 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3686 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3687 4e99b47e 2019-10-04 stsp char *refstr;
3688 4e99b47e 2019-10-04 stsp
3689 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3690 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3691 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3692 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3693 4e99b47e 2019-10-04 stsp
3694 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3695 4e99b47e 2019-10-04 stsp if (err)
3696 4e99b47e 2019-10-04 stsp return err;
3697 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3698 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3699 4e99b47e 2019-10-04 stsp marker = "* ";
3700 4e99b47e 2019-10-04 stsp else
3701 4e99b47e 2019-10-04 stsp marker = "~ ";
3702 4e99b47e 2019-10-04 stsp free(id);
3703 4e99b47e 2019-10-04 stsp }
3704 4e99b47e 2019-10-04 stsp
3705 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3706 4e99b47e 2019-10-04 stsp refname += 11;
3707 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3708 4e99b47e 2019-10-04 stsp refname += 18;
3709 4e99b47e 2019-10-04 stsp
3710 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3711 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3712 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3713 4e99b47e 2019-10-04 stsp
3714 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3715 4e99b47e 2019-10-04 stsp free(refstr);
3716 4e99b47e 2019-10-04 stsp return NULL;
3717 4e99b47e 2019-10-04 stsp }
3718 4e99b47e 2019-10-04 stsp
3719 4e99b47e 2019-10-04 stsp static const struct got_error *
3720 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3721 ad89fa31 2019-10-04 stsp {
3722 ad89fa31 2019-10-04 stsp const char *refname;
3723 ad89fa31 2019-10-04 stsp
3724 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3725 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3726 ad89fa31 2019-10-04 stsp
3727 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3728 ad89fa31 2019-10-04 stsp
3729 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3730 ad89fa31 2019-10-04 stsp refname += 11;
3731 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3732 ad89fa31 2019-10-04 stsp refname += 18;
3733 ad89fa31 2019-10-04 stsp
3734 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3735 ad89fa31 2019-10-04 stsp
3736 ad89fa31 2019-10-04 stsp return NULL;
3737 ad89fa31 2019-10-04 stsp }
3738 ad89fa31 2019-10-04 stsp
3739 ad89fa31 2019-10-04 stsp static const struct got_error *
3740 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3741 4e759de4 2019-06-26 stsp {
3742 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3743 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3744 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3745 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3746 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3747 4e759de4 2019-06-26 stsp
3748 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3749 ba882ee3 2019-07-11 stsp
3750 4e99b47e 2019-10-04 stsp if (worktree) {
3751 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3752 4e99b47e 2019-10-04 stsp worktree);
3753 4e99b47e 2019-10-04 stsp if (err)
3754 4e99b47e 2019-10-04 stsp return err;
3755 4e99b47e 2019-10-04 stsp
3756 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3757 4e99b47e 2019-10-04 stsp worktree);
3758 4e99b47e 2019-10-04 stsp if (err)
3759 4e99b47e 2019-10-04 stsp return err;
3760 4e99b47e 2019-10-04 stsp
3761 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3762 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3763 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3764 4e99b47e 2019-10-04 stsp if (err)
3765 4e99b47e 2019-10-04 stsp return err;
3766 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3767 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3768 4e99b47e 2019-10-04 stsp }
3769 4e99b47e 2019-10-04 stsp }
3770 4e99b47e 2019-10-04 stsp
3771 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3772 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3773 4e759de4 2019-06-26 stsp if (err)
3774 4e759de4 2019-06-26 stsp return err;
3775 4e759de4 2019-06-26 stsp
3776 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3777 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3778 4e759de4 2019-06-26 stsp
3779 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3780 4e759de4 2019-06-26 stsp return NULL;
3781 4e759de4 2019-06-26 stsp }
3782 4e759de4 2019-06-26 stsp
3783 4e759de4 2019-06-26 stsp static const struct got_error *
3784 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3785 45cd4e47 2019-08-25 stsp const char *branch_name)
3786 4e759de4 2019-06-26 stsp {
3787 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3788 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3789 4e759de4 2019-06-26 stsp char *refname;
3790 4e759de4 2019-06-26 stsp
3791 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3792 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3793 4e759de4 2019-06-26 stsp
3794 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3795 4e759de4 2019-06-26 stsp if (err)
3796 4e759de4 2019-06-26 stsp goto done;
3797 4e759de4 2019-06-26 stsp
3798 45cd4e47 2019-08-25 stsp if (worktree &&
3799 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3800 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3801 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3802 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3803 45cd4e47 2019-08-25 stsp goto done;
3804 45cd4e47 2019-08-25 stsp }
3805 45cd4e47 2019-08-25 stsp
3806 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3807 4e759de4 2019-06-26 stsp done:
3808 45cd4e47 2019-08-25 stsp if (ref)
3809 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3810 4e759de4 2019-06-26 stsp free(refname);
3811 4e759de4 2019-06-26 stsp return err;
3812 4e759de4 2019-06-26 stsp }
3813 4e759de4 2019-06-26 stsp
3814 4e759de4 2019-06-26 stsp static const struct got_error *
3815 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3816 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3817 4e759de4 2019-06-26 stsp {
3818 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3819 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3820 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3821 d3f84d51 2019-07-11 stsp
3822 d3f84d51 2019-07-11 stsp /*
3823 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3824 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3825 d3f84d51 2019-07-11 stsp * an unintended typo.
3826 d3f84d51 2019-07-11 stsp */
3827 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3828 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3829 4e759de4 2019-06-26 stsp
3830 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3831 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3832 4e759de4 2019-06-26 stsp goto done;
3833 4e759de4 2019-06-26 stsp }
3834 4e759de4 2019-06-26 stsp
3835 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3836 4e759de4 2019-06-26 stsp if (err == NULL) {
3837 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3838 4e759de4 2019-06-26 stsp goto done;
3839 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3840 4e759de4 2019-06-26 stsp goto done;
3841 4e759de4 2019-06-26 stsp
3842 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3843 4e759de4 2019-06-26 stsp if (err)
3844 4e759de4 2019-06-26 stsp goto done;
3845 4e759de4 2019-06-26 stsp
3846 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3847 d0eebce4 2019-03-11 stsp done:
3848 4e759de4 2019-06-26 stsp if (ref)
3849 4e759de4 2019-06-26 stsp got_ref_close(ref);
3850 4e759de4 2019-06-26 stsp free(base_refname);
3851 4e759de4 2019-06-26 stsp free(refname);
3852 4e759de4 2019-06-26 stsp return err;
3853 4e759de4 2019-06-26 stsp }
3854 4e759de4 2019-06-26 stsp
3855 4e759de4 2019-06-26 stsp static const struct got_error *
3856 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3857 4e759de4 2019-06-26 stsp {
3858 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3859 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3860 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3861 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3862 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3863 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3864 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3865 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3866 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3867 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3868 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3869 4e759de4 2019-06-26 stsp
3870 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3871 da76fce2 2020-02-24 stsp
3872 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3873 4e759de4 2019-06-26 stsp switch (ch) {
3874 a74f7e83 2019-11-10 stsp case 'c':
3875 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3876 a74f7e83 2019-11-10 stsp break;
3877 4e759de4 2019-06-26 stsp case 'd':
3878 4e759de4 2019-06-26 stsp delref = optarg;
3879 4e759de4 2019-06-26 stsp break;
3880 4e759de4 2019-06-26 stsp case 'r':
3881 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3882 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3883 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3884 9ba1d308 2019-10-21 stsp optarg);
3885 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3886 4e759de4 2019-06-26 stsp break;
3887 4e759de4 2019-06-26 stsp case 'l':
3888 4e759de4 2019-06-26 stsp do_list = 1;
3889 da76fce2 2020-02-24 stsp break;
3890 da76fce2 2020-02-24 stsp case 'n':
3891 da76fce2 2020-02-24 stsp do_update = 0;
3892 4e759de4 2019-06-26 stsp break;
3893 4e759de4 2019-06-26 stsp default:
3894 4e759de4 2019-06-26 stsp usage_branch();
3895 4e759de4 2019-06-26 stsp /* NOTREACHED */
3896 4e759de4 2019-06-26 stsp }
3897 4e759de4 2019-06-26 stsp }
3898 4e759de4 2019-06-26 stsp
3899 4e759de4 2019-06-26 stsp if (do_list && delref)
3900 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3901 4e759de4 2019-06-26 stsp
3902 4e759de4 2019-06-26 stsp argc -= optind;
3903 4e759de4 2019-06-26 stsp argv += optind;
3904 ad89fa31 2019-10-04 stsp
3905 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3906 ad89fa31 2019-10-04 stsp do_show = 1;
3907 4e759de4 2019-06-26 stsp
3908 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3909 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3910 a74f7e83 2019-11-10 stsp
3911 4e759de4 2019-06-26 stsp if (do_list || delref) {
3912 4e759de4 2019-06-26 stsp if (argc > 0)
3913 4e759de4 2019-06-26 stsp usage_branch();
3914 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3915 4e759de4 2019-06-26 stsp usage_branch();
3916 4e759de4 2019-06-26 stsp
3917 4e759de4 2019-06-26 stsp #ifndef PROFILE
3918 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3919 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3920 4e759de4 2019-06-26 stsp NULL) == -1)
3921 4e759de4 2019-06-26 stsp err(1, "pledge");
3922 4e759de4 2019-06-26 stsp } else {
3923 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3924 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3925 4e759de4 2019-06-26 stsp err(1, "pledge");
3926 4e759de4 2019-06-26 stsp }
3927 4e759de4 2019-06-26 stsp #endif
3928 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3929 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3930 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3931 4e759de4 2019-06-26 stsp goto done;
3932 4e759de4 2019-06-26 stsp }
3933 4e759de4 2019-06-26 stsp
3934 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3935 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3936 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3937 4e759de4 2019-06-26 stsp goto done;
3938 4e759de4 2019-06-26 stsp else
3939 4e759de4 2019-06-26 stsp error = NULL;
3940 4e759de4 2019-06-26 stsp if (worktree) {
3941 4e759de4 2019-06-26 stsp repo_path =
3942 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3943 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3944 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3945 4e759de4 2019-06-26 stsp if (error)
3946 4e759de4 2019-06-26 stsp goto done;
3947 4e759de4 2019-06-26 stsp } else {
3948 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3949 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3950 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3951 4e759de4 2019-06-26 stsp goto done;
3952 4e759de4 2019-06-26 stsp }
3953 4e759de4 2019-06-26 stsp }
3954 4e759de4 2019-06-26 stsp }
3955 4e759de4 2019-06-26 stsp
3956 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3957 4e759de4 2019-06-26 stsp if (error != NULL)
3958 4e759de4 2019-06-26 stsp goto done;
3959 4e759de4 2019-06-26 stsp
3960 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3961 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3962 4e759de4 2019-06-26 stsp if (error)
3963 4e759de4 2019-06-26 stsp goto done;
3964 4e759de4 2019-06-26 stsp
3965 ad89fa31 2019-10-04 stsp if (do_show)
3966 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3967 ad89fa31 2019-10-04 stsp else if (do_list)
3968 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3969 4e759de4 2019-06-26 stsp else if (delref)
3970 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3971 4e759de4 2019-06-26 stsp else {
3972 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3973 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3974 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3975 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3976 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3977 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3978 a74f7e83 2019-11-10 stsp if (error)
3979 a74f7e83 2019-11-10 stsp goto done;
3980 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3981 da76fce2 2020-02-24 stsp if (error)
3982 da76fce2 2020-02-24 stsp goto done;
3983 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3984 da76fce2 2020-02-24 stsp int did_something = 0;
3985 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3986 da76fce2 2020-02-24 stsp
3987 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3988 da76fce2 2020-02-24 stsp if (error)
3989 da76fce2 2020-02-24 stsp goto done;
3990 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3991 da76fce2 2020-02-24 stsp worktree);
3992 da76fce2 2020-02-24 stsp if (error)
3993 da76fce2 2020-02-24 stsp goto done;
3994 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3995 da76fce2 2020-02-24 stsp == -1) {
3996 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3997 da76fce2 2020-02-24 stsp goto done;
3998 da76fce2 2020-02-24 stsp }
3999 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4000 da76fce2 2020-02-24 stsp free(branch_refname);
4001 da76fce2 2020-02-24 stsp if (error)
4002 da76fce2 2020-02-24 stsp goto done;
4003 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4004 da76fce2 2020-02-24 stsp repo);
4005 da76fce2 2020-02-24 stsp if (error)
4006 da76fce2 2020-02-24 stsp goto done;
4007 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4008 da76fce2 2020-02-24 stsp commit_id);
4009 da76fce2 2020-02-24 stsp if (error)
4010 da76fce2 2020-02-24 stsp goto done;
4011 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4012 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4013 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4014 da76fce2 2020-02-24 stsp if (error)
4015 da76fce2 2020-02-24 stsp goto done;
4016 da76fce2 2020-02-24 stsp if (did_something)
4017 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4018 da76fce2 2020-02-24 stsp }
4019 4e759de4 2019-06-26 stsp }
4020 4e759de4 2019-06-26 stsp done:
4021 da76fce2 2020-02-24 stsp if (ref)
4022 da76fce2 2020-02-24 stsp got_ref_close(ref);
4023 d0eebce4 2019-03-11 stsp if (repo)
4024 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4025 d0eebce4 2019-03-11 stsp if (worktree)
4026 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4027 d0eebce4 2019-03-11 stsp free(cwd);
4028 d0eebce4 2019-03-11 stsp free(repo_path);
4029 da76fce2 2020-02-24 stsp free(commit_id);
4030 da76fce2 2020-02-24 stsp free(commit_id_str);
4031 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4032 da76fce2 2020-02-24 stsp free((char *)pe->path);
4033 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4034 d00136be 2019-03-26 stsp return error;
4035 d00136be 2019-03-26 stsp }
4036 d00136be 2019-03-26 stsp
4037 8e7bd50a 2019-08-22 stsp
4038 d00136be 2019-03-26 stsp __dead static void
4039 8e7bd50a 2019-08-22 stsp usage_tag(void)
4040 8e7bd50a 2019-08-22 stsp {
4041 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4042 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4043 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4044 8e7bd50a 2019-08-22 stsp exit(1);
4045 b8bad2ba 2019-08-23 stsp }
4046 b8bad2ba 2019-08-23 stsp
4047 b8bad2ba 2019-08-23 stsp #if 0
4048 b8bad2ba 2019-08-23 stsp static const struct got_error *
4049 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4050 b8bad2ba 2019-08-23 stsp {
4051 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4052 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4053 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4054 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4055 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4056 b8bad2ba 2019-08-23 stsp
4057 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4058 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4059 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4060 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4061 b8bad2ba 2019-08-23 stsp if (err)
4062 b8bad2ba 2019-08-23 stsp return err;
4063 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4064 b8bad2ba 2019-08-23 stsp continue;
4065 b8bad2ba 2019-08-23 stsp } else {
4066 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4067 b8bad2ba 2019-08-23 stsp if (err)
4068 b8bad2ba 2019-08-23 stsp break;
4069 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4070 b8bad2ba 2019-08-23 stsp free(re_id);
4071 b8bad2ba 2019-08-23 stsp if (err)
4072 b8bad2ba 2019-08-23 stsp break;
4073 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4074 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4075 b8bad2ba 2019-08-23 stsp }
4076 b8bad2ba 2019-08-23 stsp
4077 b8bad2ba 2019-08-23 stsp while (se) {
4078 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4079 b8bad2ba 2019-08-23 stsp if (err)
4080 b8bad2ba 2019-08-23 stsp break;
4081 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4082 b8bad2ba 2019-08-23 stsp free(se_id);
4083 b8bad2ba 2019-08-23 stsp if (err)
4084 b8bad2ba 2019-08-23 stsp break;
4085 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4086 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4087 b8bad2ba 2019-08-23 stsp
4088 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4089 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4090 b8bad2ba 2019-08-23 stsp if (err)
4091 b8bad2ba 2019-08-23 stsp return err;
4092 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4093 b8bad2ba 2019-08-23 stsp break;
4094 b8bad2ba 2019-08-23 stsp }
4095 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4096 b8bad2ba 2019-08-23 stsp continue;
4097 b8bad2ba 2019-08-23 stsp }
4098 b8bad2ba 2019-08-23 stsp }
4099 b8bad2ba 2019-08-23 stsp done:
4100 b8bad2ba 2019-08-23 stsp return err;
4101 8e7bd50a 2019-08-22 stsp }
4102 b8bad2ba 2019-08-23 stsp #endif
4103 b8bad2ba 2019-08-23 stsp
4104 b8bad2ba 2019-08-23 stsp static const struct got_error *
4105 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4106 8e7bd50a 2019-08-22 stsp {
4107 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4108 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4109 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4110 8e7bd50a 2019-08-22 stsp
4111 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4112 8e7bd50a 2019-08-22 stsp
4113 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4114 8e7bd50a 2019-08-22 stsp if (err)
4115 8e7bd50a 2019-08-22 stsp return err;
4116 8e7bd50a 2019-08-22 stsp
4117 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4118 8e7bd50a 2019-08-22 stsp const char *refname;
4119 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4120 8e7bd50a 2019-08-22 stsp char datebuf[26];
4121 d4efa91b 2020-01-14 stsp const char *tagger;
4122 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4123 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4124 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4125 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4126 8e7bd50a 2019-08-22 stsp
4127 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4128 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4129 8e7bd50a 2019-08-22 stsp continue;
4130 8e7bd50a 2019-08-22 stsp refname += 10;
4131 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4132 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4133 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4134 8e7bd50a 2019-08-22 stsp break;
4135 8e7bd50a 2019-08-22 stsp }
4136 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4137 8e7bd50a 2019-08-22 stsp free(refstr);
4138 8e7bd50a 2019-08-22 stsp
4139 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4140 8e7bd50a 2019-08-22 stsp if (err)
4141 8e7bd50a 2019-08-22 stsp break;
4142 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4143 d4efa91b 2020-01-14 stsp if (err) {
4144 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4145 d4efa91b 2020-01-14 stsp free(id);
4146 d4efa91b 2020-01-14 stsp break;
4147 d4efa91b 2020-01-14 stsp }
4148 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4149 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4150 d4efa91b 2020-01-14 stsp if (err) {
4151 d4efa91b 2020-01-14 stsp free(id);
4152 d4efa91b 2020-01-14 stsp break;
4153 d4efa91b 2020-01-14 stsp }
4154 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4155 d4efa91b 2020-01-14 stsp tagger_time =
4156 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4157 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4158 d4efa91b 2020-01-14 stsp free(id);
4159 d4efa91b 2020-01-14 stsp if (err)
4160 d4efa91b 2020-01-14 stsp break;
4161 d4efa91b 2020-01-14 stsp } else {
4162 d4efa91b 2020-01-14 stsp free(id);
4163 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4164 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4165 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4166 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4167 d4efa91b 2020-01-14 stsp if (err)
4168 d4efa91b 2020-01-14 stsp break;
4169 d4efa91b 2020-01-14 stsp }
4170 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4171 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4172 2417344c 2019-08-23 stsp if (datestr)
4173 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4174 d4efa91b 2020-01-14 stsp if (commit)
4175 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4176 d4efa91b 2020-01-14 stsp else {
4177 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4178 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4179 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4180 d4efa91b 2020-01-14 stsp id_str);
4181 d4efa91b 2020-01-14 stsp break;
4182 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4183 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4184 d4efa91b 2020-01-14 stsp id_str);
4185 d4efa91b 2020-01-14 stsp break;
4186 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4187 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4188 d4efa91b 2020-01-14 stsp id_str);
4189 d4efa91b 2020-01-14 stsp break;
4190 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4191 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4192 d4efa91b 2020-01-14 stsp id_str);
4193 d4efa91b 2020-01-14 stsp break;
4194 d4efa91b 2020-01-14 stsp default:
4195 d4efa91b 2020-01-14 stsp break;
4196 d4efa91b 2020-01-14 stsp }
4197 8e7bd50a 2019-08-22 stsp }
4198 8e7bd50a 2019-08-22 stsp free(id_str);
4199 d4efa91b 2020-01-14 stsp if (commit) {
4200 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4201 d4efa91b 2020-01-14 stsp if (err)
4202 d4efa91b 2020-01-14 stsp break;
4203 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4204 d4efa91b 2020-01-14 stsp } else {
4205 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4206 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4207 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4208 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4209 d4efa91b 2020-01-14 stsp break;
4210 d4efa91b 2020-01-14 stsp }
4211 8e7bd50a 2019-08-22 stsp }
4212 8e7bd50a 2019-08-22 stsp
4213 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4214 8e7bd50a 2019-08-22 stsp do {
4215 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4216 8e7bd50a 2019-08-22 stsp if (line)
4217 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4218 8e7bd50a 2019-08-22 stsp } while (line);
4219 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4220 8e7bd50a 2019-08-22 stsp }
4221 8e7bd50a 2019-08-22 stsp
4222 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4223 8e7bd50a 2019-08-22 stsp return NULL;
4224 8e7bd50a 2019-08-22 stsp }
4225 8e7bd50a 2019-08-22 stsp
4226 8e7bd50a 2019-08-22 stsp static const struct got_error *
4227 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4228 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4229 8e7bd50a 2019-08-22 stsp {
4230 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4231 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4232 f372d5cd 2019-10-21 stsp char *editor = NULL;
4233 8e7bd50a 2019-08-22 stsp int fd = -1;
4234 8e7bd50a 2019-08-22 stsp
4235 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4236 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4237 8e7bd50a 2019-08-22 stsp goto done;
4238 8e7bd50a 2019-08-22 stsp }
4239 8e7bd50a 2019-08-22 stsp
4240 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4241 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4242 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4243 8e7bd50a 2019-08-22 stsp goto done;
4244 8e7bd50a 2019-08-22 stsp }
4245 8e7bd50a 2019-08-22 stsp
4246 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4247 8e7bd50a 2019-08-22 stsp if (err)
4248 8e7bd50a 2019-08-22 stsp goto done;
4249 8e7bd50a 2019-08-22 stsp
4250 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4251 8e7bd50a 2019-08-22 stsp close(fd);
4252 8e7bd50a 2019-08-22 stsp
4253 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4254 8e7bd50a 2019-08-22 stsp if (err)
4255 8e7bd50a 2019-08-22 stsp goto done;
4256 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4257 8e7bd50a 2019-08-22 stsp done:
4258 8e7bd50a 2019-08-22 stsp free(initial_content);
4259 8e7bd50a 2019-08-22 stsp free(template);
4260 8e7bd50a 2019-08-22 stsp free(editor);
4261 8e7bd50a 2019-08-22 stsp
4262 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4263 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4264 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4265 8e7bd50a 2019-08-22 stsp if (err) {
4266 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4267 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4268 8e7bd50a 2019-08-22 stsp }
4269 8e7bd50a 2019-08-22 stsp }
4270 8e7bd50a 2019-08-22 stsp return err;
4271 8e7bd50a 2019-08-22 stsp }
4272 8e7bd50a 2019-08-22 stsp
4273 8e7bd50a 2019-08-22 stsp static const struct got_error *
4274 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4275 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4276 8e7bd50a 2019-08-22 stsp {
4277 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4278 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4279 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4280 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4281 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4282 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4283 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4284 8e7bd50a 2019-08-22 stsp
4285 8e7bd50a 2019-08-22 stsp /*
4286 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4287 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4288 8e7bd50a 2019-08-22 stsp * an unintended typo.
4289 8e7bd50a 2019-08-22 stsp */
4290 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4291 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4292 8e7bd50a 2019-08-22 stsp
4293 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4294 8e7bd50a 2019-08-22 stsp if (err)
4295 8e7bd50a 2019-08-22 stsp return err;
4296 8e7bd50a 2019-08-22 stsp
4297 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4298 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4299 8e7bd50a 2019-08-22 stsp if (err)
4300 8e7bd50a 2019-08-22 stsp goto done;
4301 8e7bd50a 2019-08-22 stsp
4302 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4303 8e7bd50a 2019-08-22 stsp if (err)
4304 8e7bd50a 2019-08-22 stsp goto done;
4305 8e7bd50a 2019-08-22 stsp
4306 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4307 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4308 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4309 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4310 8e7bd50a 2019-08-22 stsp goto done;
4311 8e7bd50a 2019-08-22 stsp }
4312 8e7bd50a 2019-08-22 stsp tag_name += 10;
4313 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4314 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4315 8e7bd50a 2019-08-22 stsp goto done;
4316 8e7bd50a 2019-08-22 stsp }
4317 8e7bd50a 2019-08-22 stsp
4318 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4319 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4320 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4321 8e7bd50a 2019-08-22 stsp goto done;
4322 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4323 8e7bd50a 2019-08-22 stsp goto done;
4324 8e7bd50a 2019-08-22 stsp
4325 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4326 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4327 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4328 f372d5cd 2019-10-21 stsp if (err) {
4329 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4330 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4331 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4332 8e7bd50a 2019-08-22 stsp goto done;
4333 f372d5cd 2019-10-21 stsp }
4334 8e7bd50a 2019-08-22 stsp }
4335 8e7bd50a 2019-08-22 stsp
4336 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4337 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4338 f372d5cd 2019-10-21 stsp if (err) {
4339 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4340 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4341 8e7bd50a 2019-08-22 stsp goto done;
4342 f372d5cd 2019-10-21 stsp }
4343 8e7bd50a 2019-08-22 stsp
4344 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4345 f372d5cd 2019-10-21 stsp if (err) {
4346 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4347 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4348 8e7bd50a 2019-08-22 stsp goto done;
4349 f372d5cd 2019-10-21 stsp }
4350 8e7bd50a 2019-08-22 stsp
4351 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4352 f372d5cd 2019-10-21 stsp if (err) {
4353 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4354 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4355 f372d5cd 2019-10-21 stsp goto done;
4356 f372d5cd 2019-10-21 stsp }
4357 8e7bd50a 2019-08-22 stsp
4358 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4359 f372d5cd 2019-10-21 stsp if (err) {
4360 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4361 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4362 f372d5cd 2019-10-21 stsp goto done;
4363 8e7bd50a 2019-08-22 stsp }
4364 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4365 8e7bd50a 2019-08-22 stsp done:
4366 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4367 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4368 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4369 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4370 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4371 f372d5cd 2019-10-21 stsp free(tag_id_str);
4372 8e7bd50a 2019-08-22 stsp if (ref)
4373 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4374 8e7bd50a 2019-08-22 stsp free(commit_id);
4375 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4376 8e7bd50a 2019-08-22 stsp free(refname);
4377 8e7bd50a 2019-08-22 stsp free(tagmsg);
4378 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4379 aba9c984 2019-09-08 stsp free(tagger);
4380 8e7bd50a 2019-08-22 stsp return err;
4381 8e7bd50a 2019-08-22 stsp }
4382 8e7bd50a 2019-08-22 stsp
4383 8e7bd50a 2019-08-22 stsp static const struct got_error *
4384 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4385 8e7bd50a 2019-08-22 stsp {
4386 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4387 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4388 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4389 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4390 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4391 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4392 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4393 8e7bd50a 2019-08-22 stsp
4394 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4395 8e7bd50a 2019-08-22 stsp switch (ch) {
4396 80106605 2020-02-24 stsp case 'c':
4397 80106605 2020-02-24 stsp commit_id_arg = optarg;
4398 80106605 2020-02-24 stsp break;
4399 8e7bd50a 2019-08-22 stsp case 'm':
4400 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4401 8e7bd50a 2019-08-22 stsp break;
4402 8e7bd50a 2019-08-22 stsp case 'r':
4403 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4404 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4405 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4406 9ba1d308 2019-10-21 stsp optarg);
4407 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4408 8e7bd50a 2019-08-22 stsp break;
4409 8e7bd50a 2019-08-22 stsp case 'l':
4410 8e7bd50a 2019-08-22 stsp do_list = 1;
4411 8e7bd50a 2019-08-22 stsp break;
4412 8e7bd50a 2019-08-22 stsp default:
4413 8e7bd50a 2019-08-22 stsp usage_tag();
4414 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4415 8e7bd50a 2019-08-22 stsp }
4416 8e7bd50a 2019-08-22 stsp }
4417 8e7bd50a 2019-08-22 stsp
4418 8e7bd50a 2019-08-22 stsp argc -= optind;
4419 8e7bd50a 2019-08-22 stsp argv += optind;
4420 8e7bd50a 2019-08-22 stsp
4421 8e7bd50a 2019-08-22 stsp if (do_list) {
4422 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4423 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4424 8e7bd50a 2019-08-22 stsp if (tagmsg)
4425 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4426 8e7bd50a 2019-08-22 stsp if (argc > 0)
4427 8e7bd50a 2019-08-22 stsp usage_tag();
4428 80106605 2020-02-24 stsp } else if (argc != 1)
4429 8e7bd50a 2019-08-22 stsp usage_tag();
4430 80106605 2020-02-24 stsp
4431 a2887370 2019-08-23 stsp tag_name = argv[0];
4432 8e7bd50a 2019-08-22 stsp
4433 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4434 8e7bd50a 2019-08-22 stsp if (do_list) {
4435 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4436 8e7bd50a 2019-08-22 stsp NULL) == -1)
4437 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4438 8e7bd50a 2019-08-22 stsp } else {
4439 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4440 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4441 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4442 8e7bd50a 2019-08-22 stsp }
4443 8e7bd50a 2019-08-22 stsp #endif
4444 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4445 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4446 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4447 8e7bd50a 2019-08-22 stsp goto done;
4448 8e7bd50a 2019-08-22 stsp }
4449 8e7bd50a 2019-08-22 stsp
4450 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4451 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4452 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4453 8e7bd50a 2019-08-22 stsp goto done;
4454 8e7bd50a 2019-08-22 stsp else
4455 8e7bd50a 2019-08-22 stsp error = NULL;
4456 8e7bd50a 2019-08-22 stsp if (worktree) {
4457 8e7bd50a 2019-08-22 stsp repo_path =
4458 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4459 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4460 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4461 8e7bd50a 2019-08-22 stsp if (error)
4462 8e7bd50a 2019-08-22 stsp goto done;
4463 8e7bd50a 2019-08-22 stsp } else {
4464 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4465 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4466 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4467 8e7bd50a 2019-08-22 stsp goto done;
4468 8e7bd50a 2019-08-22 stsp }
4469 8e7bd50a 2019-08-22 stsp }
4470 8e7bd50a 2019-08-22 stsp }
4471 8e7bd50a 2019-08-22 stsp
4472 8e7bd50a 2019-08-22 stsp if (do_list) {
4473 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4474 c9956ddf 2019-09-08 stsp if (error != NULL)
4475 c9956ddf 2019-09-08 stsp goto done;
4476 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4477 8e7bd50a 2019-08-22 stsp if (error)
4478 8e7bd50a 2019-08-22 stsp goto done;
4479 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4480 8e7bd50a 2019-08-22 stsp } else {
4481 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4482 c9956ddf 2019-09-08 stsp if (error)
4483 c9956ddf 2019-09-08 stsp goto done;
4484 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4485 c9956ddf 2019-09-08 stsp if (error != NULL)
4486 c9956ddf 2019-09-08 stsp goto done;
4487 c9956ddf 2019-09-08 stsp
4488 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4489 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4490 8e7bd50a 2019-08-22 stsp if (error)
4491 8e7bd50a 2019-08-22 stsp goto done;
4492 8e7bd50a 2019-08-22 stsp }
4493 8e7bd50a 2019-08-22 stsp
4494 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4495 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4496 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4497 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4498 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4499 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4500 8e7bd50a 2019-08-22 stsp if (error)
4501 8e7bd50a 2019-08-22 stsp goto done;
4502 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4503 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4504 8e7bd50a 2019-08-22 stsp if (error)
4505 8e7bd50a 2019-08-22 stsp goto done;
4506 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4507 8e7bd50a 2019-08-22 stsp free(commit_id);
4508 8e7bd50a 2019-08-22 stsp if (error)
4509 8e7bd50a 2019-08-22 stsp goto done;
4510 8e7bd50a 2019-08-22 stsp }
4511 8e7bd50a 2019-08-22 stsp
4512 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4513 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4514 8e7bd50a 2019-08-22 stsp }
4515 8e7bd50a 2019-08-22 stsp done:
4516 8e7bd50a 2019-08-22 stsp if (repo)
4517 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4518 8e7bd50a 2019-08-22 stsp if (worktree)
4519 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4520 8e7bd50a 2019-08-22 stsp free(cwd);
4521 8e7bd50a 2019-08-22 stsp free(repo_path);
4522 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4523 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4524 8e7bd50a 2019-08-22 stsp return error;
4525 8e7bd50a 2019-08-22 stsp }
4526 8e7bd50a 2019-08-22 stsp
4527 8e7bd50a 2019-08-22 stsp __dead static void
4528 d00136be 2019-03-26 stsp usage_add(void)
4529 d00136be 2019-03-26 stsp {
4530 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4531 022fae89 2019-12-06 tracey getprogname());
4532 d00136be 2019-03-26 stsp exit(1);
4533 d00136be 2019-03-26 stsp }
4534 d00136be 2019-03-26 stsp
4535 d00136be 2019-03-26 stsp static const struct got_error *
4536 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4537 4e68cba3 2019-11-23 stsp {
4538 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4539 4e68cba3 2019-11-23 stsp path++;
4540 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4541 4e68cba3 2019-11-23 stsp return NULL;
4542 4e68cba3 2019-11-23 stsp }
4543 4e68cba3 2019-11-23 stsp
4544 4e68cba3 2019-11-23 stsp static const struct got_error *
4545 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4546 d00136be 2019-03-26 stsp {
4547 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4548 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4549 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4550 1dd54920 2019-05-11 stsp char *cwd = NULL;
4551 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4552 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4553 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4554 1dd54920 2019-05-11 stsp
4555 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4556 d00136be 2019-03-26 stsp
4557 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4558 d00136be 2019-03-26 stsp switch (ch) {
4559 022fae89 2019-12-06 tracey case 'I':
4560 022fae89 2019-12-06 tracey no_ignores = 1;
4561 022fae89 2019-12-06 tracey break;
4562 4e68cba3 2019-11-23 stsp case 'R':
4563 4e68cba3 2019-11-23 stsp can_recurse = 1;
4564 4e68cba3 2019-11-23 stsp break;
4565 d00136be 2019-03-26 stsp default:
4566 d00136be 2019-03-26 stsp usage_add();
4567 d00136be 2019-03-26 stsp /* NOTREACHED */
4568 d00136be 2019-03-26 stsp }
4569 d00136be 2019-03-26 stsp }
4570 d00136be 2019-03-26 stsp
4571 d00136be 2019-03-26 stsp argc -= optind;
4572 d00136be 2019-03-26 stsp argv += optind;
4573 d00136be 2019-03-26 stsp
4574 43012d58 2019-07-14 stsp #ifndef PROFILE
4575 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4576 43012d58 2019-07-14 stsp NULL) == -1)
4577 43012d58 2019-07-14 stsp err(1, "pledge");
4578 43012d58 2019-07-14 stsp #endif
4579 723c305c 2019-05-11 jcs if (argc < 1)
4580 d00136be 2019-03-26 stsp usage_add();
4581 d00136be 2019-03-26 stsp
4582 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4583 d00136be 2019-03-26 stsp if (cwd == NULL) {
4584 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4585 d00136be 2019-03-26 stsp goto done;
4586 d00136be 2019-03-26 stsp }
4587 723c305c 2019-05-11 jcs
4588 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4589 d00136be 2019-03-26 stsp if (error)
4590 d00136be 2019-03-26 stsp goto done;
4591 d00136be 2019-03-26 stsp
4592 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4593 c9956ddf 2019-09-08 stsp NULL);
4594 031a5338 2019-03-26 stsp if (error != NULL)
4595 031a5338 2019-03-26 stsp goto done;
4596 031a5338 2019-03-26 stsp
4597 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4598 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4599 d00136be 2019-03-26 stsp if (error)
4600 d00136be 2019-03-26 stsp goto done;
4601 d00136be 2019-03-26 stsp
4602 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4603 6d022e97 2019-08-04 stsp if (error)
4604 022fae89 2019-12-06 tracey goto done;
4605 022fae89 2019-12-06 tracey
4606 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4607 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4608 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4609 6d022e97 2019-08-04 stsp goto done;
4610 723c305c 2019-05-11 jcs
4611 022fae89 2019-12-06 tracey }
4612 022fae89 2019-12-06 tracey
4613 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4614 4e68cba3 2019-11-23 stsp char *ondisk_path;
4615 4e68cba3 2019-11-23 stsp struct stat sb;
4616 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4617 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4618 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4619 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4620 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4621 4e68cba3 2019-11-23 stsp goto done;
4622 4e68cba3 2019-11-23 stsp }
4623 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4624 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4625 4e68cba3 2019-11-23 stsp free(ondisk_path);
4626 4e68cba3 2019-11-23 stsp continue;
4627 4e68cba3 2019-11-23 stsp }
4628 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4629 4e68cba3 2019-11-23 stsp ondisk_path);
4630 4e68cba3 2019-11-23 stsp free(ondisk_path);
4631 4e68cba3 2019-11-23 stsp goto done;
4632 4e68cba3 2019-11-23 stsp }
4633 4e68cba3 2019-11-23 stsp free(ondisk_path);
4634 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4635 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4636 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4637 4e68cba3 2019-11-23 stsp goto done;
4638 4e68cba3 2019-11-23 stsp }
4639 4e68cba3 2019-11-23 stsp }
4640 4e68cba3 2019-11-23 stsp }
4641 022fae89 2019-12-06 tracey
4642 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4643 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4644 d00136be 2019-03-26 stsp done:
4645 031a5338 2019-03-26 stsp if (repo)
4646 031a5338 2019-03-26 stsp got_repo_close(repo);
4647 d00136be 2019-03-26 stsp if (worktree)
4648 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4649 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4650 1dd54920 2019-05-11 stsp free((char *)pe->path);
4651 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4652 2ec1f75b 2019-03-26 stsp free(cwd);
4653 2ec1f75b 2019-03-26 stsp return error;
4654 2ec1f75b 2019-03-26 stsp }
4655 2ec1f75b 2019-03-26 stsp
4656 2ec1f75b 2019-03-26 stsp __dead static void
4657 648e4ef7 2019-07-09 stsp usage_remove(void)
4658 2ec1f75b 2019-03-26 stsp {
4659 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4660 f2a9dc41 2019-12-13 tracey getprogname());
4661 2ec1f75b 2019-03-26 stsp exit(1);
4662 2a06fe5f 2019-08-24 stsp }
4663 2a06fe5f 2019-08-24 stsp
4664 2a06fe5f 2019-08-24 stsp static const struct got_error *
4665 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4666 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4667 2a06fe5f 2019-08-24 stsp {
4668 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4669 f2a9dc41 2019-12-13 tracey path++;
4670 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4671 2a06fe5f 2019-08-24 stsp return NULL;
4672 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4673 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4674 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4675 2a06fe5f 2019-08-24 stsp return NULL;
4676 2ec1f75b 2019-03-26 stsp }
4677 2ec1f75b 2019-03-26 stsp
4678 2ec1f75b 2019-03-26 stsp static const struct got_error *
4679 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4680 2ec1f75b 2019-03-26 stsp {
4681 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4682 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4683 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4684 17ed4618 2019-06-02 stsp char *cwd = NULL;
4685 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4686 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4687 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4688 2ec1f75b 2019-03-26 stsp
4689 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4690 17ed4618 2019-06-02 stsp
4691 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4692 2ec1f75b 2019-03-26 stsp switch (ch) {
4693 2ec1f75b 2019-03-26 stsp case 'f':
4694 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4695 2ec1f75b 2019-03-26 stsp break;
4696 70e3e7f5 2019-12-13 tracey case 'k':
4697 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4698 70e3e7f5 2019-12-13 tracey break;
4699 f2a9dc41 2019-12-13 tracey case 'R':
4700 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4701 f2a9dc41 2019-12-13 tracey break;
4702 2ec1f75b 2019-03-26 stsp default:
4703 f2a9dc41 2019-12-13 tracey usage_remove();
4704 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4705 2ec1f75b 2019-03-26 stsp }
4706 2ec1f75b 2019-03-26 stsp }
4707 2ec1f75b 2019-03-26 stsp
4708 2ec1f75b 2019-03-26 stsp argc -= optind;
4709 2ec1f75b 2019-03-26 stsp argv += optind;
4710 2ec1f75b 2019-03-26 stsp
4711 43012d58 2019-07-14 stsp #ifndef PROFILE
4712 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4713 43012d58 2019-07-14 stsp NULL) == -1)
4714 43012d58 2019-07-14 stsp err(1, "pledge");
4715 43012d58 2019-07-14 stsp #endif
4716 17ed4618 2019-06-02 stsp if (argc < 1)
4717 648e4ef7 2019-07-09 stsp usage_remove();
4718 2ec1f75b 2019-03-26 stsp
4719 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4720 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4721 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4722 2ec1f75b 2019-03-26 stsp goto done;
4723 2ec1f75b 2019-03-26 stsp }
4724 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4725 2ec1f75b 2019-03-26 stsp if (error)
4726 2ec1f75b 2019-03-26 stsp goto done;
4727 2ec1f75b 2019-03-26 stsp
4728 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4729 c9956ddf 2019-09-08 stsp NULL);
4730 2af4a041 2019-05-11 jcs if (error)
4731 2ec1f75b 2019-03-26 stsp goto done;
4732 2ec1f75b 2019-03-26 stsp
4733 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4734 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4735 2ec1f75b 2019-03-26 stsp if (error)
4736 2ec1f75b 2019-03-26 stsp goto done;
4737 2ec1f75b 2019-03-26 stsp
4738 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4739 6d022e97 2019-08-04 stsp if (error)
4740 6d022e97 2019-08-04 stsp goto done;
4741 17ed4618 2019-06-02 stsp
4742 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4743 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4744 f2a9dc41 2019-12-13 tracey struct stat sb;
4745 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4746 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4747 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4748 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4749 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4750 f2a9dc41 2019-12-13 tracey goto done;
4751 f2a9dc41 2019-12-13 tracey }
4752 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4753 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4754 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4755 f2a9dc41 2019-12-13 tracey continue;
4756 f2a9dc41 2019-12-13 tracey }
4757 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4758 f2a9dc41 2019-12-13 tracey ondisk_path);
4759 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4760 f2a9dc41 2019-12-13 tracey goto done;
4761 f2a9dc41 2019-12-13 tracey }
4762 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4763 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4764 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4765 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4766 f2a9dc41 2019-12-13 tracey goto done;
4767 f2a9dc41 2019-12-13 tracey }
4768 f2a9dc41 2019-12-13 tracey }
4769 f2a9dc41 2019-12-13 tracey }
4770 f2a9dc41 2019-12-13 tracey
4771 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4772 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4773 a129376b 2019-03-28 stsp done:
4774 a129376b 2019-03-28 stsp if (repo)
4775 a129376b 2019-03-28 stsp got_repo_close(repo);
4776 a129376b 2019-03-28 stsp if (worktree)
4777 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4778 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4779 17ed4618 2019-06-02 stsp free((char *)pe->path);
4780 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4781 a129376b 2019-03-28 stsp free(cwd);
4782 a129376b 2019-03-28 stsp return error;
4783 a129376b 2019-03-28 stsp }
4784 a129376b 2019-03-28 stsp
4785 a129376b 2019-03-28 stsp __dead static void
4786 a129376b 2019-03-28 stsp usage_revert(void)
4787 a129376b 2019-03-28 stsp {
4788 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4789 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4790 a129376b 2019-03-28 stsp exit(1);
4791 a129376b 2019-03-28 stsp }
4792 a129376b 2019-03-28 stsp
4793 1ee397ad 2019-07-12 stsp static const struct got_error *
4794 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4795 a129376b 2019-03-28 stsp {
4796 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4797 fb9704af 2020-01-27 stsp return NULL;
4798 fb9704af 2020-01-27 stsp
4799 a129376b 2019-03-28 stsp while (path[0] == '/')
4800 a129376b 2019-03-28 stsp path++;
4801 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4802 33aa809d 2019-08-08 stsp return NULL;
4803 33aa809d 2019-08-08 stsp }
4804 33aa809d 2019-08-08 stsp
4805 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4806 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4807 33aa809d 2019-08-08 stsp const char *action;
4808 33aa809d 2019-08-08 stsp };
4809 33aa809d 2019-08-08 stsp
4810 33aa809d 2019-08-08 stsp static const struct got_error *
4811 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4812 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4813 33aa809d 2019-08-08 stsp {
4814 33aa809d 2019-08-08 stsp char *line = NULL;
4815 33aa809d 2019-08-08 stsp size_t linesize = 0;
4816 33aa809d 2019-08-08 stsp ssize_t linelen;
4817 33aa809d 2019-08-08 stsp
4818 33aa809d 2019-08-08 stsp switch (status) {
4819 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4820 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4821 33aa809d 2019-08-08 stsp break;
4822 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4823 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4824 33aa809d 2019-08-08 stsp break;
4825 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4826 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4827 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4828 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4829 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4830 33aa809d 2019-08-08 stsp printf("%s", line);
4831 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4832 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4833 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4834 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4835 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4836 33aa809d 2019-08-08 stsp break;
4837 33aa809d 2019-08-08 stsp default:
4838 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4839 33aa809d 2019-08-08 stsp }
4840 33aa809d 2019-08-08 stsp
4841 33aa809d 2019-08-08 stsp return NULL;
4842 33aa809d 2019-08-08 stsp }
4843 33aa809d 2019-08-08 stsp
4844 33aa809d 2019-08-08 stsp static const struct got_error *
4845 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4846 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4847 33aa809d 2019-08-08 stsp {
4848 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4849 33aa809d 2019-08-08 stsp char *line = NULL;
4850 33aa809d 2019-08-08 stsp size_t linesize = 0;
4851 33aa809d 2019-08-08 stsp ssize_t linelen;
4852 33aa809d 2019-08-08 stsp int resp = ' ';
4853 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4854 33aa809d 2019-08-08 stsp
4855 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4856 33aa809d 2019-08-08 stsp
4857 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4858 33aa809d 2019-08-08 stsp char *nl;
4859 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4860 33aa809d 2019-08-08 stsp a->action);
4861 33aa809d 2019-08-08 stsp if (err)
4862 33aa809d 2019-08-08 stsp return err;
4863 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4864 33aa809d 2019-08-08 stsp if (linelen == -1) {
4865 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4866 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4867 33aa809d 2019-08-08 stsp return NULL;
4868 33aa809d 2019-08-08 stsp }
4869 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4870 33aa809d 2019-08-08 stsp if (nl)
4871 33aa809d 2019-08-08 stsp *nl = '\0';
4872 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4873 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4874 33aa809d 2019-08-08 stsp printf("y\n");
4875 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4876 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4877 33aa809d 2019-08-08 stsp printf("n\n");
4878 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4879 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4880 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4881 33aa809d 2019-08-08 stsp printf("q\n");
4882 33aa809d 2019-08-08 stsp } else
4883 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4884 33aa809d 2019-08-08 stsp free(line);
4885 33aa809d 2019-08-08 stsp return NULL;
4886 33aa809d 2019-08-08 stsp }
4887 33aa809d 2019-08-08 stsp
4888 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4889 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4890 33aa809d 2019-08-08 stsp a->action);
4891 33aa809d 2019-08-08 stsp if (err)
4892 33aa809d 2019-08-08 stsp return err;
4893 33aa809d 2019-08-08 stsp resp = getchar();
4894 33aa809d 2019-08-08 stsp if (resp == '\n')
4895 33aa809d 2019-08-08 stsp resp = getchar();
4896 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4897 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4898 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4899 33aa809d 2019-08-08 stsp resp = ' ';
4900 33aa809d 2019-08-08 stsp }
4901 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4902 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4903 33aa809d 2019-08-08 stsp resp = ' ';
4904 33aa809d 2019-08-08 stsp }
4905 33aa809d 2019-08-08 stsp }
4906 33aa809d 2019-08-08 stsp
4907 33aa809d 2019-08-08 stsp if (resp == 'y')
4908 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4909 33aa809d 2019-08-08 stsp else if (resp == 'n')
4910 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4911 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4912 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4913 33aa809d 2019-08-08 stsp
4914 1ee397ad 2019-07-12 stsp return NULL;
4915 a129376b 2019-03-28 stsp }
4916 a129376b 2019-03-28 stsp
4917 33aa809d 2019-08-08 stsp
4918 a129376b 2019-03-28 stsp static const struct got_error *
4919 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4920 a129376b 2019-03-28 stsp {
4921 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4922 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4923 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4924 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4925 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4926 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4927 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4928 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4929 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4930 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4931 a129376b 2019-03-28 stsp
4932 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4933 e20a8b6f 2019-06-04 stsp
4934 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4935 a129376b 2019-03-28 stsp switch (ch) {
4936 33aa809d 2019-08-08 stsp case 'p':
4937 33aa809d 2019-08-08 stsp pflag = 1;
4938 33aa809d 2019-08-08 stsp break;
4939 33aa809d 2019-08-08 stsp case 'F':
4940 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4941 33aa809d 2019-08-08 stsp break;
4942 0f6d7415 2019-08-08 stsp case 'R':
4943 0f6d7415 2019-08-08 stsp can_recurse = 1;
4944 0f6d7415 2019-08-08 stsp break;
4945 a129376b 2019-03-28 stsp default:
4946 a129376b 2019-03-28 stsp usage_revert();
4947 a129376b 2019-03-28 stsp /* NOTREACHED */
4948 a129376b 2019-03-28 stsp }
4949 a129376b 2019-03-28 stsp }
4950 a129376b 2019-03-28 stsp
4951 a129376b 2019-03-28 stsp argc -= optind;
4952 a129376b 2019-03-28 stsp argv += optind;
4953 a129376b 2019-03-28 stsp
4954 43012d58 2019-07-14 stsp #ifndef PROFILE
4955 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4956 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4957 43012d58 2019-07-14 stsp err(1, "pledge");
4958 43012d58 2019-07-14 stsp #endif
4959 e20a8b6f 2019-06-04 stsp if (argc < 1)
4960 a129376b 2019-03-28 stsp usage_revert();
4961 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4962 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4963 a129376b 2019-03-28 stsp
4964 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4965 a129376b 2019-03-28 stsp if (cwd == NULL) {
4966 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4967 a129376b 2019-03-28 stsp goto done;
4968 a129376b 2019-03-28 stsp }
4969 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4970 2ec1f75b 2019-03-26 stsp if (error)
4971 2ec1f75b 2019-03-26 stsp goto done;
4972 a129376b 2019-03-28 stsp
4973 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4974 c9956ddf 2019-09-08 stsp NULL);
4975 a129376b 2019-03-28 stsp if (error != NULL)
4976 a129376b 2019-03-28 stsp goto done;
4977 a129376b 2019-03-28 stsp
4978 33aa809d 2019-08-08 stsp if (patch_script_path) {
4979 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4980 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4981 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4982 33aa809d 2019-08-08 stsp patch_script_path);
4983 33aa809d 2019-08-08 stsp goto done;
4984 33aa809d 2019-08-08 stsp }
4985 33aa809d 2019-08-08 stsp }
4986 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4987 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4988 a129376b 2019-03-28 stsp if (error)
4989 a129376b 2019-03-28 stsp goto done;
4990 a129376b 2019-03-28 stsp
4991 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4992 6d022e97 2019-08-04 stsp if (error)
4993 6d022e97 2019-08-04 stsp goto done;
4994 00db391e 2019-08-03 stsp
4995 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4996 0f6d7415 2019-08-08 stsp char *ondisk_path;
4997 0f6d7415 2019-08-08 stsp struct stat sb;
4998 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4999 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5000 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5001 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5002 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5003 0f6d7415 2019-08-08 stsp goto done;
5004 0f6d7415 2019-08-08 stsp }
5005 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5006 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5007 0f6d7415 2019-08-08 stsp free(ondisk_path);
5008 0f6d7415 2019-08-08 stsp continue;
5009 0f6d7415 2019-08-08 stsp }
5010 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5011 0f6d7415 2019-08-08 stsp ondisk_path);
5012 0f6d7415 2019-08-08 stsp free(ondisk_path);
5013 0f6d7415 2019-08-08 stsp goto done;
5014 0f6d7415 2019-08-08 stsp }
5015 0f6d7415 2019-08-08 stsp free(ondisk_path);
5016 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5017 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5018 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5019 0f6d7415 2019-08-08 stsp goto done;
5020 0f6d7415 2019-08-08 stsp }
5021 0f6d7415 2019-08-08 stsp }
5022 0f6d7415 2019-08-08 stsp }
5023 0f6d7415 2019-08-08 stsp
5024 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5025 33aa809d 2019-08-08 stsp cpa.action = "revert";
5026 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5027 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5028 2ec1f75b 2019-03-26 stsp done:
5029 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5030 33aa809d 2019-08-08 stsp error == NULL)
5031 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5032 2ec1f75b 2019-03-26 stsp if (repo)
5033 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5034 2ec1f75b 2019-03-26 stsp if (worktree)
5035 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5036 2ec1f75b 2019-03-26 stsp free(path);
5037 d00136be 2019-03-26 stsp free(cwd);
5038 6bad629b 2019-02-04 stsp return error;
5039 c4296144 2019-05-09 stsp }
5040 c4296144 2019-05-09 stsp
5041 c4296144 2019-05-09 stsp __dead static void
5042 c4296144 2019-05-09 stsp usage_commit(void)
5043 c4296144 2019-05-09 stsp {
5044 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5045 5c1e53bc 2019-07-28 stsp getprogname());
5046 c4296144 2019-05-09 stsp exit(1);
5047 33ad4cbe 2019-05-12 jcs }
5048 33ad4cbe 2019-05-12 jcs
5049 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5050 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5051 e2ba3d07 2019-05-13 stsp const char *editor;
5052 e0870e44 2019-05-13 stsp const char *worktree_path;
5053 76d98825 2019-06-03 stsp const char *branch_name;
5054 314a6357 2019-05-13 stsp const char *repo_path;
5055 e0870e44 2019-05-13 stsp char *logmsg_path;
5056 e2ba3d07 2019-05-13 stsp
5057 e2ba3d07 2019-05-13 stsp };
5058 e0870e44 2019-05-13 stsp
5059 33ad4cbe 2019-05-12 jcs static const struct got_error *
5060 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5061 33ad4cbe 2019-05-12 jcs void *arg)
5062 33ad4cbe 2019-05-12 jcs {
5063 76d98825 2019-06-03 stsp char *initial_content = NULL;
5064 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5065 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5066 e0870e44 2019-05-13 stsp char *template = NULL;
5067 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5068 3ce1b845 2019-07-15 stsp int fd;
5069 33ad4cbe 2019-05-12 jcs size_t len;
5070 33ad4cbe 2019-05-12 jcs
5071 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5072 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5073 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5074 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5075 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5076 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5077 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5078 33ad4cbe 2019-05-12 jcs return NULL;
5079 33ad4cbe 2019-05-12 jcs }
5080 33ad4cbe 2019-05-12 jcs
5081 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5082 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5083 76d98825 2019-06-03 stsp
5084 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5085 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5086 76d98825 2019-06-03 stsp a->branch_name) == -1)
5087 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5088 e0870e44 2019-05-13 stsp
5089 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5090 793c30b5 2019-05-13 stsp if (err)
5091 e0870e44 2019-05-13 stsp goto done;
5092 33ad4cbe 2019-05-12 jcs
5093 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5094 33ad4cbe 2019-05-12 jcs
5095 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5096 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5097 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5098 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5099 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5100 33ad4cbe 2019-05-12 jcs }
5101 33ad4cbe 2019-05-12 jcs close(fd);
5102 33ad4cbe 2019-05-12 jcs
5103 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5104 33ad4cbe 2019-05-12 jcs done:
5105 76d98825 2019-06-03 stsp free(initial_content);
5106 e0870e44 2019-05-13 stsp free(template);
5107 314a6357 2019-05-13 stsp
5108 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5109 3ce1b845 2019-07-15 stsp if (err == NULL) {
5110 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5111 3ce1b845 2019-07-15 stsp if (err) {
5112 3ce1b845 2019-07-15 stsp free(*logmsg);
5113 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5114 3ce1b845 2019-07-15 stsp }
5115 3ce1b845 2019-07-15 stsp }
5116 33ad4cbe 2019-05-12 jcs return err;
5117 6bad629b 2019-02-04 stsp }
5118 c4296144 2019-05-09 stsp
5119 c4296144 2019-05-09 stsp static const struct got_error *
5120 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5121 c4296144 2019-05-09 stsp {
5122 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5123 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5124 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5125 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5126 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5127 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5128 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5129 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5130 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5131 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5132 5c1e53bc 2019-07-28 stsp
5133 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5134 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5135 c4296144 2019-05-09 stsp
5136 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5137 c4296144 2019-05-09 stsp switch (ch) {
5138 c4296144 2019-05-09 stsp case 'm':
5139 c4296144 2019-05-09 stsp logmsg = optarg;
5140 c4296144 2019-05-09 stsp break;
5141 c4296144 2019-05-09 stsp default:
5142 c4296144 2019-05-09 stsp usage_commit();
5143 c4296144 2019-05-09 stsp /* NOTREACHED */
5144 c4296144 2019-05-09 stsp }
5145 c4296144 2019-05-09 stsp }
5146 c4296144 2019-05-09 stsp
5147 c4296144 2019-05-09 stsp argc -= optind;
5148 c4296144 2019-05-09 stsp argv += optind;
5149 c4296144 2019-05-09 stsp
5150 43012d58 2019-07-14 stsp #ifndef PROFILE
5151 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5152 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5153 43012d58 2019-07-14 stsp err(1, "pledge");
5154 43012d58 2019-07-14 stsp #endif
5155 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5156 c4296144 2019-05-09 stsp if (cwd == NULL) {
5157 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5158 c4296144 2019-05-09 stsp goto done;
5159 c4296144 2019-05-09 stsp }
5160 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5161 7d5807f4 2019-07-11 stsp if (error)
5162 7d5807f4 2019-07-11 stsp goto done;
5163 7d5807f4 2019-07-11 stsp
5164 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5165 c4296144 2019-05-09 stsp if (error)
5166 a698f62e 2019-07-25 stsp goto done;
5167 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5168 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5169 c4296144 2019-05-09 stsp goto done;
5170 a698f62e 2019-07-25 stsp }
5171 5c1e53bc 2019-07-28 stsp
5172 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5173 916f288c 2019-07-30 stsp worktree);
5174 5c1e53bc 2019-07-28 stsp if (error)
5175 5c1e53bc 2019-07-28 stsp goto done;
5176 c4296144 2019-05-09 stsp
5177 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5178 c9956ddf 2019-09-08 stsp if (error)
5179 c9956ddf 2019-09-08 stsp goto done;
5180 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5181 c9956ddf 2019-09-08 stsp gitconfig_path);
5182 c4296144 2019-05-09 stsp if (error != NULL)
5183 c4296144 2019-05-09 stsp goto done;
5184 c4296144 2019-05-09 stsp
5185 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5186 aba9c984 2019-09-08 stsp if (error)
5187 aba9c984 2019-09-08 stsp return error;
5188 aba9c984 2019-09-08 stsp
5189 314a6357 2019-05-13 stsp /*
5190 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5191 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5192 314a6357 2019-05-13 stsp */
5193 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5194 314a6357 2019-05-13 stsp error = get_editor(&editor);
5195 314a6357 2019-05-13 stsp else
5196 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5197 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5198 c4296144 2019-05-09 stsp if (error)
5199 c4296144 2019-05-09 stsp goto done;
5200 c4296144 2019-05-09 stsp
5201 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5202 087fb88c 2019-08-04 stsp if (error)
5203 087fb88c 2019-08-04 stsp goto done;
5204 087fb88c 2019-08-04 stsp
5205 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5206 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5207 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5208 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5209 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5210 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5211 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5212 916f288c 2019-07-30 stsp goto done;
5213 916f288c 2019-07-30 stsp }
5214 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5215 916f288c 2019-07-30 stsp }
5216 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5217 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5218 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5219 e0870e44 2019-05-13 stsp if (error) {
5220 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5221 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5222 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5223 c4296144 2019-05-09 stsp goto done;
5224 e0870e44 2019-05-13 stsp }
5225 c4296144 2019-05-09 stsp
5226 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5227 c4296144 2019-05-09 stsp if (error)
5228 c4296144 2019-05-09 stsp goto done;
5229 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5230 c4296144 2019-05-09 stsp done:
5231 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5232 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5233 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5234 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5235 7266f21f 2019-10-21 stsp error == NULL)
5236 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5237 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5238 c4296144 2019-05-09 stsp if (repo)
5239 c4296144 2019-05-09 stsp got_repo_close(repo);
5240 c4296144 2019-05-09 stsp if (worktree)
5241 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5242 c4296144 2019-05-09 stsp free(cwd);
5243 c4296144 2019-05-09 stsp free(id_str);
5244 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5245 e2ba3d07 2019-05-13 stsp free(editor);
5246 aba9c984 2019-09-08 stsp free(author);
5247 234035bc 2019-06-01 stsp return error;
5248 234035bc 2019-06-01 stsp }
5249 234035bc 2019-06-01 stsp
5250 234035bc 2019-06-01 stsp __dead static void
5251 234035bc 2019-06-01 stsp usage_cherrypick(void)
5252 234035bc 2019-06-01 stsp {
5253 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5254 234035bc 2019-06-01 stsp exit(1);
5255 234035bc 2019-06-01 stsp }
5256 234035bc 2019-06-01 stsp
5257 234035bc 2019-06-01 stsp static const struct got_error *
5258 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5259 234035bc 2019-06-01 stsp {
5260 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5261 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5262 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5263 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5264 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5265 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5266 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5267 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5268 234035bc 2019-06-01 stsp int ch, did_something = 0;
5269 234035bc 2019-06-01 stsp
5270 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5271 234035bc 2019-06-01 stsp switch (ch) {
5272 234035bc 2019-06-01 stsp default:
5273 234035bc 2019-06-01 stsp usage_cherrypick();
5274 234035bc 2019-06-01 stsp /* NOTREACHED */
5275 234035bc 2019-06-01 stsp }
5276 234035bc 2019-06-01 stsp }
5277 234035bc 2019-06-01 stsp
5278 234035bc 2019-06-01 stsp argc -= optind;
5279 234035bc 2019-06-01 stsp argv += optind;
5280 234035bc 2019-06-01 stsp
5281 43012d58 2019-07-14 stsp #ifndef PROFILE
5282 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5283 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5284 43012d58 2019-07-14 stsp err(1, "pledge");
5285 43012d58 2019-07-14 stsp #endif
5286 234035bc 2019-06-01 stsp if (argc != 1)
5287 234035bc 2019-06-01 stsp usage_cherrypick();
5288 234035bc 2019-06-01 stsp
5289 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5290 234035bc 2019-06-01 stsp if (cwd == NULL) {
5291 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5292 234035bc 2019-06-01 stsp goto done;
5293 234035bc 2019-06-01 stsp }
5294 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5295 234035bc 2019-06-01 stsp if (error)
5296 234035bc 2019-06-01 stsp goto done;
5297 234035bc 2019-06-01 stsp
5298 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5299 c9956ddf 2019-09-08 stsp NULL);
5300 234035bc 2019-06-01 stsp if (error != NULL)
5301 234035bc 2019-06-01 stsp goto done;
5302 234035bc 2019-06-01 stsp
5303 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5304 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5305 234035bc 2019-06-01 stsp if (error)
5306 234035bc 2019-06-01 stsp goto done;
5307 234035bc 2019-06-01 stsp
5308 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5309 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5310 234035bc 2019-06-01 stsp if (error != NULL) {
5311 234035bc 2019-06-01 stsp struct got_reference *ref;
5312 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5313 234035bc 2019-06-01 stsp goto done;
5314 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5315 234035bc 2019-06-01 stsp if (error != NULL)
5316 234035bc 2019-06-01 stsp goto done;
5317 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5318 234035bc 2019-06-01 stsp got_ref_close(ref);
5319 234035bc 2019-06-01 stsp if (error != NULL)
5320 234035bc 2019-06-01 stsp goto done;
5321 234035bc 2019-06-01 stsp }
5322 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5323 234035bc 2019-06-01 stsp if (error)
5324 234035bc 2019-06-01 stsp goto done;
5325 234035bc 2019-06-01 stsp
5326 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5327 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5328 234035bc 2019-06-01 stsp if (error != NULL)
5329 234035bc 2019-06-01 stsp goto done;
5330 234035bc 2019-06-01 stsp
5331 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5332 234035bc 2019-06-01 stsp if (error) {
5333 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5334 234035bc 2019-06-01 stsp goto done;
5335 234035bc 2019-06-01 stsp error = NULL;
5336 234035bc 2019-06-01 stsp } else {
5337 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5338 234035bc 2019-06-01 stsp goto done;
5339 234035bc 2019-06-01 stsp }
5340 234035bc 2019-06-01 stsp
5341 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5342 234035bc 2019-06-01 stsp if (error)
5343 234035bc 2019-06-01 stsp goto done;
5344 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5345 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5346 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5347 03415a1a 2019-06-02 stsp NULL);
5348 234035bc 2019-06-01 stsp if (error != NULL)
5349 234035bc 2019-06-01 stsp goto done;
5350 234035bc 2019-06-01 stsp
5351 234035bc 2019-06-01 stsp if (did_something)
5352 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5353 234035bc 2019-06-01 stsp done:
5354 234035bc 2019-06-01 stsp if (commit)
5355 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5356 234035bc 2019-06-01 stsp free(commit_id_str);
5357 234035bc 2019-06-01 stsp if (head_ref)
5358 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5359 234035bc 2019-06-01 stsp if (worktree)
5360 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5361 234035bc 2019-06-01 stsp if (repo)
5362 234035bc 2019-06-01 stsp got_repo_close(repo);
5363 c4296144 2019-05-09 stsp return error;
5364 c4296144 2019-05-09 stsp }
5365 5ef14e63 2019-06-02 stsp
5366 5ef14e63 2019-06-02 stsp __dead static void
5367 5ef14e63 2019-06-02 stsp usage_backout(void)
5368 5ef14e63 2019-06-02 stsp {
5369 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5370 5ef14e63 2019-06-02 stsp exit(1);
5371 5ef14e63 2019-06-02 stsp }
5372 5ef14e63 2019-06-02 stsp
5373 5ef14e63 2019-06-02 stsp static const struct got_error *
5374 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5375 5ef14e63 2019-06-02 stsp {
5376 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5377 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5378 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5379 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5380 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5381 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5382 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5383 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5384 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5385 5ef14e63 2019-06-02 stsp
5386 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5387 5ef14e63 2019-06-02 stsp switch (ch) {
5388 5ef14e63 2019-06-02 stsp default:
5389 5ef14e63 2019-06-02 stsp usage_backout();
5390 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5391 5ef14e63 2019-06-02 stsp }
5392 5ef14e63 2019-06-02 stsp }
5393 5ef14e63 2019-06-02 stsp
5394 5ef14e63 2019-06-02 stsp argc -= optind;
5395 5ef14e63 2019-06-02 stsp argv += optind;
5396 5ef14e63 2019-06-02 stsp
5397 43012d58 2019-07-14 stsp #ifndef PROFILE
5398 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5399 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5400 43012d58 2019-07-14 stsp err(1, "pledge");
5401 43012d58 2019-07-14 stsp #endif
5402 5ef14e63 2019-06-02 stsp if (argc != 1)
5403 5ef14e63 2019-06-02 stsp usage_backout();
5404 5ef14e63 2019-06-02 stsp
5405 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5406 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5407 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5408 5ef14e63 2019-06-02 stsp goto done;
5409 5ef14e63 2019-06-02 stsp }
5410 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5411 5ef14e63 2019-06-02 stsp if (error)
5412 5ef14e63 2019-06-02 stsp goto done;
5413 5ef14e63 2019-06-02 stsp
5414 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5415 c9956ddf 2019-09-08 stsp NULL);
5416 5ef14e63 2019-06-02 stsp if (error != NULL)
5417 5ef14e63 2019-06-02 stsp goto done;
5418 5ef14e63 2019-06-02 stsp
5419 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5420 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5421 5ef14e63 2019-06-02 stsp if (error)
5422 5ef14e63 2019-06-02 stsp goto done;
5423 5ef14e63 2019-06-02 stsp
5424 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5425 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5426 5ef14e63 2019-06-02 stsp if (error != NULL) {
5427 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5428 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5429 5ef14e63 2019-06-02 stsp goto done;
5430 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5431 5ef14e63 2019-06-02 stsp if (error != NULL)
5432 5ef14e63 2019-06-02 stsp goto done;
5433 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5434 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5435 5ef14e63 2019-06-02 stsp if (error != NULL)
5436 5ef14e63 2019-06-02 stsp goto done;
5437 5ef14e63 2019-06-02 stsp }
5438 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5439 5ef14e63 2019-06-02 stsp if (error)
5440 5ef14e63 2019-06-02 stsp goto done;
5441 5ef14e63 2019-06-02 stsp
5442 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5443 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5444 5ef14e63 2019-06-02 stsp if (error != NULL)
5445 5ef14e63 2019-06-02 stsp goto done;
5446 5ef14e63 2019-06-02 stsp
5447 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5448 5ef14e63 2019-06-02 stsp if (error)
5449 5ef14e63 2019-06-02 stsp goto done;
5450 5ef14e63 2019-06-02 stsp
5451 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5452 5ef14e63 2019-06-02 stsp if (error)
5453 5ef14e63 2019-06-02 stsp goto done;
5454 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5455 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5456 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5457 5ef14e63 2019-06-02 stsp goto done;
5458 5ef14e63 2019-06-02 stsp }
5459 5ef14e63 2019-06-02 stsp
5460 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5461 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5462 5ef14e63 2019-06-02 stsp if (error != NULL)
5463 5ef14e63 2019-06-02 stsp goto done;
5464 5ef14e63 2019-06-02 stsp
5465 5ef14e63 2019-06-02 stsp if (did_something)
5466 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5467 5ef14e63 2019-06-02 stsp done:
5468 5ef14e63 2019-06-02 stsp if (commit)
5469 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5470 5ef14e63 2019-06-02 stsp free(commit_id_str);
5471 5ef14e63 2019-06-02 stsp if (head_ref)
5472 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5473 818c7501 2019-07-11 stsp if (worktree)
5474 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5475 818c7501 2019-07-11 stsp if (repo)
5476 818c7501 2019-07-11 stsp got_repo_close(repo);
5477 818c7501 2019-07-11 stsp return error;
5478 818c7501 2019-07-11 stsp }
5479 818c7501 2019-07-11 stsp
5480 818c7501 2019-07-11 stsp __dead static void
5481 818c7501 2019-07-11 stsp usage_rebase(void)
5482 818c7501 2019-07-11 stsp {
5483 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5484 af54c8f8 2019-07-11 stsp getprogname());
5485 818c7501 2019-07-11 stsp exit(1);
5486 0ebf8283 2019-07-24 stsp }
5487 0ebf8283 2019-07-24 stsp
5488 0ebf8283 2019-07-24 stsp void
5489 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5490 0ebf8283 2019-07-24 stsp {
5491 0ebf8283 2019-07-24 stsp char *nl;
5492 0ebf8283 2019-07-24 stsp size_t len;
5493 0ebf8283 2019-07-24 stsp
5494 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5495 0ebf8283 2019-07-24 stsp if (len > limit)
5496 0ebf8283 2019-07-24 stsp len = limit;
5497 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5498 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5499 0ebf8283 2019-07-24 stsp if (nl)
5500 0ebf8283 2019-07-24 stsp *nl = '\0';
5501 0ebf8283 2019-07-24 stsp }
5502 0ebf8283 2019-07-24 stsp
5503 0ebf8283 2019-07-24 stsp static const struct got_error *
5504 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5505 0ebf8283 2019-07-24 stsp {
5506 5943eee2 2019-08-13 stsp const struct got_error *err;
5507 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5508 5943eee2 2019-08-13 stsp const char *s;
5509 0ebf8283 2019-07-24 stsp
5510 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5511 5943eee2 2019-08-13 stsp if (err)
5512 5943eee2 2019-08-13 stsp return err;
5513 0ebf8283 2019-07-24 stsp
5514 5943eee2 2019-08-13 stsp s = logmsg0;
5515 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5516 5943eee2 2019-08-13 stsp s++;
5517 5943eee2 2019-08-13 stsp
5518 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5519 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5520 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5521 5943eee2 2019-08-13 stsp goto done;
5522 5943eee2 2019-08-13 stsp }
5523 0ebf8283 2019-07-24 stsp
5524 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5525 5943eee2 2019-08-13 stsp done:
5526 5943eee2 2019-08-13 stsp free(logmsg0);
5527 a0ea4fc0 2020-02-28 stsp return err;
5528 a0ea4fc0 2020-02-28 stsp }
5529 a0ea4fc0 2020-02-28 stsp
5530 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5531 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5532 a0ea4fc0 2020-02-28 stsp {
5533 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5534 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5535 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5536 a0ea4fc0 2020-02-28 stsp
5537 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5538 a0ea4fc0 2020-02-28 stsp if (err)
5539 a0ea4fc0 2020-02-28 stsp return err;
5540 a0ea4fc0 2020-02-28 stsp
5541 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5542 a0ea4fc0 2020-02-28 stsp if (err)
5543 a0ea4fc0 2020-02-28 stsp goto done;
5544 a0ea4fc0 2020-02-28 stsp
5545 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5546 a0ea4fc0 2020-02-28 stsp
5547 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5548 a0ea4fc0 2020-02-28 stsp if (err)
5549 a0ea4fc0 2020-02-28 stsp goto done;
5550 a0ea4fc0 2020-02-28 stsp
5551 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5552 a0ea4fc0 2020-02-28 stsp done:
5553 a0ea4fc0 2020-02-28 stsp free(id_str);
5554 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5555 a0ea4fc0 2020-02-28 stsp free(logmsg);
5556 5943eee2 2019-08-13 stsp return err;
5557 818c7501 2019-07-11 stsp }
5558 818c7501 2019-07-11 stsp
5559 818c7501 2019-07-11 stsp static const struct got_error *
5560 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5561 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5562 818c7501 2019-07-11 stsp {
5563 818c7501 2019-07-11 stsp const struct got_error *err;
5564 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5565 818c7501 2019-07-11 stsp
5566 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5567 818c7501 2019-07-11 stsp if (err)
5568 818c7501 2019-07-11 stsp goto done;
5569 818c7501 2019-07-11 stsp
5570 ff0d2220 2019-07-11 stsp if (new_id) {
5571 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5572 ff0d2220 2019-07-11 stsp if (err)
5573 ff0d2220 2019-07-11 stsp goto done;
5574 ff0d2220 2019-07-11 stsp }
5575 818c7501 2019-07-11 stsp
5576 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5577 ff0d2220 2019-07-11 stsp if (new_id_str)
5578 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5579 0ebf8283 2019-07-24 stsp
5580 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5581 0ebf8283 2019-07-24 stsp if (err)
5582 0ebf8283 2019-07-24 stsp goto done;
5583 0ebf8283 2019-07-24 stsp
5584 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5585 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5586 818c7501 2019-07-11 stsp done:
5587 818c7501 2019-07-11 stsp free(old_id_str);
5588 818c7501 2019-07-11 stsp free(new_id_str);
5589 272a1371 2020-02-28 stsp free(logmsg);
5590 818c7501 2019-07-11 stsp return err;
5591 818c7501 2019-07-11 stsp }
5592 818c7501 2019-07-11 stsp
5593 1ee397ad 2019-07-12 stsp static const struct got_error *
5594 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5595 818c7501 2019-07-11 stsp {
5596 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5597 818c7501 2019-07-11 stsp
5598 818c7501 2019-07-11 stsp while (path[0] == '/')
5599 818c7501 2019-07-11 stsp path++;
5600 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5601 818c7501 2019-07-11 stsp
5602 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5603 1ee397ad 2019-07-12 stsp return NULL;
5604 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5605 818c7501 2019-07-11 stsp *rebase_status = status;
5606 1ee397ad 2019-07-12 stsp return NULL;
5607 818c7501 2019-07-11 stsp }
5608 818c7501 2019-07-11 stsp
5609 818c7501 2019-07-11 stsp static const struct got_error *
5610 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5611 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5612 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5613 818c7501 2019-07-11 stsp {
5614 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5615 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5616 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5617 818c7501 2019-07-11 stsp }
5618 818c7501 2019-07-11 stsp
5619 818c7501 2019-07-11 stsp static const struct got_error *
5620 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5621 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5622 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5623 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5624 ff0d2220 2019-07-11 stsp {
5625 ff0d2220 2019-07-11 stsp const struct got_error *error;
5626 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5627 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5628 ff0d2220 2019-07-11 stsp
5629 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5630 ff0d2220 2019-07-11 stsp if (error)
5631 ff0d2220 2019-07-11 stsp return error;
5632 ff0d2220 2019-07-11 stsp
5633 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5634 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5635 ff0d2220 2019-07-11 stsp if (error) {
5636 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5637 ff0d2220 2019-07-11 stsp goto done;
5638 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5639 ff0d2220 2019-07-11 stsp } else {
5640 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5641 ff0d2220 2019-07-11 stsp free(new_commit_id);
5642 ff0d2220 2019-07-11 stsp }
5643 ff0d2220 2019-07-11 stsp done:
5644 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5645 ff0d2220 2019-07-11 stsp return error;
5646 64c6d990 2019-07-11 stsp }
5647 64c6d990 2019-07-11 stsp
5648 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5649 64c6d990 2019-07-11 stsp const char *path_prefix;
5650 64c6d990 2019-07-11 stsp size_t len;
5651 8ca9bd68 2019-07-25 stsp int errcode;
5652 64c6d990 2019-07-11 stsp };
5653 64c6d990 2019-07-11 stsp
5654 64c6d990 2019-07-11 stsp static const struct got_error *
5655 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5656 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5657 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5658 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5659 64c6d990 2019-07-11 stsp {
5660 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5661 64c6d990 2019-07-11 stsp
5662 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5663 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5664 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5665 64c6d990 2019-07-11 stsp
5666 64c6d990 2019-07-11 stsp return NULL;
5667 64c6d990 2019-07-11 stsp }
5668 64c6d990 2019-07-11 stsp
5669 64c6d990 2019-07-11 stsp static const struct got_error *
5670 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5671 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5672 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5673 64c6d990 2019-07-11 stsp {
5674 64c6d990 2019-07-11 stsp const struct got_error *err;
5675 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5676 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5677 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5678 64c6d990 2019-07-11 stsp
5679 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5680 64c6d990 2019-07-11 stsp return NULL;
5681 64c6d990 2019-07-11 stsp
5682 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5683 64c6d990 2019-07-11 stsp if (err)
5684 64c6d990 2019-07-11 stsp goto done;
5685 64c6d990 2019-07-11 stsp
5686 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5687 64c6d990 2019-07-11 stsp if (err)
5688 64c6d990 2019-07-11 stsp goto done;
5689 64c6d990 2019-07-11 stsp
5690 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5691 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5692 64c6d990 2019-07-11 stsp if (err)
5693 64c6d990 2019-07-11 stsp goto done;
5694 64c6d990 2019-07-11 stsp
5695 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5696 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5697 64c6d990 2019-07-11 stsp if (err)
5698 64c6d990 2019-07-11 stsp goto done;
5699 64c6d990 2019-07-11 stsp
5700 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5701 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5702 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5703 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5704 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5705 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5706 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5707 64c6d990 2019-07-11 stsp done:
5708 64c6d990 2019-07-11 stsp if (tree1)
5709 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5710 64c6d990 2019-07-11 stsp if (tree2)
5711 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5712 64c6d990 2019-07-11 stsp if (commit)
5713 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5714 64c6d990 2019-07-11 stsp if (parent_commit)
5715 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5716 64c6d990 2019-07-11 stsp return err;
5717 ff0d2220 2019-07-11 stsp }
5718 ff0d2220 2019-07-11 stsp
5719 ff0d2220 2019-07-11 stsp static const struct got_error *
5720 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5721 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5722 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5723 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5724 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5725 af61c510 2019-07-19 stsp {
5726 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5727 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5728 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5729 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5730 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5731 af61c510 2019-07-19 stsp
5732 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5733 af61c510 2019-07-19 stsp if (err)
5734 af61c510 2019-07-19 stsp return err;
5735 af61c510 2019-07-19 stsp
5736 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5737 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5738 af61c510 2019-07-19 stsp if (err)
5739 af61c510 2019-07-19 stsp goto done;
5740 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5741 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5742 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5743 af61c510 2019-07-19 stsp if (err) {
5744 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5745 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5746 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5747 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5748 af61c510 2019-07-19 stsp "been reached?!?");
5749 ee780d5c 2020-01-04 stsp }
5750 ee780d5c 2020-01-04 stsp goto done;
5751 af61c510 2019-07-19 stsp } else {
5752 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5753 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5754 af61c510 2019-07-19 stsp if (err)
5755 af61c510 2019-07-19 stsp goto done;
5756 af61c510 2019-07-19 stsp
5757 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5758 af61c510 2019-07-19 stsp if (err)
5759 af61c510 2019-07-19 stsp goto done;
5760 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5761 af61c510 2019-07-19 stsp commit_id = parent_id;
5762 af61c510 2019-07-19 stsp }
5763 af61c510 2019-07-19 stsp }
5764 af61c510 2019-07-19 stsp done:
5765 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5766 af61c510 2019-07-19 stsp return err;
5767 af61c510 2019-07-19 stsp }
5768 af61c510 2019-07-19 stsp
5769 af61c510 2019-07-19 stsp static const struct got_error *
5770 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5771 818c7501 2019-07-11 stsp {
5772 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5773 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5774 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5775 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5776 818c7501 2019-07-11 stsp char *cwd = NULL;
5777 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5778 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5779 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5780 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5781 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5782 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5783 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5784 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5785 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5786 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5787 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5788 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5789 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5790 818c7501 2019-07-11 stsp
5791 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5792 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5793 818c7501 2019-07-11 stsp
5794 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5795 818c7501 2019-07-11 stsp switch (ch) {
5796 818c7501 2019-07-11 stsp case 'a':
5797 818c7501 2019-07-11 stsp abort_rebase = 1;
5798 818c7501 2019-07-11 stsp break;
5799 818c7501 2019-07-11 stsp case 'c':
5800 818c7501 2019-07-11 stsp continue_rebase = 1;
5801 818c7501 2019-07-11 stsp break;
5802 818c7501 2019-07-11 stsp default:
5803 818c7501 2019-07-11 stsp usage_rebase();
5804 818c7501 2019-07-11 stsp /* NOTREACHED */
5805 818c7501 2019-07-11 stsp }
5806 818c7501 2019-07-11 stsp }
5807 818c7501 2019-07-11 stsp
5808 818c7501 2019-07-11 stsp argc -= optind;
5809 818c7501 2019-07-11 stsp argv += optind;
5810 818c7501 2019-07-11 stsp
5811 43012d58 2019-07-14 stsp #ifndef PROFILE
5812 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5813 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5814 43012d58 2019-07-14 stsp err(1, "pledge");
5815 43012d58 2019-07-14 stsp #endif
5816 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5817 818c7501 2019-07-11 stsp usage_rebase();
5818 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5819 818c7501 2019-07-11 stsp if (argc != 0)
5820 818c7501 2019-07-11 stsp usage_rebase();
5821 818c7501 2019-07-11 stsp } else if (argc != 1)
5822 818c7501 2019-07-11 stsp usage_rebase();
5823 818c7501 2019-07-11 stsp
5824 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5825 818c7501 2019-07-11 stsp if (cwd == NULL) {
5826 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5827 818c7501 2019-07-11 stsp goto done;
5828 818c7501 2019-07-11 stsp }
5829 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5830 818c7501 2019-07-11 stsp if (error)
5831 818c7501 2019-07-11 stsp goto done;
5832 818c7501 2019-07-11 stsp
5833 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5834 c9956ddf 2019-09-08 stsp NULL);
5835 818c7501 2019-07-11 stsp if (error != NULL)
5836 818c7501 2019-07-11 stsp goto done;
5837 818c7501 2019-07-11 stsp
5838 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5839 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5840 7ef62c4e 2020-02-24 stsp if (error)
5841 7ef62c4e 2020-02-24 stsp goto done;
5842 7ef62c4e 2020-02-24 stsp
5843 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5844 7ef62c4e 2020-02-24 stsp worktree);
5845 818c7501 2019-07-11 stsp if (error)
5846 7ef62c4e 2020-02-24 stsp goto done;
5847 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5848 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5849 818c7501 2019-07-11 stsp goto done;
5850 7ef62c4e 2020-02-24 stsp }
5851 818c7501 2019-07-11 stsp
5852 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5853 818c7501 2019-07-11 stsp if (error)
5854 818c7501 2019-07-11 stsp goto done;
5855 818c7501 2019-07-11 stsp
5856 f6794adc 2019-07-23 stsp if (abort_rebase) {
5857 818c7501 2019-07-11 stsp int did_something;
5858 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5859 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5860 f6794adc 2019-07-23 stsp goto done;
5861 f6794adc 2019-07-23 stsp }
5862 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5863 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5864 3e3a69f1 2019-07-25 stsp worktree, repo);
5865 818c7501 2019-07-11 stsp if (error)
5866 818c7501 2019-07-11 stsp goto done;
5867 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5868 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5869 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5870 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5871 818c7501 2019-07-11 stsp if (error)
5872 818c7501 2019-07-11 stsp goto done;
5873 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5874 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5875 818c7501 2019-07-11 stsp }
5876 818c7501 2019-07-11 stsp
5877 818c7501 2019-07-11 stsp if (continue_rebase) {
5878 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5879 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5880 f6794adc 2019-07-23 stsp goto done;
5881 f6794adc 2019-07-23 stsp }
5882 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5883 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5884 3e3a69f1 2019-07-25 stsp worktree, repo);
5885 818c7501 2019-07-11 stsp if (error)
5886 818c7501 2019-07-11 stsp goto done;
5887 818c7501 2019-07-11 stsp
5888 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5889 01757395 2019-07-12 stsp resume_commit_id, repo);
5890 818c7501 2019-07-11 stsp if (error)
5891 818c7501 2019-07-11 stsp goto done;
5892 818c7501 2019-07-11 stsp
5893 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5894 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5895 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5896 818c7501 2019-07-11 stsp goto done;
5897 818c7501 2019-07-11 stsp }
5898 818c7501 2019-07-11 stsp } else {
5899 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5900 818c7501 2019-07-11 stsp if (error != NULL)
5901 818c7501 2019-07-11 stsp goto done;
5902 ff0d2220 2019-07-11 stsp }
5903 818c7501 2019-07-11 stsp
5904 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5905 ff0d2220 2019-07-11 stsp if (error)
5906 ff0d2220 2019-07-11 stsp goto done;
5907 ff0d2220 2019-07-11 stsp
5908 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5909 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5910 a51a74b3 2019-07-27 stsp
5911 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5912 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5913 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5914 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5915 818c7501 2019-07-11 stsp if (error)
5916 818c7501 2019-07-11 stsp goto done;
5917 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5918 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5919 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5920 818c7501 2019-07-11 stsp "with work tree's branch");
5921 818c7501 2019-07-11 stsp goto done;
5922 818c7501 2019-07-11 stsp }
5923 818c7501 2019-07-11 stsp
5924 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5925 a51a74b3 2019-07-27 stsp if (error) {
5926 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5927 a51a74b3 2019-07-27 stsp goto done;
5928 a51a74b3 2019-07-27 stsp error = NULL;
5929 a51a74b3 2019-07-27 stsp } else {
5930 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5931 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5932 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5933 a51a74b3 2019-07-27 stsp goto done;
5934 a51a74b3 2019-07-27 stsp }
5935 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5936 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5937 818c7501 2019-07-11 stsp if (error)
5938 818c7501 2019-07-11 stsp goto done;
5939 818c7501 2019-07-11 stsp }
5940 818c7501 2019-07-11 stsp
5941 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5942 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5943 818c7501 2019-07-11 stsp if (error)
5944 818c7501 2019-07-11 stsp goto done;
5945 818c7501 2019-07-11 stsp
5946 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5947 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5948 fc66b545 2019-08-12 stsp if (pid == NULL) {
5949 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5950 fc66b545 2019-08-12 stsp int did_something;
5951 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5952 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5953 fc66b545 2019-08-12 stsp &did_something);
5954 fc66b545 2019-08-12 stsp if (error)
5955 fc66b545 2019-08-12 stsp goto done;
5956 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5957 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5958 fc66b545 2019-08-12 stsp }
5959 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5960 fc66b545 2019-08-12 stsp goto done;
5961 fc66b545 2019-08-12 stsp }
5962 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5963 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5964 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5965 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5966 818c7501 2019-07-11 stsp commit = NULL;
5967 818c7501 2019-07-11 stsp if (error)
5968 818c7501 2019-07-11 stsp goto done;
5969 64c6d990 2019-07-11 stsp
5970 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5971 38b0338b 2019-11-29 stsp if (continue_rebase) {
5972 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5973 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5974 38b0338b 2019-11-29 stsp goto done;
5975 38b0338b 2019-11-29 stsp } else {
5976 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5977 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5978 38b0338b 2019-11-29 stsp char *id_str;
5979 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5980 38b0338b 2019-11-29 stsp new_base_branch);
5981 38b0338b 2019-11-29 stsp if (error)
5982 38b0338b 2019-11-29 stsp goto done;
5983 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5984 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5985 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5986 38b0338b 2019-11-29 stsp free(id_str);
5987 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5988 38b0338b 2019-11-29 stsp new_head_commit_id);
5989 38b0338b 2019-11-29 stsp if (error)
5990 38b0338b 2019-11-29 stsp goto done;
5991 38b0338b 2019-11-29 stsp }
5992 818c7501 2019-07-11 stsp }
5993 818c7501 2019-07-11 stsp
5994 818c7501 2019-07-11 stsp pid = NULL;
5995 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5996 818c7501 2019-07-11 stsp commit_id = qid->id;
5997 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5998 818c7501 2019-07-11 stsp pid = qid;
5999 818c7501 2019-07-11 stsp
6000 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6001 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6002 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6003 818c7501 2019-07-11 stsp if (error)
6004 818c7501 2019-07-11 stsp goto done;
6005 818c7501 2019-07-11 stsp
6006 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6007 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6008 a0ea4fc0 2020-02-28 stsp if (error)
6009 a0ea4fc0 2020-02-28 stsp goto done;
6010 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6011 818c7501 2019-07-11 stsp break;
6012 01757395 2019-07-12 stsp }
6013 818c7501 2019-07-11 stsp
6014 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6015 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6016 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6017 818c7501 2019-07-11 stsp if (error)
6018 818c7501 2019-07-11 stsp goto done;
6019 818c7501 2019-07-11 stsp }
6020 818c7501 2019-07-11 stsp
6021 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6022 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6023 818c7501 2019-07-11 stsp if (error)
6024 818c7501 2019-07-11 stsp goto done;
6025 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6026 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6027 818c7501 2019-07-11 stsp } else
6028 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6029 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6030 818c7501 2019-07-11 stsp done:
6031 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6032 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6033 818c7501 2019-07-11 stsp free(resume_commit_id);
6034 818c7501 2019-07-11 stsp free(yca_id);
6035 818c7501 2019-07-11 stsp if (commit)
6036 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6037 818c7501 2019-07-11 stsp if (branch)
6038 818c7501 2019-07-11 stsp got_ref_close(branch);
6039 818c7501 2019-07-11 stsp if (new_base_branch)
6040 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6041 818c7501 2019-07-11 stsp if (tmp_branch)
6042 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6043 5ef14e63 2019-06-02 stsp if (worktree)
6044 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6045 5ef14e63 2019-06-02 stsp if (repo)
6046 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6047 5ef14e63 2019-06-02 stsp return error;
6048 0ebf8283 2019-07-24 stsp }
6049 0ebf8283 2019-07-24 stsp
6050 0ebf8283 2019-07-24 stsp __dead static void
6051 0ebf8283 2019-07-24 stsp usage_histedit(void)
6052 0ebf8283 2019-07-24 stsp {
6053 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6054 0ebf8283 2019-07-24 stsp getprogname());
6055 0ebf8283 2019-07-24 stsp exit(1);
6056 0ebf8283 2019-07-24 stsp }
6057 0ebf8283 2019-07-24 stsp
6058 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6059 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6060 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6061 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6062 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6063 0ebf8283 2019-07-24 stsp
6064 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6065 0ebf8283 2019-07-24 stsp unsigned char code;
6066 0ebf8283 2019-07-24 stsp const char *name;
6067 0ebf8283 2019-07-24 stsp const char *desc;
6068 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6069 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6070 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6071 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6072 82997472 2020-01-29 stsp "be used" },
6073 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6074 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6075 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6076 0ebf8283 2019-07-24 stsp };
6077 0ebf8283 2019-07-24 stsp
6078 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6079 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6080 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6081 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6082 0ebf8283 2019-07-24 stsp char *logmsg;
6083 0ebf8283 2019-07-24 stsp };
6084 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6085 0ebf8283 2019-07-24 stsp
6086 0ebf8283 2019-07-24 stsp static const struct got_error *
6087 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6088 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6089 0ebf8283 2019-07-24 stsp {
6090 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6091 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6092 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6093 8138f3e1 2019-08-11 stsp int n;
6094 0ebf8283 2019-07-24 stsp
6095 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6096 0ebf8283 2019-07-24 stsp if (err)
6097 0ebf8283 2019-07-24 stsp goto done;
6098 0ebf8283 2019-07-24 stsp
6099 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6100 0ebf8283 2019-07-24 stsp if (err)
6101 0ebf8283 2019-07-24 stsp goto done;
6102 0ebf8283 2019-07-24 stsp
6103 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6104 0ebf8283 2019-07-24 stsp if (err)
6105 0ebf8283 2019-07-24 stsp goto done;
6106 0ebf8283 2019-07-24 stsp
6107 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6108 0ebf8283 2019-07-24 stsp if (n < 0)
6109 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6110 0ebf8283 2019-07-24 stsp done:
6111 0ebf8283 2019-07-24 stsp if (commit)
6112 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6113 0ebf8283 2019-07-24 stsp free(id_str);
6114 0ebf8283 2019-07-24 stsp free(logmsg);
6115 0ebf8283 2019-07-24 stsp return err;
6116 0ebf8283 2019-07-24 stsp }
6117 0ebf8283 2019-07-24 stsp
6118 0ebf8283 2019-07-24 stsp static const struct got_error *
6119 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6120 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6121 0ebf8283 2019-07-24 stsp {
6122 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6123 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6124 0ebf8283 2019-07-24 stsp
6125 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6126 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6127 0ebf8283 2019-07-24 stsp
6128 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6129 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6130 0ebf8283 2019-07-24 stsp f, repo);
6131 0ebf8283 2019-07-24 stsp if (err)
6132 0ebf8283 2019-07-24 stsp break;
6133 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6134 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6135 083957f4 2020-02-24 stsp if (n < 0) {
6136 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6137 083957f4 2020-02-24 stsp break;
6138 083957f4 2020-02-24 stsp }
6139 083957f4 2020-02-24 stsp }
6140 0ebf8283 2019-07-24 stsp }
6141 0ebf8283 2019-07-24 stsp
6142 0ebf8283 2019-07-24 stsp return err;
6143 0ebf8283 2019-07-24 stsp }
6144 0ebf8283 2019-07-24 stsp
6145 0ebf8283 2019-07-24 stsp static const struct got_error *
6146 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6147 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6148 0ebf8283 2019-07-24 stsp {
6149 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6150 0ebf8283 2019-07-24 stsp int n, i;
6151 514f2ffe 2020-01-29 stsp char *id_str;
6152 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6153 514f2ffe 2020-01-29 stsp
6154 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6155 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6156 514f2ffe 2020-01-29 stsp if (err)
6157 514f2ffe 2020-01-29 stsp return err;
6158 514f2ffe 2020-01-29 stsp
6159 514f2ffe 2020-01-29 stsp n = fprintf(f,
6160 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6161 514f2ffe 2020-01-29 stsp "# commit %s\n"
6162 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6163 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6164 514f2ffe 2020-01-29 stsp if (n < 0) {
6165 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6166 514f2ffe 2020-01-29 stsp goto done;
6167 514f2ffe 2020-01-29 stsp }
6168 0ebf8283 2019-07-24 stsp
6169 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6170 514f2ffe 2020-01-29 stsp if (n < 0) {
6171 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6172 514f2ffe 2020-01-29 stsp goto done;
6173 514f2ffe 2020-01-29 stsp }
6174 0ebf8283 2019-07-24 stsp
6175 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6176 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6177 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6178 0ebf8283 2019-07-24 stsp cmd->desc);
6179 0ebf8283 2019-07-24 stsp if (n < 0) {
6180 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6181 0ebf8283 2019-07-24 stsp break;
6182 0ebf8283 2019-07-24 stsp }
6183 0ebf8283 2019-07-24 stsp }
6184 514f2ffe 2020-01-29 stsp done:
6185 514f2ffe 2020-01-29 stsp free(id_str);
6186 0ebf8283 2019-07-24 stsp return err;
6187 0ebf8283 2019-07-24 stsp }
6188 0ebf8283 2019-07-24 stsp
6189 0ebf8283 2019-07-24 stsp static const struct got_error *
6190 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6191 0ebf8283 2019-07-24 stsp {
6192 0ebf8283 2019-07-24 stsp static char msg[42];
6193 0ebf8283 2019-07-24 stsp int ret;
6194 0ebf8283 2019-07-24 stsp
6195 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6196 0ebf8283 2019-07-24 stsp lineno);
6197 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6198 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6199 0ebf8283 2019-07-24 stsp
6200 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6201 0ebf8283 2019-07-24 stsp }
6202 0ebf8283 2019-07-24 stsp
6203 0ebf8283 2019-07-24 stsp static const struct got_error *
6204 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6205 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6206 0ebf8283 2019-07-24 stsp {
6207 0ebf8283 2019-07-24 stsp const struct got_error *err;
6208 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6209 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6210 0ebf8283 2019-07-24 stsp
6211 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6212 0ebf8283 2019-07-24 stsp if (err)
6213 0ebf8283 2019-07-24 stsp return err;
6214 0ebf8283 2019-07-24 stsp
6215 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6216 0ebf8283 2019-07-24 stsp if (err)
6217 0ebf8283 2019-07-24 stsp goto done;
6218 0ebf8283 2019-07-24 stsp
6219 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6220 5943eee2 2019-08-13 stsp if (err)
6221 5943eee2 2019-08-13 stsp goto done;
6222 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6223 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6224 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6225 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6226 0ebf8283 2019-07-24 stsp }
6227 0ebf8283 2019-07-24 stsp done:
6228 0ebf8283 2019-07-24 stsp if (folded_commit)
6229 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6230 0ebf8283 2019-07-24 stsp free(id_str);
6231 5943eee2 2019-08-13 stsp free(folded_logmsg);
6232 0ebf8283 2019-07-24 stsp return err;
6233 0ebf8283 2019-07-24 stsp }
6234 0ebf8283 2019-07-24 stsp
6235 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6236 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6237 0ebf8283 2019-07-24 stsp {
6238 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6239 0ebf8283 2019-07-24 stsp
6240 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6241 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6242 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6243 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6244 3f9de99f 2019-07-24 stsp folded = prev;
6245 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6246 0ebf8283 2019-07-24 stsp }
6247 0ebf8283 2019-07-24 stsp
6248 0ebf8283 2019-07-24 stsp return folded;
6249 0ebf8283 2019-07-24 stsp }
6250 0ebf8283 2019-07-24 stsp
6251 0ebf8283 2019-07-24 stsp static const struct got_error *
6252 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6253 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6254 0ebf8283 2019-07-24 stsp {
6255 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6256 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6257 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6258 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6259 0ebf8283 2019-07-24 stsp int fd;
6260 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6261 0ebf8283 2019-07-24 stsp
6262 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6263 0ebf8283 2019-07-24 stsp if (err)
6264 0ebf8283 2019-07-24 stsp return err;
6265 0ebf8283 2019-07-24 stsp
6266 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6267 0ebf8283 2019-07-24 stsp if (folded) {
6268 0ebf8283 2019-07-24 stsp while (folded != hle) {
6269 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6270 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6271 3f9de99f 2019-07-24 stsp continue;
6272 3f9de99f 2019-07-24 stsp }
6273 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6274 0ebf8283 2019-07-24 stsp logmsg, repo);
6275 0ebf8283 2019-07-24 stsp if (err)
6276 0ebf8283 2019-07-24 stsp goto done;
6277 0ebf8283 2019-07-24 stsp free(logmsg);
6278 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6279 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6280 0ebf8283 2019-07-24 stsp }
6281 0ebf8283 2019-07-24 stsp }
6282 0ebf8283 2019-07-24 stsp
6283 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6284 0ebf8283 2019-07-24 stsp if (err)
6285 0ebf8283 2019-07-24 stsp goto done;
6286 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6287 5943eee2 2019-08-13 stsp if (err)
6288 5943eee2 2019-08-13 stsp goto done;
6289 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6290 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6291 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6292 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6293 0ebf8283 2019-07-24 stsp goto done;
6294 0ebf8283 2019-07-24 stsp }
6295 0ebf8283 2019-07-24 stsp free(logmsg);
6296 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6297 0ebf8283 2019-07-24 stsp
6298 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6299 0ebf8283 2019-07-24 stsp if (err)
6300 0ebf8283 2019-07-24 stsp goto done;
6301 0ebf8283 2019-07-24 stsp
6302 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6303 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6304 0ebf8283 2019-07-24 stsp if (err)
6305 0ebf8283 2019-07-24 stsp goto done;
6306 0ebf8283 2019-07-24 stsp
6307 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6308 0ebf8283 2019-07-24 stsp close(fd);
6309 0ebf8283 2019-07-24 stsp
6310 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6311 0ebf8283 2019-07-24 stsp if (err)
6312 0ebf8283 2019-07-24 stsp goto done;
6313 0ebf8283 2019-07-24 stsp
6314 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6315 0ebf8283 2019-07-24 stsp if (err) {
6316 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6317 0ebf8283 2019-07-24 stsp goto done;
6318 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6319 0ebf8283 2019-07-24 stsp }
6320 0ebf8283 2019-07-24 stsp done:
6321 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6322 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6323 0ebf8283 2019-07-24 stsp free(logmsg_path);
6324 0ebf8283 2019-07-24 stsp free(logmsg);
6325 5943eee2 2019-08-13 stsp free(orig_logmsg);
6326 0ebf8283 2019-07-24 stsp free(editor);
6327 0ebf8283 2019-07-24 stsp if (commit)
6328 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6329 0ebf8283 2019-07-24 stsp return err;
6330 5ef14e63 2019-06-02 stsp }
6331 0ebf8283 2019-07-24 stsp
6332 0ebf8283 2019-07-24 stsp static const struct got_error *
6333 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6334 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6335 0ebf8283 2019-07-24 stsp {
6336 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6337 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6338 0ebf8283 2019-07-24 stsp size_t size;
6339 0ebf8283 2019-07-24 stsp ssize_t len;
6340 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6341 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6342 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6343 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6344 0ebf8283 2019-07-24 stsp
6345 0ebf8283 2019-07-24 stsp for (;;) {
6346 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6347 0ebf8283 2019-07-24 stsp if (len == -1) {
6348 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6349 0ebf8283 2019-07-24 stsp if (feof(f))
6350 0ebf8283 2019-07-24 stsp break;
6351 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6352 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6353 0ebf8283 2019-07-24 stsp break;
6354 0ebf8283 2019-07-24 stsp }
6355 0ebf8283 2019-07-24 stsp lineno++;
6356 0ebf8283 2019-07-24 stsp p = line;
6357 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6358 0ebf8283 2019-07-24 stsp p++;
6359 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6360 0ebf8283 2019-07-24 stsp free(line);
6361 0ebf8283 2019-07-24 stsp line = NULL;
6362 0ebf8283 2019-07-24 stsp continue;
6363 0ebf8283 2019-07-24 stsp }
6364 0ebf8283 2019-07-24 stsp cmd = NULL;
6365 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6366 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6367 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6368 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6369 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6370 0ebf8283 2019-07-24 stsp break;
6371 0ebf8283 2019-07-24 stsp }
6372 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6373 0ebf8283 2019-07-24 stsp p++;
6374 0ebf8283 2019-07-24 stsp break;
6375 0ebf8283 2019-07-24 stsp }
6376 0ebf8283 2019-07-24 stsp }
6377 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6378 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6379 0ebf8283 2019-07-24 stsp break;
6380 0ebf8283 2019-07-24 stsp }
6381 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6382 0ebf8283 2019-07-24 stsp p++;
6383 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6384 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6385 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6386 0ebf8283 2019-07-24 stsp break;
6387 0ebf8283 2019-07-24 stsp }
6388 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6389 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6390 0ebf8283 2019-07-24 stsp if (err)
6391 0ebf8283 2019-07-24 stsp break;
6392 0ebf8283 2019-07-24 stsp } else {
6393 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6394 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6395 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6396 0ebf8283 2019-07-24 stsp break;
6397 0ebf8283 2019-07-24 stsp }
6398 0ebf8283 2019-07-24 stsp }
6399 0ebf8283 2019-07-24 stsp free(line);
6400 0ebf8283 2019-07-24 stsp line = NULL;
6401 0ebf8283 2019-07-24 stsp continue;
6402 0ebf8283 2019-07-24 stsp } else {
6403 0ebf8283 2019-07-24 stsp end = p;
6404 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6405 0ebf8283 2019-07-24 stsp end++;
6406 0ebf8283 2019-07-24 stsp *end = '\0';
6407 0ebf8283 2019-07-24 stsp
6408 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6409 0ebf8283 2019-07-24 stsp if (err) {
6410 0ebf8283 2019-07-24 stsp /* override error code */
6411 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6412 0ebf8283 2019-07-24 stsp break;
6413 0ebf8283 2019-07-24 stsp }
6414 0ebf8283 2019-07-24 stsp }
6415 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6416 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6417 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6418 0ebf8283 2019-07-24 stsp break;
6419 0ebf8283 2019-07-24 stsp }
6420 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6421 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6422 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6423 0ebf8283 2019-07-24 stsp commit_id = NULL;
6424 0ebf8283 2019-07-24 stsp free(line);
6425 0ebf8283 2019-07-24 stsp line = NULL;
6426 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6427 0ebf8283 2019-07-24 stsp }
6428 0ebf8283 2019-07-24 stsp
6429 0ebf8283 2019-07-24 stsp free(line);
6430 0ebf8283 2019-07-24 stsp free(commit_id);
6431 0ebf8283 2019-07-24 stsp return err;
6432 0ebf8283 2019-07-24 stsp }
6433 0ebf8283 2019-07-24 stsp
6434 0ebf8283 2019-07-24 stsp static const struct got_error *
6435 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6436 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6437 bfce7f83 2019-07-27 stsp {
6438 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6439 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6440 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6441 5b87815e 2020-03-05 stsp static char msg[92];
6442 bfce7f83 2019-07-27 stsp char *id_str;
6443 bfce7f83 2019-07-27 stsp
6444 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6445 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6446 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6447 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6448 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6449 5b87815e 2020-03-05 stsp
6450 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6451 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6452 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6453 5b87815e 2020-03-05 stsp if (hle == hle2)
6454 5b87815e 2020-03-05 stsp continue;
6455 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6456 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6457 5b87815e 2020-03-05 stsp continue;
6458 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6459 5b87815e 2020-03-05 stsp if (err)
6460 5b87815e 2020-03-05 stsp return err;
6461 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6462 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6463 5b87815e 2020-03-05 stsp free(id_str);
6464 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6465 5b87815e 2020-03-05 stsp }
6466 5b87815e 2020-03-05 stsp }
6467 bfce7f83 2019-07-27 stsp
6468 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6469 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6470 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6471 bfce7f83 2019-07-27 stsp break;
6472 bfce7f83 2019-07-27 stsp }
6473 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6474 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6475 bfce7f83 2019-07-27 stsp if (err)
6476 bfce7f83 2019-07-27 stsp return err;
6477 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6478 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6479 bfce7f83 2019-07-27 stsp free(id_str);
6480 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6481 bfce7f83 2019-07-27 stsp }
6482 bfce7f83 2019-07-27 stsp }
6483 bfce7f83 2019-07-27 stsp
6484 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6485 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6486 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6487 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6488 bfce7f83 2019-07-27 stsp
6489 bfce7f83 2019-07-27 stsp return NULL;
6490 bfce7f83 2019-07-27 stsp }
6491 bfce7f83 2019-07-27 stsp
6492 bfce7f83 2019-07-27 stsp static const struct got_error *
6493 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6494 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6495 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6496 0ebf8283 2019-07-24 stsp {
6497 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6498 0ebf8283 2019-07-24 stsp char *editor;
6499 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6500 0ebf8283 2019-07-24 stsp
6501 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6502 0ebf8283 2019-07-24 stsp if (err)
6503 0ebf8283 2019-07-24 stsp return err;
6504 0ebf8283 2019-07-24 stsp
6505 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6506 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6507 0ebf8283 2019-07-24 stsp goto done;
6508 0ebf8283 2019-07-24 stsp }
6509 0ebf8283 2019-07-24 stsp
6510 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6511 0ebf8283 2019-07-24 stsp if (f == NULL) {
6512 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6513 0ebf8283 2019-07-24 stsp goto done;
6514 0ebf8283 2019-07-24 stsp }
6515 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6516 bfce7f83 2019-07-27 stsp if (err)
6517 bfce7f83 2019-07-27 stsp goto done;
6518 bfce7f83 2019-07-27 stsp
6519 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6520 0ebf8283 2019-07-24 stsp done:
6521 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6522 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6523 0ebf8283 2019-07-24 stsp free(editor);
6524 0ebf8283 2019-07-24 stsp return err;
6525 0ebf8283 2019-07-24 stsp }
6526 0ebf8283 2019-07-24 stsp
6527 0ebf8283 2019-07-24 stsp static const struct got_error *
6528 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6529 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6530 514f2ffe 2020-01-29 stsp struct got_repository *);
6531 0ebf8283 2019-07-24 stsp
6532 0ebf8283 2019-07-24 stsp static const struct got_error *
6533 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6534 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6535 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6536 0ebf8283 2019-07-24 stsp {
6537 0ebf8283 2019-07-24 stsp const struct got_error *err;
6538 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6539 0ebf8283 2019-07-24 stsp char *path = NULL;
6540 0ebf8283 2019-07-24 stsp
6541 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6542 0ebf8283 2019-07-24 stsp if (err)
6543 0ebf8283 2019-07-24 stsp return err;
6544 0ebf8283 2019-07-24 stsp
6545 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6546 0ebf8283 2019-07-24 stsp if (err)
6547 0ebf8283 2019-07-24 stsp goto done;
6548 0ebf8283 2019-07-24 stsp
6549 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6550 0ebf8283 2019-07-24 stsp if (err)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp
6553 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6554 083957f4 2020-02-24 stsp rewind(f);
6555 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6556 083957f4 2020-02-24 stsp } else {
6557 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6558 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6559 0ebf8283 2019-07-24 stsp goto done;
6560 083957f4 2020-02-24 stsp }
6561 083957f4 2020-02-24 stsp f = NULL;
6562 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6563 083957f4 2020-02-24 stsp if (err) {
6564 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6565 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6566 083957f4 2020-02-24 stsp goto done;
6567 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6568 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6569 083957f4 2020-02-24 stsp }
6570 0ebf8283 2019-07-24 stsp }
6571 0ebf8283 2019-07-24 stsp done:
6572 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6573 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6574 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6575 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6576 0ebf8283 2019-07-24 stsp free(path);
6577 0ebf8283 2019-07-24 stsp return err;
6578 0ebf8283 2019-07-24 stsp }
6579 0ebf8283 2019-07-24 stsp
6580 0ebf8283 2019-07-24 stsp static const struct got_error *
6581 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6582 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6583 0ebf8283 2019-07-24 stsp {
6584 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6585 0ebf8283 2019-07-24 stsp char *path = NULL;
6586 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6587 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6588 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6589 0ebf8283 2019-07-24 stsp
6590 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6591 0ebf8283 2019-07-24 stsp if (err)
6592 0ebf8283 2019-07-24 stsp return err;
6593 0ebf8283 2019-07-24 stsp
6594 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6595 0ebf8283 2019-07-24 stsp if (f == NULL) {
6596 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6597 0ebf8283 2019-07-24 stsp goto done;
6598 0ebf8283 2019-07-24 stsp }
6599 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6600 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6601 0ebf8283 2019-07-24 stsp repo);
6602 0ebf8283 2019-07-24 stsp if (err)
6603 0ebf8283 2019-07-24 stsp break;
6604 0ebf8283 2019-07-24 stsp
6605 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6606 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6607 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6608 0ebf8283 2019-07-24 stsp if (n < 0) {
6609 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6610 0ebf8283 2019-07-24 stsp break;
6611 0ebf8283 2019-07-24 stsp }
6612 0ebf8283 2019-07-24 stsp }
6613 0ebf8283 2019-07-24 stsp }
6614 0ebf8283 2019-07-24 stsp done:
6615 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6616 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6617 0ebf8283 2019-07-24 stsp free(path);
6618 0ebf8283 2019-07-24 stsp if (commit)
6619 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6620 0ebf8283 2019-07-24 stsp return err;
6621 0ebf8283 2019-07-24 stsp }
6622 0ebf8283 2019-07-24 stsp
6623 bfce7f83 2019-07-27 stsp void
6624 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6625 bfce7f83 2019-07-27 stsp {
6626 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6627 bfce7f83 2019-07-27 stsp
6628 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6629 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6630 bfce7f83 2019-07-27 stsp free(hle);
6631 bfce7f83 2019-07-27 stsp }
6632 bfce7f83 2019-07-27 stsp }
6633 bfce7f83 2019-07-27 stsp
6634 0ebf8283 2019-07-24 stsp static const struct got_error *
6635 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6636 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6637 0ebf8283 2019-07-24 stsp {
6638 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6639 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6640 0ebf8283 2019-07-24 stsp
6641 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6642 0ebf8283 2019-07-24 stsp if (f == NULL) {
6643 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6644 0ebf8283 2019-07-24 stsp goto done;
6645 0ebf8283 2019-07-24 stsp }
6646 0ebf8283 2019-07-24 stsp
6647 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6648 0ebf8283 2019-07-24 stsp done:
6649 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6650 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6651 0ebf8283 2019-07-24 stsp return err;
6652 0ebf8283 2019-07-24 stsp }
6653 0ebf8283 2019-07-24 stsp
6654 0ebf8283 2019-07-24 stsp static const struct got_error *
6655 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6656 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6657 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6658 0ebf8283 2019-07-24 stsp {
6659 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6660 0ebf8283 2019-07-24 stsp int resp = ' ';
6661 0ebf8283 2019-07-24 stsp
6662 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6663 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6664 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6665 0ebf8283 2019-07-24 stsp resp = getchar();
6666 a61a4414 2019-08-07 stsp if (resp == '\n')
6667 a61a4414 2019-08-07 stsp resp = getchar();
6668 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6669 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6670 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6671 bfce7f83 2019-07-27 stsp repo);
6672 426ebf2e 2019-07-25 stsp if (err) {
6673 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6674 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6675 426ebf2e 2019-07-25 stsp break;
6676 bfce7f83 2019-07-27 stsp prev_err = err;
6677 426ebf2e 2019-07-25 stsp resp = ' ';
6678 426ebf2e 2019-07-25 stsp continue;
6679 426ebf2e 2019-07-25 stsp }
6680 bfce7f83 2019-07-27 stsp break;
6681 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6682 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6683 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6684 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6685 426ebf2e 2019-07-25 stsp if (err) {
6686 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6687 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6688 426ebf2e 2019-07-25 stsp break;
6689 bfce7f83 2019-07-27 stsp prev_err = err;
6690 426ebf2e 2019-07-25 stsp resp = ' ';
6691 426ebf2e 2019-07-25 stsp continue;
6692 426ebf2e 2019-07-25 stsp }
6693 bfce7f83 2019-07-27 stsp break;
6694 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6695 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6696 0ebf8283 2019-07-24 stsp break;
6697 426ebf2e 2019-07-25 stsp } else
6698 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6699 0ebf8283 2019-07-24 stsp }
6700 0ebf8283 2019-07-24 stsp
6701 0ebf8283 2019-07-24 stsp return err;
6702 0ebf8283 2019-07-24 stsp }
6703 0ebf8283 2019-07-24 stsp
6704 0ebf8283 2019-07-24 stsp static const struct got_error *
6705 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6706 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6707 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6708 0ebf8283 2019-07-24 stsp {
6709 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6710 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6711 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6712 3e3a69f1 2019-07-25 stsp branch, repo);
6713 0ebf8283 2019-07-24 stsp }
6714 0ebf8283 2019-07-24 stsp
6715 0ebf8283 2019-07-24 stsp static const struct got_error *
6716 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6717 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6718 0ebf8283 2019-07-24 stsp {
6719 0ebf8283 2019-07-24 stsp const struct got_error *err;
6720 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6721 0ebf8283 2019-07-24 stsp
6722 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6723 0ebf8283 2019-07-24 stsp if (err)
6724 0ebf8283 2019-07-24 stsp goto done;
6725 0ebf8283 2019-07-24 stsp
6726 0ebf8283 2019-07-24 stsp if (new_id) {
6727 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6728 0ebf8283 2019-07-24 stsp if (err)
6729 0ebf8283 2019-07-24 stsp goto done;
6730 0ebf8283 2019-07-24 stsp }
6731 0ebf8283 2019-07-24 stsp
6732 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6733 0ebf8283 2019-07-24 stsp if (new_id_str)
6734 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6735 0ebf8283 2019-07-24 stsp
6736 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6737 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6738 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6739 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6740 0ebf8283 2019-07-24 stsp goto done;
6741 0ebf8283 2019-07-24 stsp }
6742 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6743 0ebf8283 2019-07-24 stsp } else {
6744 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6745 0ebf8283 2019-07-24 stsp if (err)
6746 0ebf8283 2019-07-24 stsp goto done;
6747 0ebf8283 2019-07-24 stsp }
6748 0ebf8283 2019-07-24 stsp
6749 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6750 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6751 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6752 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6753 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6754 0ebf8283 2019-07-24 stsp break;
6755 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6756 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6757 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6758 0ebf8283 2019-07-24 stsp logmsg);
6759 0ebf8283 2019-07-24 stsp break;
6760 0ebf8283 2019-07-24 stsp default:
6761 0ebf8283 2019-07-24 stsp break;
6762 0ebf8283 2019-07-24 stsp }
6763 0ebf8283 2019-07-24 stsp done:
6764 0ebf8283 2019-07-24 stsp free(old_id_str);
6765 0ebf8283 2019-07-24 stsp free(new_id_str);
6766 0ebf8283 2019-07-24 stsp return err;
6767 0ebf8283 2019-07-24 stsp }
6768 0ebf8283 2019-07-24 stsp
6769 0ebf8283 2019-07-24 stsp static const struct got_error *
6770 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6771 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6772 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6773 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6774 0ebf8283 2019-07-24 stsp {
6775 0ebf8283 2019-07-24 stsp const struct got_error *err;
6776 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6777 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6778 0ebf8283 2019-07-24 stsp
6779 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6780 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6781 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6782 0ebf8283 2019-07-24 stsp if (err)
6783 0ebf8283 2019-07-24 stsp return err;
6784 0ebf8283 2019-07-24 stsp }
6785 0ebf8283 2019-07-24 stsp
6786 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6787 0ebf8283 2019-07-24 stsp if (err)
6788 0ebf8283 2019-07-24 stsp return err;
6789 0ebf8283 2019-07-24 stsp
6790 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6791 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6792 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6793 0ebf8283 2019-07-24 stsp if (err) {
6794 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6795 0ebf8283 2019-07-24 stsp goto done;
6796 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6797 0ebf8283 2019-07-24 stsp } else {
6798 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6799 0ebf8283 2019-07-24 stsp free(new_commit_id);
6800 0ebf8283 2019-07-24 stsp }
6801 0ebf8283 2019-07-24 stsp done:
6802 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6803 0ebf8283 2019-07-24 stsp return err;
6804 0ebf8283 2019-07-24 stsp }
6805 0ebf8283 2019-07-24 stsp
6806 0ebf8283 2019-07-24 stsp static const struct got_error *
6807 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6808 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6809 0ebf8283 2019-07-24 stsp {
6810 0ebf8283 2019-07-24 stsp const struct got_error *error;
6811 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6812 0ebf8283 2019-07-24 stsp
6813 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6814 0ebf8283 2019-07-24 stsp repo);
6815 0ebf8283 2019-07-24 stsp if (error)
6816 0ebf8283 2019-07-24 stsp return error;
6817 0ebf8283 2019-07-24 stsp
6818 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6819 0ebf8283 2019-07-24 stsp if (error)
6820 0ebf8283 2019-07-24 stsp return error;
6821 0ebf8283 2019-07-24 stsp
6822 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6823 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6824 0ebf8283 2019-07-24 stsp return error;
6825 ab20a43a 2020-01-29 stsp }
6826 ab20a43a 2020-01-29 stsp
6827 ab20a43a 2020-01-29 stsp static const struct got_error *
6828 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6829 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6830 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6831 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6832 ab20a43a 2020-01-29 stsp {
6833 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6834 ab20a43a 2020-01-29 stsp
6835 ab20a43a 2020-01-29 stsp switch (status) {
6836 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6837 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6838 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6839 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6840 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6841 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6842 ab20a43a 2020-01-29 stsp default:
6843 ab20a43a 2020-01-29 stsp break;
6844 ab20a43a 2020-01-29 stsp }
6845 ab20a43a 2020-01-29 stsp
6846 ab20a43a 2020-01-29 stsp switch (staged_status) {
6847 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6848 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6849 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6850 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6851 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6852 ab20a43a 2020-01-29 stsp default:
6853 ab20a43a 2020-01-29 stsp break;
6854 ab20a43a 2020-01-29 stsp }
6855 ab20a43a 2020-01-29 stsp
6856 ab20a43a 2020-01-29 stsp return NULL;
6857 0ebf8283 2019-07-24 stsp }
6858 0ebf8283 2019-07-24 stsp
6859 0ebf8283 2019-07-24 stsp static const struct got_error *
6860 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6861 0ebf8283 2019-07-24 stsp {
6862 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6863 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6864 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6865 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6866 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6867 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6868 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6869 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6870 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6871 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6872 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6873 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6874 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6875 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6876 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6877 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6878 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6879 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6880 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6881 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6882 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6883 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6884 0ebf8283 2019-07-24 stsp
6885 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6886 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6887 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6888 0ebf8283 2019-07-24 stsp
6889 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6890 0ebf8283 2019-07-24 stsp switch (ch) {
6891 0ebf8283 2019-07-24 stsp case 'a':
6892 0ebf8283 2019-07-24 stsp abort_edit = 1;
6893 0ebf8283 2019-07-24 stsp break;
6894 0ebf8283 2019-07-24 stsp case 'c':
6895 0ebf8283 2019-07-24 stsp continue_edit = 1;
6896 0ebf8283 2019-07-24 stsp break;
6897 0ebf8283 2019-07-24 stsp case 'F':
6898 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6899 0ebf8283 2019-07-24 stsp break;
6900 083957f4 2020-02-24 stsp case 'm':
6901 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6902 083957f4 2020-02-24 stsp break;
6903 0ebf8283 2019-07-24 stsp default:
6904 0ebf8283 2019-07-24 stsp usage_histedit();
6905 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6906 0ebf8283 2019-07-24 stsp }
6907 0ebf8283 2019-07-24 stsp }
6908 0ebf8283 2019-07-24 stsp
6909 0ebf8283 2019-07-24 stsp argc -= optind;
6910 0ebf8283 2019-07-24 stsp argv += optind;
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6913 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6914 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6915 0ebf8283 2019-07-24 stsp err(1, "pledge");
6916 0ebf8283 2019-07-24 stsp #endif
6917 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6918 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6919 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6920 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6921 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6922 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6923 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6924 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6925 0ebf8283 2019-07-24 stsp if (argc != 0)
6926 0ebf8283 2019-07-24 stsp usage_histedit();
6927 0ebf8283 2019-07-24 stsp
6928 0ebf8283 2019-07-24 stsp /*
6929 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6930 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6931 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6932 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6933 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6934 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6935 0ebf8283 2019-07-24 stsp */
6936 0ebf8283 2019-07-24 stsp
6937 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6938 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6939 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6940 0ebf8283 2019-07-24 stsp goto done;
6941 0ebf8283 2019-07-24 stsp }
6942 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6943 0ebf8283 2019-07-24 stsp if (error)
6944 0ebf8283 2019-07-24 stsp goto done;
6945 0ebf8283 2019-07-24 stsp
6946 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6947 c9956ddf 2019-09-08 stsp NULL);
6948 0ebf8283 2019-07-24 stsp if (error != NULL)
6949 0ebf8283 2019-07-24 stsp goto done;
6950 0ebf8283 2019-07-24 stsp
6951 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6952 0ebf8283 2019-07-24 stsp if (error)
6953 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6955 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6956 0ebf8283 2019-07-24 stsp goto done;
6957 0ebf8283 2019-07-24 stsp }
6958 0ebf8283 2019-07-24 stsp
6959 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6960 0ebf8283 2019-07-24 stsp if (error)
6961 0ebf8283 2019-07-24 stsp goto done;
6962 0ebf8283 2019-07-24 stsp
6963 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6964 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6965 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6966 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6967 083957f4 2020-02-24 stsp "before the -m option can be used");
6968 083957f4 2020-02-24 stsp goto done;
6969 083957f4 2020-02-24 stsp }
6970 083957f4 2020-02-24 stsp
6971 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6972 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6973 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6974 3e3a69f1 2019-07-25 stsp worktree, repo);
6975 0ebf8283 2019-07-24 stsp if (error)
6976 0ebf8283 2019-07-24 stsp goto done;
6977 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6978 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6979 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6980 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6981 0ebf8283 2019-07-24 stsp if (error)
6982 0ebf8283 2019-07-24 stsp goto done;
6983 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6984 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6985 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6986 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6987 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6988 0ebf8283 2019-07-24 stsp goto done;
6989 0ebf8283 2019-07-24 stsp }
6990 0ebf8283 2019-07-24 stsp
6991 0ebf8283 2019-07-24 stsp if (continue_edit) {
6992 0ebf8283 2019-07-24 stsp char *path;
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6995 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6996 0ebf8283 2019-07-24 stsp goto done;
6997 0ebf8283 2019-07-24 stsp }
6998 0ebf8283 2019-07-24 stsp
6999 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7000 0ebf8283 2019-07-24 stsp if (error)
7001 0ebf8283 2019-07-24 stsp goto done;
7002 0ebf8283 2019-07-24 stsp
7003 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7004 0ebf8283 2019-07-24 stsp free(path);
7005 0ebf8283 2019-07-24 stsp if (error)
7006 0ebf8283 2019-07-24 stsp goto done;
7007 0ebf8283 2019-07-24 stsp
7008 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7009 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7010 3e3a69f1 2019-07-25 stsp worktree, repo);
7011 0ebf8283 2019-07-24 stsp if (error)
7012 0ebf8283 2019-07-24 stsp goto done;
7013 0ebf8283 2019-07-24 stsp
7014 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7015 0ebf8283 2019-07-24 stsp if (error)
7016 0ebf8283 2019-07-24 stsp goto done;
7017 0ebf8283 2019-07-24 stsp
7018 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7019 0ebf8283 2019-07-24 stsp head_commit_id);
7020 0ebf8283 2019-07-24 stsp if (error)
7021 0ebf8283 2019-07-24 stsp goto done;
7022 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7023 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7024 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7025 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7026 3aac7cf7 2019-07-25 stsp goto done;
7027 3aac7cf7 2019-07-25 stsp }
7028 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7029 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7030 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7031 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7032 0ebf8283 2019-07-24 stsp commit = NULL;
7033 0ebf8283 2019-07-24 stsp if (error)
7034 0ebf8283 2019-07-24 stsp goto done;
7035 0ebf8283 2019-07-24 stsp } else {
7036 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7037 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7038 0ebf8283 2019-07-24 stsp goto done;
7039 0ebf8283 2019-07-24 stsp }
7040 0ebf8283 2019-07-24 stsp
7041 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7042 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7043 0ebf8283 2019-07-24 stsp if (error != NULL)
7044 0ebf8283 2019-07-24 stsp goto done;
7045 0ebf8283 2019-07-24 stsp
7046 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7047 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7048 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7049 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7050 c7d20a3f 2019-07-30 stsp goto done;
7051 c7d20a3f 2019-07-30 stsp }
7052 c7d20a3f 2019-07-30 stsp
7053 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7054 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7055 bfce7f83 2019-07-27 stsp branch = NULL;
7056 0ebf8283 2019-07-24 stsp if (error)
7057 0ebf8283 2019-07-24 stsp goto done;
7058 0ebf8283 2019-07-24 stsp
7059 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7060 0ebf8283 2019-07-24 stsp head_commit_id);
7061 0ebf8283 2019-07-24 stsp if (error)
7062 0ebf8283 2019-07-24 stsp goto done;
7063 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7064 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7065 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7066 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7067 3aac7cf7 2019-07-25 stsp goto done;
7068 3aac7cf7 2019-07-25 stsp }
7069 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7070 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7071 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7072 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7073 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7074 0ebf8283 2019-07-24 stsp commit = NULL;
7075 0ebf8283 2019-07-24 stsp if (error)
7076 0ebf8283 2019-07-24 stsp goto done;
7077 0ebf8283 2019-07-24 stsp
7078 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7079 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7080 c5996fff 2020-01-29 stsp goto done;
7081 c5996fff 2020-01-29 stsp }
7082 c5996fff 2020-01-29 stsp
7083 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7084 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7085 bfce7f83 2019-07-27 stsp if (error)
7086 bfce7f83 2019-07-27 stsp goto done;
7087 bfce7f83 2019-07-27 stsp
7088 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7089 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7090 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7091 6ba2bea2 2019-07-27 stsp if (error) {
7092 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7093 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7094 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7095 0ebf8283 2019-07-24 stsp goto done;
7096 6ba2bea2 2019-07-27 stsp }
7097 0ebf8283 2019-07-24 stsp } else {
7098 514f2ffe 2020-01-29 stsp const char *branch_name;
7099 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7100 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7101 514f2ffe 2020-01-29 stsp branch_name += 11;
7102 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7103 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7104 6ba2bea2 2019-07-27 stsp if (error) {
7105 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7106 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7107 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7108 0ebf8283 2019-07-24 stsp goto done;
7109 6ba2bea2 2019-07-27 stsp }
7110 0ebf8283 2019-07-24 stsp
7111 0ebf8283 2019-07-24 stsp }
7112 0ebf8283 2019-07-24 stsp
7113 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7114 0ebf8283 2019-07-24 stsp repo);
7115 6ba2bea2 2019-07-27 stsp if (error) {
7116 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7117 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7118 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7119 0ebf8283 2019-07-24 stsp goto done;
7120 6ba2bea2 2019-07-27 stsp }
7121 0ebf8283 2019-07-24 stsp
7122 0ebf8283 2019-07-24 stsp }
7123 0ebf8283 2019-07-24 stsp
7124 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7125 4ec14e60 2019-08-28 hiltjo if (error)
7126 0ebf8283 2019-07-24 stsp goto done;
7127 0ebf8283 2019-07-24 stsp
7128 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7129 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7130 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7131 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7132 0ebf8283 2019-07-24 stsp continue;
7133 0ebf8283 2019-07-24 stsp
7134 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7135 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7136 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7137 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7138 0ebf8283 2019-07-24 stsp repo);
7139 ab20a43a 2020-01-29 stsp if (error)
7140 ab20a43a 2020-01-29 stsp goto done;
7141 0ebf8283 2019-07-24 stsp } else {
7142 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7143 ab20a43a 2020-01-29 stsp int have_changes = 0;
7144 ab20a43a 2020-01-29 stsp
7145 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7146 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7147 ab20a43a 2020-01-29 stsp if (error)
7148 ab20a43a 2020-01-29 stsp goto done;
7149 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7150 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7151 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7152 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7153 ab20a43a 2020-01-29 stsp if (error) {
7154 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7155 ab20a43a 2020-01-29 stsp goto done;
7156 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7157 ab20a43a 2020-01-29 stsp goto done;
7158 ab20a43a 2020-01-29 stsp }
7159 ab20a43a 2020-01-29 stsp if (have_changes) {
7160 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7161 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7162 ab20a43a 2020-01-29 stsp if (error)
7163 ab20a43a 2020-01-29 stsp goto done;
7164 ab20a43a 2020-01-29 stsp } else {
7165 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7166 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7167 ab20a43a 2020-01-29 stsp if (error)
7168 ab20a43a 2020-01-29 stsp goto done;
7169 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7170 ab20a43a 2020-01-29 stsp hle, NULL);
7171 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7172 ab20a43a 2020-01-29 stsp commit = NULL;
7173 ab20a43a 2020-01-29 stsp if (error)
7174 ab20a43a 2020-01-29 stsp goto done;
7175 ab20a43a 2020-01-29 stsp }
7176 0ebf8283 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp continue;
7178 0ebf8283 2019-07-24 stsp }
7179 0ebf8283 2019-07-24 stsp
7180 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7181 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7182 0ebf8283 2019-07-24 stsp if (error)
7183 0ebf8283 2019-07-24 stsp goto done;
7184 0ebf8283 2019-07-24 stsp continue;
7185 0ebf8283 2019-07-24 stsp }
7186 0ebf8283 2019-07-24 stsp
7187 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7188 0ebf8283 2019-07-24 stsp hle->commit_id);
7189 0ebf8283 2019-07-24 stsp if (error)
7190 0ebf8283 2019-07-24 stsp goto done;
7191 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7192 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7193 0ebf8283 2019-07-24 stsp
7194 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7195 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7196 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7197 0ebf8283 2019-07-24 stsp if (error)
7198 0ebf8283 2019-07-24 stsp goto done;
7199 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7200 0ebf8283 2019-07-24 stsp commit = NULL;
7201 0ebf8283 2019-07-24 stsp
7202 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7203 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7204 a0ea4fc0 2020-02-28 stsp repo);
7205 a0ea4fc0 2020-02-28 stsp if (error)
7206 a0ea4fc0 2020-02-28 stsp goto done;
7207 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7208 0ebf8283 2019-07-24 stsp break;
7209 0ebf8283 2019-07-24 stsp }
7210 0ebf8283 2019-07-24 stsp
7211 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7212 0ebf8283 2019-07-24 stsp char *id_str;
7213 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7214 0ebf8283 2019-07-24 stsp if (error)
7215 0ebf8283 2019-07-24 stsp goto done;
7216 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7217 0ebf8283 2019-07-24 stsp id_str);
7218 0ebf8283 2019-07-24 stsp free(id_str);
7219 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7220 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7221 3e3a69f1 2019-07-25 stsp fileindex);
7222 0ebf8283 2019-07-24 stsp goto done;
7223 0ebf8283 2019-07-24 stsp }
7224 0ebf8283 2019-07-24 stsp
7225 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7226 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7227 0ebf8283 2019-07-24 stsp if (error)
7228 0ebf8283 2019-07-24 stsp goto done;
7229 0ebf8283 2019-07-24 stsp continue;
7230 0ebf8283 2019-07-24 stsp }
7231 0ebf8283 2019-07-24 stsp
7232 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7233 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7234 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7235 0ebf8283 2019-07-24 stsp if (error)
7236 0ebf8283 2019-07-24 stsp goto done;
7237 0ebf8283 2019-07-24 stsp }
7238 0ebf8283 2019-07-24 stsp
7239 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7240 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7241 0ebf8283 2019-07-24 stsp if (error)
7242 0ebf8283 2019-07-24 stsp goto done;
7243 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7244 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7245 0ebf8283 2019-07-24 stsp } else
7246 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7247 3e3a69f1 2019-07-25 stsp branch, repo);
7248 0ebf8283 2019-07-24 stsp done:
7249 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7250 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7251 0ebf8283 2019-07-24 stsp free(head_commit_id);
7252 0ebf8283 2019-07-24 stsp free(base_commit_id);
7253 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7254 0ebf8283 2019-07-24 stsp if (commit)
7255 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7256 0ebf8283 2019-07-24 stsp if (branch)
7257 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7258 0ebf8283 2019-07-24 stsp if (tmp_branch)
7259 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7260 0ebf8283 2019-07-24 stsp if (worktree)
7261 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7262 2822a352 2019-10-15 stsp if (repo)
7263 2822a352 2019-10-15 stsp got_repo_close(repo);
7264 2822a352 2019-10-15 stsp return error;
7265 2822a352 2019-10-15 stsp }
7266 2822a352 2019-10-15 stsp
7267 2822a352 2019-10-15 stsp __dead static void
7268 2822a352 2019-10-15 stsp usage_integrate(void)
7269 2822a352 2019-10-15 stsp {
7270 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7271 2822a352 2019-10-15 stsp exit(1);
7272 2822a352 2019-10-15 stsp }
7273 2822a352 2019-10-15 stsp
7274 2822a352 2019-10-15 stsp static const struct got_error *
7275 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7276 2822a352 2019-10-15 stsp {
7277 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7278 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7279 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7280 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7281 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7282 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7283 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7284 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7285 2822a352 2019-10-15 stsp int ch, did_something = 0;
7286 2822a352 2019-10-15 stsp
7287 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7288 2822a352 2019-10-15 stsp switch (ch) {
7289 2822a352 2019-10-15 stsp default:
7290 2822a352 2019-10-15 stsp usage_integrate();
7291 2822a352 2019-10-15 stsp /* NOTREACHED */
7292 2822a352 2019-10-15 stsp }
7293 2822a352 2019-10-15 stsp }
7294 2822a352 2019-10-15 stsp
7295 2822a352 2019-10-15 stsp argc -= optind;
7296 2822a352 2019-10-15 stsp argv += optind;
7297 2822a352 2019-10-15 stsp
7298 2822a352 2019-10-15 stsp if (argc != 1)
7299 2822a352 2019-10-15 stsp usage_integrate();
7300 2822a352 2019-10-15 stsp branch_arg = argv[0];
7301 2822a352 2019-10-15 stsp
7302 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7303 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7304 2822a352 2019-10-15 stsp err(1, "pledge");
7305 2822a352 2019-10-15 stsp
7306 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7307 2822a352 2019-10-15 stsp if (cwd == NULL) {
7308 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7309 2822a352 2019-10-15 stsp goto done;
7310 2822a352 2019-10-15 stsp }
7311 2822a352 2019-10-15 stsp
7312 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7313 2822a352 2019-10-15 stsp if (error)
7314 2822a352 2019-10-15 stsp goto done;
7315 2822a352 2019-10-15 stsp
7316 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7317 2822a352 2019-10-15 stsp if (error)
7318 2822a352 2019-10-15 stsp goto done;
7319 2822a352 2019-10-15 stsp
7320 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7321 2822a352 2019-10-15 stsp NULL);
7322 2822a352 2019-10-15 stsp if (error != NULL)
7323 2822a352 2019-10-15 stsp goto done;
7324 2822a352 2019-10-15 stsp
7325 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7326 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7327 2822a352 2019-10-15 stsp if (error)
7328 2822a352 2019-10-15 stsp goto done;
7329 2822a352 2019-10-15 stsp
7330 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7331 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7332 2822a352 2019-10-15 stsp goto done;
7333 2822a352 2019-10-15 stsp }
7334 2822a352 2019-10-15 stsp
7335 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7336 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7337 2822a352 2019-10-15 stsp if (error)
7338 2822a352 2019-10-15 stsp goto done;
7339 2822a352 2019-10-15 stsp
7340 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7341 2822a352 2019-10-15 stsp if (refname == NULL) {
7342 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7343 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7344 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7345 2822a352 2019-10-15 stsp goto done;
7346 2822a352 2019-10-15 stsp }
7347 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7348 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7349 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7350 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7351 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7352 2822a352 2019-10-15 stsp goto done;
7353 2822a352 2019-10-15 stsp }
7354 2822a352 2019-10-15 stsp
7355 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7356 2822a352 2019-10-15 stsp if (error)
7357 2822a352 2019-10-15 stsp goto done;
7358 2822a352 2019-10-15 stsp
7359 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7360 2822a352 2019-10-15 stsp if (error)
7361 2822a352 2019-10-15 stsp goto done;
7362 2822a352 2019-10-15 stsp
7363 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7364 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7365 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7366 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7367 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7368 2822a352 2019-10-15 stsp goto done;
7369 2822a352 2019-10-15 stsp }
7370 2822a352 2019-10-15 stsp
7371 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7372 2822a352 2019-10-15 stsp if (error) {
7373 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7374 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7375 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7376 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7377 2822a352 2019-10-15 stsp goto done;
7378 2822a352 2019-10-15 stsp }
7379 2822a352 2019-10-15 stsp
7380 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7381 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7382 2822a352 2019-10-15 stsp check_cancelled, NULL);
7383 2822a352 2019-10-15 stsp if (error)
7384 2822a352 2019-10-15 stsp goto done;
7385 2822a352 2019-10-15 stsp
7386 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7387 2822a352 2019-10-15 stsp done:
7388 715dc77e 2019-08-03 stsp if (repo)
7389 715dc77e 2019-08-03 stsp got_repo_close(repo);
7390 2822a352 2019-10-15 stsp if (worktree)
7391 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7392 2822a352 2019-10-15 stsp free(cwd);
7393 2822a352 2019-10-15 stsp free(base_commit_id);
7394 2822a352 2019-10-15 stsp free(commit_id);
7395 2822a352 2019-10-15 stsp free(refname);
7396 2822a352 2019-10-15 stsp free(base_refname);
7397 715dc77e 2019-08-03 stsp return error;
7398 715dc77e 2019-08-03 stsp }
7399 715dc77e 2019-08-03 stsp
7400 715dc77e 2019-08-03 stsp __dead static void
7401 715dc77e 2019-08-03 stsp usage_stage(void)
7402 715dc77e 2019-08-03 stsp {
7403 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7404 f3055044 2019-08-07 stsp "[file-path ...]\n",
7405 715dc77e 2019-08-03 stsp getprogname());
7406 715dc77e 2019-08-03 stsp exit(1);
7407 715dc77e 2019-08-03 stsp }
7408 715dc77e 2019-08-03 stsp
7409 715dc77e 2019-08-03 stsp static const struct got_error *
7410 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7411 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7412 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7413 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7414 408b4ebc 2019-08-03 stsp {
7415 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7416 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7417 408b4ebc 2019-08-03 stsp
7418 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7419 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7420 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7421 408b4ebc 2019-08-03 stsp return NULL;
7422 408b4ebc 2019-08-03 stsp
7423 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7424 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7425 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7426 408b4ebc 2019-08-03 stsp else
7427 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7428 408b4ebc 2019-08-03 stsp if (err)
7429 408b4ebc 2019-08-03 stsp return err;
7430 408b4ebc 2019-08-03 stsp
7431 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7432 408b4ebc 2019-08-03 stsp free(id_str);
7433 dc424a06 2019-08-07 stsp return NULL;
7434 dc424a06 2019-08-07 stsp }
7435 2e1f37b0 2019-08-08 stsp
7436 dc424a06 2019-08-07 stsp static const struct got_error *
7437 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7438 715dc77e 2019-08-03 stsp {
7439 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7440 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7441 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7442 715dc77e 2019-08-03 stsp char *cwd = NULL;
7443 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7444 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7445 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7446 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7447 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7448 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7449 715dc77e 2019-08-03 stsp
7450 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7451 715dc77e 2019-08-03 stsp
7452 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7453 715dc77e 2019-08-03 stsp switch (ch) {
7454 408b4ebc 2019-08-03 stsp case 'l':
7455 408b4ebc 2019-08-03 stsp list_stage = 1;
7456 408b4ebc 2019-08-03 stsp break;
7457 dc424a06 2019-08-07 stsp case 'p':
7458 dc424a06 2019-08-07 stsp pflag = 1;
7459 dc424a06 2019-08-07 stsp break;
7460 dc424a06 2019-08-07 stsp case 'F':
7461 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7462 dc424a06 2019-08-07 stsp break;
7463 715dc77e 2019-08-03 stsp default:
7464 715dc77e 2019-08-03 stsp usage_stage();
7465 715dc77e 2019-08-03 stsp /* NOTREACHED */
7466 715dc77e 2019-08-03 stsp }
7467 715dc77e 2019-08-03 stsp }
7468 715dc77e 2019-08-03 stsp
7469 715dc77e 2019-08-03 stsp argc -= optind;
7470 715dc77e 2019-08-03 stsp argv += optind;
7471 715dc77e 2019-08-03 stsp
7472 715dc77e 2019-08-03 stsp #ifndef PROFILE
7473 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7474 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7475 715dc77e 2019-08-03 stsp err(1, "pledge");
7476 715dc77e 2019-08-03 stsp #endif
7477 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7478 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7479 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7480 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7481 715dc77e 2019-08-03 stsp
7482 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7483 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7484 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7485 715dc77e 2019-08-03 stsp goto done;
7486 715dc77e 2019-08-03 stsp }
7487 715dc77e 2019-08-03 stsp
7488 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7489 715dc77e 2019-08-03 stsp if (error)
7490 715dc77e 2019-08-03 stsp goto done;
7491 715dc77e 2019-08-03 stsp
7492 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7493 c9956ddf 2019-09-08 stsp NULL);
7494 715dc77e 2019-08-03 stsp if (error != NULL)
7495 715dc77e 2019-08-03 stsp goto done;
7496 715dc77e 2019-08-03 stsp
7497 dc424a06 2019-08-07 stsp if (patch_script_path) {
7498 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7499 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7500 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7501 dc424a06 2019-08-07 stsp patch_script_path);
7502 dc424a06 2019-08-07 stsp goto done;
7503 dc424a06 2019-08-07 stsp }
7504 dc424a06 2019-08-07 stsp }
7505 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7506 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7507 715dc77e 2019-08-03 stsp if (error)
7508 715dc77e 2019-08-03 stsp goto done;
7509 715dc77e 2019-08-03 stsp
7510 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7511 6d022e97 2019-08-04 stsp if (error)
7512 6d022e97 2019-08-04 stsp goto done;
7513 715dc77e 2019-08-03 stsp
7514 6d022e97 2019-08-04 stsp if (list_stage)
7515 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7516 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7517 2e1f37b0 2019-08-08 stsp else {
7518 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7519 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7520 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7521 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7522 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7523 2e1f37b0 2019-08-08 stsp }
7524 ad493afc 2019-08-03 stsp done:
7525 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7526 2e1f37b0 2019-08-08 stsp error == NULL)
7527 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7528 ad493afc 2019-08-03 stsp if (repo)
7529 ad493afc 2019-08-03 stsp got_repo_close(repo);
7530 ad493afc 2019-08-03 stsp if (worktree)
7531 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7532 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7533 ad493afc 2019-08-03 stsp free((char *)pe->path);
7534 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7535 ad493afc 2019-08-03 stsp free(cwd);
7536 ad493afc 2019-08-03 stsp return error;
7537 ad493afc 2019-08-03 stsp }
7538 ad493afc 2019-08-03 stsp
7539 ad493afc 2019-08-03 stsp __dead static void
7540 ad493afc 2019-08-03 stsp usage_unstage(void)
7541 ad493afc 2019-08-03 stsp {
7542 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7543 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7544 ad493afc 2019-08-03 stsp getprogname());
7545 ad493afc 2019-08-03 stsp exit(1);
7546 ad493afc 2019-08-03 stsp }
7547 ad493afc 2019-08-03 stsp
7548 ad493afc 2019-08-03 stsp
7549 ad493afc 2019-08-03 stsp static const struct got_error *
7550 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7551 ad493afc 2019-08-03 stsp {
7552 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7553 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7554 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7555 ad493afc 2019-08-03 stsp char *cwd = NULL;
7556 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7557 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7558 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7559 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7560 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7561 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7562 ad493afc 2019-08-03 stsp
7563 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7564 ad493afc 2019-08-03 stsp
7565 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7566 ad493afc 2019-08-03 stsp switch (ch) {
7567 2e1f37b0 2019-08-08 stsp case 'p':
7568 2e1f37b0 2019-08-08 stsp pflag = 1;
7569 2e1f37b0 2019-08-08 stsp break;
7570 2e1f37b0 2019-08-08 stsp case 'F':
7571 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7572 2e1f37b0 2019-08-08 stsp break;
7573 ad493afc 2019-08-03 stsp default:
7574 ad493afc 2019-08-03 stsp usage_unstage();
7575 ad493afc 2019-08-03 stsp /* NOTREACHED */
7576 ad493afc 2019-08-03 stsp }
7577 ad493afc 2019-08-03 stsp }
7578 ad493afc 2019-08-03 stsp
7579 ad493afc 2019-08-03 stsp argc -= optind;
7580 ad493afc 2019-08-03 stsp argv += optind;
7581 ad493afc 2019-08-03 stsp
7582 ad493afc 2019-08-03 stsp #ifndef PROFILE
7583 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7584 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7585 ad493afc 2019-08-03 stsp err(1, "pledge");
7586 ad493afc 2019-08-03 stsp #endif
7587 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7588 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7589 2e1f37b0 2019-08-08 stsp
7590 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7591 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7592 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7593 ad493afc 2019-08-03 stsp goto done;
7594 ad493afc 2019-08-03 stsp }
7595 ad493afc 2019-08-03 stsp
7596 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7597 ad493afc 2019-08-03 stsp if (error)
7598 ad493afc 2019-08-03 stsp goto done;
7599 ad493afc 2019-08-03 stsp
7600 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7601 c9956ddf 2019-09-08 stsp NULL);
7602 ad493afc 2019-08-03 stsp if (error != NULL)
7603 ad493afc 2019-08-03 stsp goto done;
7604 ad493afc 2019-08-03 stsp
7605 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7606 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7607 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7608 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7609 2e1f37b0 2019-08-08 stsp patch_script_path);
7610 2e1f37b0 2019-08-08 stsp goto done;
7611 2e1f37b0 2019-08-08 stsp }
7612 2e1f37b0 2019-08-08 stsp }
7613 2e1f37b0 2019-08-08 stsp
7614 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7615 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7616 ad493afc 2019-08-03 stsp if (error)
7617 ad493afc 2019-08-03 stsp goto done;
7618 ad493afc 2019-08-03 stsp
7619 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7620 ad493afc 2019-08-03 stsp if (error)
7621 ad493afc 2019-08-03 stsp goto done;
7622 ad493afc 2019-08-03 stsp
7623 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7624 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7625 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7626 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7627 715dc77e 2019-08-03 stsp done:
7628 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7629 2e1f37b0 2019-08-08 stsp error == NULL)
7630 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7631 0ebf8283 2019-07-24 stsp if (repo)
7632 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7633 715dc77e 2019-08-03 stsp if (worktree)
7634 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7635 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7636 715dc77e 2019-08-03 stsp free((char *)pe->path);
7637 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7638 715dc77e 2019-08-03 stsp free(cwd);
7639 01073a5d 2019-08-22 stsp return error;
7640 01073a5d 2019-08-22 stsp }
7641 01073a5d 2019-08-22 stsp
7642 01073a5d 2019-08-22 stsp __dead static void
7643 01073a5d 2019-08-22 stsp usage_cat(void)
7644 01073a5d 2019-08-22 stsp {
7645 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7646 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7647 01073a5d 2019-08-22 stsp exit(1);
7648 01073a5d 2019-08-22 stsp }
7649 01073a5d 2019-08-22 stsp
7650 01073a5d 2019-08-22 stsp static const struct got_error *
7651 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7652 01073a5d 2019-08-22 stsp {
7653 01073a5d 2019-08-22 stsp const struct got_error *err;
7654 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7655 01073a5d 2019-08-22 stsp
7656 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7657 01073a5d 2019-08-22 stsp if (err)
7658 01073a5d 2019-08-22 stsp return err;
7659 01073a5d 2019-08-22 stsp
7660 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7661 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7662 01073a5d 2019-08-22 stsp return err;
7663 01073a5d 2019-08-22 stsp }
7664 01073a5d 2019-08-22 stsp
7665 01073a5d 2019-08-22 stsp static const struct got_error *
7666 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7667 01073a5d 2019-08-22 stsp {
7668 01073a5d 2019-08-22 stsp const struct got_error *err;
7669 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7670 56e0773d 2019-11-28 stsp int nentries, i;
7671 01073a5d 2019-08-22 stsp
7672 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7673 01073a5d 2019-08-22 stsp if (err)
7674 01073a5d 2019-08-22 stsp return err;
7675 01073a5d 2019-08-22 stsp
7676 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7677 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7678 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7679 01073a5d 2019-08-22 stsp char *id_str;
7680 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7681 01073a5d 2019-08-22 stsp break;
7682 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7683 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7684 01073a5d 2019-08-22 stsp if (err)
7685 01073a5d 2019-08-22 stsp break;
7686 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7687 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7688 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7689 01073a5d 2019-08-22 stsp free(id_str);
7690 01073a5d 2019-08-22 stsp }
7691 01073a5d 2019-08-22 stsp
7692 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7693 01073a5d 2019-08-22 stsp return err;
7694 01073a5d 2019-08-22 stsp }
7695 01073a5d 2019-08-22 stsp
7696 01073a5d 2019-08-22 stsp static const struct got_error *
7697 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7698 01073a5d 2019-08-22 stsp {
7699 01073a5d 2019-08-22 stsp const struct got_error *err;
7700 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7701 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7702 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7703 01073a5d 2019-08-22 stsp char *id_str = NULL;
7704 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7705 01073a5d 2019-08-22 stsp
7706 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7707 01073a5d 2019-08-22 stsp if (err)
7708 01073a5d 2019-08-22 stsp return err;
7709 01073a5d 2019-08-22 stsp
7710 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7711 01073a5d 2019-08-22 stsp if (err)
7712 01073a5d 2019-08-22 stsp goto done;
7713 01073a5d 2019-08-22 stsp
7714 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7715 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7716 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7717 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7718 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7719 01073a5d 2019-08-22 stsp char *pid_str;
7720 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7721 01073a5d 2019-08-22 stsp if (err)
7722 01073a5d 2019-08-22 stsp goto done;
7723 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7724 01073a5d 2019-08-22 stsp free(pid_str);
7725 01073a5d 2019-08-22 stsp }
7726 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7727 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7728 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7729 01073a5d 2019-08-22 stsp
7730 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7731 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7732 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7733 01073a5d 2019-08-22 stsp
7734 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7735 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7736 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7737 01073a5d 2019-08-22 stsp done:
7738 01073a5d 2019-08-22 stsp free(id_str);
7739 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7740 01073a5d 2019-08-22 stsp return err;
7741 01073a5d 2019-08-22 stsp }
7742 01073a5d 2019-08-22 stsp
7743 01073a5d 2019-08-22 stsp static const struct got_error *
7744 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7745 01073a5d 2019-08-22 stsp {
7746 01073a5d 2019-08-22 stsp const struct got_error *err;
7747 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7748 01073a5d 2019-08-22 stsp char *id_str = NULL;
7749 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7750 01073a5d 2019-08-22 stsp
7751 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7752 01073a5d 2019-08-22 stsp if (err)
7753 01073a5d 2019-08-22 stsp return err;
7754 01073a5d 2019-08-22 stsp
7755 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7756 01073a5d 2019-08-22 stsp if (err)
7757 01073a5d 2019-08-22 stsp goto done;
7758 01073a5d 2019-08-22 stsp
7759 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7760 e15d5241 2019-08-22 stsp
7761 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7762 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7763 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7764 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7765 01073a5d 2019-08-22 stsp break;
7766 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7767 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7768 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7769 01073a5d 2019-08-22 stsp break;
7770 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7771 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7772 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7773 01073a5d 2019-08-22 stsp break;
7774 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7775 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7776 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7777 01073a5d 2019-08-22 stsp break;
7778 01073a5d 2019-08-22 stsp default:
7779 01073a5d 2019-08-22 stsp break;
7780 01073a5d 2019-08-22 stsp }
7781 01073a5d 2019-08-22 stsp
7782 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7783 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7784 e15d5241 2019-08-22 stsp
7785 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7786 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7787 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7788 01073a5d 2019-08-22 stsp
7789 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7790 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7791 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7792 01073a5d 2019-08-22 stsp done:
7793 01073a5d 2019-08-22 stsp free(id_str);
7794 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7795 01073a5d 2019-08-22 stsp return err;
7796 01073a5d 2019-08-22 stsp }
7797 01073a5d 2019-08-22 stsp
7798 01073a5d 2019-08-22 stsp static const struct got_error *
7799 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7800 01073a5d 2019-08-22 stsp {
7801 01073a5d 2019-08-22 stsp const struct got_error *error;
7802 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7803 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7804 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7805 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7806 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7807 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7808 01073a5d 2019-08-22 stsp
7809 01073a5d 2019-08-22 stsp #ifndef PROFILE
7810 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7811 01073a5d 2019-08-22 stsp NULL) == -1)
7812 01073a5d 2019-08-22 stsp err(1, "pledge");
7813 01073a5d 2019-08-22 stsp #endif
7814 01073a5d 2019-08-22 stsp
7815 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7816 01073a5d 2019-08-22 stsp switch (ch) {
7817 896e9b6f 2019-08-26 stsp case 'c':
7818 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7819 896e9b6f 2019-08-26 stsp break;
7820 01073a5d 2019-08-22 stsp case 'r':
7821 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7822 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7823 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7824 9ba1d308 2019-10-21 stsp optarg);
7825 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7826 01073a5d 2019-08-22 stsp break;
7827 896e9b6f 2019-08-26 stsp case 'P':
7828 896e9b6f 2019-08-26 stsp force_path = 1;
7829 896e9b6f 2019-08-26 stsp break;
7830 01073a5d 2019-08-22 stsp default:
7831 01073a5d 2019-08-22 stsp usage_cat();
7832 01073a5d 2019-08-22 stsp /* NOTREACHED */
7833 01073a5d 2019-08-22 stsp }
7834 01073a5d 2019-08-22 stsp }
7835 01073a5d 2019-08-22 stsp
7836 01073a5d 2019-08-22 stsp argc -= optind;
7837 01073a5d 2019-08-22 stsp argv += optind;
7838 01073a5d 2019-08-22 stsp
7839 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7840 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7841 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7842 01073a5d 2019-08-22 stsp goto done;
7843 01073a5d 2019-08-22 stsp }
7844 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7845 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7846 01073a5d 2019-08-22 stsp goto done;
7847 01073a5d 2019-08-22 stsp if (worktree) {
7848 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7849 01073a5d 2019-08-22 stsp repo_path = strdup(
7850 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7851 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7852 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7853 01073a5d 2019-08-22 stsp goto done;
7854 01073a5d 2019-08-22 stsp }
7855 01073a5d 2019-08-22 stsp }
7856 01073a5d 2019-08-22 stsp }
7857 01073a5d 2019-08-22 stsp
7858 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7859 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7860 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7861 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7862 01073a5d 2019-08-22 stsp }
7863 01073a5d 2019-08-22 stsp
7864 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7865 01073a5d 2019-08-22 stsp free(repo_path);
7866 01073a5d 2019-08-22 stsp if (error != NULL)
7867 01073a5d 2019-08-22 stsp goto done;
7868 01073a5d 2019-08-22 stsp
7869 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7870 896e9b6f 2019-08-26 stsp if (error)
7871 896e9b6f 2019-08-26 stsp goto done;
7872 896e9b6f 2019-08-26 stsp
7873 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7874 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7875 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7876 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7877 01073a5d 2019-08-22 stsp if (error)
7878 01073a5d 2019-08-22 stsp goto done;
7879 01073a5d 2019-08-22 stsp
7880 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7881 896e9b6f 2019-08-26 stsp if (force_path) {
7882 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7883 896e9b6f 2019-08-26 stsp argv[i]);
7884 896e9b6f 2019-08-26 stsp if (error)
7885 896e9b6f 2019-08-26 stsp break;
7886 896e9b6f 2019-08-26 stsp } else {
7887 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7888 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7889 896e9b6f 2019-08-26 stsp if (error) {
7890 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7891 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7892 896e9b6f 2019-08-26 stsp break;
7893 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7894 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7895 896e9b6f 2019-08-26 stsp if (error)
7896 896e9b6f 2019-08-26 stsp break;
7897 896e9b6f 2019-08-26 stsp }
7898 896e9b6f 2019-08-26 stsp }
7899 01073a5d 2019-08-22 stsp
7900 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7901 01073a5d 2019-08-22 stsp if (error)
7902 01073a5d 2019-08-22 stsp break;
7903 01073a5d 2019-08-22 stsp
7904 01073a5d 2019-08-22 stsp switch (obj_type) {
7905 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7906 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7907 01073a5d 2019-08-22 stsp break;
7908 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7909 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7910 01073a5d 2019-08-22 stsp break;
7911 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7912 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7913 01073a5d 2019-08-22 stsp break;
7914 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7915 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7916 01073a5d 2019-08-22 stsp break;
7917 01073a5d 2019-08-22 stsp default:
7918 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7919 01073a5d 2019-08-22 stsp break;
7920 01073a5d 2019-08-22 stsp }
7921 01073a5d 2019-08-22 stsp if (error)
7922 01073a5d 2019-08-22 stsp break;
7923 01073a5d 2019-08-22 stsp free(label);
7924 01073a5d 2019-08-22 stsp label = NULL;
7925 01073a5d 2019-08-22 stsp free(id);
7926 01073a5d 2019-08-22 stsp id = NULL;
7927 01073a5d 2019-08-22 stsp }
7928 01073a5d 2019-08-22 stsp done:
7929 01073a5d 2019-08-22 stsp free(label);
7930 01073a5d 2019-08-22 stsp free(id);
7931 896e9b6f 2019-08-26 stsp free(commit_id);
7932 01073a5d 2019-08-22 stsp if (worktree)
7933 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7934 01073a5d 2019-08-22 stsp if (repo) {
7935 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7936 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7937 01073a5d 2019-08-22 stsp if (error == NULL)
7938 01073a5d 2019-08-22 stsp error = repo_error;
7939 01073a5d 2019-08-22 stsp }
7940 0ebf8283 2019-07-24 stsp return error;
7941 0ebf8283 2019-07-24 stsp }