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 5c860e29 2018-03-12 stsp
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 404c43c4 2018-06-21 stsp #include "got_blame.h"
54 63219cd2 2019-01-04 stsp #include "got_privsep.h"
55 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
56 5c860e29 2018-03-12 stsp
57 5c860e29 2018-03-12 stsp #ifndef nitems
58 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 5c860e29 2018-03-12 stsp #endif
60 99437157 2018-11-11 stsp
61 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static void
65 99437157 2018-11-11 stsp catch_sigint(int signo)
66 99437157 2018-11-11 stsp {
67 99437157 2018-11-11 stsp sigint_received = 1;
68 99437157 2018-11-11 stsp }
69 99437157 2018-11-11 stsp
70 99437157 2018-11-11 stsp static void
71 99437157 2018-11-11 stsp catch_sigpipe(int signo)
72 99437157 2018-11-11 stsp {
73 99437157 2018-11-11 stsp sigpipe_received = 1;
74 99437157 2018-11-11 stsp }
75 5c860e29 2018-03-12 stsp
76 99437157 2018-11-11 stsp
77 8cfb4057 2019-07-09 stsp struct got_cmd {
78 5c860e29 2018-03-12 stsp const char *cmd_name;
79 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
81 97b3a7be 2019-07-09 stsp const char *cmd_alias;
82 5c860e29 2018-03-12 stsp };
83 5c860e29 2018-03-12 stsp
84 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
85 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
86 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
90 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
92 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
93 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
94 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
95 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
96 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
97 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
98 d00136be 2019-03-26 stsp __dead static void usage_add(void);
99 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
100 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
101 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
102 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
103 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
104 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
105 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
106 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
107 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
108 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
109 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
110 5c860e29 2018-03-12 stsp
111 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
112 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
113 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
114 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
115 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
118 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
119 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
121 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
122 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
123 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
124 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
125 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
126 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
127 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
128 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
129 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
130 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
131 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
132 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
133 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
134 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
135 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
136 5c860e29 2018-03-12 stsp
137 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
138 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
139 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
140 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
141 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
142 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
143 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
144 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
145 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
146 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
147 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
148 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
149 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
150 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
151 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
152 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
153 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
154 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
155 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
157 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
158 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
159 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
160 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
161 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
162 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
163 5c860e29 2018-03-12 stsp };
164 ce5b7c56 2019-07-09 stsp
165 ce5b7c56 2019-07-09 stsp static void
166 ce5b7c56 2019-07-09 stsp list_commands(void)
167 ce5b7c56 2019-07-09 stsp {
168 ce5b7c56 2019-07-09 stsp int i;
169 ce5b7c56 2019-07-09 stsp
170 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
171 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
172 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
173 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
174 ce5b7c56 2019-07-09 stsp }
175 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
176 ce5b7c56 2019-07-09 stsp }
177 5c860e29 2018-03-12 stsp
178 5c860e29 2018-03-12 stsp int
179 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
180 5c860e29 2018-03-12 stsp {
181 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
182 5c860e29 2018-03-12 stsp unsigned int i;
183 5c860e29 2018-03-12 stsp int ch;
184 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
185 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
186 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
187 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
188 83cd27f8 2020-01-13 stsp };
189 5c860e29 2018-03-12 stsp
190 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
191 5c860e29 2018-03-12 stsp
192 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 5c860e29 2018-03-12 stsp switch (ch) {
194 1b6b95a8 2018-03-12 stsp case 'h':
195 1b6b95a8 2018-03-12 stsp hflag = 1;
196 53ccebc2 2019-07-30 stsp break;
197 53ccebc2 2019-07-30 stsp case 'V':
198 53ccebc2 2019-07-30 stsp Vflag = 1;
199 1b6b95a8 2018-03-12 stsp break;
200 5c860e29 2018-03-12 stsp default:
201 ce5b7c56 2019-07-09 stsp usage(hflag);
202 5c860e29 2018-03-12 stsp /* NOTREACHED */
203 5c860e29 2018-03-12 stsp }
204 5c860e29 2018-03-12 stsp }
205 5c860e29 2018-03-12 stsp
206 5c860e29 2018-03-12 stsp argc -= optind;
207 5c860e29 2018-03-12 stsp argv += optind;
208 1e70621d 2018-03-27 stsp optind = 0;
209 53ccebc2 2019-07-30 stsp
210 53ccebc2 2019-07-30 stsp if (Vflag) {
211 53ccebc2 2019-07-30 stsp got_version_print_str();
212 53ccebc2 2019-07-30 stsp return 1;
213 53ccebc2 2019-07-30 stsp }
214 5c860e29 2018-03-12 stsp
215 5c860e29 2018-03-12 stsp if (argc <= 0)
216 ce5b7c56 2019-07-09 stsp usage(hflag);
217 5c860e29 2018-03-12 stsp
218 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
219 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
220 99437157 2018-11-11 stsp
221 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
222 d7d4f210 2018-03-12 stsp const struct got_error *error;
223 d7d4f210 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
225 5c860e29 2018-03-12 stsp
226 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
228 5c860e29 2018-03-12 stsp continue;
229 5c860e29 2018-03-12 stsp
230 1b6b95a8 2018-03-12 stsp if (hflag)
231 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
232 1b6b95a8 2018-03-12 stsp
233 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
234 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
235 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
236 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
237 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 70015d7a 2019-11-08 stsp !(sigint_received &&
239 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
241 d7d4f210 2018-03-12 stsp return 1;
242 d7d4f210 2018-03-12 stsp }
243 d7d4f210 2018-03-12 stsp
244 d7d4f210 2018-03-12 stsp return 0;
245 5c860e29 2018-03-12 stsp }
246 5c860e29 2018-03-12 stsp
247 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 ce5b7c56 2019-07-09 stsp list_commands();
249 5c860e29 2018-03-12 stsp return 1;
250 5c860e29 2018-03-12 stsp }
251 5c860e29 2018-03-12 stsp
252 4ed7e80c 2018-05-20 stsp __dead static void
253 ce5b7c56 2019-07-09 stsp usage(int hflag)
254 5c860e29 2018-03-12 stsp {
255 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 53ccebc2 2019-07-30 stsp getprogname());
257 ce5b7c56 2019-07-09 stsp if (hflag)
258 ce5b7c56 2019-07-09 stsp list_commands();
259 5c860e29 2018-03-12 stsp exit(1);
260 5c860e29 2018-03-12 stsp }
261 5c860e29 2018-03-12 stsp
262 0266afb7 2019-01-04 stsp static const struct got_error *
263 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
264 e2ba3d07 2019-05-13 stsp {
265 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
266 e2ba3d07 2019-05-13 stsp const char *editor;
267 8920fa04 2019-08-18 stsp
268 8920fa04 2019-08-18 stsp *abspath = NULL;
269 e2ba3d07 2019-05-13 stsp
270 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
271 e2ba3d07 2019-05-13 stsp if (editor == NULL)
272 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
273 e2ba3d07 2019-05-13 stsp
274 0ee7065d 2019-05-13 stsp if (editor) {
275 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
276 0ee7065d 2019-05-13 stsp if (err)
277 0ee7065d 2019-05-13 stsp return err;
278 0ee7065d 2019-05-13 stsp }
279 e2ba3d07 2019-05-13 stsp
280 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
281 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
282 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
283 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
284 0ee7065d 2019-05-13 stsp }
285 0ee7065d 2019-05-13 stsp
286 e2ba3d07 2019-05-13 stsp return NULL;
287 e2ba3d07 2019-05-13 stsp }
288 e2ba3d07 2019-05-13 stsp
289 e2ba3d07 2019-05-13 stsp static const struct got_error *
290 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
291 c530dc23 2019-07-23 stsp const char *worktree_path)
292 0266afb7 2019-01-04 stsp {
293 163ce85a 2019-05-13 stsp const struct got_error *err;
294 0266afb7 2019-01-04 stsp
295 37c06ea4 2019-07-15 stsp #ifdef PROFILE
296 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
297 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
298 37c06ea4 2019-07-15 stsp #endif
299 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
301 0266afb7 2019-01-04 stsp
302 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
304 0266afb7 2019-01-04 stsp
305 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
307 0266afb7 2019-01-04 stsp
308 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
309 163ce85a 2019-05-13 stsp if (err != NULL)
310 163ce85a 2019-05-13 stsp return err;
311 0266afb7 2019-01-04 stsp
312 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
313 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
314 0266afb7 2019-01-04 stsp
315 0266afb7 2019-01-04 stsp return NULL;
316 2c7829a4 2019-06-17 stsp }
317 2c7829a4 2019-06-17 stsp
318 2c7829a4 2019-06-17 stsp __dead static void
319 2c7829a4 2019-06-17 stsp usage_init(void)
320 2c7829a4 2019-06-17 stsp {
321 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
322 2c7829a4 2019-06-17 stsp exit(1);
323 2c7829a4 2019-06-17 stsp }
324 2c7829a4 2019-06-17 stsp
325 2c7829a4 2019-06-17 stsp static const struct got_error *
326 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
327 2c7829a4 2019-06-17 stsp {
328 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
329 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
330 2c7829a4 2019-06-17 stsp int ch;
331 2c7829a4 2019-06-17 stsp
332 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
333 2c7829a4 2019-06-17 stsp switch (ch) {
334 2c7829a4 2019-06-17 stsp default:
335 2c7829a4 2019-06-17 stsp usage_init();
336 2c7829a4 2019-06-17 stsp /* NOTREACHED */
337 2c7829a4 2019-06-17 stsp }
338 2c7829a4 2019-06-17 stsp }
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp argc -= optind;
341 2c7829a4 2019-06-17 stsp argv += optind;
342 2c7829a4 2019-06-17 stsp
343 2c7829a4 2019-06-17 stsp #ifndef PROFILE
344 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 2c7829a4 2019-06-17 stsp err(1, "pledge");
346 2c7829a4 2019-06-17 stsp #endif
347 2c7829a4 2019-06-17 stsp if (argc != 1)
348 bc20e173 2019-06-17 stsp usage_init();
349 2c7829a4 2019-06-17 stsp
350 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
351 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
352 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
355 2c7829a4 2019-06-17 stsp
356 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
357 2c7829a4 2019-06-17 stsp if (error &&
358 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
359 2c7829a4 2019-06-17 stsp goto done;
360 2c7829a4 2019-06-17 stsp
361 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
362 2c7829a4 2019-06-17 stsp if (error)
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
366 3ce1b845 2019-07-15 stsp done:
367 3ce1b845 2019-07-15 stsp free(repo_path);
368 3ce1b845 2019-07-15 stsp return error;
369 3ce1b845 2019-07-15 stsp }
370 3ce1b845 2019-07-15 stsp
371 3ce1b845 2019-07-15 stsp __dead static void
372 3ce1b845 2019-07-15 stsp usage_import(void)
373 3ce1b845 2019-07-15 stsp {
374 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
376 3ce1b845 2019-07-15 stsp exit(1);
377 3ce1b845 2019-07-15 stsp }
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp int
380 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
381 3ce1b845 2019-07-15 stsp {
382 3ce1b845 2019-07-15 stsp pid_t pid;
383 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
384 3ce1b845 2019-07-15 stsp int st = -1;
385 3ce1b845 2019-07-15 stsp
386 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
387 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
391 3ce1b845 2019-07-15 stsp case -1:
392 3ce1b845 2019-07-15 stsp goto doneediting;
393 3ce1b845 2019-07-15 stsp case 0:
394 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
395 3ce1b845 2019-07-15 stsp _exit(127);
396 3ce1b845 2019-07-15 stsp }
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
399 3ce1b845 2019-07-15 stsp if (errno != EINTR)
400 3ce1b845 2019-07-15 stsp break;
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp doneediting:
403 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
404 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
408 3ce1b845 2019-07-15 stsp errno = EINTR;
409 3ce1b845 2019-07-15 stsp return -1;
410 3ce1b845 2019-07-15 stsp }
411 3ce1b845 2019-07-15 stsp
412 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
413 3ce1b845 2019-07-15 stsp }
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp static const struct got_error *
416 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 3ce1b845 2019-07-15 stsp const char *initial_content)
418 3ce1b845 2019-07-15 stsp {
419 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
420 3ce1b845 2019-07-15 stsp char buf[1024];
421 3ce1b845 2019-07-15 stsp struct stat st, st2;
422 3ce1b845 2019-07-15 stsp FILE *fp;
423 3ce1b845 2019-07-15 stsp int content_changed = 0;
424 3ce1b845 2019-07-15 stsp size_t len;
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp *logmsg = NULL;
427 3ce1b845 2019-07-15 stsp
428 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
429 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
430 3ce1b845 2019-07-15 stsp
431 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
432 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
435 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
436 3ce1b845 2019-07-15 stsp
437 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
442 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
444 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
445 3ce1b845 2019-07-15 stsp len = 0;
446 3ce1b845 2019-07-15 stsp
447 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
448 3ce1b845 2019-07-15 stsp if (fp == NULL) {
449 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
450 3ce1b845 2019-07-15 stsp goto done;
451 3ce1b845 2019-07-15 stsp }
452 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
453 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
454 3ce1b845 2019-07-15 stsp content_changed = 1;
455 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
457 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
458 3ce1b845 2019-07-15 stsp }
459 3ce1b845 2019-07-15 stsp fclose(fp);
460 3ce1b845 2019-07-15 stsp
461 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
463 3ce1b845 2019-07-15 stsp len--;
464 3ce1b845 2019-07-15 stsp }
465 3ce1b845 2019-07-15 stsp
466 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
467 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
469 3ce1b845 2019-07-15 stsp done:
470 3ce1b845 2019-07-15 stsp if (err) {
471 3ce1b845 2019-07-15 stsp free(*logmsg);
472 3ce1b845 2019-07-15 stsp *logmsg = NULL;
473 3ce1b845 2019-07-15 stsp }
474 3ce1b845 2019-07-15 stsp return err;
475 3ce1b845 2019-07-15 stsp }
476 3ce1b845 2019-07-15 stsp
477 3ce1b845 2019-07-15 stsp static const struct got_error *
478 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
480 3ce1b845 2019-07-15 stsp {
481 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
482 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
483 3ce1b845 2019-07-15 stsp int fd;
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
486 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
487 3ce1b845 2019-07-15 stsp branch_name) == -1)
488 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
489 3ce1b845 2019-07-15 stsp
490 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
491 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
492 3ce1b845 2019-07-15 stsp if (err)
493 3ce1b845 2019-07-15 stsp goto done;
494 3ce1b845 2019-07-15 stsp
495 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
496 3ce1b845 2019-07-15 stsp close(fd);
497 3ce1b845 2019-07-15 stsp
498 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
499 3ce1b845 2019-07-15 stsp done:
500 3ce1b845 2019-07-15 stsp free(initial_content);
501 3ce1b845 2019-07-15 stsp return err;
502 3ce1b845 2019-07-15 stsp }
503 3ce1b845 2019-07-15 stsp
504 3ce1b845 2019-07-15 stsp static const struct got_error *
505 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
506 3ce1b845 2019-07-15 stsp {
507 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
508 3ce1b845 2019-07-15 stsp return NULL;
509 3ce1b845 2019-07-15 stsp }
510 3ce1b845 2019-07-15 stsp
511 3ce1b845 2019-07-15 stsp static const struct got_error *
512 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
513 84792843 2019-08-09 stsp {
514 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
515 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
516 84792843 2019-08-09 stsp
517 84792843 2019-08-09 stsp *author = NULL;
518 aba9c984 2019-09-08 stsp
519 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
520 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
521 c9956ddf 2019-09-08 stsp if (name && email) {
522 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
523 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
524 aba9c984 2019-09-08 stsp return NULL;
525 aba9c984 2019-09-08 stsp }
526 84792843 2019-08-09 stsp
527 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
528 84792843 2019-08-09 stsp if (got_author == NULL) {
529 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
530 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
531 c9956ddf 2019-09-08 stsp if (name && email) {
532 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
533 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
534 c9956ddf 2019-09-08 stsp return NULL;
535 c9956ddf 2019-09-08 stsp }
536 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
537 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
538 84792843 2019-08-09 stsp }
539 84792843 2019-08-09 stsp
540 aba9c984 2019-09-08 stsp *author = strdup(got_author);
541 aba9c984 2019-09-08 stsp if (*author == NULL)
542 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
543 84792843 2019-08-09 stsp
544 84792843 2019-08-09 stsp /*
545 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
546 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
547 84792843 2019-08-09 stsp */
548 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
549 84792843 2019-08-09 stsp got_author++;
550 aba9c984 2019-09-08 stsp if (*got_author != '<') {
551 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 aba9c984 2019-09-08 stsp goto done;
553 aba9c984 2019-09-08 stsp }
554 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
555 84792843 2019-08-09 stsp got_author++;
556 aba9c984 2019-09-08 stsp if (*got_author != '@') {
557 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 aba9c984 2019-09-08 stsp goto done;
559 aba9c984 2019-09-08 stsp }
560 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
561 84792843 2019-08-09 stsp got_author++;
562 84792843 2019-08-09 stsp if (*got_author != '>')
563 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 aba9c984 2019-09-08 stsp done:
565 aba9c984 2019-09-08 stsp if (err) {
566 aba9c984 2019-09-08 stsp free(*author);
567 aba9c984 2019-09-08 stsp *author = NULL;
568 aba9c984 2019-09-08 stsp }
569 aba9c984 2019-09-08 stsp return err;
570 c9956ddf 2019-09-08 stsp }
571 c9956ddf 2019-09-08 stsp
572 c9956ddf 2019-09-08 stsp static const struct got_error *
573 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
574 c9956ddf 2019-09-08 stsp {
575 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
576 c9956ddf 2019-09-08 stsp
577 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
578 c9956ddf 2019-09-08 stsp if (homedir) {
579 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
581 c9956ddf 2019-09-08 stsp
582 c9956ddf 2019-09-08 stsp }
583 c9956ddf 2019-09-08 stsp return NULL;
584 84792843 2019-08-09 stsp }
585 84792843 2019-08-09 stsp
586 84792843 2019-08-09 stsp static const struct got_error *
587 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
588 3ce1b845 2019-07-15 stsp {
589 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
590 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
593 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
595 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
597 3ce1b845 2019-07-15 stsp int ch;
598 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
600 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
601 3ce1b845 2019-07-15 stsp
602 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
603 3ce1b845 2019-07-15 stsp
604 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 3ce1b845 2019-07-15 stsp switch (ch) {
606 3ce1b845 2019-07-15 stsp case 'b':
607 3ce1b845 2019-07-15 stsp branch_name = optarg;
608 3ce1b845 2019-07-15 stsp break;
609 3ce1b845 2019-07-15 stsp case 'm':
610 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
611 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
612 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
613 3ce1b845 2019-07-15 stsp goto done;
614 3ce1b845 2019-07-15 stsp }
615 3ce1b845 2019-07-15 stsp break;
616 3ce1b845 2019-07-15 stsp case 'r':
617 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
618 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
619 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
620 9ba1d308 2019-10-21 stsp optarg);
621 3ce1b845 2019-07-15 stsp goto done;
622 3ce1b845 2019-07-15 stsp }
623 3ce1b845 2019-07-15 stsp break;
624 3ce1b845 2019-07-15 stsp case 'I':
625 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
626 3ce1b845 2019-07-15 stsp break;
627 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
628 3ce1b845 2019-07-15 stsp NULL);
629 3ce1b845 2019-07-15 stsp if (error)
630 3ce1b845 2019-07-15 stsp goto done;
631 3ce1b845 2019-07-15 stsp break;
632 3ce1b845 2019-07-15 stsp default:
633 b2b65d18 2019-08-22 stsp usage_import();
634 3ce1b845 2019-07-15 stsp /* NOTREACHED */
635 3ce1b845 2019-07-15 stsp }
636 3ce1b845 2019-07-15 stsp }
637 3ce1b845 2019-07-15 stsp
638 3ce1b845 2019-07-15 stsp argc -= optind;
639 3ce1b845 2019-07-15 stsp argv += optind;
640 3ce1b845 2019-07-15 stsp
641 3ce1b845 2019-07-15 stsp #ifndef PROFILE
642 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
643 aba9c984 2019-09-08 stsp "unveil",
644 3ce1b845 2019-07-15 stsp NULL) == -1)
645 3ce1b845 2019-07-15 stsp err(1, "pledge");
646 3ce1b845 2019-07-15 stsp #endif
647 3ce1b845 2019-07-15 stsp if (argc != 1)
648 3ce1b845 2019-07-15 stsp usage_import();
649 2c7829a4 2019-06-17 stsp
650 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
651 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
652 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
653 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
654 3ce1b845 2019-07-15 stsp }
655 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
656 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
657 c9956ddf 2019-09-08 stsp if (error)
658 c9956ddf 2019-09-08 stsp goto done;
659 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
660 3ce1b845 2019-07-15 stsp if (error)
661 3ce1b845 2019-07-15 stsp goto done;
662 aba9c984 2019-09-08 stsp
663 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
664 aba9c984 2019-09-08 stsp if (error)
665 aba9c984 2019-09-08 stsp return error;
666 e560b7e0 2019-11-28 stsp
667 e560b7e0 2019-11-28 stsp /*
668 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
669 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
670 e560b7e0 2019-11-28 stsp * an unintended typo.
671 e560b7e0 2019-11-28 stsp */
672 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
673 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
674 3ce1b845 2019-07-15 stsp
675 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
677 3ce1b845 2019-07-15 stsp goto done;
678 3ce1b845 2019-07-15 stsp }
679 3ce1b845 2019-07-15 stsp
680 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
681 3ce1b845 2019-07-15 stsp if (error) {
682 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
683 3ce1b845 2019-07-15 stsp goto done;
684 3ce1b845 2019-07-15 stsp } else {
685 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 3ce1b845 2019-07-15 stsp "import target branch already exists");
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp }
689 3ce1b845 2019-07-15 stsp
690 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
691 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
692 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
693 3ce1b845 2019-07-15 stsp goto done;
694 3ce1b845 2019-07-15 stsp }
695 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
696 3ce1b845 2019-07-15 stsp
697 3ce1b845 2019-07-15 stsp /*
698 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
699 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
700 3ce1b845 2019-07-15 stsp */
701 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
702 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
703 3ce1b845 2019-07-15 stsp if (error)
704 3ce1b845 2019-07-15 stsp goto done;
705 8e158b01 2019-09-22 stsp free(logmsg);
706 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 ef293bdd 2019-10-21 stsp path_dir, refname);
708 ef293bdd 2019-10-21 stsp if (error) {
709 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
712 3ce1b845 2019-07-15 stsp goto done;
713 ef293bdd 2019-10-21 stsp }
714 3ce1b845 2019-07-15 stsp }
715 3ce1b845 2019-07-15 stsp
716 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
717 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
718 ef293bdd 2019-10-21 stsp if (logmsg_path)
719 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
720 3ce1b845 2019-07-15 stsp goto done;
721 ef293bdd 2019-10-21 stsp }
722 3ce1b845 2019-07-15 stsp
723 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 ef293bdd 2019-10-21 stsp if (error) {
725 ef293bdd 2019-10-21 stsp if (logmsg_path)
726 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
727 ef293bdd 2019-10-21 stsp goto done;
728 ef293bdd 2019-10-21 stsp }
729 ef293bdd 2019-10-21 stsp
730 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
732 ef293bdd 2019-10-21 stsp if (error) {
733 ef293bdd 2019-10-21 stsp if (logmsg_path)
734 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
735 3ce1b845 2019-07-15 stsp goto done;
736 ef293bdd 2019-10-21 stsp }
737 3ce1b845 2019-07-15 stsp
738 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 ef293bdd 2019-10-21 stsp if (error) {
740 ef293bdd 2019-10-21 stsp if (logmsg_path)
741 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
742 3ce1b845 2019-07-15 stsp goto done;
743 ef293bdd 2019-10-21 stsp }
744 3ce1b845 2019-07-15 stsp
745 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (logmsg_path)
748 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
749 3ce1b845 2019-07-15 stsp goto done;
750 ef293bdd 2019-10-21 stsp }
751 3ce1b845 2019-07-15 stsp
752 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
753 ef293bdd 2019-10-21 stsp if (error) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
756 3ce1b845 2019-07-15 stsp goto done;
757 ef293bdd 2019-10-21 stsp }
758 3ce1b845 2019-07-15 stsp
759 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 3ce1b845 2019-07-15 stsp if (error) {
761 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
762 ef293bdd 2019-10-21 stsp if (logmsg_path)
763 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
764 3ce1b845 2019-07-15 stsp goto done;
765 ef293bdd 2019-10-21 stsp }
766 3ce1b845 2019-07-15 stsp
767 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 3ce1b845 2019-07-15 stsp branch_ref);
769 ef293bdd 2019-10-21 stsp if (error) {
770 ef293bdd 2019-10-21 stsp if (logmsg_path)
771 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
772 3ce1b845 2019-07-15 stsp goto done;
773 ef293bdd 2019-10-21 stsp }
774 3ce1b845 2019-07-15 stsp
775 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
776 ef293bdd 2019-10-21 stsp if (error) {
777 ef293bdd 2019-10-21 stsp if (logmsg_path)
778 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
779 3ce1b845 2019-07-15 stsp goto done;
780 ef293bdd 2019-10-21 stsp }
781 3ce1b845 2019-07-15 stsp }
782 3ce1b845 2019-07-15 stsp
783 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
784 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
785 2c7829a4 2019-06-17 stsp done:
786 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
787 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
788 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
789 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
791 8e158b01 2019-09-22 stsp free(logmsg);
792 ef293bdd 2019-10-21 stsp free(logmsg_path);
793 2c7829a4 2019-06-17 stsp free(repo_path);
794 3ce1b845 2019-07-15 stsp free(editor);
795 3ce1b845 2019-07-15 stsp free(refname);
796 3ce1b845 2019-07-15 stsp free(new_commit_id);
797 3ce1b845 2019-07-15 stsp free(id_str);
798 aba9c984 2019-09-08 stsp free(author);
799 c9956ddf 2019-09-08 stsp free(gitconfig_path);
800 3ce1b845 2019-07-15 stsp if (branch_ref)
801 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
802 3ce1b845 2019-07-15 stsp if (head_ref)
803 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
804 2c7829a4 2019-06-17 stsp return error;
805 93658fb9 2020-03-18 stsp }
806 93658fb9 2020-03-18 stsp
807 93658fb9 2020-03-18 stsp __dead static void
808 93658fb9 2020-03-18 stsp usage_clone(void)
809 93658fb9 2020-03-18 stsp {
810 93658fb9 2020-03-18 stsp fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
811 93658fb9 2020-03-18 stsp exit(1);
812 0266afb7 2019-01-04 stsp }
813 0266afb7 2019-01-04 stsp
814 4ed7e80c 2018-05-20 stsp __dead static void
815 c09a553d 2018-03-12 stsp usage_checkout(void)
816 c09a553d 2018-03-12 stsp {
817 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
819 c09a553d 2018-03-12 stsp exit(1);
820 92a684f4 2018-03-12 stsp }
821 92a684f4 2018-03-12 stsp
822 7f47418f 2019-12-20 stsp static void
823 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
824 7f47418f 2019-12-20 stsp {
825 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
826 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
827 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
828 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
829 7f47418f 2019-12-20 stsp getprogname());
830 7f47418f 2019-12-20 stsp }
831 7f47418f 2019-12-20 stsp
832 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
833 7f47418f 2019-12-20 stsp const char *worktree_path;
834 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
835 7f47418f 2019-12-20 stsp };
836 7f47418f 2019-12-20 stsp
837 1ee397ad 2019-07-12 stsp static const struct got_error *
838 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
839 92a684f4 2018-03-12 stsp {
840 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
841 a484d721 2019-06-10 stsp
842 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
843 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
844 7f47418f 2019-12-20 stsp return NULL;
845 7f47418f 2019-12-20 stsp
846 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
847 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
848 1ee397ad 2019-07-12 stsp return NULL;
849 7f47418f 2019-12-20 stsp }
850 92a684f4 2018-03-12 stsp
851 92a684f4 2018-03-12 stsp while (path[0] == '/')
852 92a684f4 2018-03-12 stsp path++;
853 92a684f4 2018-03-12 stsp
854 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
855 1ee397ad 2019-07-12 stsp return NULL;
856 99437157 2018-11-11 stsp }
857 99437157 2018-11-11 stsp
858 99437157 2018-11-11 stsp static const struct got_error *
859 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
860 99437157 2018-11-11 stsp {
861 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
862 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
863 99437157 2018-11-11 stsp return NULL;
864 8069f636 2019-01-12 stsp }
865 8069f636 2019-01-12 stsp
866 8069f636 2019-01-12 stsp static const struct got_error *
867 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
868 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 3aef623b 2019-10-15 stsp struct got_repository *repo)
870 8069f636 2019-01-12 stsp {
871 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
872 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
873 8069f636 2019-01-12 stsp
874 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
876 36a38700 2019-05-10 stsp if (err)
877 36a38700 2019-05-10 stsp return err;
878 8069f636 2019-01-12 stsp
879 d5bea539 2019-05-13 stsp if (yca_id == NULL)
880 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
881 8069f636 2019-01-12 stsp
882 d5bea539 2019-05-13 stsp /*
883 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
884 d5bea539 2019-05-13 stsp * and the work tree's base commit.
885 d5bea539 2019-05-13 stsp *
886 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
887 d5bea539 2019-05-13 stsp *
888 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
889 d5bea539 2019-05-13 stsp * \ /
890 d5bea539 2019-05-13 stsp * C E
891 d5bea539 2019-05-13 stsp * \ /
892 d5bea539 2019-05-13 stsp * B (yca)
893 d5bea539 2019-05-13 stsp * |
894 d5bea539 2019-05-13 stsp * A
895 d5bea539 2019-05-13 stsp *
896 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
897 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
898 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
899 d5bea539 2019-05-13 stsp */
900 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
901 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
903 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
905 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
906 8069f636 2019-01-12 stsp
907 d5bea539 2019-05-13 stsp free(yca_id);
908 d5bea539 2019-05-13 stsp return NULL;
909 c09a553d 2018-03-12 stsp }
910 a367ff0f 2019-05-14 stsp
911 a367ff0f 2019-05-14 stsp static const struct got_error *
912 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
913 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
914 a51a74b3 2019-07-27 stsp struct got_repository *repo)
915 a367ff0f 2019-05-14 stsp {
916 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
917 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
918 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
919 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
920 a367ff0f 2019-05-14 stsp
921 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
922 a367ff0f 2019-05-14 stsp if (err)
923 a51a74b3 2019-07-27 stsp goto done;
924 a51a74b3 2019-07-27 stsp
925 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 a51a74b3 2019-07-27 stsp is_same_branch = 1;
927 a51a74b3 2019-07-27 stsp goto done;
928 a51a74b3 2019-07-27 stsp }
929 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 a51a74b3 2019-07-27 stsp is_same_branch = 1;
931 a367ff0f 2019-05-14 stsp goto done;
932 a51a74b3 2019-07-27 stsp }
933 a367ff0f 2019-05-14 stsp
934 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
935 a367ff0f 2019-05-14 stsp if (err)
936 a367ff0f 2019-05-14 stsp goto done;
937 a367ff0f 2019-05-14 stsp
938 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
940 a367ff0f 2019-05-14 stsp if (err)
941 a367ff0f 2019-05-14 stsp goto done;
942 a367ff0f 2019-05-14 stsp
943 a367ff0f 2019-05-14 stsp for (;;) {
944 a367ff0f 2019-05-14 stsp struct got_object_id *id;
945 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
946 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
947 a367ff0f 2019-05-14 stsp if (err) {
948 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
949 a367ff0f 2019-05-14 stsp err = NULL;
950 ee780d5c 2020-01-04 stsp break;
951 a367ff0f 2019-05-14 stsp }
952 c09a553d 2018-03-12 stsp
953 a367ff0f 2019-05-14 stsp if (id) {
954 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
955 a51a74b3 2019-07-27 stsp break;
956 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
957 a367ff0f 2019-05-14 stsp is_same_branch = 1;
958 a367ff0f 2019-05-14 stsp break;
959 a367ff0f 2019-05-14 stsp }
960 a367ff0f 2019-05-14 stsp }
961 a367ff0f 2019-05-14 stsp }
962 a367ff0f 2019-05-14 stsp done:
963 a367ff0f 2019-05-14 stsp if (graph)
964 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
965 a367ff0f 2019-05-14 stsp free(head_commit_id);
966 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
967 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
968 a367ff0f 2019-05-14 stsp return err;
969 93658fb9 2020-03-18 stsp }
970 93658fb9 2020-03-18 stsp
971 93658fb9 2020-03-18 stsp static const struct got_error *
972 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
973 93658fb9 2020-03-18 stsp {
974 93658fb9 2020-03-18 stsp char *uri, *branch_filter, *dirname;
975 93658fb9 2020-03-18 stsp int ch;
976 93658fb9 2020-03-18 stsp
977 93658fb9 2020-03-18 stsp while ((ch = getopt(argc, argv, "b:")) != -1) {
978 93658fb9 2020-03-18 stsp switch (ch) {
979 93658fb9 2020-03-18 stsp case 'b':
980 93658fb9 2020-03-18 stsp branch_filter = optarg;
981 93658fb9 2020-03-18 stsp break;
982 93658fb9 2020-03-18 stsp default:
983 93658fb9 2020-03-18 stsp usage_clone();
984 93658fb9 2020-03-18 stsp break;
985 93658fb9 2020-03-18 stsp }
986 93658fb9 2020-03-18 stsp }
987 93658fb9 2020-03-18 stsp argc -= optind;
988 93658fb9 2020-03-18 stsp argv += optind;
989 93658fb9 2020-03-18 stsp uri = argv[0];
990 93658fb9 2020-03-18 stsp if(argc == 1)
991 93658fb9 2020-03-18 stsp dirname = NULL;
992 93658fb9 2020-03-18 stsp else if(argc == 2)
993 93658fb9 2020-03-18 stsp dirname = argv[1];
994 93658fb9 2020-03-18 stsp else
995 93658fb9 2020-03-18 stsp usage_clone();
996 84f2fa52 2020-03-18 stsp return got_fetch(argv[0], branch_filter, dirname);
997 a367ff0f 2019-05-14 stsp }
998 8069f636 2019-01-12 stsp
999 4ed7e80c 2018-05-20 stsp static const struct got_error *
1000 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1001 4b6c9460 2020-03-05 stsp {
1002 4b6c9460 2020-03-05 stsp static char msg[512];
1003 4b6c9460 2020-03-05 stsp const char *branch_name;
1004 4b6c9460 2020-03-05 stsp
1005 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1006 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1007 4b6c9460 2020-03-05 stsp else
1008 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1009 4b6c9460 2020-03-05 stsp
1010 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1011 4b6c9460 2020-03-05 stsp branch_name += 11;
1012 4b6c9460 2020-03-05 stsp
1013 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1014 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1015 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1016 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1017 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1018 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1019 4b6c9460 2020-03-05 stsp
1020 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1021 4b6c9460 2020-03-05 stsp }
1022 4b6c9460 2020-03-05 stsp
1023 4b6c9460 2020-03-05 stsp static const struct got_error *
1024 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1025 c09a553d 2018-03-12 stsp {
1026 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1027 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1028 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1029 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1030 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1031 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1032 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1033 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1034 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1035 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1036 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1037 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1038 f2ea84fa 2019-07-27 stsp
1039 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1040 c09a553d 2018-03-12 stsp
1041 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1042 0bb8a95e 2018-03-12 stsp switch (ch) {
1043 08573d5b 2019-05-14 stsp case 'b':
1044 08573d5b 2019-05-14 stsp branch_name = optarg;
1045 08573d5b 2019-05-14 stsp break;
1046 8069f636 2019-01-12 stsp case 'c':
1047 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1048 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1049 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1050 bb51a5b4 2020-01-13 stsp break;
1051 bb51a5b4 2020-01-13 stsp case 'E':
1052 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1053 8069f636 2019-01-12 stsp break;
1054 0bb8a95e 2018-03-12 stsp case 'p':
1055 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1056 0bb8a95e 2018-03-12 stsp break;
1057 0bb8a95e 2018-03-12 stsp default:
1058 2deda0b9 2019-03-07 stsp usage_checkout();
1059 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1060 0bb8a95e 2018-03-12 stsp }
1061 0bb8a95e 2018-03-12 stsp }
1062 0bb8a95e 2018-03-12 stsp
1063 0bb8a95e 2018-03-12 stsp argc -= optind;
1064 0bb8a95e 2018-03-12 stsp argv += optind;
1065 0bb8a95e 2018-03-12 stsp
1066 6715a751 2018-03-16 stsp #ifndef PROFILE
1067 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1068 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1069 c09a553d 2018-03-12 stsp err(1, "pledge");
1070 6715a751 2018-03-16 stsp #endif
1071 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1072 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1073 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1074 76089277 2018-04-01 stsp if (repo_path == NULL)
1075 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1076 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1077 76089277 2018-04-01 stsp if (cwd == NULL) {
1078 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1079 76089277 2018-04-01 stsp goto done;
1080 76089277 2018-04-01 stsp }
1081 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1082 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1083 230a42bd 2019-05-11 jcs if (base == NULL) {
1084 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1085 230a42bd 2019-05-11 jcs path_prefix);
1086 230a42bd 2019-05-11 jcs goto done;
1087 230a42bd 2019-05-11 jcs }
1088 230a42bd 2019-05-11 jcs } else {
1089 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1090 230a42bd 2019-05-11 jcs if (base == NULL) {
1091 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1092 230a42bd 2019-05-11 jcs repo_path);
1093 230a42bd 2019-05-11 jcs goto done;
1094 230a42bd 2019-05-11 jcs }
1095 76089277 2018-04-01 stsp }
1096 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1097 c09a553d 2018-03-12 stsp if (dotgit)
1098 c09a553d 2018-03-12 stsp *dotgit = '\0';
1099 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1100 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1101 c09a553d 2018-03-12 stsp free(cwd);
1102 76089277 2018-04-01 stsp goto done;
1103 c09a553d 2018-03-12 stsp }
1104 c09a553d 2018-03-12 stsp free(cwd);
1105 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1106 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1107 76089277 2018-04-01 stsp if (repo_path == NULL) {
1108 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1109 76089277 2018-04-01 stsp goto done;
1110 76089277 2018-04-01 stsp }
1111 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1112 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1113 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1114 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1115 b4b3a7dd 2019-07-22 stsp argv[1]);
1116 b4b3a7dd 2019-07-22 stsp goto done;
1117 b4b3a7dd 2019-07-22 stsp }
1118 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1119 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1120 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1121 b4b3a7dd 2019-07-22 stsp goto done;
1122 b4b3a7dd 2019-07-22 stsp }
1123 76089277 2018-04-01 stsp }
1124 c09a553d 2018-03-12 stsp } else
1125 c09a553d 2018-03-12 stsp usage_checkout();
1126 c09a553d 2018-03-12 stsp
1127 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1128 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1129 13bfb272 2019-05-10 jcs
1130 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1131 c09a553d 2018-03-12 stsp if (error != NULL)
1132 c02c541e 2019-03-29 stsp goto done;
1133 c02c541e 2019-03-29 stsp
1134 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1135 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1136 c530dc23 2019-07-23 stsp if (error) {
1137 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1138 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1139 c530dc23 2019-07-23 stsp goto done;
1140 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1141 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1142 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1143 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1144 c530dc23 2019-07-23 stsp goto done;
1145 c530dc23 2019-07-23 stsp }
1146 c530dc23 2019-07-23 stsp }
1147 c530dc23 2019-07-23 stsp
1148 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1149 c02c541e 2019-03-29 stsp if (error)
1150 c09a553d 2018-03-12 stsp goto done;
1151 8069f636 2019-01-12 stsp
1152 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1153 c09a553d 2018-03-12 stsp if (error != NULL)
1154 c09a553d 2018-03-12 stsp goto done;
1155 c09a553d 2018-03-12 stsp
1156 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1157 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1158 c09a553d 2018-03-12 stsp goto done;
1159 c09a553d 2018-03-12 stsp
1160 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1161 c09a553d 2018-03-12 stsp if (error != NULL)
1162 c09a553d 2018-03-12 stsp goto done;
1163 c09a553d 2018-03-12 stsp
1164 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1165 e5dc7198 2018-12-29 stsp path_prefix);
1166 e5dc7198 2018-12-29 stsp if (error != NULL)
1167 e5dc7198 2018-12-29 stsp goto done;
1168 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1169 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1170 49520a32 2018-12-29 stsp goto done;
1171 49520a32 2018-12-29 stsp }
1172 49520a32 2018-12-29 stsp
1173 8069f636 2019-01-12 stsp if (commit_id_str) {
1174 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1175 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1176 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1177 30837e32 2019-07-25 stsp if (error)
1178 8069f636 2019-01-12 stsp goto done;
1179 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1180 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1181 8069f636 2019-01-12 stsp if (error != NULL) {
1182 8069f636 2019-01-12 stsp free(commit_id);
1183 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1184 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1185 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1186 4b6c9460 2020-03-05 stsp }
1187 8069f636 2019-01-12 stsp goto done;
1188 8069f636 2019-01-12 stsp }
1189 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1190 4b6c9460 2020-03-05 stsp if (error) {
1191 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1192 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1193 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1194 4b6c9460 2020-03-05 stsp }
1195 45d344f6 2019-05-14 stsp goto done;
1196 4b6c9460 2020-03-05 stsp }
1197 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1198 8069f636 2019-01-12 stsp commit_id);
1199 8069f636 2019-01-12 stsp free(commit_id);
1200 8069f636 2019-01-12 stsp if (error)
1201 8069f636 2019-01-12 stsp goto done;
1202 8069f636 2019-01-12 stsp }
1203 8069f636 2019-01-12 stsp
1204 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1205 f2ea84fa 2019-07-27 stsp if (error)
1206 f2ea84fa 2019-07-27 stsp goto done;
1207 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1208 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1209 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1210 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1211 c09a553d 2018-03-12 stsp if (error != NULL)
1212 c09a553d 2018-03-12 stsp goto done;
1213 c09a553d 2018-03-12 stsp
1214 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1215 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1216 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1217 c09a553d 2018-03-12 stsp done:
1218 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1219 8069f636 2019-01-12 stsp free(commit_id_str);
1220 76089277 2018-04-01 stsp free(repo_path);
1221 507dc3bb 2018-12-29 stsp free(worktree_path);
1222 507dc3bb 2018-12-29 stsp return error;
1223 507dc3bb 2018-12-29 stsp }
1224 507dc3bb 2018-12-29 stsp
1225 507dc3bb 2018-12-29 stsp __dead static void
1226 507dc3bb 2018-12-29 stsp usage_update(void)
1227 507dc3bb 2018-12-29 stsp {
1228 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1229 507dc3bb 2018-12-29 stsp getprogname());
1230 507dc3bb 2018-12-29 stsp exit(1);
1231 507dc3bb 2018-12-29 stsp }
1232 507dc3bb 2018-12-29 stsp
1233 1ee397ad 2019-07-12 stsp static const struct got_error *
1234 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1235 507dc3bb 2018-12-29 stsp {
1236 784955db 2019-01-12 stsp int *did_something = arg;
1237 784955db 2019-01-12 stsp
1238 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1239 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1240 1ee397ad 2019-07-12 stsp return NULL;
1241 507dc3bb 2018-12-29 stsp
1242 1545c615 2019-02-10 stsp *did_something = 1;
1243 a484d721 2019-06-10 stsp
1244 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1245 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1246 1ee397ad 2019-07-12 stsp return NULL;
1247 a484d721 2019-06-10 stsp
1248 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1249 507dc3bb 2018-12-29 stsp path++;
1250 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1251 1ee397ad 2019-07-12 stsp return NULL;
1252 be7061eb 2018-12-30 stsp }
1253 be7061eb 2018-12-30 stsp
1254 be7061eb 2018-12-30 stsp static const struct got_error *
1255 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1256 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1257 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1258 a1fb16d8 2019-05-24 stsp {
1259 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1260 a1fb16d8 2019-05-24 stsp char *base_id_str;
1261 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1262 a1fb16d8 2019-05-24 stsp
1263 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1264 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1265 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1266 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1267 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1268 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1269 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1270 a1fb16d8 2019-05-24 stsp }
1271 a1fb16d8 2019-05-24 stsp
1272 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1273 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1274 a1fb16d8 2019-05-24 stsp if (err) {
1275 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1276 a1fb16d8 2019-05-24 stsp return err;
1277 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1278 a1fb16d8 2019-05-24 stsp }
1279 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1280 a1fb16d8 2019-05-24 stsp return NULL;
1281 a1fb16d8 2019-05-24 stsp
1282 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1283 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1284 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1285 a1fb16d8 2019-05-24 stsp if (err)
1286 a1fb16d8 2019-05-24 stsp return err;
1287 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1288 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1289 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1290 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1291 0ebf8283 2019-07-24 stsp return NULL;
1292 0ebf8283 2019-07-24 stsp }
1293 0ebf8283 2019-07-24 stsp
1294 0ebf8283 2019-07-24 stsp static const struct got_error *
1295 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1296 0ebf8283 2019-07-24 stsp {
1297 0ebf8283 2019-07-24 stsp const struct got_error *err;
1298 0ebf8283 2019-07-24 stsp int in_progress;
1299 0ebf8283 2019-07-24 stsp
1300 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1301 0ebf8283 2019-07-24 stsp if (err)
1302 0ebf8283 2019-07-24 stsp return err;
1303 0ebf8283 2019-07-24 stsp if (in_progress)
1304 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1305 0ebf8283 2019-07-24 stsp
1306 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1307 0ebf8283 2019-07-24 stsp if (err)
1308 0ebf8283 2019-07-24 stsp return err;
1309 0ebf8283 2019-07-24 stsp if (in_progress)
1310 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1311 0ebf8283 2019-07-24 stsp
1312 a1fb16d8 2019-05-24 stsp return NULL;
1313 a5edda0a 2019-07-27 stsp }
1314 a5edda0a 2019-07-27 stsp
1315 a5edda0a 2019-07-27 stsp static const struct got_error *
1316 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1317 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1318 a5edda0a 2019-07-27 stsp {
1319 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1320 a5edda0a 2019-07-27 stsp char *path;
1321 a5edda0a 2019-07-27 stsp int i;
1322 a5edda0a 2019-07-27 stsp
1323 a5edda0a 2019-07-27 stsp if (argc == 0) {
1324 a5edda0a 2019-07-27 stsp path = strdup("");
1325 a5edda0a 2019-07-27 stsp if (path == NULL)
1326 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1327 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1328 a5edda0a 2019-07-27 stsp }
1329 a5edda0a 2019-07-27 stsp
1330 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1331 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1332 a5edda0a 2019-07-27 stsp if (err)
1333 a5edda0a 2019-07-27 stsp break;
1334 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1335 a5edda0a 2019-07-27 stsp if (err) {
1336 a5edda0a 2019-07-27 stsp free(path);
1337 a5edda0a 2019-07-27 stsp break;
1338 a5edda0a 2019-07-27 stsp }
1339 a5edda0a 2019-07-27 stsp }
1340 a5edda0a 2019-07-27 stsp
1341 a5edda0a 2019-07-27 stsp return err;
1342 a1fb16d8 2019-05-24 stsp }
1343 a1fb16d8 2019-05-24 stsp
1344 a1fb16d8 2019-05-24 stsp static const struct got_error *
1345 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1346 507dc3bb 2018-12-29 stsp {
1347 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1348 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1349 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1350 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1351 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1352 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1353 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1354 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1355 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1356 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1357 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1358 507dc3bb 2018-12-29 stsp
1359 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1360 f2ea84fa 2019-07-27 stsp
1361 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1362 507dc3bb 2018-12-29 stsp switch (ch) {
1363 024e9686 2019-05-14 stsp case 'b':
1364 024e9686 2019-05-14 stsp branch_name = optarg;
1365 024e9686 2019-05-14 stsp break;
1366 507dc3bb 2018-12-29 stsp case 'c':
1367 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1368 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1369 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1370 507dc3bb 2018-12-29 stsp break;
1371 507dc3bb 2018-12-29 stsp default:
1372 2deda0b9 2019-03-07 stsp usage_update();
1373 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1374 507dc3bb 2018-12-29 stsp }
1375 507dc3bb 2018-12-29 stsp }
1376 507dc3bb 2018-12-29 stsp
1377 507dc3bb 2018-12-29 stsp argc -= optind;
1378 507dc3bb 2018-12-29 stsp argv += optind;
1379 507dc3bb 2018-12-29 stsp
1380 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1381 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1382 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1383 507dc3bb 2018-12-29 stsp err(1, "pledge");
1384 507dc3bb 2018-12-29 stsp #endif
1385 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1386 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1387 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1388 c4cdcb68 2019-04-03 stsp goto done;
1389 c4cdcb68 2019-04-03 stsp }
1390 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1391 7d5807f4 2019-07-11 stsp if (error)
1392 7d5807f4 2019-07-11 stsp goto done;
1393 7d5807f4 2019-07-11 stsp
1394 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1395 f2ea84fa 2019-07-27 stsp if (error)
1396 f2ea84fa 2019-07-27 stsp goto done;
1397 507dc3bb 2018-12-29 stsp
1398 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1399 c9956ddf 2019-09-08 stsp NULL);
1400 507dc3bb 2018-12-29 stsp if (error != NULL)
1401 507dc3bb 2018-12-29 stsp goto done;
1402 507dc3bb 2018-12-29 stsp
1403 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1404 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1405 087fb88c 2019-08-04 stsp if (error)
1406 087fb88c 2019-08-04 stsp goto done;
1407 087fb88c 2019-08-04 stsp
1408 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1409 0266afb7 2019-01-04 stsp if (error)
1410 0266afb7 2019-01-04 stsp goto done;
1411 0266afb7 2019-01-04 stsp
1412 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1413 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1414 024e9686 2019-05-14 stsp if (error != NULL)
1415 024e9686 2019-05-14 stsp goto done;
1416 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1417 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1418 9c4b8182 2019-01-02 stsp if (error != NULL)
1419 9c4b8182 2019-01-02 stsp goto done;
1420 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1421 507dc3bb 2018-12-29 stsp if (error != NULL)
1422 507dc3bb 2018-12-29 stsp goto done;
1423 507dc3bb 2018-12-29 stsp } else {
1424 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1425 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1426 04f57cb3 2019-07-25 stsp free(commit_id_str);
1427 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1428 30837e32 2019-07-25 stsp if (error)
1429 a297e751 2019-07-11 stsp goto done;
1430 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1431 a297e751 2019-07-11 stsp if (error)
1432 507dc3bb 2018-12-29 stsp goto done;
1433 507dc3bb 2018-12-29 stsp }
1434 35c965b2 2018-12-29 stsp
1435 a1fb16d8 2019-05-24 stsp if (branch_name) {
1436 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1437 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1438 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1439 f2ea84fa 2019-07-27 stsp continue;
1440 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1441 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1442 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1443 024e9686 2019-05-14 stsp goto done;
1444 024e9686 2019-05-14 stsp }
1445 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1446 024e9686 2019-05-14 stsp if (error)
1447 024e9686 2019-05-14 stsp goto done;
1448 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1449 3aef623b 2019-10-15 stsp repo);
1450 a367ff0f 2019-05-14 stsp free(head_commit_id);
1451 024e9686 2019-05-14 stsp if (error != NULL)
1452 024e9686 2019-05-14 stsp goto done;
1453 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1454 a367ff0f 2019-05-14 stsp if (error)
1455 a367ff0f 2019-05-14 stsp goto done;
1456 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1457 024e9686 2019-05-14 stsp if (error)
1458 024e9686 2019-05-14 stsp goto done;
1459 024e9686 2019-05-14 stsp } else {
1460 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1461 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1462 a367ff0f 2019-05-14 stsp if (error != NULL) {
1463 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1464 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1465 024e9686 2019-05-14 stsp goto done;
1466 a367ff0f 2019-05-14 stsp }
1467 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1468 a367ff0f 2019-05-14 stsp if (error)
1469 a367ff0f 2019-05-14 stsp goto done;
1470 024e9686 2019-05-14 stsp }
1471 507dc3bb 2018-12-29 stsp
1472 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1473 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1474 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1475 507dc3bb 2018-12-29 stsp commit_id);
1476 507dc3bb 2018-12-29 stsp if (error)
1477 507dc3bb 2018-12-29 stsp goto done;
1478 507dc3bb 2018-12-29 stsp }
1479 507dc3bb 2018-12-29 stsp
1480 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1481 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1482 507dc3bb 2018-12-29 stsp if (error != NULL)
1483 507dc3bb 2018-12-29 stsp goto done;
1484 9c4b8182 2019-01-02 stsp
1485 784955db 2019-01-12 stsp if (did_something)
1486 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1487 784955db 2019-01-12 stsp else
1488 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1489 507dc3bb 2018-12-29 stsp done:
1490 c09a553d 2018-03-12 stsp free(worktree_path);
1491 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1492 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1493 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1494 507dc3bb 2018-12-29 stsp free(commit_id);
1495 9c4b8182 2019-01-02 stsp free(commit_id_str);
1496 c09a553d 2018-03-12 stsp return error;
1497 c09a553d 2018-03-12 stsp }
1498 c09a553d 2018-03-12 stsp
1499 f42b1b34 2018-03-12 stsp static const struct got_error *
1500 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1501 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1502 63035f9f 2019-10-06 stsp struct got_repository *repo)
1503 5c860e29 2018-03-12 stsp {
1504 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1505 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1506 79109fed 2018-03-27 stsp
1507 44392932 2019-08-25 stsp if (blob_id1) {
1508 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1509 44392932 2019-08-25 stsp if (err)
1510 44392932 2019-08-25 stsp goto done;
1511 44392932 2019-08-25 stsp }
1512 79109fed 2018-03-27 stsp
1513 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1514 44392932 2019-08-25 stsp if (err)
1515 44392932 2019-08-25 stsp goto done;
1516 79109fed 2018-03-27 stsp
1517 44392932 2019-08-25 stsp while (path[0] == '/')
1518 44392932 2019-08-25 stsp path++;
1519 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1520 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1521 44392932 2019-08-25 stsp done:
1522 44392932 2019-08-25 stsp if (blob1)
1523 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1524 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1525 44392932 2019-08-25 stsp return err;
1526 44392932 2019-08-25 stsp }
1527 44392932 2019-08-25 stsp
1528 44392932 2019-08-25 stsp static const struct got_error *
1529 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1530 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1531 63035f9f 2019-10-06 stsp struct got_repository *repo)
1532 44392932 2019-08-25 stsp {
1533 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1534 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1535 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1536 44392932 2019-08-25 stsp
1537 44392932 2019-08-25 stsp if (tree_id1) {
1538 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1539 79109fed 2018-03-27 stsp if (err)
1540 44392932 2019-08-25 stsp goto done;
1541 79109fed 2018-03-27 stsp }
1542 79109fed 2018-03-27 stsp
1543 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1544 0f2b3dca 2018-12-22 stsp if (err)
1545 0f2b3dca 2018-12-22 stsp goto done;
1546 0f2b3dca 2018-12-22 stsp
1547 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1548 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1549 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1550 44392932 2019-08-25 stsp while (path[0] == '/')
1551 44392932 2019-08-25 stsp path++;
1552 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1553 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1554 0f2b3dca 2018-12-22 stsp done:
1555 79109fed 2018-03-27 stsp if (tree1)
1556 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1557 366e0a5f 2019-10-10 stsp if (tree2)
1558 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1559 44392932 2019-08-25 stsp return err;
1560 44392932 2019-08-25 stsp }
1561 44392932 2019-08-25 stsp
1562 44392932 2019-08-25 stsp static const struct got_error *
1563 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1564 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1565 44392932 2019-08-25 stsp {
1566 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1567 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1568 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1569 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1570 44392932 2019-08-25 stsp struct got_object_qid *qid;
1571 44392932 2019-08-25 stsp
1572 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1573 44392932 2019-08-25 stsp if (qid != NULL) {
1574 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1575 44392932 2019-08-25 stsp qid->id);
1576 44392932 2019-08-25 stsp if (err)
1577 44392932 2019-08-25 stsp return err;
1578 44392932 2019-08-25 stsp }
1579 44392932 2019-08-25 stsp
1580 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1581 44392932 2019-08-25 stsp int obj_type;
1582 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1583 44392932 2019-08-25 stsp if (err)
1584 44392932 2019-08-25 stsp goto done;
1585 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1586 44392932 2019-08-25 stsp if (err) {
1587 44392932 2019-08-25 stsp free(obj_id2);
1588 44392932 2019-08-25 stsp goto done;
1589 44392932 2019-08-25 stsp }
1590 44392932 2019-08-25 stsp if (pcommit) {
1591 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1592 44392932 2019-08-25 stsp qid->id, path);
1593 44392932 2019-08-25 stsp if (err) {
1594 44392932 2019-08-25 stsp free(obj_id2);
1595 44392932 2019-08-25 stsp goto done;
1596 44392932 2019-08-25 stsp }
1597 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1598 44392932 2019-08-25 stsp if (err) {
1599 44392932 2019-08-25 stsp free(obj_id2);
1600 44392932 2019-08-25 stsp goto done;
1601 44392932 2019-08-25 stsp }
1602 44392932 2019-08-25 stsp }
1603 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1604 44392932 2019-08-25 stsp if (err) {
1605 44392932 2019-08-25 stsp free(obj_id2);
1606 44392932 2019-08-25 stsp goto done;
1607 44392932 2019-08-25 stsp }
1608 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1609 44392932 2019-08-25 stsp switch (obj_type) {
1610 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1611 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1612 63035f9f 2019-10-06 stsp 0, repo);
1613 44392932 2019-08-25 stsp break;
1614 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1615 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1616 63035f9f 2019-10-06 stsp 0, repo);
1617 44392932 2019-08-25 stsp break;
1618 44392932 2019-08-25 stsp default:
1619 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1620 44392932 2019-08-25 stsp break;
1621 44392932 2019-08-25 stsp }
1622 44392932 2019-08-25 stsp free(obj_id1);
1623 44392932 2019-08-25 stsp free(obj_id2);
1624 44392932 2019-08-25 stsp } else {
1625 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1626 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1627 44392932 2019-08-25 stsp if (err)
1628 44392932 2019-08-25 stsp goto done;
1629 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1630 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1631 44392932 2019-08-25 stsp if (err)
1632 44392932 2019-08-25 stsp goto done;
1633 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1634 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1635 44392932 2019-08-25 stsp }
1636 44392932 2019-08-25 stsp done:
1637 0f2b3dca 2018-12-22 stsp free(id_str1);
1638 0f2b3dca 2018-12-22 stsp free(id_str2);
1639 44392932 2019-08-25 stsp if (pcommit)
1640 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1641 79109fed 2018-03-27 stsp return err;
1642 79109fed 2018-03-27 stsp }
1643 79109fed 2018-03-27 stsp
1644 4bb494d5 2018-06-16 stsp static char *
1645 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1646 6c281f94 2018-06-11 stsp {
1647 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1648 09867e48 2019-08-13 stsp char *p, *s;
1649 09867e48 2019-08-13 stsp
1650 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1651 09867e48 2019-08-13 stsp if (tm == NULL)
1652 09867e48 2019-08-13 stsp return NULL;
1653 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1654 09867e48 2019-08-13 stsp if (s == NULL)
1655 09867e48 2019-08-13 stsp return NULL;
1656 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1657 6c281f94 2018-06-11 stsp if (p)
1658 6c281f94 2018-06-11 stsp *p = '\0';
1659 6c281f94 2018-06-11 stsp return s;
1660 6c281f94 2018-06-11 stsp }
1661 dc424a06 2019-08-07 stsp
1662 6841bf13 2019-11-29 kn static const struct got_error *
1663 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1664 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1665 6841bf13 2019-11-29 kn {
1666 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1667 6841bf13 2019-11-29 kn regmatch_t regmatch;
1668 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1669 6841bf13 2019-11-29 kn
1670 6841bf13 2019-11-29 kn *have_match = 0;
1671 6841bf13 2019-11-29 kn
1672 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1673 6841bf13 2019-11-29 kn if (err)
1674 6841bf13 2019-11-29 kn return err;
1675 6841bf13 2019-11-29 kn
1676 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1677 6841bf13 2019-11-29 kn if (err)
1678 6841bf13 2019-11-29 kn goto done;
1679 6841bf13 2019-11-29 kn
1680 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1681 6841bf13 2019-11-29 kn *have_match = 1;
1682 6841bf13 2019-11-29 kn done:
1683 6841bf13 2019-11-29 kn free(id_str);
1684 6841bf13 2019-11-29 kn free(logmsg);
1685 6841bf13 2019-11-29 kn return err;
1686 6841bf13 2019-11-29 kn }
1687 6841bf13 2019-11-29 kn
1688 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1689 6c281f94 2018-06-11 stsp
1690 79109fed 2018-03-27 stsp static const struct got_error *
1691 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1692 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1693 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1694 79109fed 2018-03-27 stsp {
1695 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1696 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1697 6c281f94 2018-06-11 stsp char datebuf[26];
1698 45d799e2 2018-12-23 stsp time_t committer_time;
1699 45d799e2 2018-12-23 stsp const char *author, *committer;
1700 199a4027 2019-02-02 stsp char *refs_str = NULL;
1701 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1702 5c860e29 2018-03-12 stsp
1703 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1704 199a4027 2019-02-02 stsp char *s;
1705 199a4027 2019-02-02 stsp const char *name;
1706 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1707 a436ad14 2019-08-13 stsp int cmp;
1708 a436ad14 2019-08-13 stsp
1709 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1710 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1711 d9498b20 2019-02-05 stsp continue;
1712 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1713 199a4027 2019-02-02 stsp name += 5;
1714 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1715 7143d404 2019-03-12 stsp continue;
1716 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1717 e34f9ed6 2019-02-02 stsp name += 6;
1718 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1719 141c2bff 2019-02-04 stsp name += 8;
1720 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1721 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1722 5d844a1e 2019-08-13 stsp if (err) {
1723 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1724 5d844a1e 2019-08-13 stsp return err;
1725 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1726 5d844a1e 2019-08-13 stsp err = NULL;
1727 5d844a1e 2019-08-13 stsp tag = NULL;
1728 5d844a1e 2019-08-13 stsp }
1729 a436ad14 2019-08-13 stsp }
1730 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1731 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1732 a436ad14 2019-08-13 stsp if (tag)
1733 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1734 a436ad14 2019-08-13 stsp if (cmp != 0)
1735 a436ad14 2019-08-13 stsp continue;
1736 199a4027 2019-02-02 stsp s = refs_str;
1737 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1738 199a4027 2019-02-02 stsp name) == -1) {
1739 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1740 199a4027 2019-02-02 stsp free(s);
1741 34ca4898 2019-08-13 stsp return err;
1742 199a4027 2019-02-02 stsp }
1743 199a4027 2019-02-02 stsp free(s);
1744 199a4027 2019-02-02 stsp }
1745 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1746 8bf5b3c9 2018-03-17 stsp if (err)
1747 8bf5b3c9 2018-03-17 stsp return err;
1748 788c352e 2018-06-16 stsp
1749 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1750 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1751 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1752 832c249c 2018-06-10 stsp free(id_str);
1753 d3d493d7 2019-02-21 stsp id_str = NULL;
1754 d3d493d7 2019-02-21 stsp free(refs_str);
1755 d3d493d7 2019-02-21 stsp refs_str = NULL;
1756 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1757 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1758 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1759 09867e48 2019-08-13 stsp if (datestr)
1760 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1761 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1762 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1763 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1764 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1765 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1766 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1767 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1768 3fe1abad 2018-06-10 stsp int n = 1;
1769 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1770 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1771 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1772 a0603db2 2018-06-10 stsp if (err)
1773 a0603db2 2018-06-10 stsp return err;
1774 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1775 a0603db2 2018-06-10 stsp free(id_str);
1776 a0603db2 2018-06-10 stsp }
1777 a0603db2 2018-06-10 stsp }
1778 832c249c 2018-06-10 stsp
1779 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1780 5943eee2 2019-08-13 stsp if (err)
1781 5943eee2 2019-08-13 stsp return err;
1782 8bf5b3c9 2018-03-17 stsp
1783 621015ac 2018-07-23 stsp logmsg = logmsg0;
1784 832c249c 2018-06-10 stsp do {
1785 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1786 832c249c 2018-06-10 stsp if (line)
1787 832c249c 2018-06-10 stsp printf(" %s\n", line);
1788 832c249c 2018-06-10 stsp } while (line);
1789 621015ac 2018-07-23 stsp free(logmsg0);
1790 832c249c 2018-06-10 stsp
1791 971751ac 2018-03-27 stsp if (show_patch) {
1792 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1793 971751ac 2018-03-27 stsp if (err == 0)
1794 971751ac 2018-03-27 stsp printf("\n");
1795 971751ac 2018-03-27 stsp }
1796 07862c20 2018-09-15 stsp
1797 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1798 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1799 07862c20 2018-09-15 stsp return err;
1800 f42b1b34 2018-03-12 stsp }
1801 5c860e29 2018-03-12 stsp
1802 f42b1b34 2018-03-12 stsp static const struct got_error *
1803 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1804 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1805 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1806 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1807 f42b1b34 2018-03-12 stsp {
1808 f42b1b34 2018-03-12 stsp const struct got_error *err;
1809 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1810 6841bf13 2019-11-29 kn regex_t regex;
1811 6841bf13 2019-11-29 kn int have_match;
1812 372ccdbb 2018-06-10 stsp
1813 6841bf13 2019-11-29 kn if (search_pattern &&
1814 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1815 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1816 6841bf13 2019-11-29 kn
1817 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1818 f42b1b34 2018-03-12 stsp if (err)
1819 f42b1b34 2018-03-12 stsp return err;
1820 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1821 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1822 372ccdbb 2018-06-10 stsp if (err)
1823 fcc85cad 2018-07-22 stsp goto done;
1824 656b1f76 2019-05-11 jcs for (;;) {
1825 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1826 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1827 8bf5b3c9 2018-03-17 stsp
1828 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1829 84453469 2018-11-11 stsp break;
1830 84453469 2018-11-11 stsp
1831 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1832 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1833 372ccdbb 2018-06-10 stsp if (err) {
1834 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1835 9ba79e04 2018-06-11 stsp err = NULL;
1836 ee780d5c 2020-01-04 stsp break;
1837 7e665116 2018-04-02 stsp }
1838 b43fbaa0 2018-06-11 stsp if (id == NULL)
1839 7e665116 2018-04-02 stsp break;
1840 b43fbaa0 2018-06-11 stsp
1841 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1842 b43fbaa0 2018-06-11 stsp if (err)
1843 fcc85cad 2018-07-22 stsp break;
1844 6841bf13 2019-11-29 kn
1845 6841bf13 2019-11-29 kn if (search_pattern) {
1846 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1847 6841bf13 2019-11-29 kn if (err) {
1848 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1849 6841bf13 2019-11-29 kn break;
1850 6841bf13 2019-11-29 kn }
1851 6841bf13 2019-11-29 kn if (have_match == 0) {
1852 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1853 6841bf13 2019-11-29 kn continue;
1854 6841bf13 2019-11-29 kn }
1855 6841bf13 2019-11-29 kn }
1856 6841bf13 2019-11-29 kn
1857 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1858 44392932 2019-08-25 stsp diff_context, refs);
1859 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1860 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1861 7e665116 2018-04-02 stsp break;
1862 31cedeaf 2018-09-15 stsp }
1863 fcc85cad 2018-07-22 stsp done:
1864 6841bf13 2019-11-29 kn if (search_pattern)
1865 6841bf13 2019-11-29 kn regfree(&regex);
1866 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1867 f42b1b34 2018-03-12 stsp return err;
1868 f42b1b34 2018-03-12 stsp }
1869 5c860e29 2018-03-12 stsp
1870 4ed7e80c 2018-05-20 stsp __dead static void
1871 6f3d1eb0 2018-03-12 stsp usage_log(void)
1872 6f3d1eb0 2018-03-12 stsp {
1873 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1874 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1875 6f3d1eb0 2018-03-12 stsp exit(1);
1876 b1ebc001 2019-08-13 stsp }
1877 b1ebc001 2019-08-13 stsp
1878 b1ebc001 2019-08-13 stsp static int
1879 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1880 b1ebc001 2019-08-13 stsp {
1881 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1882 b1ebc001 2019-08-13 stsp long long n;
1883 b1ebc001 2019-08-13 stsp const char *errstr;
1884 b1ebc001 2019-08-13 stsp
1885 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1886 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1887 b1ebc001 2019-08-13 stsp return 0;
1888 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1889 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1890 b1ebc001 2019-08-13 stsp return 0;
1891 b1ebc001 2019-08-13 stsp return n;
1892 6f3d1eb0 2018-03-12 stsp }
1893 6f3d1eb0 2018-03-12 stsp
1894 4ed7e80c 2018-05-20 stsp static const struct got_error *
1895 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1896 f42b1b34 2018-03-12 stsp {
1897 f42b1b34 2018-03-12 stsp const struct got_error *error;
1898 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1899 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1900 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1901 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1902 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1903 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1904 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1905 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
1906 64a96a6d 2018-04-01 stsp const char *errstr;
1907 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1908 1b3893a2 2019-03-18 stsp
1909 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1910 5c860e29 2018-03-12 stsp
1911 6715a751 2018-03-16 stsp #ifndef PROFILE
1912 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1913 6098196c 2019-01-04 stsp NULL)
1914 ad242220 2018-09-08 stsp == -1)
1915 f42b1b34 2018-03-12 stsp err(1, "pledge");
1916 6715a751 2018-03-16 stsp #endif
1917 79109fed 2018-03-27 stsp
1918 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1919 b1ebc001 2019-08-13 stsp
1920 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1921 79109fed 2018-03-27 stsp switch (ch) {
1922 79109fed 2018-03-27 stsp case 'p':
1923 79109fed 2018-03-27 stsp show_patch = 1;
1924 d142fc45 2018-04-01 stsp break;
1925 d142fc45 2018-04-01 stsp case 'c':
1926 d142fc45 2018-04-01 stsp start_commit = optarg;
1927 64a96a6d 2018-04-01 stsp break;
1928 c0cc5c62 2018-10-18 stsp case 'C':
1929 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1930 4a8520aa 2018-10-18 stsp &errstr);
1931 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1932 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1933 c0cc5c62 2018-10-18 stsp break;
1934 64a96a6d 2018-04-01 stsp case 'l':
1935 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1936 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1937 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1938 cc54c501 2019-07-15 stsp break;
1939 48c8c60d 2020-01-27 stsp case 'b':
1940 48c8c60d 2020-01-27 stsp log_branches = 1;
1941 a0603db2 2018-06-10 stsp break;
1942 04ca23f4 2018-07-16 stsp case 'r':
1943 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1944 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1945 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1946 9ba1d308 2019-10-21 stsp optarg);
1947 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1948 04ca23f4 2018-07-16 stsp break;
1949 6841bf13 2019-11-29 kn case 's':
1950 6841bf13 2019-11-29 kn search_pattern = optarg;
1951 6841bf13 2019-11-29 kn break;
1952 79109fed 2018-03-27 stsp default:
1953 2deda0b9 2019-03-07 stsp usage_log();
1954 79109fed 2018-03-27 stsp /* NOTREACHED */
1955 79109fed 2018-03-27 stsp }
1956 79109fed 2018-03-27 stsp }
1957 79109fed 2018-03-27 stsp
1958 79109fed 2018-03-27 stsp argc -= optind;
1959 79109fed 2018-03-27 stsp argv += optind;
1960 f42b1b34 2018-03-12 stsp
1961 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1962 dc1edbfa 2019-11-29 kn diff_context = 3;
1963 dc1edbfa 2019-11-29 kn else if (!show_patch)
1964 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1965 dc1edbfa 2019-11-29 kn
1966 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1967 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1968 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1969 04ca23f4 2018-07-16 stsp goto done;
1970 04ca23f4 2018-07-16 stsp }
1971 cffc0aa4 2019-02-05 stsp
1972 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1973 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1974 cffc0aa4 2019-02-05 stsp goto done;
1975 cffc0aa4 2019-02-05 stsp error = NULL;
1976 cffc0aa4 2019-02-05 stsp
1977 e7301579 2019-03-18 stsp if (argc == 0) {
1978 cbd1af7a 2019-03-18 stsp path = strdup("");
1979 e7301579 2019-03-18 stsp if (path == NULL) {
1980 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1981 cbd1af7a 2019-03-18 stsp goto done;
1982 e7301579 2019-03-18 stsp }
1983 e7301579 2019-03-18 stsp } else if (argc == 1) {
1984 e7301579 2019-03-18 stsp if (worktree) {
1985 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1986 e7301579 2019-03-18 stsp argv[0]);
1987 e7301579 2019-03-18 stsp if (error)
1988 e7301579 2019-03-18 stsp goto done;
1989 e7301579 2019-03-18 stsp } else {
1990 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1991 e7301579 2019-03-18 stsp if (path == NULL) {
1992 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1993 e7301579 2019-03-18 stsp goto done;
1994 e7301579 2019-03-18 stsp }
1995 e7301579 2019-03-18 stsp }
1996 cbd1af7a 2019-03-18 stsp } else
1997 cbd1af7a 2019-03-18 stsp usage_log();
1998 cbd1af7a 2019-03-18 stsp
1999 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2000 5486daa2 2019-05-11 stsp repo_path = worktree ?
2001 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2002 5486daa2 2019-05-11 stsp }
2003 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2004 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2005 cffc0aa4 2019-02-05 stsp goto done;
2006 04ca23f4 2018-07-16 stsp }
2007 6098196c 2019-01-04 stsp
2008 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2009 d7d4f210 2018-03-12 stsp if (error != NULL)
2010 04ca23f4 2018-07-16 stsp goto done;
2011 f42b1b34 2018-03-12 stsp
2012 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2013 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2014 c02c541e 2019-03-29 stsp if (error)
2015 c02c541e 2019-03-29 stsp goto done;
2016 c02c541e 2019-03-29 stsp
2017 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2018 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2019 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2020 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2021 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2022 3235492e 2018-04-01 stsp if (error != NULL)
2023 3235492e 2018-04-01 stsp return error;
2024 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2025 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2026 3235492e 2018-04-01 stsp if (error != NULL)
2027 3235492e 2018-04-01 stsp return error;
2028 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2029 3235492e 2018-04-01 stsp } else {
2030 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2031 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2032 3235492e 2018-04-01 stsp if (error == NULL) {
2033 1caad61b 2019-02-01 stsp int obj_type;
2034 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2035 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2036 d1f2edc9 2018-06-13 stsp if (error != NULL)
2037 1caad61b 2019-02-01 stsp goto done;
2038 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2039 1caad61b 2019-02-01 stsp if (error != NULL)
2040 1caad61b 2019-02-01 stsp goto done;
2041 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2042 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2043 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2044 1caad61b 2019-02-01 stsp if (error != NULL)
2045 1caad61b 2019-02-01 stsp goto done;
2046 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2047 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2048 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2049 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2050 1caad61b 2019-02-01 stsp goto done;
2051 1caad61b 2019-02-01 stsp }
2052 1caad61b 2019-02-01 stsp free(id);
2053 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2054 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2055 1caad61b 2019-02-01 stsp if (id == NULL)
2056 638f9024 2019-05-13 stsp error = got_error_from_errno(
2057 230a42bd 2019-05-11 jcs "got_object_id_dup");
2058 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2059 1caad61b 2019-02-01 stsp if (error)
2060 1caad61b 2019-02-01 stsp goto done;
2061 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2062 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2063 1caad61b 2019-02-01 stsp goto done;
2064 1caad61b 2019-02-01 stsp }
2065 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2066 d1f2edc9 2018-06-13 stsp if (error != NULL)
2067 1caad61b 2019-02-01 stsp goto done;
2068 d1f2edc9 2018-06-13 stsp }
2069 15a94983 2018-12-23 stsp if (commit == NULL) {
2070 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2071 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2072 d1f2edc9 2018-06-13 stsp if (error != NULL)
2073 d1f2edc9 2018-06-13 stsp return error;
2074 3235492e 2018-04-01 stsp }
2075 3235492e 2018-04-01 stsp }
2076 d7d4f210 2018-03-12 stsp if (error != NULL)
2077 04ca23f4 2018-07-16 stsp goto done;
2078 04ca23f4 2018-07-16 stsp
2079 deeabeae 2019-08-27 stsp if (worktree) {
2080 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2081 deeabeae 2019-08-27 stsp char *p;
2082 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2083 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2084 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2085 deeabeae 2019-08-27 stsp goto done;
2086 deeabeae 2019-08-27 stsp }
2087 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2088 deeabeae 2019-08-27 stsp free(p);
2089 deeabeae 2019-08-27 stsp } else
2090 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2091 04ca23f4 2018-07-16 stsp if (error != NULL)
2092 04ca23f4 2018-07-16 stsp goto done;
2093 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2094 04ca23f4 2018-07-16 stsp free(path);
2095 04ca23f4 2018-07-16 stsp path = in_repo_path;
2096 04ca23f4 2018-07-16 stsp }
2097 199a4027 2019-02-02 stsp
2098 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2099 199a4027 2019-02-02 stsp if (error)
2100 199a4027 2019-02-02 stsp goto done;
2101 04ca23f4 2018-07-16 stsp
2102 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2103 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2104 04ca23f4 2018-07-16 stsp done:
2105 04ca23f4 2018-07-16 stsp free(path);
2106 04ca23f4 2018-07-16 stsp free(repo_path);
2107 04ca23f4 2018-07-16 stsp free(cwd);
2108 f42b1b34 2018-03-12 stsp free(id);
2109 cffc0aa4 2019-02-05 stsp if (worktree)
2110 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2111 ad242220 2018-09-08 stsp if (repo) {
2112 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2113 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2114 ad242220 2018-09-08 stsp if (error == NULL)
2115 ad242220 2018-09-08 stsp error = repo_error;
2116 ad242220 2018-09-08 stsp }
2117 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2118 8bf5b3c9 2018-03-17 stsp return error;
2119 5c860e29 2018-03-12 stsp }
2120 5c860e29 2018-03-12 stsp
2121 4ed7e80c 2018-05-20 stsp __dead static void
2122 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2123 3f8b7d6a 2018-04-01 stsp {
2124 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2125 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2126 3f8b7d6a 2018-04-01 stsp exit(1);
2127 b00d56cd 2018-04-01 stsp }
2128 b00d56cd 2018-04-01 stsp
2129 b72f483a 2019-02-05 stsp struct print_diff_arg {
2130 b72f483a 2019-02-05 stsp struct got_repository *repo;
2131 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2132 b72f483a 2019-02-05 stsp int diff_context;
2133 3fc0c068 2019-02-10 stsp const char *id_str;
2134 3fc0c068 2019-02-10 stsp int header_shown;
2135 98eaaa12 2019-08-03 stsp int diff_staged;
2136 63035f9f 2019-10-06 stsp int ignore_whitespace;
2137 b72f483a 2019-02-05 stsp };
2138 b72f483a 2019-02-05 stsp
2139 4ed7e80c 2018-05-20 stsp static const struct got_error *
2140 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2141 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2142 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2143 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2144 b72f483a 2019-02-05 stsp {
2145 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2146 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2147 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2148 12463d8b 2019-12-13 stsp int fd = -1;
2149 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2150 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2151 b72f483a 2019-02-05 stsp struct stat sb;
2152 b72f483a 2019-02-05 stsp
2153 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2154 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2155 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2156 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2157 98eaaa12 2019-08-03 stsp return NULL;
2158 98eaaa12 2019-08-03 stsp } else {
2159 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2160 98eaaa12 2019-08-03 stsp return NULL;
2161 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2162 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2163 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2164 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2165 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2166 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2167 98eaaa12 2019-08-03 stsp return NULL;
2168 98eaaa12 2019-08-03 stsp }
2169 3fc0c068 2019-02-10 stsp
2170 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2171 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2172 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2173 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2174 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2175 3fc0c068 2019-02-10 stsp }
2176 b72f483a 2019-02-05 stsp
2177 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2178 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2179 98eaaa12 2019-08-03 stsp switch (staged_status) {
2180 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2181 98eaaa12 2019-08-03 stsp label1 = path;
2182 98eaaa12 2019-08-03 stsp label2 = path;
2183 98eaaa12 2019-08-03 stsp break;
2184 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2185 98eaaa12 2019-08-03 stsp label2 = path;
2186 98eaaa12 2019-08-03 stsp break;
2187 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2188 98eaaa12 2019-08-03 stsp label1 = path;
2189 98eaaa12 2019-08-03 stsp break;
2190 98eaaa12 2019-08-03 stsp default:
2191 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2192 98eaaa12 2019-08-03 stsp }
2193 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2194 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2195 63035f9f 2019-10-06 stsp a->repo, stdout);
2196 98eaaa12 2019-08-03 stsp }
2197 98eaaa12 2019-08-03 stsp
2198 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2199 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2200 4ce46740 2019-08-08 stsp char *id_str;
2201 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2202 408b4ebc 2019-08-03 stsp 8192);
2203 4ce46740 2019-08-08 stsp if (err)
2204 4ce46740 2019-08-08 stsp goto done;
2205 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2206 4ce46740 2019-08-08 stsp if (err)
2207 4ce46740 2019-08-08 stsp goto done;
2208 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2209 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2210 4ce46740 2019-08-08 stsp free(id_str);
2211 4ce46740 2019-08-08 stsp goto done;
2212 4ce46740 2019-08-08 stsp }
2213 4ce46740 2019-08-08 stsp free(id_str);
2214 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2215 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2216 4ce46740 2019-08-08 stsp if (err)
2217 4ce46740 2019-08-08 stsp goto done;
2218 4ce46740 2019-08-08 stsp }
2219 d00136be 2019-03-26 stsp
2220 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2221 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2222 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2223 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2224 2ec1f75b 2019-03-26 stsp goto done;
2225 2ec1f75b 2019-03-26 stsp }
2226 b72f483a 2019-02-05 stsp
2227 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2228 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2229 12463d8b 2019-12-13 stsp if (fd == -1) {
2230 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2231 12463d8b 2019-12-13 stsp goto done;
2232 12463d8b 2019-12-13 stsp }
2233 12463d8b 2019-12-13 stsp } else {
2234 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2235 12463d8b 2019-12-13 stsp if (fd == -1) {
2236 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2237 12463d8b 2019-12-13 stsp goto done;
2238 12463d8b 2019-12-13 stsp }
2239 12463d8b 2019-12-13 stsp }
2240 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2241 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2242 2ec1f75b 2019-03-26 stsp goto done;
2243 2ec1f75b 2019-03-26 stsp }
2244 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2245 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2246 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2247 2ec1f75b 2019-03-26 stsp goto done;
2248 2ec1f75b 2019-03-26 stsp }
2249 12463d8b 2019-12-13 stsp fd = -1;
2250 2ec1f75b 2019-03-26 stsp } else
2251 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2252 b72f483a 2019-02-05 stsp
2253 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2254 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2255 b72f483a 2019-02-05 stsp done:
2256 b72f483a 2019-02-05 stsp if (blob1)
2257 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2258 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2259 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2260 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2261 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2262 b72f483a 2019-02-05 stsp free(abspath);
2263 927df6b7 2019-02-10 stsp return err;
2264 927df6b7 2019-02-10 stsp }
2265 927df6b7 2019-02-10 stsp
2266 927df6b7 2019-02-10 stsp static const struct got_error *
2267 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2268 b00d56cd 2018-04-01 stsp {
2269 b00d56cd 2018-04-01 stsp const struct got_error *error;
2270 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2271 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2272 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2273 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2274 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2275 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2276 15a94983 2018-12-23 stsp int type1, type2;
2277 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2278 c0cc5c62 2018-10-18 stsp const char *errstr;
2279 927df6b7 2019-02-10 stsp char *path = NULL;
2280 b00d56cd 2018-04-01 stsp
2281 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2282 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2283 25eccc22 2019-01-04 stsp NULL) == -1)
2284 b00d56cd 2018-04-01 stsp err(1, "pledge");
2285 b00d56cd 2018-04-01 stsp #endif
2286 b00d56cd 2018-04-01 stsp
2287 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2288 b00d56cd 2018-04-01 stsp switch (ch) {
2289 c0cc5c62 2018-10-18 stsp case 'C':
2290 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2291 dfcab68b 2019-11-29 kn &errstr);
2292 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2293 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2294 c0cc5c62 2018-10-18 stsp break;
2295 b72f483a 2019-02-05 stsp case 'r':
2296 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2297 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2298 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2299 9ba1d308 2019-10-21 stsp optarg);
2300 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2301 b72f483a 2019-02-05 stsp break;
2302 98eaaa12 2019-08-03 stsp case 's':
2303 98eaaa12 2019-08-03 stsp diff_staged = 1;
2304 63035f9f 2019-10-06 stsp break;
2305 63035f9f 2019-10-06 stsp case 'w':
2306 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2307 98eaaa12 2019-08-03 stsp break;
2308 b00d56cd 2018-04-01 stsp default:
2309 2deda0b9 2019-03-07 stsp usage_diff();
2310 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2311 b00d56cd 2018-04-01 stsp }
2312 b00d56cd 2018-04-01 stsp }
2313 b00d56cd 2018-04-01 stsp
2314 b00d56cd 2018-04-01 stsp argc -= optind;
2315 b00d56cd 2018-04-01 stsp argv += optind;
2316 b00d56cd 2018-04-01 stsp
2317 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2318 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2319 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2320 b72f483a 2019-02-05 stsp goto done;
2321 b72f483a 2019-02-05 stsp }
2322 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2323 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2324 927df6b7 2019-02-10 stsp goto done;
2325 927df6b7 2019-02-10 stsp if (argc <= 1) {
2326 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2327 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2328 927df6b7 2019-02-10 stsp goto done;
2329 927df6b7 2019-02-10 stsp }
2330 b72f483a 2019-02-05 stsp if (repo_path)
2331 b72f483a 2019-02-05 stsp errx(1,
2332 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2333 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2334 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2335 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2336 927df6b7 2019-02-10 stsp goto done;
2337 927df6b7 2019-02-10 stsp }
2338 927df6b7 2019-02-10 stsp if (argc == 1) {
2339 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2340 cbd1af7a 2019-03-18 stsp argv[0]);
2341 927df6b7 2019-02-10 stsp if (error)
2342 927df6b7 2019-02-10 stsp goto done;
2343 927df6b7 2019-02-10 stsp } else {
2344 927df6b7 2019-02-10 stsp path = strdup("");
2345 927df6b7 2019-02-10 stsp if (path == NULL) {
2346 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2347 927df6b7 2019-02-10 stsp goto done;
2348 927df6b7 2019-02-10 stsp }
2349 927df6b7 2019-02-10 stsp }
2350 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2351 98eaaa12 2019-08-03 stsp if (diff_staged)
2352 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2353 98eaaa12 2019-08-03 stsp "objects in repository");
2354 15a94983 2018-12-23 stsp id_str1 = argv[0];
2355 15a94983 2018-12-23 stsp id_str2 = argv[1];
2356 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2357 30db809c 2019-06-05 stsp repo_path =
2358 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2359 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2360 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2361 30db809c 2019-06-05 stsp goto done;
2362 30db809c 2019-06-05 stsp }
2363 30db809c 2019-06-05 stsp }
2364 b00d56cd 2018-04-01 stsp } else
2365 b00d56cd 2018-04-01 stsp usage_diff();
2366 25eccc22 2019-01-04 stsp
2367 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2368 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2369 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2370 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2371 b72f483a 2019-02-05 stsp }
2372 b00d56cd 2018-04-01 stsp
2373 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2374 76089277 2018-04-01 stsp free(repo_path);
2375 b00d56cd 2018-04-01 stsp if (error != NULL)
2376 b00d56cd 2018-04-01 stsp goto done;
2377 b00d56cd 2018-04-01 stsp
2378 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2379 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2380 c02c541e 2019-03-29 stsp if (error)
2381 c02c541e 2019-03-29 stsp goto done;
2382 c02c541e 2019-03-29 stsp
2383 30db809c 2019-06-05 stsp if (argc <= 1) {
2384 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2385 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2386 b72f483a 2019-02-05 stsp char *id_str;
2387 72ea6654 2019-07-27 stsp
2388 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2389 72ea6654 2019-07-27 stsp
2390 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2391 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2392 b72f483a 2019-02-05 stsp if (error)
2393 b72f483a 2019-02-05 stsp goto done;
2394 b72f483a 2019-02-05 stsp arg.repo = repo;
2395 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2396 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2397 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2398 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2399 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2400 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2401 b72f483a 2019-02-05 stsp
2402 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2403 72ea6654 2019-07-27 stsp if (error)
2404 72ea6654 2019-07-27 stsp goto done;
2405 72ea6654 2019-07-27 stsp
2406 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2407 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2408 b72f483a 2019-02-05 stsp free(id_str);
2409 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2410 b72f483a 2019-02-05 stsp goto done;
2411 b72f483a 2019-02-05 stsp }
2412 b72f483a 2019-02-05 stsp
2413 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2414 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2415 0adb7196 2019-08-11 stsp if (error)
2416 0adb7196 2019-08-11 stsp goto done;
2417 b00d56cd 2018-04-01 stsp
2418 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2419 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2420 0adb7196 2019-08-11 stsp if (error)
2421 0adb7196 2019-08-11 stsp goto done;
2422 b00d56cd 2018-04-01 stsp
2423 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2424 15a94983 2018-12-23 stsp if (error)
2425 15a94983 2018-12-23 stsp goto done;
2426 15a94983 2018-12-23 stsp
2427 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2428 15a94983 2018-12-23 stsp if (error)
2429 15a94983 2018-12-23 stsp goto done;
2430 15a94983 2018-12-23 stsp
2431 15a94983 2018-12-23 stsp if (type1 != type2) {
2432 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2433 b00d56cd 2018-04-01 stsp goto done;
2434 b00d56cd 2018-04-01 stsp }
2435 b00d56cd 2018-04-01 stsp
2436 15a94983 2018-12-23 stsp switch (type1) {
2437 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2438 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2439 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2440 b00d56cd 2018-04-01 stsp break;
2441 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2442 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2443 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2444 b00d56cd 2018-04-01 stsp break;
2445 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2446 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2447 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2448 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2449 b00d56cd 2018-04-01 stsp break;
2450 b00d56cd 2018-04-01 stsp default:
2451 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2452 b00d56cd 2018-04-01 stsp }
2453 b00d56cd 2018-04-01 stsp done:
2454 5e70831e 2019-05-28 stsp free(label1);
2455 5e70831e 2019-05-28 stsp free(label2);
2456 15a94983 2018-12-23 stsp free(id1);
2457 15a94983 2018-12-23 stsp free(id2);
2458 927df6b7 2019-02-10 stsp free(path);
2459 b72f483a 2019-02-05 stsp if (worktree)
2460 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2461 ad242220 2018-09-08 stsp if (repo) {
2462 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2463 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2464 ad242220 2018-09-08 stsp if (error == NULL)
2465 ad242220 2018-09-08 stsp error = repo_error;
2466 ad242220 2018-09-08 stsp }
2467 b00d56cd 2018-04-01 stsp return error;
2468 404c43c4 2018-06-21 stsp }
2469 404c43c4 2018-06-21 stsp
2470 404c43c4 2018-06-21 stsp __dead static void
2471 404c43c4 2018-06-21 stsp usage_blame(void)
2472 404c43c4 2018-06-21 stsp {
2473 9270e621 2019-02-05 stsp fprintf(stderr,
2474 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2475 404c43c4 2018-06-21 stsp getprogname());
2476 404c43c4 2018-06-21 stsp exit(1);
2477 b00d56cd 2018-04-01 stsp }
2478 b00d56cd 2018-04-01 stsp
2479 28315671 2019-08-14 stsp struct blame_line {
2480 28315671 2019-08-14 stsp int annotated;
2481 28315671 2019-08-14 stsp char *id_str;
2482 82f6abb8 2019-08-14 stsp char *committer;
2483 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2484 28315671 2019-08-14 stsp };
2485 28315671 2019-08-14 stsp
2486 28315671 2019-08-14 stsp struct blame_cb_args {
2487 28315671 2019-08-14 stsp struct blame_line *lines;
2488 28315671 2019-08-14 stsp int nlines;
2489 7ef28ff8 2019-08-14 stsp int nlines_prec;
2490 28315671 2019-08-14 stsp int lineno_cur;
2491 28315671 2019-08-14 stsp off_t *line_offsets;
2492 28315671 2019-08-14 stsp FILE *f;
2493 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2494 28315671 2019-08-14 stsp };
2495 28315671 2019-08-14 stsp
2496 404c43c4 2018-06-21 stsp static const struct got_error *
2497 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2498 28315671 2019-08-14 stsp {
2499 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2500 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2501 28315671 2019-08-14 stsp struct blame_line *bline;
2502 82f6abb8 2019-08-14 stsp char *line = NULL;
2503 28315671 2019-08-14 stsp size_t linesize = 0;
2504 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2505 28315671 2019-08-14 stsp off_t offset;
2506 bcb49d15 2019-08-14 stsp struct tm tm;
2507 bcb49d15 2019-08-14 stsp time_t committer_time;
2508 28315671 2019-08-14 stsp
2509 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2510 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2511 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2512 28315671 2019-08-14 stsp
2513 28315671 2019-08-14 stsp if (sigint_received)
2514 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2515 28315671 2019-08-14 stsp
2516 2839f8b9 2019-08-18 stsp if (lineno == -1)
2517 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2518 2839f8b9 2019-08-18 stsp
2519 28315671 2019-08-14 stsp /* Annotate this line. */
2520 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2521 28315671 2019-08-14 stsp if (bline->annotated)
2522 28315671 2019-08-14 stsp return NULL;
2523 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2524 28315671 2019-08-14 stsp if (err)
2525 28315671 2019-08-14 stsp return err;
2526 82f6abb8 2019-08-14 stsp
2527 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2528 82f6abb8 2019-08-14 stsp if (err)
2529 82f6abb8 2019-08-14 stsp goto done;
2530 82f6abb8 2019-08-14 stsp
2531 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2532 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2533 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2534 bcb49d15 2019-08-14 stsp goto done;
2535 bcb49d15 2019-08-14 stsp }
2536 bcb49d15 2019-08-14 stsp
2537 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2538 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2539 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2540 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2541 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2542 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2543 82f6abb8 2019-08-14 stsp goto done;
2544 82f6abb8 2019-08-14 stsp }
2545 28315671 2019-08-14 stsp bline->annotated = 1;
2546 28315671 2019-08-14 stsp
2547 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2548 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2549 28315671 2019-08-14 stsp if (!bline->annotated)
2550 82f6abb8 2019-08-14 stsp goto done;
2551 28315671 2019-08-14 stsp
2552 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2553 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2554 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2555 82f6abb8 2019-08-14 stsp goto done;
2556 82f6abb8 2019-08-14 stsp }
2557 28315671 2019-08-14 stsp
2558 28315671 2019-08-14 stsp while (bline->annotated) {
2559 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2560 82f6abb8 2019-08-14 stsp size_t len;
2561 82f6abb8 2019-08-14 stsp
2562 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2563 28315671 2019-08-14 stsp if (ferror(a->f))
2564 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2565 28315671 2019-08-14 stsp break;
2566 28315671 2019-08-14 stsp }
2567 82f6abb8 2019-08-14 stsp
2568 82f6abb8 2019-08-14 stsp committer = bline->committer;
2569 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2570 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2571 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2572 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2573 82f6abb8 2019-08-14 stsp if (at)
2574 82f6abb8 2019-08-14 stsp *at = '\0';
2575 82f6abb8 2019-08-14 stsp len = strlen(committer);
2576 82f6abb8 2019-08-14 stsp if (len >= 9)
2577 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2578 28315671 2019-08-14 stsp
2579 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2580 28315671 2019-08-14 stsp if (nl)
2581 28315671 2019-08-14 stsp *nl = '\0';
2582 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2583 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2584 28315671 2019-08-14 stsp
2585 28315671 2019-08-14 stsp a->lineno_cur++;
2586 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2587 28315671 2019-08-14 stsp }
2588 82f6abb8 2019-08-14 stsp done:
2589 82f6abb8 2019-08-14 stsp if (commit)
2590 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2591 28315671 2019-08-14 stsp free(line);
2592 28315671 2019-08-14 stsp return err;
2593 28315671 2019-08-14 stsp }
2594 28315671 2019-08-14 stsp
2595 28315671 2019-08-14 stsp static const struct got_error *
2596 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2597 404c43c4 2018-06-21 stsp {
2598 404c43c4 2018-06-21 stsp const struct got_error *error;
2599 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2600 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2601 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2602 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2603 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2604 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2605 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2606 28315671 2019-08-14 stsp struct blame_cb_args bca;
2607 28315671 2019-08-14 stsp int ch, obj_type, i;
2608 b02560ec 2019-08-19 stsp size_t filesize;
2609 90f3c347 2019-08-19 stsp
2610 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2611 404c43c4 2018-06-21 stsp
2612 404c43c4 2018-06-21 stsp #ifndef PROFILE
2613 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2614 36e2fb66 2019-01-04 stsp NULL) == -1)
2615 404c43c4 2018-06-21 stsp err(1, "pledge");
2616 404c43c4 2018-06-21 stsp #endif
2617 404c43c4 2018-06-21 stsp
2618 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2619 404c43c4 2018-06-21 stsp switch (ch) {
2620 404c43c4 2018-06-21 stsp case 'c':
2621 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2622 404c43c4 2018-06-21 stsp break;
2623 66bea077 2018-08-02 stsp case 'r':
2624 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2625 66bea077 2018-08-02 stsp if (repo_path == NULL)
2626 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2627 9ba1d308 2019-10-21 stsp optarg);
2628 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2629 66bea077 2018-08-02 stsp break;
2630 404c43c4 2018-06-21 stsp default:
2631 2deda0b9 2019-03-07 stsp usage_blame();
2632 404c43c4 2018-06-21 stsp /* NOTREACHED */
2633 404c43c4 2018-06-21 stsp }
2634 404c43c4 2018-06-21 stsp }
2635 404c43c4 2018-06-21 stsp
2636 404c43c4 2018-06-21 stsp argc -= optind;
2637 404c43c4 2018-06-21 stsp argv += optind;
2638 404c43c4 2018-06-21 stsp
2639 a39318fd 2018-08-02 stsp if (argc == 1)
2640 404c43c4 2018-06-21 stsp path = argv[0];
2641 a39318fd 2018-08-02 stsp else
2642 404c43c4 2018-06-21 stsp usage_blame();
2643 404c43c4 2018-06-21 stsp
2644 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2645 66bea077 2018-08-02 stsp if (cwd == NULL) {
2646 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2647 66bea077 2018-08-02 stsp goto done;
2648 66bea077 2018-08-02 stsp }
2649 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2650 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2651 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2652 66bea077 2018-08-02 stsp goto done;
2653 0c06baac 2019-02-05 stsp else
2654 0c06baac 2019-02-05 stsp error = NULL;
2655 0c06baac 2019-02-05 stsp if (worktree) {
2656 0c06baac 2019-02-05 stsp repo_path =
2657 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2658 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2659 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2660 0c06baac 2019-02-05 stsp if (error)
2661 0c06baac 2019-02-05 stsp goto done;
2662 0c06baac 2019-02-05 stsp } else {
2663 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2664 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2665 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2666 0c06baac 2019-02-05 stsp goto done;
2667 0c06baac 2019-02-05 stsp }
2668 66bea077 2018-08-02 stsp }
2669 66bea077 2018-08-02 stsp }
2670 36e2fb66 2019-01-04 stsp
2671 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2672 c02c541e 2019-03-29 stsp if (error != NULL)
2673 36e2fb66 2019-01-04 stsp goto done;
2674 66bea077 2018-08-02 stsp
2675 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2676 c02c541e 2019-03-29 stsp if (error)
2677 404c43c4 2018-06-21 stsp goto done;
2678 404c43c4 2018-06-21 stsp
2679 0c06baac 2019-02-05 stsp if (worktree) {
2680 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2681 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2682 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2683 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2684 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2685 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2686 6efaaa2d 2019-02-05 stsp path) == -1) {
2687 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2688 0c06baac 2019-02-05 stsp goto done;
2689 0c06baac 2019-02-05 stsp }
2690 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2691 0c06baac 2019-02-05 stsp free(p);
2692 0c06baac 2019-02-05 stsp } else {
2693 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2694 0c06baac 2019-02-05 stsp }
2695 0c06baac 2019-02-05 stsp if (error)
2696 66bea077 2018-08-02 stsp goto done;
2697 66bea077 2018-08-02 stsp
2698 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2699 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2700 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2701 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2702 404c43c4 2018-06-21 stsp if (error != NULL)
2703 66bea077 2018-08-02 stsp goto done;
2704 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2705 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2706 404c43c4 2018-06-21 stsp if (error != NULL)
2707 66bea077 2018-08-02 stsp goto done;
2708 404c43c4 2018-06-21 stsp } else {
2709 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2710 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2711 30837e32 2019-07-25 stsp if (error)
2712 66bea077 2018-08-02 stsp goto done;
2713 404c43c4 2018-06-21 stsp }
2714 404c43c4 2018-06-21 stsp
2715 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2716 28315671 2019-08-14 stsp if (error)
2717 28315671 2019-08-14 stsp goto done;
2718 28315671 2019-08-14 stsp
2719 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2720 28315671 2019-08-14 stsp if (error)
2721 28315671 2019-08-14 stsp goto done;
2722 28315671 2019-08-14 stsp
2723 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2724 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2725 28315671 2019-08-14 stsp goto done;
2726 28315671 2019-08-14 stsp }
2727 28315671 2019-08-14 stsp
2728 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2729 28315671 2019-08-14 stsp if (error)
2730 28315671 2019-08-14 stsp goto done;
2731 28315671 2019-08-14 stsp bca.f = got_opentemp();
2732 28315671 2019-08-14 stsp if (bca.f == NULL) {
2733 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2734 28315671 2019-08-14 stsp goto done;
2735 28315671 2019-08-14 stsp }
2736 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2737 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2738 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2739 28315671 2019-08-14 stsp goto done;
2740 28315671 2019-08-14 stsp
2741 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2742 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2743 b02560ec 2019-08-19 stsp bca.nlines--;
2744 b02560ec 2019-08-19 stsp
2745 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2746 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2747 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2748 28315671 2019-08-14 stsp goto done;
2749 28315671 2019-08-14 stsp }
2750 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2751 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2752 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2753 7ef28ff8 2019-08-14 stsp while (i > 0) {
2754 7ef28ff8 2019-08-14 stsp i /= 10;
2755 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2756 7ef28ff8 2019-08-14 stsp }
2757 82f6abb8 2019-08-14 stsp bca.repo = repo;
2758 7ef28ff8 2019-08-14 stsp
2759 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2760 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2761 404c43c4 2018-06-21 stsp done:
2762 66bea077 2018-08-02 stsp free(in_repo_path);
2763 66bea077 2018-08-02 stsp free(repo_path);
2764 66bea077 2018-08-02 stsp free(cwd);
2765 404c43c4 2018-06-21 stsp free(commit_id);
2766 28315671 2019-08-14 stsp free(obj_id);
2767 28315671 2019-08-14 stsp if (blob)
2768 28315671 2019-08-14 stsp got_object_blob_close(blob);
2769 0c06baac 2019-02-05 stsp if (worktree)
2770 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2771 ad242220 2018-09-08 stsp if (repo) {
2772 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2773 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2774 ad242220 2018-09-08 stsp if (error == NULL)
2775 ad242220 2018-09-08 stsp error = repo_error;
2776 ad242220 2018-09-08 stsp }
2777 3affba96 2019-09-22 stsp if (bca.lines) {
2778 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2779 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2780 3affba96 2019-09-22 stsp free(bline->id_str);
2781 3affba96 2019-09-22 stsp free(bline->committer);
2782 3affba96 2019-09-22 stsp }
2783 3affba96 2019-09-22 stsp free(bca.lines);
2784 28315671 2019-08-14 stsp }
2785 28315671 2019-08-14 stsp free(bca.line_offsets);
2786 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2787 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2788 404c43c4 2018-06-21 stsp return error;
2789 5de5890b 2018-10-18 stsp }
2790 5de5890b 2018-10-18 stsp
2791 5de5890b 2018-10-18 stsp __dead static void
2792 5de5890b 2018-10-18 stsp usage_tree(void)
2793 5de5890b 2018-10-18 stsp {
2794 c1669e2e 2019-01-09 stsp fprintf(stderr,
2795 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2796 5de5890b 2018-10-18 stsp getprogname());
2797 5de5890b 2018-10-18 stsp exit(1);
2798 5de5890b 2018-10-18 stsp }
2799 5de5890b 2018-10-18 stsp
2800 c1669e2e 2019-01-09 stsp static void
2801 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2802 c1669e2e 2019-01-09 stsp const char *root_path)
2803 c1669e2e 2019-01-09 stsp {
2804 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2805 848d6979 2019-08-12 stsp const char *modestr = "";
2806 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2807 5de5890b 2018-10-18 stsp
2808 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2809 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2810 c1669e2e 2019-01-09 stsp path++;
2811 c1669e2e 2019-01-09 stsp
2812 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2813 63c5ca5d 2019-08-24 stsp modestr = "$";
2814 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2815 848d6979 2019-08-12 stsp modestr = "@";
2816 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2817 848d6979 2019-08-12 stsp modestr = "/";
2818 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2819 848d6979 2019-08-12 stsp modestr = "*";
2820 848d6979 2019-08-12 stsp
2821 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2822 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2823 c1669e2e 2019-01-09 stsp }
2824 c1669e2e 2019-01-09 stsp
2825 5de5890b 2018-10-18 stsp static const struct got_error *
2826 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2827 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2828 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2829 5de5890b 2018-10-18 stsp {
2830 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2831 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2832 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2833 56e0773d 2019-11-28 stsp int nentries, i;
2834 5de5890b 2018-10-18 stsp
2835 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2836 5de5890b 2018-10-18 stsp if (err)
2837 5de5890b 2018-10-18 stsp goto done;
2838 5de5890b 2018-10-18 stsp
2839 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2840 5de5890b 2018-10-18 stsp if (err)
2841 5de5890b 2018-10-18 stsp goto done;
2842 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2843 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2844 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2845 5de5890b 2018-10-18 stsp char *id = NULL;
2846 84453469 2018-11-11 stsp
2847 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2848 84453469 2018-11-11 stsp break;
2849 84453469 2018-11-11 stsp
2850 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2851 5de5890b 2018-10-18 stsp if (show_ids) {
2852 5de5890b 2018-10-18 stsp char *id_str;
2853 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2854 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2855 5de5890b 2018-10-18 stsp if (err)
2856 5de5890b 2018-10-18 stsp goto done;
2857 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2858 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2859 5de5890b 2018-10-18 stsp free(id_str);
2860 5de5890b 2018-10-18 stsp goto done;
2861 5de5890b 2018-10-18 stsp }
2862 5de5890b 2018-10-18 stsp free(id_str);
2863 5de5890b 2018-10-18 stsp }
2864 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2865 5de5890b 2018-10-18 stsp free(id);
2866 c1669e2e 2019-01-09 stsp
2867 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2868 c1669e2e 2019-01-09 stsp char *child_path;
2869 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2870 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2871 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2872 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2873 c1669e2e 2019-01-09 stsp goto done;
2874 c1669e2e 2019-01-09 stsp }
2875 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2876 c1669e2e 2019-01-09 stsp root_path, repo);
2877 c1669e2e 2019-01-09 stsp free(child_path);
2878 c1669e2e 2019-01-09 stsp if (err)
2879 c1669e2e 2019-01-09 stsp goto done;
2880 c1669e2e 2019-01-09 stsp }
2881 5de5890b 2018-10-18 stsp }
2882 5de5890b 2018-10-18 stsp done:
2883 5de5890b 2018-10-18 stsp if (tree)
2884 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2885 5de5890b 2018-10-18 stsp free(tree_id);
2886 5de5890b 2018-10-18 stsp return err;
2887 404c43c4 2018-06-21 stsp }
2888 404c43c4 2018-06-21 stsp
2889 5de5890b 2018-10-18 stsp static const struct got_error *
2890 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2891 5de5890b 2018-10-18 stsp {
2892 5de5890b 2018-10-18 stsp const struct got_error *error;
2893 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2894 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2895 7a2c19d6 2019-02-05 stsp const char *path;
2896 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2897 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2898 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2899 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2900 5de5890b 2018-10-18 stsp int ch;
2901 5de5890b 2018-10-18 stsp
2902 5de5890b 2018-10-18 stsp #ifndef PROFILE
2903 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2904 0f8d269b 2019-01-04 stsp NULL) == -1)
2905 5de5890b 2018-10-18 stsp err(1, "pledge");
2906 5de5890b 2018-10-18 stsp #endif
2907 5de5890b 2018-10-18 stsp
2908 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2909 5de5890b 2018-10-18 stsp switch (ch) {
2910 5de5890b 2018-10-18 stsp case 'c':
2911 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2912 5de5890b 2018-10-18 stsp break;
2913 5de5890b 2018-10-18 stsp case 'r':
2914 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2915 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2916 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2917 9ba1d308 2019-10-21 stsp optarg);
2918 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2919 5de5890b 2018-10-18 stsp break;
2920 5de5890b 2018-10-18 stsp case 'i':
2921 5de5890b 2018-10-18 stsp show_ids = 1;
2922 5de5890b 2018-10-18 stsp break;
2923 c1669e2e 2019-01-09 stsp case 'R':
2924 c1669e2e 2019-01-09 stsp recurse = 1;
2925 c1669e2e 2019-01-09 stsp break;
2926 5de5890b 2018-10-18 stsp default:
2927 2deda0b9 2019-03-07 stsp usage_tree();
2928 5de5890b 2018-10-18 stsp /* NOTREACHED */
2929 5de5890b 2018-10-18 stsp }
2930 5de5890b 2018-10-18 stsp }
2931 5de5890b 2018-10-18 stsp
2932 5de5890b 2018-10-18 stsp argc -= optind;
2933 5de5890b 2018-10-18 stsp argv += optind;
2934 5de5890b 2018-10-18 stsp
2935 5de5890b 2018-10-18 stsp if (argc == 1)
2936 5de5890b 2018-10-18 stsp path = argv[0];
2937 5de5890b 2018-10-18 stsp else if (argc > 1)
2938 5de5890b 2018-10-18 stsp usage_tree();
2939 5de5890b 2018-10-18 stsp else
2940 7a2c19d6 2019-02-05 stsp path = NULL;
2941 7a2c19d6 2019-02-05 stsp
2942 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2943 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2944 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2945 9bf7a39b 2019-02-05 stsp goto done;
2946 9bf7a39b 2019-02-05 stsp }
2947 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2948 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2949 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2950 7a2c19d6 2019-02-05 stsp goto done;
2951 7a2c19d6 2019-02-05 stsp else
2952 7a2c19d6 2019-02-05 stsp error = NULL;
2953 7a2c19d6 2019-02-05 stsp if (worktree) {
2954 7a2c19d6 2019-02-05 stsp repo_path =
2955 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2956 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2957 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2958 7a2c19d6 2019-02-05 stsp if (error)
2959 7a2c19d6 2019-02-05 stsp goto done;
2960 7a2c19d6 2019-02-05 stsp } else {
2961 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2962 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2963 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2964 7a2c19d6 2019-02-05 stsp goto done;
2965 7a2c19d6 2019-02-05 stsp }
2966 5de5890b 2018-10-18 stsp }
2967 5de5890b 2018-10-18 stsp }
2968 5de5890b 2018-10-18 stsp
2969 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2970 5de5890b 2018-10-18 stsp if (error != NULL)
2971 c02c541e 2019-03-29 stsp goto done;
2972 c02c541e 2019-03-29 stsp
2973 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2974 c02c541e 2019-03-29 stsp if (error)
2975 5de5890b 2018-10-18 stsp goto done;
2976 5de5890b 2018-10-18 stsp
2977 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2978 9bf7a39b 2019-02-05 stsp if (worktree) {
2979 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2980 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2981 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2982 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2983 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2984 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2985 9bf7a39b 2019-02-05 stsp goto done;
2986 9bf7a39b 2019-02-05 stsp }
2987 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2988 9bf7a39b 2019-02-05 stsp free(p);
2989 9bf7a39b 2019-02-05 stsp if (error)
2990 9bf7a39b 2019-02-05 stsp goto done;
2991 9bf7a39b 2019-02-05 stsp } else
2992 9bf7a39b 2019-02-05 stsp path = "/";
2993 9bf7a39b 2019-02-05 stsp }
2994 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2995 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2996 9bf7a39b 2019-02-05 stsp if (error != NULL)
2997 9bf7a39b 2019-02-05 stsp goto done;
2998 9bf7a39b 2019-02-05 stsp }
2999 5de5890b 2018-10-18 stsp
3000 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3001 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3002 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3003 5de5890b 2018-10-18 stsp if (error != NULL)
3004 5de5890b 2018-10-18 stsp goto done;
3005 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3006 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3007 5de5890b 2018-10-18 stsp if (error != NULL)
3008 5de5890b 2018-10-18 stsp goto done;
3009 5de5890b 2018-10-18 stsp } else {
3010 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3011 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3012 30837e32 2019-07-25 stsp if (error)
3013 5de5890b 2018-10-18 stsp goto done;
3014 5de5890b 2018-10-18 stsp }
3015 5de5890b 2018-10-18 stsp
3016 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3017 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3018 5de5890b 2018-10-18 stsp done:
3019 5de5890b 2018-10-18 stsp free(in_repo_path);
3020 5de5890b 2018-10-18 stsp free(repo_path);
3021 5de5890b 2018-10-18 stsp free(cwd);
3022 5de5890b 2018-10-18 stsp free(commit_id);
3023 7a2c19d6 2019-02-05 stsp if (worktree)
3024 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3025 5de5890b 2018-10-18 stsp if (repo) {
3026 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3027 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3028 5de5890b 2018-10-18 stsp if (error == NULL)
3029 5de5890b 2018-10-18 stsp error = repo_error;
3030 5de5890b 2018-10-18 stsp }
3031 5de5890b 2018-10-18 stsp return error;
3032 5de5890b 2018-10-18 stsp }
3033 5de5890b 2018-10-18 stsp
3034 6bad629b 2019-02-04 stsp __dead static void
3035 6bad629b 2019-02-04 stsp usage_status(void)
3036 6bad629b 2019-02-04 stsp {
3037 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3038 6bad629b 2019-02-04 stsp exit(1);
3039 6bad629b 2019-02-04 stsp }
3040 5c860e29 2018-03-12 stsp
3041 b72f483a 2019-02-05 stsp static const struct got_error *
3042 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3043 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3044 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3045 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3046 6bad629b 2019-02-04 stsp {
3047 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3048 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3049 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3050 b72f483a 2019-02-05 stsp return NULL;
3051 6bad629b 2019-02-04 stsp }
3052 5c860e29 2018-03-12 stsp
3053 6bad629b 2019-02-04 stsp static const struct got_error *
3054 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3055 6bad629b 2019-02-04 stsp {
3056 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3057 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3058 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3059 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3060 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3061 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3062 a5edda0a 2019-07-27 stsp int ch;
3063 5c860e29 2018-03-12 stsp
3064 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3065 72ea6654 2019-07-27 stsp
3066 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3067 6bad629b 2019-02-04 stsp switch (ch) {
3068 5c860e29 2018-03-12 stsp default:
3069 2deda0b9 2019-03-07 stsp usage_status();
3070 6bad629b 2019-02-04 stsp /* NOTREACHED */
3071 5c860e29 2018-03-12 stsp }
3072 5c860e29 2018-03-12 stsp }
3073 5c860e29 2018-03-12 stsp
3074 6bad629b 2019-02-04 stsp argc -= optind;
3075 6bad629b 2019-02-04 stsp argv += optind;
3076 5c860e29 2018-03-12 stsp
3077 6bad629b 2019-02-04 stsp #ifndef PROFILE
3078 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3079 6bad629b 2019-02-04 stsp NULL) == -1)
3080 6bad629b 2019-02-04 stsp err(1, "pledge");
3081 f42b1b34 2018-03-12 stsp #endif
3082 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3083 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3084 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3085 927df6b7 2019-02-10 stsp goto done;
3086 927df6b7 2019-02-10 stsp }
3087 927df6b7 2019-02-10 stsp
3088 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3089 927df6b7 2019-02-10 stsp if (error != NULL)
3090 a5edda0a 2019-07-27 stsp goto done;
3091 6bad629b 2019-02-04 stsp
3092 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3093 c9956ddf 2019-09-08 stsp NULL);
3094 6bad629b 2019-02-04 stsp if (error != NULL)
3095 6bad629b 2019-02-04 stsp goto done;
3096 6bad629b 2019-02-04 stsp
3097 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3098 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3099 087fb88c 2019-08-04 stsp if (error)
3100 087fb88c 2019-08-04 stsp goto done;
3101 087fb88c 2019-08-04 stsp
3102 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3103 6bad629b 2019-02-04 stsp if (error)
3104 6bad629b 2019-02-04 stsp goto done;
3105 6bad629b 2019-02-04 stsp
3106 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3107 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3108 6bad629b 2019-02-04 stsp done:
3109 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3110 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3111 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3112 927df6b7 2019-02-10 stsp free(cwd);
3113 d0eebce4 2019-03-11 stsp return error;
3114 d0eebce4 2019-03-11 stsp }
3115 d0eebce4 2019-03-11 stsp
3116 d0eebce4 2019-03-11 stsp __dead static void
3117 d0eebce4 2019-03-11 stsp usage_ref(void)
3118 d0eebce4 2019-03-11 stsp {
3119 d0eebce4 2019-03-11 stsp fprintf(stderr,
3120 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3121 d0eebce4 2019-03-11 stsp getprogname());
3122 d0eebce4 2019-03-11 stsp exit(1);
3123 d0eebce4 2019-03-11 stsp }
3124 d0eebce4 2019-03-11 stsp
3125 d0eebce4 2019-03-11 stsp static const struct got_error *
3126 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3127 d0eebce4 2019-03-11 stsp {
3128 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3129 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3130 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3131 d0eebce4 2019-03-11 stsp
3132 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3133 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3134 d0eebce4 2019-03-11 stsp if (err)
3135 d0eebce4 2019-03-11 stsp return err;
3136 d0eebce4 2019-03-11 stsp
3137 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3138 d0eebce4 2019-03-11 stsp char *refstr;
3139 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3140 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3141 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3142 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3143 d0eebce4 2019-03-11 stsp free(refstr);
3144 d0eebce4 2019-03-11 stsp }
3145 d0eebce4 2019-03-11 stsp
3146 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3147 d0eebce4 2019-03-11 stsp return NULL;
3148 d0eebce4 2019-03-11 stsp }
3149 d0eebce4 2019-03-11 stsp
3150 d0eebce4 2019-03-11 stsp static const struct got_error *
3151 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3152 d0eebce4 2019-03-11 stsp {
3153 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3154 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3155 d0eebce4 2019-03-11 stsp
3156 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3157 d0eebce4 2019-03-11 stsp if (err)
3158 d0eebce4 2019-03-11 stsp return err;
3159 d0eebce4 2019-03-11 stsp
3160 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3161 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3162 d0eebce4 2019-03-11 stsp return err;
3163 d0eebce4 2019-03-11 stsp }
3164 d0eebce4 2019-03-11 stsp
3165 d0eebce4 2019-03-11 stsp static const struct got_error *
3166 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3167 d0eebce4 2019-03-11 stsp {
3168 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3169 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3170 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3171 d1644381 2019-07-14 stsp
3172 d1644381 2019-07-14 stsp /*
3173 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3174 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3175 d1644381 2019-07-14 stsp * an unintended typo.
3176 d1644381 2019-07-14 stsp */
3177 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3178 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3179 d0eebce4 2019-03-11 stsp
3180 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3181 dd88155e 2019-06-29 stsp repo);
3182 d83d9d5c 2019-05-13 stsp if (err) {
3183 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3184 d0eebce4 2019-03-11 stsp
3185 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3186 d83d9d5c 2019-05-13 stsp return err;
3187 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3188 d83d9d5c 2019-05-13 stsp if (err)
3189 d83d9d5c 2019-05-13 stsp return err;
3190 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3191 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3192 d83d9d5c 2019-05-13 stsp if (err)
3193 d83d9d5c 2019-05-13 stsp return err;
3194 d83d9d5c 2019-05-13 stsp }
3195 d83d9d5c 2019-05-13 stsp
3196 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3197 d0eebce4 2019-03-11 stsp if (err)
3198 d0eebce4 2019-03-11 stsp goto done;
3199 d0eebce4 2019-03-11 stsp
3200 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3201 d0eebce4 2019-03-11 stsp done:
3202 d0eebce4 2019-03-11 stsp if (ref)
3203 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3204 d0eebce4 2019-03-11 stsp free(id);
3205 d1c1ae5f 2019-08-12 stsp return err;
3206 d1c1ae5f 2019-08-12 stsp }
3207 d1c1ae5f 2019-08-12 stsp
3208 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3209 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3210 d1c1ae5f 2019-08-12 stsp {
3211 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3212 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3213 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3214 d1c1ae5f 2019-08-12 stsp
3215 d1c1ae5f 2019-08-12 stsp /*
3216 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3217 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3218 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3219 d1c1ae5f 2019-08-12 stsp */
3220 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3221 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3222 d1c1ae5f 2019-08-12 stsp
3223 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3224 d1c1ae5f 2019-08-12 stsp if (err)
3225 d1c1ae5f 2019-08-12 stsp return err;
3226 d1c1ae5f 2019-08-12 stsp
3227 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3228 d1c1ae5f 2019-08-12 stsp if (err)
3229 d1c1ae5f 2019-08-12 stsp goto done;
3230 d1c1ae5f 2019-08-12 stsp
3231 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3232 d1c1ae5f 2019-08-12 stsp done:
3233 d1c1ae5f 2019-08-12 stsp if (target_ref)
3234 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3235 d1c1ae5f 2019-08-12 stsp if (ref)
3236 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3237 d0eebce4 2019-03-11 stsp return err;
3238 d0eebce4 2019-03-11 stsp }
3239 d0eebce4 2019-03-11 stsp
3240 d0eebce4 2019-03-11 stsp static const struct got_error *
3241 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3242 d0eebce4 2019-03-11 stsp {
3243 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3244 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3245 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3246 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3247 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3248 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3249 d0eebce4 2019-03-11 stsp
3250 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3251 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3252 d0eebce4 2019-03-11 stsp switch (ch) {
3253 d0eebce4 2019-03-11 stsp case 'd':
3254 d0eebce4 2019-03-11 stsp delref = optarg;
3255 d0eebce4 2019-03-11 stsp break;
3256 d0eebce4 2019-03-11 stsp case 'r':
3257 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3258 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3259 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3260 9ba1d308 2019-10-21 stsp optarg);
3261 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3262 d0eebce4 2019-03-11 stsp break;
3263 d0eebce4 2019-03-11 stsp case 'l':
3264 d0eebce4 2019-03-11 stsp do_list = 1;
3265 d1c1ae5f 2019-08-12 stsp break;
3266 d1c1ae5f 2019-08-12 stsp case 's':
3267 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3268 d0eebce4 2019-03-11 stsp break;
3269 d0eebce4 2019-03-11 stsp default:
3270 d0eebce4 2019-03-11 stsp usage_ref();
3271 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3272 d0eebce4 2019-03-11 stsp }
3273 d0eebce4 2019-03-11 stsp }
3274 d0eebce4 2019-03-11 stsp
3275 d0eebce4 2019-03-11 stsp if (do_list && delref)
3276 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3277 d0eebce4 2019-03-11 stsp
3278 d0eebce4 2019-03-11 stsp argc -= optind;
3279 d0eebce4 2019-03-11 stsp argv += optind;
3280 d0eebce4 2019-03-11 stsp
3281 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3282 d1c1ae5f 2019-08-12 stsp if (create_symref)
3283 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3284 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3285 d0eebce4 2019-03-11 stsp if (argc > 0)
3286 d0eebce4 2019-03-11 stsp usage_ref();
3287 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3288 d0eebce4 2019-03-11 stsp usage_ref();
3289 d0eebce4 2019-03-11 stsp
3290 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3291 e0b57350 2019-03-12 stsp if (do_list) {
3292 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3293 e0b57350 2019-03-12 stsp NULL) == -1)
3294 e0b57350 2019-03-12 stsp err(1, "pledge");
3295 e0b57350 2019-03-12 stsp } else {
3296 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3297 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3298 e0b57350 2019-03-12 stsp err(1, "pledge");
3299 e0b57350 2019-03-12 stsp }
3300 d0eebce4 2019-03-11 stsp #endif
3301 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3302 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3303 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3304 d0eebce4 2019-03-11 stsp goto done;
3305 d0eebce4 2019-03-11 stsp }
3306 d0eebce4 2019-03-11 stsp
3307 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3308 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3309 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3310 d0eebce4 2019-03-11 stsp goto done;
3311 d0eebce4 2019-03-11 stsp else
3312 d0eebce4 2019-03-11 stsp error = NULL;
3313 d0eebce4 2019-03-11 stsp if (worktree) {
3314 d0eebce4 2019-03-11 stsp repo_path =
3315 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3316 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3317 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3318 d0eebce4 2019-03-11 stsp if (error)
3319 d0eebce4 2019-03-11 stsp goto done;
3320 d0eebce4 2019-03-11 stsp } else {
3321 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3322 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3323 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3324 d0eebce4 2019-03-11 stsp goto done;
3325 d0eebce4 2019-03-11 stsp }
3326 d0eebce4 2019-03-11 stsp }
3327 d0eebce4 2019-03-11 stsp }
3328 d0eebce4 2019-03-11 stsp
3329 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3330 d0eebce4 2019-03-11 stsp if (error != NULL)
3331 d0eebce4 2019-03-11 stsp goto done;
3332 d0eebce4 2019-03-11 stsp
3333 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3334 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3335 c02c541e 2019-03-29 stsp if (error)
3336 c02c541e 2019-03-29 stsp goto done;
3337 c02c541e 2019-03-29 stsp
3338 d0eebce4 2019-03-11 stsp if (do_list)
3339 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3340 d0eebce4 2019-03-11 stsp else if (delref)
3341 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3342 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3343 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3344 d0eebce4 2019-03-11 stsp else
3345 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3346 4e759de4 2019-06-26 stsp done:
3347 4e759de4 2019-06-26 stsp if (repo)
3348 4e759de4 2019-06-26 stsp got_repo_close(repo);
3349 4e759de4 2019-06-26 stsp if (worktree)
3350 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3351 4e759de4 2019-06-26 stsp free(cwd);
3352 4e759de4 2019-06-26 stsp free(repo_path);
3353 4e759de4 2019-06-26 stsp return error;
3354 4e759de4 2019-06-26 stsp }
3355 4e759de4 2019-06-26 stsp
3356 4e759de4 2019-06-26 stsp __dead static void
3357 4e759de4 2019-06-26 stsp usage_branch(void)
3358 4e759de4 2019-06-26 stsp {
3359 4e759de4 2019-06-26 stsp fprintf(stderr,
3360 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3361 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3362 4e759de4 2019-06-26 stsp exit(1);
3363 4e759de4 2019-06-26 stsp }
3364 4e759de4 2019-06-26 stsp
3365 4e759de4 2019-06-26 stsp static const struct got_error *
3366 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3367 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3368 4e99b47e 2019-10-04 stsp {
3369 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3370 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3371 4e99b47e 2019-10-04 stsp char *refstr;
3372 4e99b47e 2019-10-04 stsp
3373 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3374 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3375 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3376 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3377 4e99b47e 2019-10-04 stsp
3378 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3379 4e99b47e 2019-10-04 stsp if (err)
3380 4e99b47e 2019-10-04 stsp return err;
3381 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3382 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3383 4e99b47e 2019-10-04 stsp marker = "* ";
3384 4e99b47e 2019-10-04 stsp else
3385 4e99b47e 2019-10-04 stsp marker = "~ ";
3386 4e99b47e 2019-10-04 stsp free(id);
3387 4e99b47e 2019-10-04 stsp }
3388 4e99b47e 2019-10-04 stsp
3389 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3390 4e99b47e 2019-10-04 stsp refname += 11;
3391 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3392 4e99b47e 2019-10-04 stsp refname += 18;
3393 4e99b47e 2019-10-04 stsp
3394 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3395 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3396 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3397 4e99b47e 2019-10-04 stsp
3398 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3399 4e99b47e 2019-10-04 stsp free(refstr);
3400 4e99b47e 2019-10-04 stsp return NULL;
3401 4e99b47e 2019-10-04 stsp }
3402 4e99b47e 2019-10-04 stsp
3403 4e99b47e 2019-10-04 stsp static const struct got_error *
3404 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3405 ad89fa31 2019-10-04 stsp {
3406 ad89fa31 2019-10-04 stsp const char *refname;
3407 ad89fa31 2019-10-04 stsp
3408 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3409 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3410 ad89fa31 2019-10-04 stsp
3411 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3412 ad89fa31 2019-10-04 stsp
3413 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3414 ad89fa31 2019-10-04 stsp refname += 11;
3415 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3416 ad89fa31 2019-10-04 stsp refname += 18;
3417 ad89fa31 2019-10-04 stsp
3418 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3419 ad89fa31 2019-10-04 stsp
3420 ad89fa31 2019-10-04 stsp return NULL;
3421 ad89fa31 2019-10-04 stsp }
3422 ad89fa31 2019-10-04 stsp
3423 ad89fa31 2019-10-04 stsp static const struct got_error *
3424 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3425 4e759de4 2019-06-26 stsp {
3426 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3427 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3428 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3429 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3430 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3431 4e759de4 2019-06-26 stsp
3432 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3433 ba882ee3 2019-07-11 stsp
3434 4e99b47e 2019-10-04 stsp if (worktree) {
3435 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3436 4e99b47e 2019-10-04 stsp worktree);
3437 4e99b47e 2019-10-04 stsp if (err)
3438 4e99b47e 2019-10-04 stsp return err;
3439 4e99b47e 2019-10-04 stsp
3440 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3441 4e99b47e 2019-10-04 stsp worktree);
3442 4e99b47e 2019-10-04 stsp if (err)
3443 4e99b47e 2019-10-04 stsp return err;
3444 4e99b47e 2019-10-04 stsp
3445 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3446 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3447 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3448 4e99b47e 2019-10-04 stsp if (err)
3449 4e99b47e 2019-10-04 stsp return err;
3450 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3451 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3452 4e99b47e 2019-10-04 stsp }
3453 4e99b47e 2019-10-04 stsp }
3454 4e99b47e 2019-10-04 stsp
3455 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3456 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3457 4e759de4 2019-06-26 stsp if (err)
3458 4e759de4 2019-06-26 stsp return err;
3459 4e759de4 2019-06-26 stsp
3460 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3461 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3462 4e759de4 2019-06-26 stsp
3463 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3464 4e759de4 2019-06-26 stsp return NULL;
3465 4e759de4 2019-06-26 stsp }
3466 4e759de4 2019-06-26 stsp
3467 4e759de4 2019-06-26 stsp static const struct got_error *
3468 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3469 45cd4e47 2019-08-25 stsp const char *branch_name)
3470 4e759de4 2019-06-26 stsp {
3471 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3472 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3473 4e759de4 2019-06-26 stsp char *refname;
3474 4e759de4 2019-06-26 stsp
3475 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3476 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3477 4e759de4 2019-06-26 stsp
3478 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3479 4e759de4 2019-06-26 stsp if (err)
3480 4e759de4 2019-06-26 stsp goto done;
3481 4e759de4 2019-06-26 stsp
3482 45cd4e47 2019-08-25 stsp if (worktree &&
3483 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3484 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3485 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3486 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3487 45cd4e47 2019-08-25 stsp goto done;
3488 45cd4e47 2019-08-25 stsp }
3489 45cd4e47 2019-08-25 stsp
3490 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3491 4e759de4 2019-06-26 stsp done:
3492 45cd4e47 2019-08-25 stsp if (ref)
3493 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3494 4e759de4 2019-06-26 stsp free(refname);
3495 4e759de4 2019-06-26 stsp return err;
3496 4e759de4 2019-06-26 stsp }
3497 4e759de4 2019-06-26 stsp
3498 4e759de4 2019-06-26 stsp static const struct got_error *
3499 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3500 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3501 4e759de4 2019-06-26 stsp {
3502 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3503 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3504 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3505 d3f84d51 2019-07-11 stsp
3506 d3f84d51 2019-07-11 stsp /*
3507 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3508 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3509 d3f84d51 2019-07-11 stsp * an unintended typo.
3510 d3f84d51 2019-07-11 stsp */
3511 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3512 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3513 4e759de4 2019-06-26 stsp
3514 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3515 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3516 4e759de4 2019-06-26 stsp goto done;
3517 4e759de4 2019-06-26 stsp }
3518 4e759de4 2019-06-26 stsp
3519 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3520 4e759de4 2019-06-26 stsp if (err == NULL) {
3521 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3522 4e759de4 2019-06-26 stsp goto done;
3523 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3524 4e759de4 2019-06-26 stsp goto done;
3525 4e759de4 2019-06-26 stsp
3526 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3527 4e759de4 2019-06-26 stsp if (err)
3528 4e759de4 2019-06-26 stsp goto done;
3529 4e759de4 2019-06-26 stsp
3530 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3531 d0eebce4 2019-03-11 stsp done:
3532 4e759de4 2019-06-26 stsp if (ref)
3533 4e759de4 2019-06-26 stsp got_ref_close(ref);
3534 4e759de4 2019-06-26 stsp free(base_refname);
3535 4e759de4 2019-06-26 stsp free(refname);
3536 4e759de4 2019-06-26 stsp return err;
3537 4e759de4 2019-06-26 stsp }
3538 4e759de4 2019-06-26 stsp
3539 4e759de4 2019-06-26 stsp static const struct got_error *
3540 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3541 4e759de4 2019-06-26 stsp {
3542 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3543 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3544 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3545 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3546 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3547 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3548 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3549 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3550 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3551 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3552 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3553 4e759de4 2019-06-26 stsp
3554 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3555 da76fce2 2020-02-24 stsp
3556 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3557 4e759de4 2019-06-26 stsp switch (ch) {
3558 a74f7e83 2019-11-10 stsp case 'c':
3559 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3560 a74f7e83 2019-11-10 stsp break;
3561 4e759de4 2019-06-26 stsp case 'd':
3562 4e759de4 2019-06-26 stsp delref = optarg;
3563 4e759de4 2019-06-26 stsp break;
3564 4e759de4 2019-06-26 stsp case 'r':
3565 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3566 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3567 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3568 9ba1d308 2019-10-21 stsp optarg);
3569 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3570 4e759de4 2019-06-26 stsp break;
3571 4e759de4 2019-06-26 stsp case 'l':
3572 4e759de4 2019-06-26 stsp do_list = 1;
3573 da76fce2 2020-02-24 stsp break;
3574 da76fce2 2020-02-24 stsp case 'n':
3575 da76fce2 2020-02-24 stsp do_update = 0;
3576 4e759de4 2019-06-26 stsp break;
3577 4e759de4 2019-06-26 stsp default:
3578 4e759de4 2019-06-26 stsp usage_branch();
3579 4e759de4 2019-06-26 stsp /* NOTREACHED */
3580 4e759de4 2019-06-26 stsp }
3581 4e759de4 2019-06-26 stsp }
3582 4e759de4 2019-06-26 stsp
3583 4e759de4 2019-06-26 stsp if (do_list && delref)
3584 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3585 4e759de4 2019-06-26 stsp
3586 4e759de4 2019-06-26 stsp argc -= optind;
3587 4e759de4 2019-06-26 stsp argv += optind;
3588 ad89fa31 2019-10-04 stsp
3589 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3590 ad89fa31 2019-10-04 stsp do_show = 1;
3591 4e759de4 2019-06-26 stsp
3592 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3593 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3594 a74f7e83 2019-11-10 stsp
3595 4e759de4 2019-06-26 stsp if (do_list || delref) {
3596 4e759de4 2019-06-26 stsp if (argc > 0)
3597 4e759de4 2019-06-26 stsp usage_branch();
3598 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3599 4e759de4 2019-06-26 stsp usage_branch();
3600 4e759de4 2019-06-26 stsp
3601 4e759de4 2019-06-26 stsp #ifndef PROFILE
3602 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3603 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3604 4e759de4 2019-06-26 stsp NULL) == -1)
3605 4e759de4 2019-06-26 stsp err(1, "pledge");
3606 4e759de4 2019-06-26 stsp } else {
3607 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3608 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3609 4e759de4 2019-06-26 stsp err(1, "pledge");
3610 4e759de4 2019-06-26 stsp }
3611 4e759de4 2019-06-26 stsp #endif
3612 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3613 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3614 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3615 4e759de4 2019-06-26 stsp goto done;
3616 4e759de4 2019-06-26 stsp }
3617 4e759de4 2019-06-26 stsp
3618 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3619 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3620 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3621 4e759de4 2019-06-26 stsp goto done;
3622 4e759de4 2019-06-26 stsp else
3623 4e759de4 2019-06-26 stsp error = NULL;
3624 4e759de4 2019-06-26 stsp if (worktree) {
3625 4e759de4 2019-06-26 stsp repo_path =
3626 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3627 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3628 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3629 4e759de4 2019-06-26 stsp if (error)
3630 4e759de4 2019-06-26 stsp goto done;
3631 4e759de4 2019-06-26 stsp } else {
3632 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3633 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3634 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3635 4e759de4 2019-06-26 stsp goto done;
3636 4e759de4 2019-06-26 stsp }
3637 4e759de4 2019-06-26 stsp }
3638 4e759de4 2019-06-26 stsp }
3639 4e759de4 2019-06-26 stsp
3640 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3641 4e759de4 2019-06-26 stsp if (error != NULL)
3642 4e759de4 2019-06-26 stsp goto done;
3643 4e759de4 2019-06-26 stsp
3644 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3645 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3646 4e759de4 2019-06-26 stsp if (error)
3647 4e759de4 2019-06-26 stsp goto done;
3648 4e759de4 2019-06-26 stsp
3649 ad89fa31 2019-10-04 stsp if (do_show)
3650 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3651 ad89fa31 2019-10-04 stsp else if (do_list)
3652 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3653 4e759de4 2019-06-26 stsp else if (delref)
3654 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3655 4e759de4 2019-06-26 stsp else {
3656 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3657 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3658 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3659 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3660 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3661 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3662 a74f7e83 2019-11-10 stsp if (error)
3663 a74f7e83 2019-11-10 stsp goto done;
3664 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3665 da76fce2 2020-02-24 stsp if (error)
3666 da76fce2 2020-02-24 stsp goto done;
3667 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3668 da76fce2 2020-02-24 stsp int did_something = 0;
3669 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3670 da76fce2 2020-02-24 stsp
3671 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3672 da76fce2 2020-02-24 stsp if (error)
3673 da76fce2 2020-02-24 stsp goto done;
3674 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3675 da76fce2 2020-02-24 stsp worktree);
3676 da76fce2 2020-02-24 stsp if (error)
3677 da76fce2 2020-02-24 stsp goto done;
3678 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3679 da76fce2 2020-02-24 stsp == -1) {
3680 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3681 da76fce2 2020-02-24 stsp goto done;
3682 da76fce2 2020-02-24 stsp }
3683 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3684 da76fce2 2020-02-24 stsp free(branch_refname);
3685 da76fce2 2020-02-24 stsp if (error)
3686 da76fce2 2020-02-24 stsp goto done;
3687 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3688 da76fce2 2020-02-24 stsp repo);
3689 da76fce2 2020-02-24 stsp if (error)
3690 da76fce2 2020-02-24 stsp goto done;
3691 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3692 da76fce2 2020-02-24 stsp commit_id);
3693 da76fce2 2020-02-24 stsp if (error)
3694 da76fce2 2020-02-24 stsp goto done;
3695 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3696 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3697 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3698 da76fce2 2020-02-24 stsp if (error)
3699 da76fce2 2020-02-24 stsp goto done;
3700 da76fce2 2020-02-24 stsp if (did_something)
3701 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3702 da76fce2 2020-02-24 stsp }
3703 4e759de4 2019-06-26 stsp }
3704 4e759de4 2019-06-26 stsp done:
3705 da76fce2 2020-02-24 stsp if (ref)
3706 da76fce2 2020-02-24 stsp got_ref_close(ref);
3707 d0eebce4 2019-03-11 stsp if (repo)
3708 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3709 d0eebce4 2019-03-11 stsp if (worktree)
3710 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3711 d0eebce4 2019-03-11 stsp free(cwd);
3712 d0eebce4 2019-03-11 stsp free(repo_path);
3713 da76fce2 2020-02-24 stsp free(commit_id);
3714 da76fce2 2020-02-24 stsp free(commit_id_str);
3715 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3716 da76fce2 2020-02-24 stsp free((char *)pe->path);
3717 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3718 d00136be 2019-03-26 stsp return error;
3719 d00136be 2019-03-26 stsp }
3720 d00136be 2019-03-26 stsp
3721 8e7bd50a 2019-08-22 stsp
3722 d00136be 2019-03-26 stsp __dead static void
3723 8e7bd50a 2019-08-22 stsp usage_tag(void)
3724 8e7bd50a 2019-08-22 stsp {
3725 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3726 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3727 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3728 8e7bd50a 2019-08-22 stsp exit(1);
3729 b8bad2ba 2019-08-23 stsp }
3730 b8bad2ba 2019-08-23 stsp
3731 b8bad2ba 2019-08-23 stsp #if 0
3732 b8bad2ba 2019-08-23 stsp static const struct got_error *
3733 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3734 b8bad2ba 2019-08-23 stsp {
3735 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3736 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3737 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3738 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3739 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3740 b8bad2ba 2019-08-23 stsp
3741 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3742 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3743 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3744 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3745 b8bad2ba 2019-08-23 stsp if (err)
3746 b8bad2ba 2019-08-23 stsp return err;
3747 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3748 b8bad2ba 2019-08-23 stsp continue;
3749 b8bad2ba 2019-08-23 stsp } else {
3750 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3751 b8bad2ba 2019-08-23 stsp if (err)
3752 b8bad2ba 2019-08-23 stsp break;
3753 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3754 b8bad2ba 2019-08-23 stsp free(re_id);
3755 b8bad2ba 2019-08-23 stsp if (err)
3756 b8bad2ba 2019-08-23 stsp break;
3757 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3758 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3759 b8bad2ba 2019-08-23 stsp }
3760 b8bad2ba 2019-08-23 stsp
3761 b8bad2ba 2019-08-23 stsp while (se) {
3762 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3763 b8bad2ba 2019-08-23 stsp if (err)
3764 b8bad2ba 2019-08-23 stsp break;
3765 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3766 b8bad2ba 2019-08-23 stsp free(se_id);
3767 b8bad2ba 2019-08-23 stsp if (err)
3768 b8bad2ba 2019-08-23 stsp break;
3769 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3770 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3771 b8bad2ba 2019-08-23 stsp
3772 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3773 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3774 b8bad2ba 2019-08-23 stsp if (err)
3775 b8bad2ba 2019-08-23 stsp return err;
3776 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3777 b8bad2ba 2019-08-23 stsp break;
3778 b8bad2ba 2019-08-23 stsp }
3779 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3780 b8bad2ba 2019-08-23 stsp continue;
3781 b8bad2ba 2019-08-23 stsp }
3782 b8bad2ba 2019-08-23 stsp }
3783 b8bad2ba 2019-08-23 stsp done:
3784 b8bad2ba 2019-08-23 stsp return err;
3785 8e7bd50a 2019-08-22 stsp }
3786 b8bad2ba 2019-08-23 stsp #endif
3787 b8bad2ba 2019-08-23 stsp
3788 b8bad2ba 2019-08-23 stsp static const struct got_error *
3789 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3790 8e7bd50a 2019-08-22 stsp {
3791 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3792 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3793 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3794 8e7bd50a 2019-08-22 stsp
3795 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3796 8e7bd50a 2019-08-22 stsp
3797 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3798 8e7bd50a 2019-08-22 stsp if (err)
3799 8e7bd50a 2019-08-22 stsp return err;
3800 8e7bd50a 2019-08-22 stsp
3801 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3802 8e7bd50a 2019-08-22 stsp const char *refname;
3803 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3804 8e7bd50a 2019-08-22 stsp char datebuf[26];
3805 d4efa91b 2020-01-14 stsp const char *tagger;
3806 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3807 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3808 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3809 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3810 8e7bd50a 2019-08-22 stsp
3811 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3812 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3813 8e7bd50a 2019-08-22 stsp continue;
3814 8e7bd50a 2019-08-22 stsp refname += 10;
3815 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3816 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3817 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3818 8e7bd50a 2019-08-22 stsp break;
3819 8e7bd50a 2019-08-22 stsp }
3820 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3821 8e7bd50a 2019-08-22 stsp free(refstr);
3822 8e7bd50a 2019-08-22 stsp
3823 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3824 8e7bd50a 2019-08-22 stsp if (err)
3825 8e7bd50a 2019-08-22 stsp break;
3826 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3827 d4efa91b 2020-01-14 stsp if (err) {
3828 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3829 d4efa91b 2020-01-14 stsp free(id);
3830 d4efa91b 2020-01-14 stsp break;
3831 d4efa91b 2020-01-14 stsp }
3832 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3833 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3834 d4efa91b 2020-01-14 stsp if (err) {
3835 d4efa91b 2020-01-14 stsp free(id);
3836 d4efa91b 2020-01-14 stsp break;
3837 d4efa91b 2020-01-14 stsp }
3838 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3839 d4efa91b 2020-01-14 stsp tagger_time =
3840 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3841 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3842 d4efa91b 2020-01-14 stsp free(id);
3843 d4efa91b 2020-01-14 stsp if (err)
3844 d4efa91b 2020-01-14 stsp break;
3845 d4efa91b 2020-01-14 stsp } else {
3846 d4efa91b 2020-01-14 stsp free(id);
3847 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3848 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3849 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3850 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3851 d4efa91b 2020-01-14 stsp if (err)
3852 d4efa91b 2020-01-14 stsp break;
3853 d4efa91b 2020-01-14 stsp }
3854 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3855 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3856 2417344c 2019-08-23 stsp if (datestr)
3857 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3858 d4efa91b 2020-01-14 stsp if (commit)
3859 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3860 d4efa91b 2020-01-14 stsp else {
3861 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3862 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
3863 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3864 d4efa91b 2020-01-14 stsp id_str);
3865 d4efa91b 2020-01-14 stsp break;
3866 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
3867 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3868 d4efa91b 2020-01-14 stsp id_str);
3869 d4efa91b 2020-01-14 stsp break;
3870 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
3871 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3872 d4efa91b 2020-01-14 stsp id_str);
3873 d4efa91b 2020-01-14 stsp break;
3874 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
3875 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3876 d4efa91b 2020-01-14 stsp id_str);
3877 d4efa91b 2020-01-14 stsp break;
3878 d4efa91b 2020-01-14 stsp default:
3879 d4efa91b 2020-01-14 stsp break;
3880 d4efa91b 2020-01-14 stsp }
3881 8e7bd50a 2019-08-22 stsp }
3882 8e7bd50a 2019-08-22 stsp free(id_str);
3883 d4efa91b 2020-01-14 stsp if (commit) {
3884 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
3885 d4efa91b 2020-01-14 stsp if (err)
3886 d4efa91b 2020-01-14 stsp break;
3887 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
3888 d4efa91b 2020-01-14 stsp } else {
3889 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3890 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
3891 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
3892 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
3893 d4efa91b 2020-01-14 stsp break;
3894 d4efa91b 2020-01-14 stsp }
3895 8e7bd50a 2019-08-22 stsp }
3896 8e7bd50a 2019-08-22 stsp
3897 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3898 8e7bd50a 2019-08-22 stsp do {
3899 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3900 8e7bd50a 2019-08-22 stsp if (line)
3901 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3902 8e7bd50a 2019-08-22 stsp } while (line);
3903 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3904 8e7bd50a 2019-08-22 stsp }
3905 8e7bd50a 2019-08-22 stsp
3906 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3907 8e7bd50a 2019-08-22 stsp return NULL;
3908 8e7bd50a 2019-08-22 stsp }
3909 8e7bd50a 2019-08-22 stsp
3910 8e7bd50a 2019-08-22 stsp static const struct got_error *
3911 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3912 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3913 8e7bd50a 2019-08-22 stsp {
3914 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3915 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3916 f372d5cd 2019-10-21 stsp char *editor = NULL;
3917 8e7bd50a 2019-08-22 stsp int fd = -1;
3918 8e7bd50a 2019-08-22 stsp
3919 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3920 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3921 8e7bd50a 2019-08-22 stsp goto done;
3922 8e7bd50a 2019-08-22 stsp }
3923 8e7bd50a 2019-08-22 stsp
3924 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3925 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3926 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3927 8e7bd50a 2019-08-22 stsp goto done;
3928 8e7bd50a 2019-08-22 stsp }
3929 8e7bd50a 2019-08-22 stsp
3930 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3931 8e7bd50a 2019-08-22 stsp if (err)
3932 8e7bd50a 2019-08-22 stsp goto done;
3933 8e7bd50a 2019-08-22 stsp
3934 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3935 8e7bd50a 2019-08-22 stsp close(fd);
3936 8e7bd50a 2019-08-22 stsp
3937 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3938 8e7bd50a 2019-08-22 stsp if (err)
3939 8e7bd50a 2019-08-22 stsp goto done;
3940 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3941 8e7bd50a 2019-08-22 stsp done:
3942 8e7bd50a 2019-08-22 stsp free(initial_content);
3943 8e7bd50a 2019-08-22 stsp free(template);
3944 8e7bd50a 2019-08-22 stsp free(editor);
3945 8e7bd50a 2019-08-22 stsp
3946 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3947 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3948 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3949 8e7bd50a 2019-08-22 stsp if (err) {
3950 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3951 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3952 8e7bd50a 2019-08-22 stsp }
3953 8e7bd50a 2019-08-22 stsp }
3954 8e7bd50a 2019-08-22 stsp return err;
3955 8e7bd50a 2019-08-22 stsp }
3956 8e7bd50a 2019-08-22 stsp
3957 8e7bd50a 2019-08-22 stsp static const struct got_error *
3958 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3959 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3960 8e7bd50a 2019-08-22 stsp {
3961 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3962 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3963 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3964 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3965 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3966 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3967 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3968 8e7bd50a 2019-08-22 stsp
3969 8e7bd50a 2019-08-22 stsp /*
3970 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3971 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3972 8e7bd50a 2019-08-22 stsp * an unintended typo.
3973 8e7bd50a 2019-08-22 stsp */
3974 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3975 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3976 8e7bd50a 2019-08-22 stsp
3977 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3978 8e7bd50a 2019-08-22 stsp if (err)
3979 8e7bd50a 2019-08-22 stsp return err;
3980 8e7bd50a 2019-08-22 stsp
3981 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3982 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3983 8e7bd50a 2019-08-22 stsp if (err)
3984 8e7bd50a 2019-08-22 stsp goto done;
3985 8e7bd50a 2019-08-22 stsp
3986 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3987 8e7bd50a 2019-08-22 stsp if (err)
3988 8e7bd50a 2019-08-22 stsp goto done;
3989 8e7bd50a 2019-08-22 stsp
3990 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3991 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3992 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3993 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3994 8e7bd50a 2019-08-22 stsp goto done;
3995 8e7bd50a 2019-08-22 stsp }
3996 8e7bd50a 2019-08-22 stsp tag_name += 10;
3997 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3998 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3999 8e7bd50a 2019-08-22 stsp goto done;
4000 8e7bd50a 2019-08-22 stsp }
4001 8e7bd50a 2019-08-22 stsp
4002 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4003 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4004 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4005 8e7bd50a 2019-08-22 stsp goto done;
4006 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4007 8e7bd50a 2019-08-22 stsp goto done;
4008 8e7bd50a 2019-08-22 stsp
4009 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4010 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4011 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4012 f372d5cd 2019-10-21 stsp if (err) {
4013 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4014 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4015 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4016 8e7bd50a 2019-08-22 stsp goto done;
4017 f372d5cd 2019-10-21 stsp }
4018 8e7bd50a 2019-08-22 stsp }
4019 8e7bd50a 2019-08-22 stsp
4020 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4021 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4022 f372d5cd 2019-10-21 stsp if (err) {
4023 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4024 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4025 8e7bd50a 2019-08-22 stsp goto done;
4026 f372d5cd 2019-10-21 stsp }
4027 8e7bd50a 2019-08-22 stsp
4028 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4029 f372d5cd 2019-10-21 stsp if (err) {
4030 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4031 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4032 8e7bd50a 2019-08-22 stsp goto done;
4033 f372d5cd 2019-10-21 stsp }
4034 8e7bd50a 2019-08-22 stsp
4035 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4036 f372d5cd 2019-10-21 stsp if (err) {
4037 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4038 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4039 f372d5cd 2019-10-21 stsp goto done;
4040 f372d5cd 2019-10-21 stsp }
4041 8e7bd50a 2019-08-22 stsp
4042 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4043 f372d5cd 2019-10-21 stsp if (err) {
4044 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4045 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4046 f372d5cd 2019-10-21 stsp goto done;
4047 8e7bd50a 2019-08-22 stsp }
4048 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4049 8e7bd50a 2019-08-22 stsp done:
4050 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4051 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4052 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4053 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4054 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4055 f372d5cd 2019-10-21 stsp free(tag_id_str);
4056 8e7bd50a 2019-08-22 stsp if (ref)
4057 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4058 8e7bd50a 2019-08-22 stsp free(commit_id);
4059 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4060 8e7bd50a 2019-08-22 stsp free(refname);
4061 8e7bd50a 2019-08-22 stsp free(tagmsg);
4062 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4063 aba9c984 2019-09-08 stsp free(tagger);
4064 8e7bd50a 2019-08-22 stsp return err;
4065 8e7bd50a 2019-08-22 stsp }
4066 8e7bd50a 2019-08-22 stsp
4067 8e7bd50a 2019-08-22 stsp static const struct got_error *
4068 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4069 8e7bd50a 2019-08-22 stsp {
4070 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4071 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4072 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4073 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4074 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4075 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4076 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4077 8e7bd50a 2019-08-22 stsp
4078 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4079 8e7bd50a 2019-08-22 stsp switch (ch) {
4080 80106605 2020-02-24 stsp case 'c':
4081 80106605 2020-02-24 stsp commit_id_arg = optarg;
4082 80106605 2020-02-24 stsp break;
4083 8e7bd50a 2019-08-22 stsp case 'm':
4084 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4085 8e7bd50a 2019-08-22 stsp break;
4086 8e7bd50a 2019-08-22 stsp case 'r':
4087 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4088 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4089 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4090 9ba1d308 2019-10-21 stsp optarg);
4091 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4092 8e7bd50a 2019-08-22 stsp break;
4093 8e7bd50a 2019-08-22 stsp case 'l':
4094 8e7bd50a 2019-08-22 stsp do_list = 1;
4095 8e7bd50a 2019-08-22 stsp break;
4096 8e7bd50a 2019-08-22 stsp default:
4097 8e7bd50a 2019-08-22 stsp usage_tag();
4098 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4099 8e7bd50a 2019-08-22 stsp }
4100 8e7bd50a 2019-08-22 stsp }
4101 8e7bd50a 2019-08-22 stsp
4102 8e7bd50a 2019-08-22 stsp argc -= optind;
4103 8e7bd50a 2019-08-22 stsp argv += optind;
4104 8e7bd50a 2019-08-22 stsp
4105 8e7bd50a 2019-08-22 stsp if (do_list) {
4106 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4107 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4108 8e7bd50a 2019-08-22 stsp if (tagmsg)
4109 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4110 8e7bd50a 2019-08-22 stsp if (argc > 0)
4111 8e7bd50a 2019-08-22 stsp usage_tag();
4112 80106605 2020-02-24 stsp } else if (argc != 1)
4113 8e7bd50a 2019-08-22 stsp usage_tag();
4114 80106605 2020-02-24 stsp
4115 a2887370 2019-08-23 stsp tag_name = argv[0];
4116 8e7bd50a 2019-08-22 stsp
4117 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4118 8e7bd50a 2019-08-22 stsp if (do_list) {
4119 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4120 8e7bd50a 2019-08-22 stsp NULL) == -1)
4121 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4122 8e7bd50a 2019-08-22 stsp } else {
4123 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4124 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4125 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4126 8e7bd50a 2019-08-22 stsp }
4127 8e7bd50a 2019-08-22 stsp #endif
4128 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4129 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4130 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4131 8e7bd50a 2019-08-22 stsp goto done;
4132 8e7bd50a 2019-08-22 stsp }
4133 8e7bd50a 2019-08-22 stsp
4134 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4135 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4136 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4137 8e7bd50a 2019-08-22 stsp goto done;
4138 8e7bd50a 2019-08-22 stsp else
4139 8e7bd50a 2019-08-22 stsp error = NULL;
4140 8e7bd50a 2019-08-22 stsp if (worktree) {
4141 8e7bd50a 2019-08-22 stsp repo_path =
4142 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4143 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4144 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4145 8e7bd50a 2019-08-22 stsp if (error)
4146 8e7bd50a 2019-08-22 stsp goto done;
4147 8e7bd50a 2019-08-22 stsp } else {
4148 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4149 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4150 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4151 8e7bd50a 2019-08-22 stsp goto done;
4152 8e7bd50a 2019-08-22 stsp }
4153 8e7bd50a 2019-08-22 stsp }
4154 8e7bd50a 2019-08-22 stsp }
4155 8e7bd50a 2019-08-22 stsp
4156 8e7bd50a 2019-08-22 stsp if (do_list) {
4157 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4158 c9956ddf 2019-09-08 stsp if (error != NULL)
4159 c9956ddf 2019-09-08 stsp goto done;
4160 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4161 8e7bd50a 2019-08-22 stsp if (error)
4162 8e7bd50a 2019-08-22 stsp goto done;
4163 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4164 8e7bd50a 2019-08-22 stsp } else {
4165 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4166 c9956ddf 2019-09-08 stsp if (error)
4167 c9956ddf 2019-09-08 stsp goto done;
4168 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4169 c9956ddf 2019-09-08 stsp if (error != NULL)
4170 c9956ddf 2019-09-08 stsp goto done;
4171 c9956ddf 2019-09-08 stsp
4172 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4173 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4174 8e7bd50a 2019-08-22 stsp if (error)
4175 8e7bd50a 2019-08-22 stsp goto done;
4176 8e7bd50a 2019-08-22 stsp }
4177 8e7bd50a 2019-08-22 stsp
4178 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4179 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4180 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4181 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4182 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4183 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4184 8e7bd50a 2019-08-22 stsp if (error)
4185 8e7bd50a 2019-08-22 stsp goto done;
4186 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4187 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4188 8e7bd50a 2019-08-22 stsp if (error)
4189 8e7bd50a 2019-08-22 stsp goto done;
4190 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4191 8e7bd50a 2019-08-22 stsp free(commit_id);
4192 8e7bd50a 2019-08-22 stsp if (error)
4193 8e7bd50a 2019-08-22 stsp goto done;
4194 8e7bd50a 2019-08-22 stsp }
4195 8e7bd50a 2019-08-22 stsp
4196 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4197 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4198 8e7bd50a 2019-08-22 stsp }
4199 8e7bd50a 2019-08-22 stsp done:
4200 8e7bd50a 2019-08-22 stsp if (repo)
4201 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4202 8e7bd50a 2019-08-22 stsp if (worktree)
4203 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4204 8e7bd50a 2019-08-22 stsp free(cwd);
4205 8e7bd50a 2019-08-22 stsp free(repo_path);
4206 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4207 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4208 8e7bd50a 2019-08-22 stsp return error;
4209 8e7bd50a 2019-08-22 stsp }
4210 8e7bd50a 2019-08-22 stsp
4211 8e7bd50a 2019-08-22 stsp __dead static void
4212 d00136be 2019-03-26 stsp usage_add(void)
4213 d00136be 2019-03-26 stsp {
4214 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4215 022fae89 2019-12-06 tracey getprogname());
4216 d00136be 2019-03-26 stsp exit(1);
4217 d00136be 2019-03-26 stsp }
4218 d00136be 2019-03-26 stsp
4219 d00136be 2019-03-26 stsp static const struct got_error *
4220 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4221 4e68cba3 2019-11-23 stsp {
4222 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4223 4e68cba3 2019-11-23 stsp path++;
4224 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4225 4e68cba3 2019-11-23 stsp return NULL;
4226 4e68cba3 2019-11-23 stsp }
4227 4e68cba3 2019-11-23 stsp
4228 4e68cba3 2019-11-23 stsp static const struct got_error *
4229 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4230 d00136be 2019-03-26 stsp {
4231 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4232 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4233 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4234 1dd54920 2019-05-11 stsp char *cwd = NULL;
4235 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4236 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4237 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4238 1dd54920 2019-05-11 stsp
4239 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4240 d00136be 2019-03-26 stsp
4241 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4242 d00136be 2019-03-26 stsp switch (ch) {
4243 022fae89 2019-12-06 tracey case 'I':
4244 022fae89 2019-12-06 tracey no_ignores = 1;
4245 022fae89 2019-12-06 tracey break;
4246 4e68cba3 2019-11-23 stsp case 'R':
4247 4e68cba3 2019-11-23 stsp can_recurse = 1;
4248 4e68cba3 2019-11-23 stsp break;
4249 d00136be 2019-03-26 stsp default:
4250 d00136be 2019-03-26 stsp usage_add();
4251 d00136be 2019-03-26 stsp /* NOTREACHED */
4252 d00136be 2019-03-26 stsp }
4253 d00136be 2019-03-26 stsp }
4254 d00136be 2019-03-26 stsp
4255 d00136be 2019-03-26 stsp argc -= optind;
4256 d00136be 2019-03-26 stsp argv += optind;
4257 d00136be 2019-03-26 stsp
4258 43012d58 2019-07-14 stsp #ifndef PROFILE
4259 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4260 43012d58 2019-07-14 stsp NULL) == -1)
4261 43012d58 2019-07-14 stsp err(1, "pledge");
4262 43012d58 2019-07-14 stsp #endif
4263 723c305c 2019-05-11 jcs if (argc < 1)
4264 d00136be 2019-03-26 stsp usage_add();
4265 d00136be 2019-03-26 stsp
4266 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4267 d00136be 2019-03-26 stsp if (cwd == NULL) {
4268 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4269 d00136be 2019-03-26 stsp goto done;
4270 d00136be 2019-03-26 stsp }
4271 723c305c 2019-05-11 jcs
4272 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4273 d00136be 2019-03-26 stsp if (error)
4274 d00136be 2019-03-26 stsp goto done;
4275 d00136be 2019-03-26 stsp
4276 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4277 c9956ddf 2019-09-08 stsp NULL);
4278 031a5338 2019-03-26 stsp if (error != NULL)
4279 031a5338 2019-03-26 stsp goto done;
4280 031a5338 2019-03-26 stsp
4281 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4282 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4283 d00136be 2019-03-26 stsp if (error)
4284 d00136be 2019-03-26 stsp goto done;
4285 d00136be 2019-03-26 stsp
4286 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4287 6d022e97 2019-08-04 stsp if (error)
4288 022fae89 2019-12-06 tracey goto done;
4289 022fae89 2019-12-06 tracey
4290 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4291 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4292 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4293 6d022e97 2019-08-04 stsp goto done;
4294 723c305c 2019-05-11 jcs
4295 022fae89 2019-12-06 tracey }
4296 022fae89 2019-12-06 tracey
4297 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4298 4e68cba3 2019-11-23 stsp char *ondisk_path;
4299 4e68cba3 2019-11-23 stsp struct stat sb;
4300 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4301 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4302 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4303 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4304 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4305 4e68cba3 2019-11-23 stsp goto done;
4306 4e68cba3 2019-11-23 stsp }
4307 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4308 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4309 4e68cba3 2019-11-23 stsp free(ondisk_path);
4310 4e68cba3 2019-11-23 stsp continue;
4311 4e68cba3 2019-11-23 stsp }
4312 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4313 4e68cba3 2019-11-23 stsp ondisk_path);
4314 4e68cba3 2019-11-23 stsp free(ondisk_path);
4315 4e68cba3 2019-11-23 stsp goto done;
4316 4e68cba3 2019-11-23 stsp }
4317 4e68cba3 2019-11-23 stsp free(ondisk_path);
4318 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4319 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4320 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4321 4e68cba3 2019-11-23 stsp goto done;
4322 4e68cba3 2019-11-23 stsp }
4323 4e68cba3 2019-11-23 stsp }
4324 4e68cba3 2019-11-23 stsp }
4325 022fae89 2019-12-06 tracey
4326 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4327 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4328 d00136be 2019-03-26 stsp done:
4329 031a5338 2019-03-26 stsp if (repo)
4330 031a5338 2019-03-26 stsp got_repo_close(repo);
4331 d00136be 2019-03-26 stsp if (worktree)
4332 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4333 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4334 1dd54920 2019-05-11 stsp free((char *)pe->path);
4335 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4336 2ec1f75b 2019-03-26 stsp free(cwd);
4337 2ec1f75b 2019-03-26 stsp return error;
4338 2ec1f75b 2019-03-26 stsp }
4339 2ec1f75b 2019-03-26 stsp
4340 2ec1f75b 2019-03-26 stsp __dead static void
4341 648e4ef7 2019-07-09 stsp usage_remove(void)
4342 2ec1f75b 2019-03-26 stsp {
4343 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4344 f2a9dc41 2019-12-13 tracey getprogname());
4345 2ec1f75b 2019-03-26 stsp exit(1);
4346 2a06fe5f 2019-08-24 stsp }
4347 2a06fe5f 2019-08-24 stsp
4348 2a06fe5f 2019-08-24 stsp static const struct got_error *
4349 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4350 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4351 2a06fe5f 2019-08-24 stsp {
4352 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4353 f2a9dc41 2019-12-13 tracey path++;
4354 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4355 2a06fe5f 2019-08-24 stsp return NULL;
4356 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4357 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4358 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4359 2a06fe5f 2019-08-24 stsp return NULL;
4360 2ec1f75b 2019-03-26 stsp }
4361 2ec1f75b 2019-03-26 stsp
4362 2ec1f75b 2019-03-26 stsp static const struct got_error *
4363 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4364 2ec1f75b 2019-03-26 stsp {
4365 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4366 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4367 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4368 17ed4618 2019-06-02 stsp char *cwd = NULL;
4369 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4370 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4371 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4372 2ec1f75b 2019-03-26 stsp
4373 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4374 17ed4618 2019-06-02 stsp
4375 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4376 2ec1f75b 2019-03-26 stsp switch (ch) {
4377 2ec1f75b 2019-03-26 stsp case 'f':
4378 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4379 2ec1f75b 2019-03-26 stsp break;
4380 70e3e7f5 2019-12-13 tracey case 'k':
4381 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4382 70e3e7f5 2019-12-13 tracey break;
4383 f2a9dc41 2019-12-13 tracey case 'R':
4384 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4385 f2a9dc41 2019-12-13 tracey break;
4386 2ec1f75b 2019-03-26 stsp default:
4387 f2a9dc41 2019-12-13 tracey usage_remove();
4388 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4389 2ec1f75b 2019-03-26 stsp }
4390 2ec1f75b 2019-03-26 stsp }
4391 2ec1f75b 2019-03-26 stsp
4392 2ec1f75b 2019-03-26 stsp argc -= optind;
4393 2ec1f75b 2019-03-26 stsp argv += optind;
4394 2ec1f75b 2019-03-26 stsp
4395 43012d58 2019-07-14 stsp #ifndef PROFILE
4396 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4397 43012d58 2019-07-14 stsp NULL) == -1)
4398 43012d58 2019-07-14 stsp err(1, "pledge");
4399 43012d58 2019-07-14 stsp #endif
4400 17ed4618 2019-06-02 stsp if (argc < 1)
4401 648e4ef7 2019-07-09 stsp usage_remove();
4402 2ec1f75b 2019-03-26 stsp
4403 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4404 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4405 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4406 2ec1f75b 2019-03-26 stsp goto done;
4407 2ec1f75b 2019-03-26 stsp }
4408 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4409 2ec1f75b 2019-03-26 stsp if (error)
4410 2ec1f75b 2019-03-26 stsp goto done;
4411 2ec1f75b 2019-03-26 stsp
4412 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4413 c9956ddf 2019-09-08 stsp NULL);
4414 2af4a041 2019-05-11 jcs if (error)
4415 2ec1f75b 2019-03-26 stsp goto done;
4416 2ec1f75b 2019-03-26 stsp
4417 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4418 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4419 2ec1f75b 2019-03-26 stsp if (error)
4420 2ec1f75b 2019-03-26 stsp goto done;
4421 2ec1f75b 2019-03-26 stsp
4422 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4423 6d022e97 2019-08-04 stsp if (error)
4424 6d022e97 2019-08-04 stsp goto done;
4425 17ed4618 2019-06-02 stsp
4426 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4427 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4428 f2a9dc41 2019-12-13 tracey struct stat sb;
4429 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4430 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4431 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4432 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4433 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4434 f2a9dc41 2019-12-13 tracey goto done;
4435 f2a9dc41 2019-12-13 tracey }
4436 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4437 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4438 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4439 f2a9dc41 2019-12-13 tracey continue;
4440 f2a9dc41 2019-12-13 tracey }
4441 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4442 f2a9dc41 2019-12-13 tracey ondisk_path);
4443 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4444 f2a9dc41 2019-12-13 tracey goto done;
4445 f2a9dc41 2019-12-13 tracey }
4446 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4447 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4448 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4449 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4450 f2a9dc41 2019-12-13 tracey goto done;
4451 f2a9dc41 2019-12-13 tracey }
4452 f2a9dc41 2019-12-13 tracey }
4453 f2a9dc41 2019-12-13 tracey }
4454 f2a9dc41 2019-12-13 tracey
4455 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4456 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4457 a129376b 2019-03-28 stsp done:
4458 a129376b 2019-03-28 stsp if (repo)
4459 a129376b 2019-03-28 stsp got_repo_close(repo);
4460 a129376b 2019-03-28 stsp if (worktree)
4461 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4462 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4463 17ed4618 2019-06-02 stsp free((char *)pe->path);
4464 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4465 a129376b 2019-03-28 stsp free(cwd);
4466 a129376b 2019-03-28 stsp return error;
4467 a129376b 2019-03-28 stsp }
4468 a129376b 2019-03-28 stsp
4469 a129376b 2019-03-28 stsp __dead static void
4470 a129376b 2019-03-28 stsp usage_revert(void)
4471 a129376b 2019-03-28 stsp {
4472 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4473 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4474 a129376b 2019-03-28 stsp exit(1);
4475 a129376b 2019-03-28 stsp }
4476 a129376b 2019-03-28 stsp
4477 1ee397ad 2019-07-12 stsp static const struct got_error *
4478 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4479 a129376b 2019-03-28 stsp {
4480 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4481 fb9704af 2020-01-27 stsp return NULL;
4482 fb9704af 2020-01-27 stsp
4483 a129376b 2019-03-28 stsp while (path[0] == '/')
4484 a129376b 2019-03-28 stsp path++;
4485 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4486 33aa809d 2019-08-08 stsp return NULL;
4487 33aa809d 2019-08-08 stsp }
4488 33aa809d 2019-08-08 stsp
4489 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4490 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4491 33aa809d 2019-08-08 stsp const char *action;
4492 33aa809d 2019-08-08 stsp };
4493 33aa809d 2019-08-08 stsp
4494 33aa809d 2019-08-08 stsp static const struct got_error *
4495 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4496 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4497 33aa809d 2019-08-08 stsp {
4498 33aa809d 2019-08-08 stsp char *line = NULL;
4499 33aa809d 2019-08-08 stsp size_t linesize = 0;
4500 33aa809d 2019-08-08 stsp ssize_t linelen;
4501 33aa809d 2019-08-08 stsp
4502 33aa809d 2019-08-08 stsp switch (status) {
4503 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4504 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4505 33aa809d 2019-08-08 stsp break;
4506 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4507 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4508 33aa809d 2019-08-08 stsp break;
4509 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4510 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4511 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4512 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4513 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4514 33aa809d 2019-08-08 stsp printf("%s", line);
4515 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4516 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4517 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4518 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4519 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4520 33aa809d 2019-08-08 stsp break;
4521 33aa809d 2019-08-08 stsp default:
4522 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4523 33aa809d 2019-08-08 stsp }
4524 33aa809d 2019-08-08 stsp
4525 33aa809d 2019-08-08 stsp return NULL;
4526 33aa809d 2019-08-08 stsp }
4527 33aa809d 2019-08-08 stsp
4528 33aa809d 2019-08-08 stsp static const struct got_error *
4529 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4530 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4531 33aa809d 2019-08-08 stsp {
4532 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4533 33aa809d 2019-08-08 stsp char *line = NULL;
4534 33aa809d 2019-08-08 stsp size_t linesize = 0;
4535 33aa809d 2019-08-08 stsp ssize_t linelen;
4536 33aa809d 2019-08-08 stsp int resp = ' ';
4537 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4538 33aa809d 2019-08-08 stsp
4539 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4540 33aa809d 2019-08-08 stsp
4541 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4542 33aa809d 2019-08-08 stsp char *nl;
4543 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4544 33aa809d 2019-08-08 stsp a->action);
4545 33aa809d 2019-08-08 stsp if (err)
4546 33aa809d 2019-08-08 stsp return err;
4547 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4548 33aa809d 2019-08-08 stsp if (linelen == -1) {
4549 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4550 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4551 33aa809d 2019-08-08 stsp return NULL;
4552 33aa809d 2019-08-08 stsp }
4553 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4554 33aa809d 2019-08-08 stsp if (nl)
4555 33aa809d 2019-08-08 stsp *nl = '\0';
4556 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4557 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4558 33aa809d 2019-08-08 stsp printf("y\n");
4559 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4560 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4561 33aa809d 2019-08-08 stsp printf("n\n");
4562 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4563 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4564 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4565 33aa809d 2019-08-08 stsp printf("q\n");
4566 33aa809d 2019-08-08 stsp } else
4567 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4568 33aa809d 2019-08-08 stsp free(line);
4569 33aa809d 2019-08-08 stsp return NULL;
4570 33aa809d 2019-08-08 stsp }
4571 33aa809d 2019-08-08 stsp
4572 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4573 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4574 33aa809d 2019-08-08 stsp a->action);
4575 33aa809d 2019-08-08 stsp if (err)
4576 33aa809d 2019-08-08 stsp return err;
4577 33aa809d 2019-08-08 stsp resp = getchar();
4578 33aa809d 2019-08-08 stsp if (resp == '\n')
4579 33aa809d 2019-08-08 stsp resp = getchar();
4580 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4581 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4582 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4583 33aa809d 2019-08-08 stsp resp = ' ';
4584 33aa809d 2019-08-08 stsp }
4585 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4586 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4587 33aa809d 2019-08-08 stsp resp = ' ';
4588 33aa809d 2019-08-08 stsp }
4589 33aa809d 2019-08-08 stsp }
4590 33aa809d 2019-08-08 stsp
4591 33aa809d 2019-08-08 stsp if (resp == 'y')
4592 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4593 33aa809d 2019-08-08 stsp else if (resp == 'n')
4594 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4595 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4596 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4597 33aa809d 2019-08-08 stsp
4598 1ee397ad 2019-07-12 stsp return NULL;
4599 a129376b 2019-03-28 stsp }
4600 a129376b 2019-03-28 stsp
4601 33aa809d 2019-08-08 stsp
4602 a129376b 2019-03-28 stsp static const struct got_error *
4603 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4604 a129376b 2019-03-28 stsp {
4605 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4606 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4607 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4608 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4609 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4610 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4611 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4612 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4613 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4614 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4615 a129376b 2019-03-28 stsp
4616 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4617 e20a8b6f 2019-06-04 stsp
4618 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4619 a129376b 2019-03-28 stsp switch (ch) {
4620 33aa809d 2019-08-08 stsp case 'p':
4621 33aa809d 2019-08-08 stsp pflag = 1;
4622 33aa809d 2019-08-08 stsp break;
4623 33aa809d 2019-08-08 stsp case 'F':
4624 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4625 33aa809d 2019-08-08 stsp break;
4626 0f6d7415 2019-08-08 stsp case 'R':
4627 0f6d7415 2019-08-08 stsp can_recurse = 1;
4628 0f6d7415 2019-08-08 stsp break;
4629 a129376b 2019-03-28 stsp default:
4630 a129376b 2019-03-28 stsp usage_revert();
4631 a129376b 2019-03-28 stsp /* NOTREACHED */
4632 a129376b 2019-03-28 stsp }
4633 a129376b 2019-03-28 stsp }
4634 a129376b 2019-03-28 stsp
4635 a129376b 2019-03-28 stsp argc -= optind;
4636 a129376b 2019-03-28 stsp argv += optind;
4637 a129376b 2019-03-28 stsp
4638 43012d58 2019-07-14 stsp #ifndef PROFILE
4639 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4640 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4641 43012d58 2019-07-14 stsp err(1, "pledge");
4642 43012d58 2019-07-14 stsp #endif
4643 e20a8b6f 2019-06-04 stsp if (argc < 1)
4644 a129376b 2019-03-28 stsp usage_revert();
4645 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4646 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4647 a129376b 2019-03-28 stsp
4648 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4649 a129376b 2019-03-28 stsp if (cwd == NULL) {
4650 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4651 a129376b 2019-03-28 stsp goto done;
4652 a129376b 2019-03-28 stsp }
4653 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4654 2ec1f75b 2019-03-26 stsp if (error)
4655 2ec1f75b 2019-03-26 stsp goto done;
4656 a129376b 2019-03-28 stsp
4657 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4658 c9956ddf 2019-09-08 stsp NULL);
4659 a129376b 2019-03-28 stsp if (error != NULL)
4660 a129376b 2019-03-28 stsp goto done;
4661 a129376b 2019-03-28 stsp
4662 33aa809d 2019-08-08 stsp if (patch_script_path) {
4663 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4664 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4665 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4666 33aa809d 2019-08-08 stsp patch_script_path);
4667 33aa809d 2019-08-08 stsp goto done;
4668 33aa809d 2019-08-08 stsp }
4669 33aa809d 2019-08-08 stsp }
4670 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4671 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4672 a129376b 2019-03-28 stsp if (error)
4673 a129376b 2019-03-28 stsp goto done;
4674 a129376b 2019-03-28 stsp
4675 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4676 6d022e97 2019-08-04 stsp if (error)
4677 6d022e97 2019-08-04 stsp goto done;
4678 00db391e 2019-08-03 stsp
4679 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4680 0f6d7415 2019-08-08 stsp char *ondisk_path;
4681 0f6d7415 2019-08-08 stsp struct stat sb;
4682 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4683 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4684 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4685 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4686 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4687 0f6d7415 2019-08-08 stsp goto done;
4688 0f6d7415 2019-08-08 stsp }
4689 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4690 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4691 0f6d7415 2019-08-08 stsp free(ondisk_path);
4692 0f6d7415 2019-08-08 stsp continue;
4693 0f6d7415 2019-08-08 stsp }
4694 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4695 0f6d7415 2019-08-08 stsp ondisk_path);
4696 0f6d7415 2019-08-08 stsp free(ondisk_path);
4697 0f6d7415 2019-08-08 stsp goto done;
4698 0f6d7415 2019-08-08 stsp }
4699 0f6d7415 2019-08-08 stsp free(ondisk_path);
4700 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4701 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4702 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4703 0f6d7415 2019-08-08 stsp goto done;
4704 0f6d7415 2019-08-08 stsp }
4705 0f6d7415 2019-08-08 stsp }
4706 0f6d7415 2019-08-08 stsp }
4707 0f6d7415 2019-08-08 stsp
4708 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4709 33aa809d 2019-08-08 stsp cpa.action = "revert";
4710 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4711 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4712 2ec1f75b 2019-03-26 stsp done:
4713 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4714 33aa809d 2019-08-08 stsp error == NULL)
4715 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4716 2ec1f75b 2019-03-26 stsp if (repo)
4717 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4718 2ec1f75b 2019-03-26 stsp if (worktree)
4719 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4720 2ec1f75b 2019-03-26 stsp free(path);
4721 d00136be 2019-03-26 stsp free(cwd);
4722 6bad629b 2019-02-04 stsp return error;
4723 c4296144 2019-05-09 stsp }
4724 c4296144 2019-05-09 stsp
4725 c4296144 2019-05-09 stsp __dead static void
4726 c4296144 2019-05-09 stsp usage_commit(void)
4727 c4296144 2019-05-09 stsp {
4728 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4729 5c1e53bc 2019-07-28 stsp getprogname());
4730 c4296144 2019-05-09 stsp exit(1);
4731 33ad4cbe 2019-05-12 jcs }
4732 33ad4cbe 2019-05-12 jcs
4733 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4734 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4735 e2ba3d07 2019-05-13 stsp const char *editor;
4736 e0870e44 2019-05-13 stsp const char *worktree_path;
4737 76d98825 2019-06-03 stsp const char *branch_name;
4738 314a6357 2019-05-13 stsp const char *repo_path;
4739 e0870e44 2019-05-13 stsp char *logmsg_path;
4740 e2ba3d07 2019-05-13 stsp
4741 e2ba3d07 2019-05-13 stsp };
4742 e0870e44 2019-05-13 stsp
4743 33ad4cbe 2019-05-12 jcs static const struct got_error *
4744 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4745 33ad4cbe 2019-05-12 jcs void *arg)
4746 33ad4cbe 2019-05-12 jcs {
4747 76d98825 2019-06-03 stsp char *initial_content = NULL;
4748 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4749 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4750 e0870e44 2019-05-13 stsp char *template = NULL;
4751 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4752 3ce1b845 2019-07-15 stsp int fd;
4753 33ad4cbe 2019-05-12 jcs size_t len;
4754 33ad4cbe 2019-05-12 jcs
4755 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4756 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4757 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4758 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4759 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4760 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4761 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4762 33ad4cbe 2019-05-12 jcs return NULL;
4763 33ad4cbe 2019-05-12 jcs }
4764 33ad4cbe 2019-05-12 jcs
4765 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4766 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4767 76d98825 2019-06-03 stsp
4768 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4769 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4770 76d98825 2019-06-03 stsp a->branch_name) == -1)
4771 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4772 e0870e44 2019-05-13 stsp
4773 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4774 793c30b5 2019-05-13 stsp if (err)
4775 e0870e44 2019-05-13 stsp goto done;
4776 33ad4cbe 2019-05-12 jcs
4777 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4778 33ad4cbe 2019-05-12 jcs
4779 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4780 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4781 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4782 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4783 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4784 33ad4cbe 2019-05-12 jcs }
4785 33ad4cbe 2019-05-12 jcs close(fd);
4786 33ad4cbe 2019-05-12 jcs
4787 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4788 33ad4cbe 2019-05-12 jcs done:
4789 76d98825 2019-06-03 stsp free(initial_content);
4790 e0870e44 2019-05-13 stsp free(template);
4791 314a6357 2019-05-13 stsp
4792 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4793 3ce1b845 2019-07-15 stsp if (err == NULL) {
4794 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4795 3ce1b845 2019-07-15 stsp if (err) {
4796 3ce1b845 2019-07-15 stsp free(*logmsg);
4797 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4798 3ce1b845 2019-07-15 stsp }
4799 3ce1b845 2019-07-15 stsp }
4800 33ad4cbe 2019-05-12 jcs return err;
4801 6bad629b 2019-02-04 stsp }
4802 c4296144 2019-05-09 stsp
4803 c4296144 2019-05-09 stsp static const struct got_error *
4804 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4805 c4296144 2019-05-09 stsp {
4806 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4807 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4808 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4809 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4810 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4811 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4812 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4813 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4814 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4815 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4816 5c1e53bc 2019-07-28 stsp
4817 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4818 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4819 c4296144 2019-05-09 stsp
4820 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4821 c4296144 2019-05-09 stsp switch (ch) {
4822 c4296144 2019-05-09 stsp case 'm':
4823 c4296144 2019-05-09 stsp logmsg = optarg;
4824 c4296144 2019-05-09 stsp break;
4825 c4296144 2019-05-09 stsp default:
4826 c4296144 2019-05-09 stsp usage_commit();
4827 c4296144 2019-05-09 stsp /* NOTREACHED */
4828 c4296144 2019-05-09 stsp }
4829 c4296144 2019-05-09 stsp }
4830 c4296144 2019-05-09 stsp
4831 c4296144 2019-05-09 stsp argc -= optind;
4832 c4296144 2019-05-09 stsp argv += optind;
4833 c4296144 2019-05-09 stsp
4834 43012d58 2019-07-14 stsp #ifndef PROFILE
4835 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4836 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4837 43012d58 2019-07-14 stsp err(1, "pledge");
4838 43012d58 2019-07-14 stsp #endif
4839 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4840 c4296144 2019-05-09 stsp if (cwd == NULL) {
4841 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4842 c4296144 2019-05-09 stsp goto done;
4843 c4296144 2019-05-09 stsp }
4844 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4845 7d5807f4 2019-07-11 stsp if (error)
4846 7d5807f4 2019-07-11 stsp goto done;
4847 7d5807f4 2019-07-11 stsp
4848 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4849 c4296144 2019-05-09 stsp if (error)
4850 a698f62e 2019-07-25 stsp goto done;
4851 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4852 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4853 c4296144 2019-05-09 stsp goto done;
4854 a698f62e 2019-07-25 stsp }
4855 5c1e53bc 2019-07-28 stsp
4856 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4857 916f288c 2019-07-30 stsp worktree);
4858 5c1e53bc 2019-07-28 stsp if (error)
4859 5c1e53bc 2019-07-28 stsp goto done;
4860 c4296144 2019-05-09 stsp
4861 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4862 c9956ddf 2019-09-08 stsp if (error)
4863 c9956ddf 2019-09-08 stsp goto done;
4864 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4865 c9956ddf 2019-09-08 stsp gitconfig_path);
4866 c4296144 2019-05-09 stsp if (error != NULL)
4867 c4296144 2019-05-09 stsp goto done;
4868 c4296144 2019-05-09 stsp
4869 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4870 aba9c984 2019-09-08 stsp if (error)
4871 aba9c984 2019-09-08 stsp return error;
4872 aba9c984 2019-09-08 stsp
4873 314a6357 2019-05-13 stsp /*
4874 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4875 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4876 314a6357 2019-05-13 stsp */
4877 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4878 314a6357 2019-05-13 stsp error = get_editor(&editor);
4879 314a6357 2019-05-13 stsp else
4880 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4881 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4882 c4296144 2019-05-09 stsp if (error)
4883 c4296144 2019-05-09 stsp goto done;
4884 c4296144 2019-05-09 stsp
4885 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4886 087fb88c 2019-08-04 stsp if (error)
4887 087fb88c 2019-08-04 stsp goto done;
4888 087fb88c 2019-08-04 stsp
4889 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4890 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4891 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4892 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4893 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4894 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4895 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4896 916f288c 2019-07-30 stsp goto done;
4897 916f288c 2019-07-30 stsp }
4898 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4899 916f288c 2019-07-30 stsp }
4900 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4901 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4902 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4903 e0870e44 2019-05-13 stsp if (error) {
4904 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4905 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4906 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4907 c4296144 2019-05-09 stsp goto done;
4908 e0870e44 2019-05-13 stsp }
4909 c4296144 2019-05-09 stsp
4910 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4911 c4296144 2019-05-09 stsp if (error)
4912 c4296144 2019-05-09 stsp goto done;
4913 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4914 c4296144 2019-05-09 stsp done:
4915 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4916 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4917 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4918 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4919 7266f21f 2019-10-21 stsp error == NULL)
4920 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4921 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4922 c4296144 2019-05-09 stsp if (repo)
4923 c4296144 2019-05-09 stsp got_repo_close(repo);
4924 c4296144 2019-05-09 stsp if (worktree)
4925 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4926 c4296144 2019-05-09 stsp free(cwd);
4927 c4296144 2019-05-09 stsp free(id_str);
4928 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4929 e2ba3d07 2019-05-13 stsp free(editor);
4930 aba9c984 2019-09-08 stsp free(author);
4931 234035bc 2019-06-01 stsp return error;
4932 234035bc 2019-06-01 stsp }
4933 234035bc 2019-06-01 stsp
4934 234035bc 2019-06-01 stsp __dead static void
4935 234035bc 2019-06-01 stsp usage_cherrypick(void)
4936 234035bc 2019-06-01 stsp {
4937 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4938 234035bc 2019-06-01 stsp exit(1);
4939 234035bc 2019-06-01 stsp }
4940 234035bc 2019-06-01 stsp
4941 234035bc 2019-06-01 stsp static const struct got_error *
4942 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4943 234035bc 2019-06-01 stsp {
4944 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4945 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4946 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4947 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4948 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4949 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4950 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4951 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4952 234035bc 2019-06-01 stsp int ch, did_something = 0;
4953 234035bc 2019-06-01 stsp
4954 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4955 234035bc 2019-06-01 stsp switch (ch) {
4956 234035bc 2019-06-01 stsp default:
4957 234035bc 2019-06-01 stsp usage_cherrypick();
4958 234035bc 2019-06-01 stsp /* NOTREACHED */
4959 234035bc 2019-06-01 stsp }
4960 234035bc 2019-06-01 stsp }
4961 234035bc 2019-06-01 stsp
4962 234035bc 2019-06-01 stsp argc -= optind;
4963 234035bc 2019-06-01 stsp argv += optind;
4964 234035bc 2019-06-01 stsp
4965 43012d58 2019-07-14 stsp #ifndef PROFILE
4966 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4967 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4968 43012d58 2019-07-14 stsp err(1, "pledge");
4969 43012d58 2019-07-14 stsp #endif
4970 234035bc 2019-06-01 stsp if (argc != 1)
4971 234035bc 2019-06-01 stsp usage_cherrypick();
4972 234035bc 2019-06-01 stsp
4973 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4974 234035bc 2019-06-01 stsp if (cwd == NULL) {
4975 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4976 234035bc 2019-06-01 stsp goto done;
4977 234035bc 2019-06-01 stsp }
4978 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4979 234035bc 2019-06-01 stsp if (error)
4980 234035bc 2019-06-01 stsp goto done;
4981 234035bc 2019-06-01 stsp
4982 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4983 c9956ddf 2019-09-08 stsp NULL);
4984 234035bc 2019-06-01 stsp if (error != NULL)
4985 234035bc 2019-06-01 stsp goto done;
4986 234035bc 2019-06-01 stsp
4987 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4988 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4989 234035bc 2019-06-01 stsp if (error)
4990 234035bc 2019-06-01 stsp goto done;
4991 234035bc 2019-06-01 stsp
4992 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4993 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4994 234035bc 2019-06-01 stsp if (error != NULL) {
4995 234035bc 2019-06-01 stsp struct got_reference *ref;
4996 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4997 234035bc 2019-06-01 stsp goto done;
4998 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4999 234035bc 2019-06-01 stsp if (error != NULL)
5000 234035bc 2019-06-01 stsp goto done;
5001 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5002 234035bc 2019-06-01 stsp got_ref_close(ref);
5003 234035bc 2019-06-01 stsp if (error != NULL)
5004 234035bc 2019-06-01 stsp goto done;
5005 234035bc 2019-06-01 stsp }
5006 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5007 234035bc 2019-06-01 stsp if (error)
5008 234035bc 2019-06-01 stsp goto done;
5009 234035bc 2019-06-01 stsp
5010 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5011 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5012 234035bc 2019-06-01 stsp if (error != NULL)
5013 234035bc 2019-06-01 stsp goto done;
5014 234035bc 2019-06-01 stsp
5015 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5016 234035bc 2019-06-01 stsp if (error) {
5017 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5018 234035bc 2019-06-01 stsp goto done;
5019 234035bc 2019-06-01 stsp error = NULL;
5020 234035bc 2019-06-01 stsp } else {
5021 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5022 234035bc 2019-06-01 stsp goto done;
5023 234035bc 2019-06-01 stsp }
5024 234035bc 2019-06-01 stsp
5025 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5026 234035bc 2019-06-01 stsp if (error)
5027 234035bc 2019-06-01 stsp goto done;
5028 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5029 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5030 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5031 03415a1a 2019-06-02 stsp NULL);
5032 234035bc 2019-06-01 stsp if (error != NULL)
5033 234035bc 2019-06-01 stsp goto done;
5034 234035bc 2019-06-01 stsp
5035 234035bc 2019-06-01 stsp if (did_something)
5036 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5037 234035bc 2019-06-01 stsp done:
5038 234035bc 2019-06-01 stsp if (commit)
5039 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5040 234035bc 2019-06-01 stsp free(commit_id_str);
5041 234035bc 2019-06-01 stsp if (head_ref)
5042 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5043 234035bc 2019-06-01 stsp if (worktree)
5044 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5045 234035bc 2019-06-01 stsp if (repo)
5046 234035bc 2019-06-01 stsp got_repo_close(repo);
5047 c4296144 2019-05-09 stsp return error;
5048 c4296144 2019-05-09 stsp }
5049 5ef14e63 2019-06-02 stsp
5050 5ef14e63 2019-06-02 stsp __dead static void
5051 5ef14e63 2019-06-02 stsp usage_backout(void)
5052 5ef14e63 2019-06-02 stsp {
5053 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5054 5ef14e63 2019-06-02 stsp exit(1);
5055 5ef14e63 2019-06-02 stsp }
5056 5ef14e63 2019-06-02 stsp
5057 5ef14e63 2019-06-02 stsp static const struct got_error *
5058 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5059 5ef14e63 2019-06-02 stsp {
5060 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5061 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5062 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5063 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5064 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5065 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5066 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5067 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5068 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5069 5ef14e63 2019-06-02 stsp
5070 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5071 5ef14e63 2019-06-02 stsp switch (ch) {
5072 5ef14e63 2019-06-02 stsp default:
5073 5ef14e63 2019-06-02 stsp usage_backout();
5074 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5075 5ef14e63 2019-06-02 stsp }
5076 5ef14e63 2019-06-02 stsp }
5077 5ef14e63 2019-06-02 stsp
5078 5ef14e63 2019-06-02 stsp argc -= optind;
5079 5ef14e63 2019-06-02 stsp argv += optind;
5080 5ef14e63 2019-06-02 stsp
5081 43012d58 2019-07-14 stsp #ifndef PROFILE
5082 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5083 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5084 43012d58 2019-07-14 stsp err(1, "pledge");
5085 43012d58 2019-07-14 stsp #endif
5086 5ef14e63 2019-06-02 stsp if (argc != 1)
5087 5ef14e63 2019-06-02 stsp usage_backout();
5088 5ef14e63 2019-06-02 stsp
5089 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5090 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5091 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5092 5ef14e63 2019-06-02 stsp goto done;
5093 5ef14e63 2019-06-02 stsp }
5094 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5095 5ef14e63 2019-06-02 stsp if (error)
5096 5ef14e63 2019-06-02 stsp goto done;
5097 5ef14e63 2019-06-02 stsp
5098 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5099 c9956ddf 2019-09-08 stsp NULL);
5100 5ef14e63 2019-06-02 stsp if (error != NULL)
5101 5ef14e63 2019-06-02 stsp goto done;
5102 5ef14e63 2019-06-02 stsp
5103 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5104 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5105 5ef14e63 2019-06-02 stsp if (error)
5106 5ef14e63 2019-06-02 stsp goto done;
5107 5ef14e63 2019-06-02 stsp
5108 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5109 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5110 5ef14e63 2019-06-02 stsp if (error != NULL) {
5111 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5112 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5113 5ef14e63 2019-06-02 stsp goto done;
5114 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5115 5ef14e63 2019-06-02 stsp if (error != NULL)
5116 5ef14e63 2019-06-02 stsp goto done;
5117 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5118 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5119 5ef14e63 2019-06-02 stsp if (error != NULL)
5120 5ef14e63 2019-06-02 stsp goto done;
5121 5ef14e63 2019-06-02 stsp }
5122 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5123 5ef14e63 2019-06-02 stsp if (error)
5124 5ef14e63 2019-06-02 stsp goto done;
5125 5ef14e63 2019-06-02 stsp
5126 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5127 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5128 5ef14e63 2019-06-02 stsp if (error != NULL)
5129 5ef14e63 2019-06-02 stsp goto done;
5130 5ef14e63 2019-06-02 stsp
5131 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5132 5ef14e63 2019-06-02 stsp if (error)
5133 5ef14e63 2019-06-02 stsp goto done;
5134 5ef14e63 2019-06-02 stsp
5135 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5136 5ef14e63 2019-06-02 stsp if (error)
5137 5ef14e63 2019-06-02 stsp goto done;
5138 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5139 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5140 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5141 5ef14e63 2019-06-02 stsp goto done;
5142 5ef14e63 2019-06-02 stsp }
5143 5ef14e63 2019-06-02 stsp
5144 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5145 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5146 5ef14e63 2019-06-02 stsp if (error != NULL)
5147 5ef14e63 2019-06-02 stsp goto done;
5148 5ef14e63 2019-06-02 stsp
5149 5ef14e63 2019-06-02 stsp if (did_something)
5150 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5151 5ef14e63 2019-06-02 stsp done:
5152 5ef14e63 2019-06-02 stsp if (commit)
5153 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5154 5ef14e63 2019-06-02 stsp free(commit_id_str);
5155 5ef14e63 2019-06-02 stsp if (head_ref)
5156 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5157 818c7501 2019-07-11 stsp if (worktree)
5158 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5159 818c7501 2019-07-11 stsp if (repo)
5160 818c7501 2019-07-11 stsp got_repo_close(repo);
5161 818c7501 2019-07-11 stsp return error;
5162 818c7501 2019-07-11 stsp }
5163 818c7501 2019-07-11 stsp
5164 818c7501 2019-07-11 stsp __dead static void
5165 818c7501 2019-07-11 stsp usage_rebase(void)
5166 818c7501 2019-07-11 stsp {
5167 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5168 af54c8f8 2019-07-11 stsp getprogname());
5169 818c7501 2019-07-11 stsp exit(1);
5170 0ebf8283 2019-07-24 stsp }
5171 0ebf8283 2019-07-24 stsp
5172 0ebf8283 2019-07-24 stsp void
5173 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5174 0ebf8283 2019-07-24 stsp {
5175 0ebf8283 2019-07-24 stsp char *nl;
5176 0ebf8283 2019-07-24 stsp size_t len;
5177 0ebf8283 2019-07-24 stsp
5178 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5179 0ebf8283 2019-07-24 stsp if (len > limit)
5180 0ebf8283 2019-07-24 stsp len = limit;
5181 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5182 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5183 0ebf8283 2019-07-24 stsp if (nl)
5184 0ebf8283 2019-07-24 stsp *nl = '\0';
5185 0ebf8283 2019-07-24 stsp }
5186 0ebf8283 2019-07-24 stsp
5187 0ebf8283 2019-07-24 stsp static const struct got_error *
5188 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5189 0ebf8283 2019-07-24 stsp {
5190 5943eee2 2019-08-13 stsp const struct got_error *err;
5191 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5192 5943eee2 2019-08-13 stsp const char *s;
5193 0ebf8283 2019-07-24 stsp
5194 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5195 5943eee2 2019-08-13 stsp if (err)
5196 5943eee2 2019-08-13 stsp return err;
5197 0ebf8283 2019-07-24 stsp
5198 5943eee2 2019-08-13 stsp s = logmsg0;
5199 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5200 5943eee2 2019-08-13 stsp s++;
5201 5943eee2 2019-08-13 stsp
5202 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5203 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5204 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5205 5943eee2 2019-08-13 stsp goto done;
5206 5943eee2 2019-08-13 stsp }
5207 0ebf8283 2019-07-24 stsp
5208 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5209 5943eee2 2019-08-13 stsp done:
5210 5943eee2 2019-08-13 stsp free(logmsg0);
5211 a0ea4fc0 2020-02-28 stsp return err;
5212 a0ea4fc0 2020-02-28 stsp }
5213 a0ea4fc0 2020-02-28 stsp
5214 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5215 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5216 a0ea4fc0 2020-02-28 stsp {
5217 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5218 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5219 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5220 a0ea4fc0 2020-02-28 stsp
5221 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5222 a0ea4fc0 2020-02-28 stsp if (err)
5223 a0ea4fc0 2020-02-28 stsp return err;
5224 a0ea4fc0 2020-02-28 stsp
5225 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5226 a0ea4fc0 2020-02-28 stsp if (err)
5227 a0ea4fc0 2020-02-28 stsp goto done;
5228 a0ea4fc0 2020-02-28 stsp
5229 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5230 a0ea4fc0 2020-02-28 stsp
5231 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5232 a0ea4fc0 2020-02-28 stsp if (err)
5233 a0ea4fc0 2020-02-28 stsp goto done;
5234 a0ea4fc0 2020-02-28 stsp
5235 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5236 a0ea4fc0 2020-02-28 stsp done:
5237 a0ea4fc0 2020-02-28 stsp free(id_str);
5238 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5239 a0ea4fc0 2020-02-28 stsp free(logmsg);
5240 5943eee2 2019-08-13 stsp return err;
5241 818c7501 2019-07-11 stsp }
5242 818c7501 2019-07-11 stsp
5243 818c7501 2019-07-11 stsp static const struct got_error *
5244 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5245 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5246 818c7501 2019-07-11 stsp {
5247 818c7501 2019-07-11 stsp const struct got_error *err;
5248 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5249 818c7501 2019-07-11 stsp
5250 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5251 818c7501 2019-07-11 stsp if (err)
5252 818c7501 2019-07-11 stsp goto done;
5253 818c7501 2019-07-11 stsp
5254 ff0d2220 2019-07-11 stsp if (new_id) {
5255 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5256 ff0d2220 2019-07-11 stsp if (err)
5257 ff0d2220 2019-07-11 stsp goto done;
5258 ff0d2220 2019-07-11 stsp }
5259 818c7501 2019-07-11 stsp
5260 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5261 ff0d2220 2019-07-11 stsp if (new_id_str)
5262 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5263 0ebf8283 2019-07-24 stsp
5264 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5265 0ebf8283 2019-07-24 stsp if (err)
5266 0ebf8283 2019-07-24 stsp goto done;
5267 0ebf8283 2019-07-24 stsp
5268 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5269 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5270 818c7501 2019-07-11 stsp done:
5271 818c7501 2019-07-11 stsp free(old_id_str);
5272 818c7501 2019-07-11 stsp free(new_id_str);
5273 272a1371 2020-02-28 stsp free(logmsg);
5274 818c7501 2019-07-11 stsp return err;
5275 818c7501 2019-07-11 stsp }
5276 818c7501 2019-07-11 stsp
5277 1ee397ad 2019-07-12 stsp static const struct got_error *
5278 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5279 818c7501 2019-07-11 stsp {
5280 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5281 818c7501 2019-07-11 stsp
5282 818c7501 2019-07-11 stsp while (path[0] == '/')
5283 818c7501 2019-07-11 stsp path++;
5284 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5285 818c7501 2019-07-11 stsp
5286 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5287 1ee397ad 2019-07-12 stsp return NULL;
5288 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5289 818c7501 2019-07-11 stsp *rebase_status = status;
5290 1ee397ad 2019-07-12 stsp return NULL;
5291 818c7501 2019-07-11 stsp }
5292 818c7501 2019-07-11 stsp
5293 818c7501 2019-07-11 stsp static const struct got_error *
5294 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5295 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5296 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5297 818c7501 2019-07-11 stsp {
5298 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5299 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5300 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5301 818c7501 2019-07-11 stsp }
5302 818c7501 2019-07-11 stsp
5303 818c7501 2019-07-11 stsp static const struct got_error *
5304 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5305 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5306 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5307 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5308 ff0d2220 2019-07-11 stsp {
5309 ff0d2220 2019-07-11 stsp const struct got_error *error;
5310 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5311 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5312 ff0d2220 2019-07-11 stsp
5313 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5314 ff0d2220 2019-07-11 stsp if (error)
5315 ff0d2220 2019-07-11 stsp return error;
5316 ff0d2220 2019-07-11 stsp
5317 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5318 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5319 ff0d2220 2019-07-11 stsp if (error) {
5320 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5321 ff0d2220 2019-07-11 stsp goto done;
5322 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5323 ff0d2220 2019-07-11 stsp } else {
5324 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5325 ff0d2220 2019-07-11 stsp free(new_commit_id);
5326 ff0d2220 2019-07-11 stsp }
5327 ff0d2220 2019-07-11 stsp done:
5328 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5329 ff0d2220 2019-07-11 stsp return error;
5330 64c6d990 2019-07-11 stsp }
5331 64c6d990 2019-07-11 stsp
5332 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5333 64c6d990 2019-07-11 stsp const char *path_prefix;
5334 64c6d990 2019-07-11 stsp size_t len;
5335 8ca9bd68 2019-07-25 stsp int errcode;
5336 64c6d990 2019-07-11 stsp };
5337 64c6d990 2019-07-11 stsp
5338 64c6d990 2019-07-11 stsp static const struct got_error *
5339 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5340 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5341 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5342 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5343 64c6d990 2019-07-11 stsp {
5344 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5345 64c6d990 2019-07-11 stsp
5346 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5347 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5348 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5349 64c6d990 2019-07-11 stsp
5350 64c6d990 2019-07-11 stsp return NULL;
5351 64c6d990 2019-07-11 stsp }
5352 64c6d990 2019-07-11 stsp
5353 64c6d990 2019-07-11 stsp static const struct got_error *
5354 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5355 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5356 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5357 64c6d990 2019-07-11 stsp {
5358 64c6d990 2019-07-11 stsp const struct got_error *err;
5359 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5360 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5361 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5362 64c6d990 2019-07-11 stsp
5363 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5364 64c6d990 2019-07-11 stsp return NULL;
5365 64c6d990 2019-07-11 stsp
5366 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5367 64c6d990 2019-07-11 stsp if (err)
5368 64c6d990 2019-07-11 stsp goto done;
5369 64c6d990 2019-07-11 stsp
5370 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5371 64c6d990 2019-07-11 stsp if (err)
5372 64c6d990 2019-07-11 stsp goto done;
5373 64c6d990 2019-07-11 stsp
5374 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5375 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5376 64c6d990 2019-07-11 stsp if (err)
5377 64c6d990 2019-07-11 stsp goto done;
5378 64c6d990 2019-07-11 stsp
5379 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5380 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5381 64c6d990 2019-07-11 stsp if (err)
5382 64c6d990 2019-07-11 stsp goto done;
5383 64c6d990 2019-07-11 stsp
5384 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5385 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5386 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5387 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5388 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5389 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5390 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5391 64c6d990 2019-07-11 stsp done:
5392 64c6d990 2019-07-11 stsp if (tree1)
5393 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5394 64c6d990 2019-07-11 stsp if (tree2)
5395 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5396 64c6d990 2019-07-11 stsp if (commit)
5397 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5398 64c6d990 2019-07-11 stsp if (parent_commit)
5399 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5400 64c6d990 2019-07-11 stsp return err;
5401 ff0d2220 2019-07-11 stsp }
5402 ff0d2220 2019-07-11 stsp
5403 ff0d2220 2019-07-11 stsp static const struct got_error *
5404 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5405 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5406 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5407 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5408 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5409 af61c510 2019-07-19 stsp {
5410 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5411 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5412 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5413 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5414 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5415 af61c510 2019-07-19 stsp
5416 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5417 af61c510 2019-07-19 stsp if (err)
5418 af61c510 2019-07-19 stsp return err;
5419 af61c510 2019-07-19 stsp
5420 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5421 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5422 af61c510 2019-07-19 stsp if (err)
5423 af61c510 2019-07-19 stsp goto done;
5424 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5425 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5426 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5427 af61c510 2019-07-19 stsp if (err) {
5428 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5429 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5430 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5431 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5432 af61c510 2019-07-19 stsp "been reached?!?");
5433 ee780d5c 2020-01-04 stsp }
5434 ee780d5c 2020-01-04 stsp goto done;
5435 af61c510 2019-07-19 stsp } else {
5436 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5437 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5438 af61c510 2019-07-19 stsp if (err)
5439 af61c510 2019-07-19 stsp goto done;
5440 af61c510 2019-07-19 stsp
5441 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5442 af61c510 2019-07-19 stsp if (err)
5443 af61c510 2019-07-19 stsp goto done;
5444 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5445 af61c510 2019-07-19 stsp commit_id = parent_id;
5446 af61c510 2019-07-19 stsp }
5447 af61c510 2019-07-19 stsp }
5448 af61c510 2019-07-19 stsp done:
5449 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5450 af61c510 2019-07-19 stsp return err;
5451 af61c510 2019-07-19 stsp }
5452 af61c510 2019-07-19 stsp
5453 af61c510 2019-07-19 stsp static const struct got_error *
5454 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5455 818c7501 2019-07-11 stsp {
5456 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5457 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5458 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5459 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5460 818c7501 2019-07-11 stsp char *cwd = NULL;
5461 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5462 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5463 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5464 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5465 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5466 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5467 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5468 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5469 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5470 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5471 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5472 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5473 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5474 818c7501 2019-07-11 stsp
5475 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5476 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5477 818c7501 2019-07-11 stsp
5478 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5479 818c7501 2019-07-11 stsp switch (ch) {
5480 818c7501 2019-07-11 stsp case 'a':
5481 818c7501 2019-07-11 stsp abort_rebase = 1;
5482 818c7501 2019-07-11 stsp break;
5483 818c7501 2019-07-11 stsp case 'c':
5484 818c7501 2019-07-11 stsp continue_rebase = 1;
5485 818c7501 2019-07-11 stsp break;
5486 818c7501 2019-07-11 stsp default:
5487 818c7501 2019-07-11 stsp usage_rebase();
5488 818c7501 2019-07-11 stsp /* NOTREACHED */
5489 818c7501 2019-07-11 stsp }
5490 818c7501 2019-07-11 stsp }
5491 818c7501 2019-07-11 stsp
5492 818c7501 2019-07-11 stsp argc -= optind;
5493 818c7501 2019-07-11 stsp argv += optind;
5494 818c7501 2019-07-11 stsp
5495 43012d58 2019-07-14 stsp #ifndef PROFILE
5496 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5497 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5498 43012d58 2019-07-14 stsp err(1, "pledge");
5499 43012d58 2019-07-14 stsp #endif
5500 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5501 818c7501 2019-07-11 stsp usage_rebase();
5502 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5503 818c7501 2019-07-11 stsp if (argc != 0)
5504 818c7501 2019-07-11 stsp usage_rebase();
5505 818c7501 2019-07-11 stsp } else if (argc != 1)
5506 818c7501 2019-07-11 stsp usage_rebase();
5507 818c7501 2019-07-11 stsp
5508 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5509 818c7501 2019-07-11 stsp if (cwd == NULL) {
5510 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5511 818c7501 2019-07-11 stsp goto done;
5512 818c7501 2019-07-11 stsp }
5513 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5514 818c7501 2019-07-11 stsp if (error)
5515 818c7501 2019-07-11 stsp goto done;
5516 818c7501 2019-07-11 stsp
5517 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5518 c9956ddf 2019-09-08 stsp NULL);
5519 818c7501 2019-07-11 stsp if (error != NULL)
5520 818c7501 2019-07-11 stsp goto done;
5521 818c7501 2019-07-11 stsp
5522 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5523 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5524 7ef62c4e 2020-02-24 stsp if (error)
5525 7ef62c4e 2020-02-24 stsp goto done;
5526 7ef62c4e 2020-02-24 stsp
5527 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5528 7ef62c4e 2020-02-24 stsp worktree);
5529 818c7501 2019-07-11 stsp if (error)
5530 7ef62c4e 2020-02-24 stsp goto done;
5531 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5532 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5533 818c7501 2019-07-11 stsp goto done;
5534 7ef62c4e 2020-02-24 stsp }
5535 818c7501 2019-07-11 stsp
5536 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5537 818c7501 2019-07-11 stsp if (error)
5538 818c7501 2019-07-11 stsp goto done;
5539 818c7501 2019-07-11 stsp
5540 f6794adc 2019-07-23 stsp if (abort_rebase) {
5541 818c7501 2019-07-11 stsp int did_something;
5542 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5543 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5544 f6794adc 2019-07-23 stsp goto done;
5545 f6794adc 2019-07-23 stsp }
5546 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5547 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5548 3e3a69f1 2019-07-25 stsp worktree, repo);
5549 818c7501 2019-07-11 stsp if (error)
5550 818c7501 2019-07-11 stsp goto done;
5551 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5552 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5553 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5554 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5555 818c7501 2019-07-11 stsp if (error)
5556 818c7501 2019-07-11 stsp goto done;
5557 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5558 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5559 818c7501 2019-07-11 stsp }
5560 818c7501 2019-07-11 stsp
5561 818c7501 2019-07-11 stsp if (continue_rebase) {
5562 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5563 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5564 f6794adc 2019-07-23 stsp goto done;
5565 f6794adc 2019-07-23 stsp }
5566 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5567 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5568 3e3a69f1 2019-07-25 stsp worktree, repo);
5569 818c7501 2019-07-11 stsp if (error)
5570 818c7501 2019-07-11 stsp goto done;
5571 818c7501 2019-07-11 stsp
5572 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5573 01757395 2019-07-12 stsp resume_commit_id, repo);
5574 818c7501 2019-07-11 stsp if (error)
5575 818c7501 2019-07-11 stsp goto done;
5576 818c7501 2019-07-11 stsp
5577 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5578 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5579 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5580 818c7501 2019-07-11 stsp goto done;
5581 818c7501 2019-07-11 stsp }
5582 818c7501 2019-07-11 stsp } else {
5583 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5584 818c7501 2019-07-11 stsp if (error != NULL)
5585 818c7501 2019-07-11 stsp goto done;
5586 ff0d2220 2019-07-11 stsp }
5587 818c7501 2019-07-11 stsp
5588 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5589 ff0d2220 2019-07-11 stsp if (error)
5590 ff0d2220 2019-07-11 stsp goto done;
5591 ff0d2220 2019-07-11 stsp
5592 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5593 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5594 a51a74b3 2019-07-27 stsp
5595 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5596 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5597 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5598 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5599 818c7501 2019-07-11 stsp if (error)
5600 818c7501 2019-07-11 stsp goto done;
5601 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5602 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5603 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5604 818c7501 2019-07-11 stsp "with work tree's branch");
5605 818c7501 2019-07-11 stsp goto done;
5606 818c7501 2019-07-11 stsp }
5607 818c7501 2019-07-11 stsp
5608 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5609 a51a74b3 2019-07-27 stsp if (error) {
5610 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5611 a51a74b3 2019-07-27 stsp goto done;
5612 a51a74b3 2019-07-27 stsp error = NULL;
5613 a51a74b3 2019-07-27 stsp } else {
5614 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5615 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5616 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5617 a51a74b3 2019-07-27 stsp goto done;
5618 a51a74b3 2019-07-27 stsp }
5619 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5620 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5621 818c7501 2019-07-11 stsp if (error)
5622 818c7501 2019-07-11 stsp goto done;
5623 818c7501 2019-07-11 stsp }
5624 818c7501 2019-07-11 stsp
5625 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5626 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5627 818c7501 2019-07-11 stsp if (error)
5628 818c7501 2019-07-11 stsp goto done;
5629 818c7501 2019-07-11 stsp
5630 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5631 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5632 fc66b545 2019-08-12 stsp if (pid == NULL) {
5633 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5634 fc66b545 2019-08-12 stsp int did_something;
5635 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5636 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5637 fc66b545 2019-08-12 stsp &did_something);
5638 fc66b545 2019-08-12 stsp if (error)
5639 fc66b545 2019-08-12 stsp goto done;
5640 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5641 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5642 fc66b545 2019-08-12 stsp }
5643 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5644 fc66b545 2019-08-12 stsp goto done;
5645 fc66b545 2019-08-12 stsp }
5646 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5647 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5648 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5649 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5650 818c7501 2019-07-11 stsp commit = NULL;
5651 818c7501 2019-07-11 stsp if (error)
5652 818c7501 2019-07-11 stsp goto done;
5653 64c6d990 2019-07-11 stsp
5654 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5655 38b0338b 2019-11-29 stsp if (continue_rebase) {
5656 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5657 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5658 38b0338b 2019-11-29 stsp goto done;
5659 38b0338b 2019-11-29 stsp } else {
5660 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5661 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5662 38b0338b 2019-11-29 stsp char *id_str;
5663 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5664 38b0338b 2019-11-29 stsp new_base_branch);
5665 38b0338b 2019-11-29 stsp if (error)
5666 38b0338b 2019-11-29 stsp goto done;
5667 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5668 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5669 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5670 38b0338b 2019-11-29 stsp free(id_str);
5671 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5672 38b0338b 2019-11-29 stsp new_head_commit_id);
5673 38b0338b 2019-11-29 stsp if (error)
5674 38b0338b 2019-11-29 stsp goto done;
5675 38b0338b 2019-11-29 stsp }
5676 818c7501 2019-07-11 stsp }
5677 818c7501 2019-07-11 stsp
5678 818c7501 2019-07-11 stsp pid = NULL;
5679 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5680 818c7501 2019-07-11 stsp commit_id = qid->id;
5681 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5682 818c7501 2019-07-11 stsp pid = qid;
5683 818c7501 2019-07-11 stsp
5684 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5685 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5686 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5687 818c7501 2019-07-11 stsp if (error)
5688 818c7501 2019-07-11 stsp goto done;
5689 818c7501 2019-07-11 stsp
5690 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5691 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5692 a0ea4fc0 2020-02-28 stsp if (error)
5693 a0ea4fc0 2020-02-28 stsp goto done;
5694 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5695 818c7501 2019-07-11 stsp break;
5696 01757395 2019-07-12 stsp }
5697 818c7501 2019-07-11 stsp
5698 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5699 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5700 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5701 818c7501 2019-07-11 stsp if (error)
5702 818c7501 2019-07-11 stsp goto done;
5703 818c7501 2019-07-11 stsp }
5704 818c7501 2019-07-11 stsp
5705 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5706 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5707 818c7501 2019-07-11 stsp if (error)
5708 818c7501 2019-07-11 stsp goto done;
5709 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5710 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5711 818c7501 2019-07-11 stsp } else
5712 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5713 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5714 818c7501 2019-07-11 stsp done:
5715 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5716 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5717 818c7501 2019-07-11 stsp free(resume_commit_id);
5718 818c7501 2019-07-11 stsp free(yca_id);
5719 818c7501 2019-07-11 stsp if (commit)
5720 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5721 818c7501 2019-07-11 stsp if (branch)
5722 818c7501 2019-07-11 stsp got_ref_close(branch);
5723 818c7501 2019-07-11 stsp if (new_base_branch)
5724 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5725 818c7501 2019-07-11 stsp if (tmp_branch)
5726 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5727 5ef14e63 2019-06-02 stsp if (worktree)
5728 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5729 5ef14e63 2019-06-02 stsp if (repo)
5730 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5731 5ef14e63 2019-06-02 stsp return error;
5732 0ebf8283 2019-07-24 stsp }
5733 0ebf8283 2019-07-24 stsp
5734 0ebf8283 2019-07-24 stsp __dead static void
5735 0ebf8283 2019-07-24 stsp usage_histedit(void)
5736 0ebf8283 2019-07-24 stsp {
5737 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5738 0ebf8283 2019-07-24 stsp getprogname());
5739 0ebf8283 2019-07-24 stsp exit(1);
5740 0ebf8283 2019-07-24 stsp }
5741 0ebf8283 2019-07-24 stsp
5742 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5743 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5744 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5745 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5746 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5747 0ebf8283 2019-07-24 stsp
5748 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5749 0ebf8283 2019-07-24 stsp unsigned char code;
5750 0ebf8283 2019-07-24 stsp const char *name;
5751 0ebf8283 2019-07-24 stsp const char *desc;
5752 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5753 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5754 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5755 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5756 82997472 2020-01-29 stsp "be used" },
5757 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5758 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5759 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5760 0ebf8283 2019-07-24 stsp };
5761 0ebf8283 2019-07-24 stsp
5762 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5763 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5764 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5765 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5766 0ebf8283 2019-07-24 stsp char *logmsg;
5767 0ebf8283 2019-07-24 stsp };
5768 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5769 0ebf8283 2019-07-24 stsp
5770 0ebf8283 2019-07-24 stsp static const struct got_error *
5771 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5772 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5773 0ebf8283 2019-07-24 stsp {
5774 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5775 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5776 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5777 8138f3e1 2019-08-11 stsp int n;
5778 0ebf8283 2019-07-24 stsp
5779 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5780 0ebf8283 2019-07-24 stsp if (err)
5781 0ebf8283 2019-07-24 stsp goto done;
5782 0ebf8283 2019-07-24 stsp
5783 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5784 0ebf8283 2019-07-24 stsp if (err)
5785 0ebf8283 2019-07-24 stsp goto done;
5786 0ebf8283 2019-07-24 stsp
5787 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5788 0ebf8283 2019-07-24 stsp if (err)
5789 0ebf8283 2019-07-24 stsp goto done;
5790 0ebf8283 2019-07-24 stsp
5791 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5792 0ebf8283 2019-07-24 stsp if (n < 0)
5793 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5794 0ebf8283 2019-07-24 stsp done:
5795 0ebf8283 2019-07-24 stsp if (commit)
5796 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5797 0ebf8283 2019-07-24 stsp free(id_str);
5798 0ebf8283 2019-07-24 stsp free(logmsg);
5799 0ebf8283 2019-07-24 stsp return err;
5800 0ebf8283 2019-07-24 stsp }
5801 0ebf8283 2019-07-24 stsp
5802 0ebf8283 2019-07-24 stsp static const struct got_error *
5803 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5804 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5805 0ebf8283 2019-07-24 stsp {
5806 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5807 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5808 0ebf8283 2019-07-24 stsp
5809 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5810 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5811 0ebf8283 2019-07-24 stsp
5812 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5813 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5814 0ebf8283 2019-07-24 stsp f, repo);
5815 0ebf8283 2019-07-24 stsp if (err)
5816 0ebf8283 2019-07-24 stsp break;
5817 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5818 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5819 083957f4 2020-02-24 stsp if (n < 0) {
5820 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5821 083957f4 2020-02-24 stsp break;
5822 083957f4 2020-02-24 stsp }
5823 083957f4 2020-02-24 stsp }
5824 0ebf8283 2019-07-24 stsp }
5825 0ebf8283 2019-07-24 stsp
5826 0ebf8283 2019-07-24 stsp return err;
5827 0ebf8283 2019-07-24 stsp }
5828 0ebf8283 2019-07-24 stsp
5829 0ebf8283 2019-07-24 stsp static const struct got_error *
5830 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5831 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5832 0ebf8283 2019-07-24 stsp {
5833 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5834 0ebf8283 2019-07-24 stsp int n, i;
5835 514f2ffe 2020-01-29 stsp char *id_str;
5836 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
5837 514f2ffe 2020-01-29 stsp
5838 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
5839 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
5840 514f2ffe 2020-01-29 stsp if (err)
5841 514f2ffe 2020-01-29 stsp return err;
5842 514f2ffe 2020-01-29 stsp
5843 514f2ffe 2020-01-29 stsp n = fprintf(f,
5844 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
5845 514f2ffe 2020-01-29 stsp "# commit %s\n"
5846 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
5847 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
5848 514f2ffe 2020-01-29 stsp if (n < 0) {
5849 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5850 514f2ffe 2020-01-29 stsp goto done;
5851 514f2ffe 2020-01-29 stsp }
5852 0ebf8283 2019-07-24 stsp
5853 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5854 514f2ffe 2020-01-29 stsp if (n < 0) {
5855 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5856 514f2ffe 2020-01-29 stsp goto done;
5857 514f2ffe 2020-01-29 stsp }
5858 0ebf8283 2019-07-24 stsp
5859 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5860 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5861 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5862 0ebf8283 2019-07-24 stsp cmd->desc);
5863 0ebf8283 2019-07-24 stsp if (n < 0) {
5864 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5865 0ebf8283 2019-07-24 stsp break;
5866 0ebf8283 2019-07-24 stsp }
5867 0ebf8283 2019-07-24 stsp }
5868 514f2ffe 2020-01-29 stsp done:
5869 514f2ffe 2020-01-29 stsp free(id_str);
5870 0ebf8283 2019-07-24 stsp return err;
5871 0ebf8283 2019-07-24 stsp }
5872 0ebf8283 2019-07-24 stsp
5873 0ebf8283 2019-07-24 stsp static const struct got_error *
5874 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5875 0ebf8283 2019-07-24 stsp {
5876 0ebf8283 2019-07-24 stsp static char msg[42];
5877 0ebf8283 2019-07-24 stsp int ret;
5878 0ebf8283 2019-07-24 stsp
5879 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5880 0ebf8283 2019-07-24 stsp lineno);
5881 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5882 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5883 0ebf8283 2019-07-24 stsp
5884 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5885 0ebf8283 2019-07-24 stsp }
5886 0ebf8283 2019-07-24 stsp
5887 0ebf8283 2019-07-24 stsp static const struct got_error *
5888 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5889 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5890 0ebf8283 2019-07-24 stsp {
5891 0ebf8283 2019-07-24 stsp const struct got_error *err;
5892 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5893 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5894 0ebf8283 2019-07-24 stsp
5895 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5896 0ebf8283 2019-07-24 stsp if (err)
5897 0ebf8283 2019-07-24 stsp return err;
5898 0ebf8283 2019-07-24 stsp
5899 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5900 0ebf8283 2019-07-24 stsp if (err)
5901 0ebf8283 2019-07-24 stsp goto done;
5902 0ebf8283 2019-07-24 stsp
5903 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5904 5943eee2 2019-08-13 stsp if (err)
5905 5943eee2 2019-08-13 stsp goto done;
5906 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5907 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5908 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5909 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5910 0ebf8283 2019-07-24 stsp }
5911 0ebf8283 2019-07-24 stsp done:
5912 0ebf8283 2019-07-24 stsp if (folded_commit)
5913 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5914 0ebf8283 2019-07-24 stsp free(id_str);
5915 5943eee2 2019-08-13 stsp free(folded_logmsg);
5916 0ebf8283 2019-07-24 stsp return err;
5917 0ebf8283 2019-07-24 stsp }
5918 0ebf8283 2019-07-24 stsp
5919 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5920 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5921 0ebf8283 2019-07-24 stsp {
5922 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5923 0ebf8283 2019-07-24 stsp
5924 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5925 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5926 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5927 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5928 3f9de99f 2019-07-24 stsp folded = prev;
5929 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5930 0ebf8283 2019-07-24 stsp }
5931 0ebf8283 2019-07-24 stsp
5932 0ebf8283 2019-07-24 stsp return folded;
5933 0ebf8283 2019-07-24 stsp }
5934 0ebf8283 2019-07-24 stsp
5935 0ebf8283 2019-07-24 stsp static const struct got_error *
5936 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5937 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5938 0ebf8283 2019-07-24 stsp {
5939 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5940 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5941 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5942 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5943 0ebf8283 2019-07-24 stsp int fd;
5944 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5945 0ebf8283 2019-07-24 stsp
5946 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5947 0ebf8283 2019-07-24 stsp if (err)
5948 0ebf8283 2019-07-24 stsp return err;
5949 0ebf8283 2019-07-24 stsp
5950 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5951 0ebf8283 2019-07-24 stsp if (folded) {
5952 0ebf8283 2019-07-24 stsp while (folded != hle) {
5953 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5954 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5955 3f9de99f 2019-07-24 stsp continue;
5956 3f9de99f 2019-07-24 stsp }
5957 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5958 0ebf8283 2019-07-24 stsp logmsg, repo);
5959 0ebf8283 2019-07-24 stsp if (err)
5960 0ebf8283 2019-07-24 stsp goto done;
5961 0ebf8283 2019-07-24 stsp free(logmsg);
5962 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5963 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5964 0ebf8283 2019-07-24 stsp }
5965 0ebf8283 2019-07-24 stsp }
5966 0ebf8283 2019-07-24 stsp
5967 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5968 0ebf8283 2019-07-24 stsp if (err)
5969 0ebf8283 2019-07-24 stsp goto done;
5970 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5971 5943eee2 2019-08-13 stsp if (err)
5972 5943eee2 2019-08-13 stsp goto done;
5973 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5974 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5975 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5976 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5977 0ebf8283 2019-07-24 stsp goto done;
5978 0ebf8283 2019-07-24 stsp }
5979 0ebf8283 2019-07-24 stsp free(logmsg);
5980 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5981 0ebf8283 2019-07-24 stsp
5982 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5983 0ebf8283 2019-07-24 stsp if (err)
5984 0ebf8283 2019-07-24 stsp goto done;
5985 0ebf8283 2019-07-24 stsp
5986 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
5987 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
5988 0ebf8283 2019-07-24 stsp if (err)
5989 0ebf8283 2019-07-24 stsp goto done;
5990 0ebf8283 2019-07-24 stsp
5991 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5992 0ebf8283 2019-07-24 stsp close(fd);
5993 0ebf8283 2019-07-24 stsp
5994 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5995 0ebf8283 2019-07-24 stsp if (err)
5996 0ebf8283 2019-07-24 stsp goto done;
5997 0ebf8283 2019-07-24 stsp
5998 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5999 0ebf8283 2019-07-24 stsp if (err) {
6000 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6001 0ebf8283 2019-07-24 stsp goto done;
6002 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6003 0ebf8283 2019-07-24 stsp }
6004 0ebf8283 2019-07-24 stsp done:
6005 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6006 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6007 0ebf8283 2019-07-24 stsp free(logmsg_path);
6008 0ebf8283 2019-07-24 stsp free(logmsg);
6009 5943eee2 2019-08-13 stsp free(orig_logmsg);
6010 0ebf8283 2019-07-24 stsp free(editor);
6011 0ebf8283 2019-07-24 stsp if (commit)
6012 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6013 0ebf8283 2019-07-24 stsp return err;
6014 5ef14e63 2019-06-02 stsp }
6015 0ebf8283 2019-07-24 stsp
6016 0ebf8283 2019-07-24 stsp static const struct got_error *
6017 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6018 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6019 0ebf8283 2019-07-24 stsp {
6020 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6021 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6022 0ebf8283 2019-07-24 stsp size_t size;
6023 0ebf8283 2019-07-24 stsp ssize_t len;
6024 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6025 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6026 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6027 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6028 0ebf8283 2019-07-24 stsp
6029 0ebf8283 2019-07-24 stsp for (;;) {
6030 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6031 0ebf8283 2019-07-24 stsp if (len == -1) {
6032 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6033 0ebf8283 2019-07-24 stsp if (feof(f))
6034 0ebf8283 2019-07-24 stsp break;
6035 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6036 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6037 0ebf8283 2019-07-24 stsp break;
6038 0ebf8283 2019-07-24 stsp }
6039 0ebf8283 2019-07-24 stsp lineno++;
6040 0ebf8283 2019-07-24 stsp p = line;
6041 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6042 0ebf8283 2019-07-24 stsp p++;
6043 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6044 0ebf8283 2019-07-24 stsp free(line);
6045 0ebf8283 2019-07-24 stsp line = NULL;
6046 0ebf8283 2019-07-24 stsp continue;
6047 0ebf8283 2019-07-24 stsp }
6048 0ebf8283 2019-07-24 stsp cmd = NULL;
6049 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6050 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6051 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6052 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6053 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6054 0ebf8283 2019-07-24 stsp break;
6055 0ebf8283 2019-07-24 stsp }
6056 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6057 0ebf8283 2019-07-24 stsp p++;
6058 0ebf8283 2019-07-24 stsp break;
6059 0ebf8283 2019-07-24 stsp }
6060 0ebf8283 2019-07-24 stsp }
6061 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6062 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6063 0ebf8283 2019-07-24 stsp break;
6064 0ebf8283 2019-07-24 stsp }
6065 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6066 0ebf8283 2019-07-24 stsp p++;
6067 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6068 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6069 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6070 0ebf8283 2019-07-24 stsp break;
6071 0ebf8283 2019-07-24 stsp }
6072 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6073 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6074 0ebf8283 2019-07-24 stsp if (err)
6075 0ebf8283 2019-07-24 stsp break;
6076 0ebf8283 2019-07-24 stsp } else {
6077 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6078 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6079 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6080 0ebf8283 2019-07-24 stsp break;
6081 0ebf8283 2019-07-24 stsp }
6082 0ebf8283 2019-07-24 stsp }
6083 0ebf8283 2019-07-24 stsp free(line);
6084 0ebf8283 2019-07-24 stsp line = NULL;
6085 0ebf8283 2019-07-24 stsp continue;
6086 0ebf8283 2019-07-24 stsp } else {
6087 0ebf8283 2019-07-24 stsp end = p;
6088 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6089 0ebf8283 2019-07-24 stsp end++;
6090 0ebf8283 2019-07-24 stsp *end = '\0';
6091 0ebf8283 2019-07-24 stsp
6092 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6093 0ebf8283 2019-07-24 stsp if (err) {
6094 0ebf8283 2019-07-24 stsp /* override error code */
6095 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6096 0ebf8283 2019-07-24 stsp break;
6097 0ebf8283 2019-07-24 stsp }
6098 0ebf8283 2019-07-24 stsp }
6099 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6100 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6101 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6102 0ebf8283 2019-07-24 stsp break;
6103 0ebf8283 2019-07-24 stsp }
6104 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6105 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6106 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6107 0ebf8283 2019-07-24 stsp commit_id = NULL;
6108 0ebf8283 2019-07-24 stsp free(line);
6109 0ebf8283 2019-07-24 stsp line = NULL;
6110 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6111 0ebf8283 2019-07-24 stsp }
6112 0ebf8283 2019-07-24 stsp
6113 0ebf8283 2019-07-24 stsp free(line);
6114 0ebf8283 2019-07-24 stsp free(commit_id);
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 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6120 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6121 bfce7f83 2019-07-27 stsp {
6122 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6123 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6124 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6125 5b87815e 2020-03-05 stsp static char msg[92];
6126 bfce7f83 2019-07-27 stsp char *id_str;
6127 bfce7f83 2019-07-27 stsp
6128 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6129 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6130 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6131 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6132 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6133 5b87815e 2020-03-05 stsp
6134 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6135 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6136 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6137 5b87815e 2020-03-05 stsp if (hle == hle2)
6138 5b87815e 2020-03-05 stsp continue;
6139 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6140 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6141 5b87815e 2020-03-05 stsp continue;
6142 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6143 5b87815e 2020-03-05 stsp if (err)
6144 5b87815e 2020-03-05 stsp return err;
6145 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6146 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6147 5b87815e 2020-03-05 stsp free(id_str);
6148 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6149 5b87815e 2020-03-05 stsp }
6150 5b87815e 2020-03-05 stsp }
6151 bfce7f83 2019-07-27 stsp
6152 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6153 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6154 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6155 bfce7f83 2019-07-27 stsp break;
6156 bfce7f83 2019-07-27 stsp }
6157 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6158 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6159 bfce7f83 2019-07-27 stsp if (err)
6160 bfce7f83 2019-07-27 stsp return err;
6161 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6162 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6163 bfce7f83 2019-07-27 stsp free(id_str);
6164 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6165 bfce7f83 2019-07-27 stsp }
6166 bfce7f83 2019-07-27 stsp }
6167 bfce7f83 2019-07-27 stsp
6168 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6169 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6170 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6171 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6172 bfce7f83 2019-07-27 stsp
6173 bfce7f83 2019-07-27 stsp return NULL;
6174 bfce7f83 2019-07-27 stsp }
6175 bfce7f83 2019-07-27 stsp
6176 bfce7f83 2019-07-27 stsp static const struct got_error *
6177 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6178 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6179 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6180 0ebf8283 2019-07-24 stsp {
6181 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6182 0ebf8283 2019-07-24 stsp char *editor;
6183 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6184 0ebf8283 2019-07-24 stsp
6185 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6186 0ebf8283 2019-07-24 stsp if (err)
6187 0ebf8283 2019-07-24 stsp return err;
6188 0ebf8283 2019-07-24 stsp
6189 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6190 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6191 0ebf8283 2019-07-24 stsp goto done;
6192 0ebf8283 2019-07-24 stsp }
6193 0ebf8283 2019-07-24 stsp
6194 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6195 0ebf8283 2019-07-24 stsp if (f == NULL) {
6196 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6197 0ebf8283 2019-07-24 stsp goto done;
6198 0ebf8283 2019-07-24 stsp }
6199 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6200 bfce7f83 2019-07-27 stsp if (err)
6201 bfce7f83 2019-07-27 stsp goto done;
6202 bfce7f83 2019-07-27 stsp
6203 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6204 0ebf8283 2019-07-24 stsp done:
6205 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6206 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6207 0ebf8283 2019-07-24 stsp free(editor);
6208 0ebf8283 2019-07-24 stsp return err;
6209 0ebf8283 2019-07-24 stsp }
6210 0ebf8283 2019-07-24 stsp
6211 0ebf8283 2019-07-24 stsp static const struct got_error *
6212 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6213 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6214 514f2ffe 2020-01-29 stsp struct got_repository *);
6215 0ebf8283 2019-07-24 stsp
6216 0ebf8283 2019-07-24 stsp static const struct got_error *
6217 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6218 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6219 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6220 0ebf8283 2019-07-24 stsp {
6221 0ebf8283 2019-07-24 stsp const struct got_error *err;
6222 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6223 0ebf8283 2019-07-24 stsp char *path = NULL;
6224 0ebf8283 2019-07-24 stsp
6225 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6226 0ebf8283 2019-07-24 stsp if (err)
6227 0ebf8283 2019-07-24 stsp return err;
6228 0ebf8283 2019-07-24 stsp
6229 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6230 0ebf8283 2019-07-24 stsp if (err)
6231 0ebf8283 2019-07-24 stsp goto done;
6232 0ebf8283 2019-07-24 stsp
6233 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6234 0ebf8283 2019-07-24 stsp if (err)
6235 0ebf8283 2019-07-24 stsp goto done;
6236 0ebf8283 2019-07-24 stsp
6237 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6238 083957f4 2020-02-24 stsp rewind(f);
6239 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6240 083957f4 2020-02-24 stsp } else {
6241 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6242 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6243 0ebf8283 2019-07-24 stsp goto done;
6244 083957f4 2020-02-24 stsp }
6245 083957f4 2020-02-24 stsp f = NULL;
6246 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6247 083957f4 2020-02-24 stsp if (err) {
6248 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6249 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6250 083957f4 2020-02-24 stsp goto done;
6251 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6252 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6253 083957f4 2020-02-24 stsp }
6254 0ebf8283 2019-07-24 stsp }
6255 0ebf8283 2019-07-24 stsp done:
6256 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6257 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6258 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6259 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6260 0ebf8283 2019-07-24 stsp free(path);
6261 0ebf8283 2019-07-24 stsp return err;
6262 0ebf8283 2019-07-24 stsp }
6263 0ebf8283 2019-07-24 stsp
6264 0ebf8283 2019-07-24 stsp static const struct got_error *
6265 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6266 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6267 0ebf8283 2019-07-24 stsp {
6268 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6269 0ebf8283 2019-07-24 stsp char *path = NULL;
6270 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6271 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6272 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6273 0ebf8283 2019-07-24 stsp
6274 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6275 0ebf8283 2019-07-24 stsp if (err)
6276 0ebf8283 2019-07-24 stsp return err;
6277 0ebf8283 2019-07-24 stsp
6278 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6279 0ebf8283 2019-07-24 stsp if (f == NULL) {
6280 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6281 0ebf8283 2019-07-24 stsp goto done;
6282 0ebf8283 2019-07-24 stsp }
6283 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6284 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6285 0ebf8283 2019-07-24 stsp repo);
6286 0ebf8283 2019-07-24 stsp if (err)
6287 0ebf8283 2019-07-24 stsp break;
6288 0ebf8283 2019-07-24 stsp
6289 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6290 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6291 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6292 0ebf8283 2019-07-24 stsp if (n < 0) {
6293 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6294 0ebf8283 2019-07-24 stsp break;
6295 0ebf8283 2019-07-24 stsp }
6296 0ebf8283 2019-07-24 stsp }
6297 0ebf8283 2019-07-24 stsp }
6298 0ebf8283 2019-07-24 stsp done:
6299 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6300 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6301 0ebf8283 2019-07-24 stsp free(path);
6302 0ebf8283 2019-07-24 stsp if (commit)
6303 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6304 0ebf8283 2019-07-24 stsp return err;
6305 0ebf8283 2019-07-24 stsp }
6306 0ebf8283 2019-07-24 stsp
6307 bfce7f83 2019-07-27 stsp void
6308 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6309 bfce7f83 2019-07-27 stsp {
6310 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6311 bfce7f83 2019-07-27 stsp
6312 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6313 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6314 bfce7f83 2019-07-27 stsp free(hle);
6315 bfce7f83 2019-07-27 stsp }
6316 bfce7f83 2019-07-27 stsp }
6317 bfce7f83 2019-07-27 stsp
6318 0ebf8283 2019-07-24 stsp static const struct got_error *
6319 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6320 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6321 0ebf8283 2019-07-24 stsp {
6322 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6323 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6324 0ebf8283 2019-07-24 stsp
6325 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6326 0ebf8283 2019-07-24 stsp if (f == NULL) {
6327 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6328 0ebf8283 2019-07-24 stsp goto done;
6329 0ebf8283 2019-07-24 stsp }
6330 0ebf8283 2019-07-24 stsp
6331 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6332 0ebf8283 2019-07-24 stsp done:
6333 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6334 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6335 0ebf8283 2019-07-24 stsp return err;
6336 0ebf8283 2019-07-24 stsp }
6337 0ebf8283 2019-07-24 stsp
6338 0ebf8283 2019-07-24 stsp static const struct got_error *
6339 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6340 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6341 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6342 0ebf8283 2019-07-24 stsp {
6343 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6344 0ebf8283 2019-07-24 stsp int resp = ' ';
6345 0ebf8283 2019-07-24 stsp
6346 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6347 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6348 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6349 0ebf8283 2019-07-24 stsp resp = getchar();
6350 a61a4414 2019-08-07 stsp if (resp == '\n')
6351 a61a4414 2019-08-07 stsp resp = getchar();
6352 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6353 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6354 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6355 bfce7f83 2019-07-27 stsp repo);
6356 426ebf2e 2019-07-25 stsp if (err) {
6357 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6358 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6359 426ebf2e 2019-07-25 stsp break;
6360 bfce7f83 2019-07-27 stsp prev_err = err;
6361 426ebf2e 2019-07-25 stsp resp = ' ';
6362 426ebf2e 2019-07-25 stsp continue;
6363 426ebf2e 2019-07-25 stsp }
6364 bfce7f83 2019-07-27 stsp break;
6365 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6366 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6367 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6368 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6369 426ebf2e 2019-07-25 stsp if (err) {
6370 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6371 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6372 426ebf2e 2019-07-25 stsp break;
6373 bfce7f83 2019-07-27 stsp prev_err = err;
6374 426ebf2e 2019-07-25 stsp resp = ' ';
6375 426ebf2e 2019-07-25 stsp continue;
6376 426ebf2e 2019-07-25 stsp }
6377 bfce7f83 2019-07-27 stsp break;
6378 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6379 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6380 0ebf8283 2019-07-24 stsp break;
6381 426ebf2e 2019-07-25 stsp } else
6382 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6383 0ebf8283 2019-07-24 stsp }
6384 0ebf8283 2019-07-24 stsp
6385 0ebf8283 2019-07-24 stsp return err;
6386 0ebf8283 2019-07-24 stsp }
6387 0ebf8283 2019-07-24 stsp
6388 0ebf8283 2019-07-24 stsp static const struct got_error *
6389 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6390 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6391 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6392 0ebf8283 2019-07-24 stsp {
6393 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6394 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6395 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6396 3e3a69f1 2019-07-25 stsp branch, repo);
6397 0ebf8283 2019-07-24 stsp }
6398 0ebf8283 2019-07-24 stsp
6399 0ebf8283 2019-07-24 stsp static const struct got_error *
6400 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6401 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6402 0ebf8283 2019-07-24 stsp {
6403 0ebf8283 2019-07-24 stsp const struct got_error *err;
6404 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6405 0ebf8283 2019-07-24 stsp
6406 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6407 0ebf8283 2019-07-24 stsp if (err)
6408 0ebf8283 2019-07-24 stsp goto done;
6409 0ebf8283 2019-07-24 stsp
6410 0ebf8283 2019-07-24 stsp if (new_id) {
6411 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6412 0ebf8283 2019-07-24 stsp if (err)
6413 0ebf8283 2019-07-24 stsp goto done;
6414 0ebf8283 2019-07-24 stsp }
6415 0ebf8283 2019-07-24 stsp
6416 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6417 0ebf8283 2019-07-24 stsp if (new_id_str)
6418 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6419 0ebf8283 2019-07-24 stsp
6420 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6421 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6422 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6423 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6424 0ebf8283 2019-07-24 stsp goto done;
6425 0ebf8283 2019-07-24 stsp }
6426 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6427 0ebf8283 2019-07-24 stsp } else {
6428 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6429 0ebf8283 2019-07-24 stsp if (err)
6430 0ebf8283 2019-07-24 stsp goto done;
6431 0ebf8283 2019-07-24 stsp }
6432 0ebf8283 2019-07-24 stsp
6433 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6434 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6435 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6436 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6437 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6438 0ebf8283 2019-07-24 stsp break;
6439 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6440 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6441 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6442 0ebf8283 2019-07-24 stsp logmsg);
6443 0ebf8283 2019-07-24 stsp break;
6444 0ebf8283 2019-07-24 stsp default:
6445 0ebf8283 2019-07-24 stsp break;
6446 0ebf8283 2019-07-24 stsp }
6447 0ebf8283 2019-07-24 stsp done:
6448 0ebf8283 2019-07-24 stsp free(old_id_str);
6449 0ebf8283 2019-07-24 stsp free(new_id_str);
6450 0ebf8283 2019-07-24 stsp return err;
6451 0ebf8283 2019-07-24 stsp }
6452 0ebf8283 2019-07-24 stsp
6453 0ebf8283 2019-07-24 stsp static const struct got_error *
6454 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6455 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6456 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6457 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6458 0ebf8283 2019-07-24 stsp {
6459 0ebf8283 2019-07-24 stsp const struct got_error *err;
6460 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6461 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6462 0ebf8283 2019-07-24 stsp
6463 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6464 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6465 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6466 0ebf8283 2019-07-24 stsp if (err)
6467 0ebf8283 2019-07-24 stsp return err;
6468 0ebf8283 2019-07-24 stsp }
6469 0ebf8283 2019-07-24 stsp
6470 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6471 0ebf8283 2019-07-24 stsp if (err)
6472 0ebf8283 2019-07-24 stsp return err;
6473 0ebf8283 2019-07-24 stsp
6474 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6475 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6476 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6477 0ebf8283 2019-07-24 stsp if (err) {
6478 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6479 0ebf8283 2019-07-24 stsp goto done;
6480 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6481 0ebf8283 2019-07-24 stsp } else {
6482 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6483 0ebf8283 2019-07-24 stsp free(new_commit_id);
6484 0ebf8283 2019-07-24 stsp }
6485 0ebf8283 2019-07-24 stsp done:
6486 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6487 0ebf8283 2019-07-24 stsp return err;
6488 0ebf8283 2019-07-24 stsp }
6489 0ebf8283 2019-07-24 stsp
6490 0ebf8283 2019-07-24 stsp static const struct got_error *
6491 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6492 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6493 0ebf8283 2019-07-24 stsp {
6494 0ebf8283 2019-07-24 stsp const struct got_error *error;
6495 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6496 0ebf8283 2019-07-24 stsp
6497 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6498 0ebf8283 2019-07-24 stsp repo);
6499 0ebf8283 2019-07-24 stsp if (error)
6500 0ebf8283 2019-07-24 stsp return error;
6501 0ebf8283 2019-07-24 stsp
6502 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6503 0ebf8283 2019-07-24 stsp if (error)
6504 0ebf8283 2019-07-24 stsp return error;
6505 0ebf8283 2019-07-24 stsp
6506 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6507 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6508 0ebf8283 2019-07-24 stsp return error;
6509 ab20a43a 2020-01-29 stsp }
6510 ab20a43a 2020-01-29 stsp
6511 ab20a43a 2020-01-29 stsp static const struct got_error *
6512 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6513 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6514 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6515 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6516 ab20a43a 2020-01-29 stsp {
6517 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6518 ab20a43a 2020-01-29 stsp
6519 ab20a43a 2020-01-29 stsp switch (status) {
6520 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6521 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6522 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6523 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6524 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6525 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6526 ab20a43a 2020-01-29 stsp default:
6527 ab20a43a 2020-01-29 stsp break;
6528 ab20a43a 2020-01-29 stsp }
6529 ab20a43a 2020-01-29 stsp
6530 ab20a43a 2020-01-29 stsp switch (staged_status) {
6531 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6532 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6533 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6534 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6535 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6536 ab20a43a 2020-01-29 stsp default:
6537 ab20a43a 2020-01-29 stsp break;
6538 ab20a43a 2020-01-29 stsp }
6539 ab20a43a 2020-01-29 stsp
6540 ab20a43a 2020-01-29 stsp return NULL;
6541 0ebf8283 2019-07-24 stsp }
6542 0ebf8283 2019-07-24 stsp
6543 0ebf8283 2019-07-24 stsp static const struct got_error *
6544 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6545 0ebf8283 2019-07-24 stsp {
6546 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6547 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6548 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6549 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6550 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6551 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6552 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6553 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6554 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6555 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6556 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6557 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6558 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6559 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6560 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6561 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6562 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6563 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6564 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6565 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6566 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6567 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6568 0ebf8283 2019-07-24 stsp
6569 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6570 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6571 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6572 0ebf8283 2019-07-24 stsp
6573 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6574 0ebf8283 2019-07-24 stsp switch (ch) {
6575 0ebf8283 2019-07-24 stsp case 'a':
6576 0ebf8283 2019-07-24 stsp abort_edit = 1;
6577 0ebf8283 2019-07-24 stsp break;
6578 0ebf8283 2019-07-24 stsp case 'c':
6579 0ebf8283 2019-07-24 stsp continue_edit = 1;
6580 0ebf8283 2019-07-24 stsp break;
6581 0ebf8283 2019-07-24 stsp case 'F':
6582 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6583 0ebf8283 2019-07-24 stsp break;
6584 083957f4 2020-02-24 stsp case 'm':
6585 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6586 083957f4 2020-02-24 stsp break;
6587 0ebf8283 2019-07-24 stsp default:
6588 0ebf8283 2019-07-24 stsp usage_histedit();
6589 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6590 0ebf8283 2019-07-24 stsp }
6591 0ebf8283 2019-07-24 stsp }
6592 0ebf8283 2019-07-24 stsp
6593 0ebf8283 2019-07-24 stsp argc -= optind;
6594 0ebf8283 2019-07-24 stsp argv += optind;
6595 0ebf8283 2019-07-24 stsp
6596 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6597 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6598 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6599 0ebf8283 2019-07-24 stsp err(1, "pledge");
6600 0ebf8283 2019-07-24 stsp #endif
6601 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6602 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6603 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6604 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6605 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6606 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6607 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6608 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6609 0ebf8283 2019-07-24 stsp if (argc != 0)
6610 0ebf8283 2019-07-24 stsp usage_histedit();
6611 0ebf8283 2019-07-24 stsp
6612 0ebf8283 2019-07-24 stsp /*
6613 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6614 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6615 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6616 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6617 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6618 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6619 0ebf8283 2019-07-24 stsp */
6620 0ebf8283 2019-07-24 stsp
6621 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6622 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6623 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6624 0ebf8283 2019-07-24 stsp goto done;
6625 0ebf8283 2019-07-24 stsp }
6626 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6627 0ebf8283 2019-07-24 stsp if (error)
6628 0ebf8283 2019-07-24 stsp goto done;
6629 0ebf8283 2019-07-24 stsp
6630 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6631 c9956ddf 2019-09-08 stsp NULL);
6632 0ebf8283 2019-07-24 stsp if (error != NULL)
6633 0ebf8283 2019-07-24 stsp goto done;
6634 0ebf8283 2019-07-24 stsp
6635 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6636 0ebf8283 2019-07-24 stsp if (error)
6637 0ebf8283 2019-07-24 stsp goto done;
6638 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6639 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6640 0ebf8283 2019-07-24 stsp goto done;
6641 0ebf8283 2019-07-24 stsp }
6642 0ebf8283 2019-07-24 stsp
6643 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6644 0ebf8283 2019-07-24 stsp if (error)
6645 0ebf8283 2019-07-24 stsp goto done;
6646 0ebf8283 2019-07-24 stsp
6647 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6648 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6649 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6650 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6651 083957f4 2020-02-24 stsp "before the -m option can be used");
6652 083957f4 2020-02-24 stsp goto done;
6653 083957f4 2020-02-24 stsp }
6654 083957f4 2020-02-24 stsp
6655 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6656 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6657 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6658 3e3a69f1 2019-07-25 stsp worktree, repo);
6659 0ebf8283 2019-07-24 stsp if (error)
6660 0ebf8283 2019-07-24 stsp goto done;
6661 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6662 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6663 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6664 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6665 0ebf8283 2019-07-24 stsp if (error)
6666 0ebf8283 2019-07-24 stsp goto done;
6667 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6668 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6669 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6670 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6671 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6672 0ebf8283 2019-07-24 stsp goto done;
6673 0ebf8283 2019-07-24 stsp }
6674 0ebf8283 2019-07-24 stsp
6675 0ebf8283 2019-07-24 stsp if (continue_edit) {
6676 0ebf8283 2019-07-24 stsp char *path;
6677 0ebf8283 2019-07-24 stsp
6678 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6679 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6680 0ebf8283 2019-07-24 stsp goto done;
6681 0ebf8283 2019-07-24 stsp }
6682 0ebf8283 2019-07-24 stsp
6683 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6684 0ebf8283 2019-07-24 stsp if (error)
6685 0ebf8283 2019-07-24 stsp goto done;
6686 0ebf8283 2019-07-24 stsp
6687 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6688 0ebf8283 2019-07-24 stsp free(path);
6689 0ebf8283 2019-07-24 stsp if (error)
6690 0ebf8283 2019-07-24 stsp goto done;
6691 0ebf8283 2019-07-24 stsp
6692 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6693 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6694 3e3a69f1 2019-07-25 stsp worktree, repo);
6695 0ebf8283 2019-07-24 stsp if (error)
6696 0ebf8283 2019-07-24 stsp goto done;
6697 0ebf8283 2019-07-24 stsp
6698 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6699 0ebf8283 2019-07-24 stsp if (error)
6700 0ebf8283 2019-07-24 stsp goto done;
6701 0ebf8283 2019-07-24 stsp
6702 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6703 0ebf8283 2019-07-24 stsp head_commit_id);
6704 0ebf8283 2019-07-24 stsp if (error)
6705 0ebf8283 2019-07-24 stsp goto done;
6706 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6707 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6708 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6709 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6710 3aac7cf7 2019-07-25 stsp goto done;
6711 3aac7cf7 2019-07-25 stsp }
6712 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6713 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6714 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6715 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6716 0ebf8283 2019-07-24 stsp commit = NULL;
6717 0ebf8283 2019-07-24 stsp if (error)
6718 0ebf8283 2019-07-24 stsp goto done;
6719 0ebf8283 2019-07-24 stsp } else {
6720 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6721 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6722 0ebf8283 2019-07-24 stsp goto done;
6723 0ebf8283 2019-07-24 stsp }
6724 0ebf8283 2019-07-24 stsp
6725 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6726 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6727 0ebf8283 2019-07-24 stsp if (error != NULL)
6728 0ebf8283 2019-07-24 stsp goto done;
6729 0ebf8283 2019-07-24 stsp
6730 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6731 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6732 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6733 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6734 c7d20a3f 2019-07-30 stsp goto done;
6735 c7d20a3f 2019-07-30 stsp }
6736 c7d20a3f 2019-07-30 stsp
6737 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6738 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6739 bfce7f83 2019-07-27 stsp branch = NULL;
6740 0ebf8283 2019-07-24 stsp if (error)
6741 0ebf8283 2019-07-24 stsp goto done;
6742 0ebf8283 2019-07-24 stsp
6743 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6744 0ebf8283 2019-07-24 stsp head_commit_id);
6745 0ebf8283 2019-07-24 stsp if (error)
6746 0ebf8283 2019-07-24 stsp goto done;
6747 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6748 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6749 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6750 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6751 3aac7cf7 2019-07-25 stsp goto done;
6752 3aac7cf7 2019-07-25 stsp }
6753 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6754 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6755 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6756 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6757 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6758 0ebf8283 2019-07-24 stsp commit = NULL;
6759 0ebf8283 2019-07-24 stsp if (error)
6760 0ebf8283 2019-07-24 stsp goto done;
6761 0ebf8283 2019-07-24 stsp
6762 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6763 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6764 c5996fff 2020-01-29 stsp goto done;
6765 c5996fff 2020-01-29 stsp }
6766 c5996fff 2020-01-29 stsp
6767 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6768 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6769 bfce7f83 2019-07-27 stsp if (error)
6770 bfce7f83 2019-07-27 stsp goto done;
6771 bfce7f83 2019-07-27 stsp
6772 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6773 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6774 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6775 6ba2bea2 2019-07-27 stsp if (error) {
6776 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6777 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6778 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6779 0ebf8283 2019-07-24 stsp goto done;
6780 6ba2bea2 2019-07-27 stsp }
6781 0ebf8283 2019-07-24 stsp } else {
6782 514f2ffe 2020-01-29 stsp const char *branch_name;
6783 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6784 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6785 514f2ffe 2020-01-29 stsp branch_name += 11;
6786 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6787 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6788 6ba2bea2 2019-07-27 stsp if (error) {
6789 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6790 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6791 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6792 0ebf8283 2019-07-24 stsp goto done;
6793 6ba2bea2 2019-07-27 stsp }
6794 0ebf8283 2019-07-24 stsp
6795 0ebf8283 2019-07-24 stsp }
6796 0ebf8283 2019-07-24 stsp
6797 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6798 0ebf8283 2019-07-24 stsp repo);
6799 6ba2bea2 2019-07-27 stsp if (error) {
6800 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6801 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6802 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6803 0ebf8283 2019-07-24 stsp goto done;
6804 6ba2bea2 2019-07-27 stsp }
6805 0ebf8283 2019-07-24 stsp
6806 0ebf8283 2019-07-24 stsp }
6807 0ebf8283 2019-07-24 stsp
6808 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6809 4ec14e60 2019-08-28 hiltjo if (error)
6810 0ebf8283 2019-07-24 stsp goto done;
6811 0ebf8283 2019-07-24 stsp
6812 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6813 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6814 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6815 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6816 0ebf8283 2019-07-24 stsp continue;
6817 0ebf8283 2019-07-24 stsp
6818 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6819 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6820 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6821 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6822 0ebf8283 2019-07-24 stsp repo);
6823 ab20a43a 2020-01-29 stsp if (error)
6824 ab20a43a 2020-01-29 stsp goto done;
6825 0ebf8283 2019-07-24 stsp } else {
6826 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6827 ab20a43a 2020-01-29 stsp int have_changes = 0;
6828 ab20a43a 2020-01-29 stsp
6829 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6830 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6831 ab20a43a 2020-01-29 stsp if (error)
6832 ab20a43a 2020-01-29 stsp goto done;
6833 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
6834 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
6835 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
6836 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
6837 ab20a43a 2020-01-29 stsp if (error) {
6838 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
6839 ab20a43a 2020-01-29 stsp goto done;
6840 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
6841 ab20a43a 2020-01-29 stsp goto done;
6842 ab20a43a 2020-01-29 stsp }
6843 ab20a43a 2020-01-29 stsp if (have_changes) {
6844 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
6845 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
6846 ab20a43a 2020-01-29 stsp if (error)
6847 ab20a43a 2020-01-29 stsp goto done;
6848 ab20a43a 2020-01-29 stsp } else {
6849 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
6850 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
6851 ab20a43a 2020-01-29 stsp if (error)
6852 ab20a43a 2020-01-29 stsp goto done;
6853 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
6854 ab20a43a 2020-01-29 stsp hle, NULL);
6855 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
6856 ab20a43a 2020-01-29 stsp commit = NULL;
6857 ab20a43a 2020-01-29 stsp if (error)
6858 ab20a43a 2020-01-29 stsp goto done;
6859 ab20a43a 2020-01-29 stsp }
6860 0ebf8283 2019-07-24 stsp }
6861 0ebf8283 2019-07-24 stsp continue;
6862 0ebf8283 2019-07-24 stsp }
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6865 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6866 0ebf8283 2019-07-24 stsp if (error)
6867 0ebf8283 2019-07-24 stsp goto done;
6868 0ebf8283 2019-07-24 stsp continue;
6869 0ebf8283 2019-07-24 stsp }
6870 0ebf8283 2019-07-24 stsp
6871 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6872 0ebf8283 2019-07-24 stsp hle->commit_id);
6873 0ebf8283 2019-07-24 stsp if (error)
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6876 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6877 0ebf8283 2019-07-24 stsp
6878 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6879 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6880 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6881 0ebf8283 2019-07-24 stsp if (error)
6882 0ebf8283 2019-07-24 stsp goto done;
6883 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6884 0ebf8283 2019-07-24 stsp commit = NULL;
6885 0ebf8283 2019-07-24 stsp
6886 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6887 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
6888 a0ea4fc0 2020-02-28 stsp repo);
6889 a0ea4fc0 2020-02-28 stsp if (error)
6890 a0ea4fc0 2020-02-28 stsp goto done;
6891 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6892 0ebf8283 2019-07-24 stsp break;
6893 0ebf8283 2019-07-24 stsp }
6894 0ebf8283 2019-07-24 stsp
6895 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6896 0ebf8283 2019-07-24 stsp char *id_str;
6897 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6898 0ebf8283 2019-07-24 stsp if (error)
6899 0ebf8283 2019-07-24 stsp goto done;
6900 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6901 0ebf8283 2019-07-24 stsp id_str);
6902 0ebf8283 2019-07-24 stsp free(id_str);
6903 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6904 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6905 3e3a69f1 2019-07-25 stsp fileindex);
6906 0ebf8283 2019-07-24 stsp goto done;
6907 0ebf8283 2019-07-24 stsp }
6908 0ebf8283 2019-07-24 stsp
6909 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6910 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6911 0ebf8283 2019-07-24 stsp if (error)
6912 0ebf8283 2019-07-24 stsp goto done;
6913 0ebf8283 2019-07-24 stsp continue;
6914 0ebf8283 2019-07-24 stsp }
6915 0ebf8283 2019-07-24 stsp
6916 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6917 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6918 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6919 0ebf8283 2019-07-24 stsp if (error)
6920 0ebf8283 2019-07-24 stsp goto done;
6921 0ebf8283 2019-07-24 stsp }
6922 0ebf8283 2019-07-24 stsp
6923 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6924 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6925 0ebf8283 2019-07-24 stsp if (error)
6926 0ebf8283 2019-07-24 stsp goto done;
6927 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6928 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
6929 0ebf8283 2019-07-24 stsp } else
6930 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6931 3e3a69f1 2019-07-25 stsp branch, repo);
6932 0ebf8283 2019-07-24 stsp done:
6933 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6934 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6935 0ebf8283 2019-07-24 stsp free(head_commit_id);
6936 0ebf8283 2019-07-24 stsp free(base_commit_id);
6937 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6938 0ebf8283 2019-07-24 stsp if (commit)
6939 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6940 0ebf8283 2019-07-24 stsp if (branch)
6941 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6942 0ebf8283 2019-07-24 stsp if (tmp_branch)
6943 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6944 0ebf8283 2019-07-24 stsp if (worktree)
6945 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6946 2822a352 2019-10-15 stsp if (repo)
6947 2822a352 2019-10-15 stsp got_repo_close(repo);
6948 2822a352 2019-10-15 stsp return error;
6949 2822a352 2019-10-15 stsp }
6950 2822a352 2019-10-15 stsp
6951 2822a352 2019-10-15 stsp __dead static void
6952 2822a352 2019-10-15 stsp usage_integrate(void)
6953 2822a352 2019-10-15 stsp {
6954 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6955 2822a352 2019-10-15 stsp exit(1);
6956 2822a352 2019-10-15 stsp }
6957 2822a352 2019-10-15 stsp
6958 2822a352 2019-10-15 stsp static const struct got_error *
6959 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6960 2822a352 2019-10-15 stsp {
6961 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6962 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6963 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6964 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6965 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6966 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6967 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6968 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6969 2822a352 2019-10-15 stsp int ch, did_something = 0;
6970 2822a352 2019-10-15 stsp
6971 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6972 2822a352 2019-10-15 stsp switch (ch) {
6973 2822a352 2019-10-15 stsp default:
6974 2822a352 2019-10-15 stsp usage_integrate();
6975 2822a352 2019-10-15 stsp /* NOTREACHED */
6976 2822a352 2019-10-15 stsp }
6977 2822a352 2019-10-15 stsp }
6978 2822a352 2019-10-15 stsp
6979 2822a352 2019-10-15 stsp argc -= optind;
6980 2822a352 2019-10-15 stsp argv += optind;
6981 2822a352 2019-10-15 stsp
6982 2822a352 2019-10-15 stsp if (argc != 1)
6983 2822a352 2019-10-15 stsp usage_integrate();
6984 2822a352 2019-10-15 stsp branch_arg = argv[0];
6985 2822a352 2019-10-15 stsp
6986 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6987 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6988 2822a352 2019-10-15 stsp err(1, "pledge");
6989 2822a352 2019-10-15 stsp
6990 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6991 2822a352 2019-10-15 stsp if (cwd == NULL) {
6992 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6993 2822a352 2019-10-15 stsp goto done;
6994 2822a352 2019-10-15 stsp }
6995 2822a352 2019-10-15 stsp
6996 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6997 2822a352 2019-10-15 stsp if (error)
6998 2822a352 2019-10-15 stsp goto done;
6999 2822a352 2019-10-15 stsp
7000 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7001 2822a352 2019-10-15 stsp if (error)
7002 2822a352 2019-10-15 stsp goto done;
7003 2822a352 2019-10-15 stsp
7004 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7005 2822a352 2019-10-15 stsp NULL);
7006 2822a352 2019-10-15 stsp if (error != NULL)
7007 2822a352 2019-10-15 stsp goto done;
7008 2822a352 2019-10-15 stsp
7009 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7010 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7011 2822a352 2019-10-15 stsp if (error)
7012 2822a352 2019-10-15 stsp goto done;
7013 2822a352 2019-10-15 stsp
7014 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7015 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7016 2822a352 2019-10-15 stsp goto done;
7017 2822a352 2019-10-15 stsp }
7018 2822a352 2019-10-15 stsp
7019 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7020 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7021 2822a352 2019-10-15 stsp if (error)
7022 2822a352 2019-10-15 stsp goto done;
7023 2822a352 2019-10-15 stsp
7024 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7025 2822a352 2019-10-15 stsp if (refname == NULL) {
7026 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7027 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7028 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7029 2822a352 2019-10-15 stsp goto done;
7030 2822a352 2019-10-15 stsp }
7031 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7032 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7033 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7034 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7035 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7036 2822a352 2019-10-15 stsp goto done;
7037 2822a352 2019-10-15 stsp }
7038 2822a352 2019-10-15 stsp
7039 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7040 2822a352 2019-10-15 stsp if (error)
7041 2822a352 2019-10-15 stsp goto done;
7042 2822a352 2019-10-15 stsp
7043 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7044 2822a352 2019-10-15 stsp if (error)
7045 2822a352 2019-10-15 stsp goto done;
7046 2822a352 2019-10-15 stsp
7047 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7048 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7049 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7050 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7051 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7052 2822a352 2019-10-15 stsp goto done;
7053 2822a352 2019-10-15 stsp }
7054 2822a352 2019-10-15 stsp
7055 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7056 2822a352 2019-10-15 stsp if (error) {
7057 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7058 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7059 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7060 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7061 2822a352 2019-10-15 stsp goto done;
7062 2822a352 2019-10-15 stsp }
7063 2822a352 2019-10-15 stsp
7064 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7065 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7066 2822a352 2019-10-15 stsp check_cancelled, NULL);
7067 2822a352 2019-10-15 stsp if (error)
7068 2822a352 2019-10-15 stsp goto done;
7069 2822a352 2019-10-15 stsp
7070 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7071 2822a352 2019-10-15 stsp done:
7072 715dc77e 2019-08-03 stsp if (repo)
7073 715dc77e 2019-08-03 stsp got_repo_close(repo);
7074 2822a352 2019-10-15 stsp if (worktree)
7075 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7076 2822a352 2019-10-15 stsp free(cwd);
7077 2822a352 2019-10-15 stsp free(base_commit_id);
7078 2822a352 2019-10-15 stsp free(commit_id);
7079 2822a352 2019-10-15 stsp free(refname);
7080 2822a352 2019-10-15 stsp free(base_refname);
7081 715dc77e 2019-08-03 stsp return error;
7082 715dc77e 2019-08-03 stsp }
7083 715dc77e 2019-08-03 stsp
7084 715dc77e 2019-08-03 stsp __dead static void
7085 715dc77e 2019-08-03 stsp usage_stage(void)
7086 715dc77e 2019-08-03 stsp {
7087 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7088 f3055044 2019-08-07 stsp "[file-path ...]\n",
7089 715dc77e 2019-08-03 stsp getprogname());
7090 715dc77e 2019-08-03 stsp exit(1);
7091 715dc77e 2019-08-03 stsp }
7092 715dc77e 2019-08-03 stsp
7093 715dc77e 2019-08-03 stsp static const struct got_error *
7094 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7095 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7096 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7097 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7098 408b4ebc 2019-08-03 stsp {
7099 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7100 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7101 408b4ebc 2019-08-03 stsp
7102 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7103 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7104 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7105 408b4ebc 2019-08-03 stsp return NULL;
7106 408b4ebc 2019-08-03 stsp
7107 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7108 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7109 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7110 408b4ebc 2019-08-03 stsp else
7111 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7112 408b4ebc 2019-08-03 stsp if (err)
7113 408b4ebc 2019-08-03 stsp return err;
7114 408b4ebc 2019-08-03 stsp
7115 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7116 408b4ebc 2019-08-03 stsp free(id_str);
7117 dc424a06 2019-08-07 stsp return NULL;
7118 dc424a06 2019-08-07 stsp }
7119 2e1f37b0 2019-08-08 stsp
7120 dc424a06 2019-08-07 stsp static const struct got_error *
7121 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7122 715dc77e 2019-08-03 stsp {
7123 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7124 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7125 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7126 715dc77e 2019-08-03 stsp char *cwd = NULL;
7127 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7128 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7129 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7130 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7131 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7132 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7133 715dc77e 2019-08-03 stsp
7134 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7135 715dc77e 2019-08-03 stsp
7136 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7137 715dc77e 2019-08-03 stsp switch (ch) {
7138 408b4ebc 2019-08-03 stsp case 'l':
7139 408b4ebc 2019-08-03 stsp list_stage = 1;
7140 408b4ebc 2019-08-03 stsp break;
7141 dc424a06 2019-08-07 stsp case 'p':
7142 dc424a06 2019-08-07 stsp pflag = 1;
7143 dc424a06 2019-08-07 stsp break;
7144 dc424a06 2019-08-07 stsp case 'F':
7145 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7146 dc424a06 2019-08-07 stsp break;
7147 715dc77e 2019-08-03 stsp default:
7148 715dc77e 2019-08-03 stsp usage_stage();
7149 715dc77e 2019-08-03 stsp /* NOTREACHED */
7150 715dc77e 2019-08-03 stsp }
7151 715dc77e 2019-08-03 stsp }
7152 715dc77e 2019-08-03 stsp
7153 715dc77e 2019-08-03 stsp argc -= optind;
7154 715dc77e 2019-08-03 stsp argv += optind;
7155 715dc77e 2019-08-03 stsp
7156 715dc77e 2019-08-03 stsp #ifndef PROFILE
7157 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7158 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7159 715dc77e 2019-08-03 stsp err(1, "pledge");
7160 715dc77e 2019-08-03 stsp #endif
7161 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7162 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7163 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7164 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7165 715dc77e 2019-08-03 stsp
7166 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7167 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7168 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7169 715dc77e 2019-08-03 stsp goto done;
7170 715dc77e 2019-08-03 stsp }
7171 715dc77e 2019-08-03 stsp
7172 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7173 715dc77e 2019-08-03 stsp if (error)
7174 715dc77e 2019-08-03 stsp goto done;
7175 715dc77e 2019-08-03 stsp
7176 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7177 c9956ddf 2019-09-08 stsp NULL);
7178 715dc77e 2019-08-03 stsp if (error != NULL)
7179 715dc77e 2019-08-03 stsp goto done;
7180 715dc77e 2019-08-03 stsp
7181 dc424a06 2019-08-07 stsp if (patch_script_path) {
7182 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7183 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7184 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7185 dc424a06 2019-08-07 stsp patch_script_path);
7186 dc424a06 2019-08-07 stsp goto done;
7187 dc424a06 2019-08-07 stsp }
7188 dc424a06 2019-08-07 stsp }
7189 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7190 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7191 715dc77e 2019-08-03 stsp if (error)
7192 715dc77e 2019-08-03 stsp goto done;
7193 715dc77e 2019-08-03 stsp
7194 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7195 6d022e97 2019-08-04 stsp if (error)
7196 6d022e97 2019-08-04 stsp goto done;
7197 715dc77e 2019-08-03 stsp
7198 6d022e97 2019-08-04 stsp if (list_stage)
7199 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7200 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7201 2e1f37b0 2019-08-08 stsp else {
7202 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7203 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7204 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7205 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7206 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7207 2e1f37b0 2019-08-08 stsp }
7208 ad493afc 2019-08-03 stsp done:
7209 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7210 2e1f37b0 2019-08-08 stsp error == NULL)
7211 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7212 ad493afc 2019-08-03 stsp if (repo)
7213 ad493afc 2019-08-03 stsp got_repo_close(repo);
7214 ad493afc 2019-08-03 stsp if (worktree)
7215 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7216 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7217 ad493afc 2019-08-03 stsp free((char *)pe->path);
7218 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7219 ad493afc 2019-08-03 stsp free(cwd);
7220 ad493afc 2019-08-03 stsp return error;
7221 ad493afc 2019-08-03 stsp }
7222 ad493afc 2019-08-03 stsp
7223 ad493afc 2019-08-03 stsp __dead static void
7224 ad493afc 2019-08-03 stsp usage_unstage(void)
7225 ad493afc 2019-08-03 stsp {
7226 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7227 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7228 ad493afc 2019-08-03 stsp getprogname());
7229 ad493afc 2019-08-03 stsp exit(1);
7230 ad493afc 2019-08-03 stsp }
7231 ad493afc 2019-08-03 stsp
7232 ad493afc 2019-08-03 stsp
7233 ad493afc 2019-08-03 stsp static const struct got_error *
7234 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7235 ad493afc 2019-08-03 stsp {
7236 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7237 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7238 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7239 ad493afc 2019-08-03 stsp char *cwd = NULL;
7240 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7241 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7242 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7243 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7244 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7245 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7246 ad493afc 2019-08-03 stsp
7247 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7248 ad493afc 2019-08-03 stsp
7249 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7250 ad493afc 2019-08-03 stsp switch (ch) {
7251 2e1f37b0 2019-08-08 stsp case 'p':
7252 2e1f37b0 2019-08-08 stsp pflag = 1;
7253 2e1f37b0 2019-08-08 stsp break;
7254 2e1f37b0 2019-08-08 stsp case 'F':
7255 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7256 2e1f37b0 2019-08-08 stsp break;
7257 ad493afc 2019-08-03 stsp default:
7258 ad493afc 2019-08-03 stsp usage_unstage();
7259 ad493afc 2019-08-03 stsp /* NOTREACHED */
7260 ad493afc 2019-08-03 stsp }
7261 ad493afc 2019-08-03 stsp }
7262 ad493afc 2019-08-03 stsp
7263 ad493afc 2019-08-03 stsp argc -= optind;
7264 ad493afc 2019-08-03 stsp argv += optind;
7265 ad493afc 2019-08-03 stsp
7266 ad493afc 2019-08-03 stsp #ifndef PROFILE
7267 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7268 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7269 ad493afc 2019-08-03 stsp err(1, "pledge");
7270 ad493afc 2019-08-03 stsp #endif
7271 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7272 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7273 2e1f37b0 2019-08-08 stsp
7274 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7275 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7276 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7277 ad493afc 2019-08-03 stsp goto done;
7278 ad493afc 2019-08-03 stsp }
7279 ad493afc 2019-08-03 stsp
7280 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7281 ad493afc 2019-08-03 stsp if (error)
7282 ad493afc 2019-08-03 stsp goto done;
7283 ad493afc 2019-08-03 stsp
7284 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7285 c9956ddf 2019-09-08 stsp NULL);
7286 ad493afc 2019-08-03 stsp if (error != NULL)
7287 ad493afc 2019-08-03 stsp goto done;
7288 ad493afc 2019-08-03 stsp
7289 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7290 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7291 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7292 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7293 2e1f37b0 2019-08-08 stsp patch_script_path);
7294 2e1f37b0 2019-08-08 stsp goto done;
7295 2e1f37b0 2019-08-08 stsp }
7296 2e1f37b0 2019-08-08 stsp }
7297 2e1f37b0 2019-08-08 stsp
7298 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7299 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7300 ad493afc 2019-08-03 stsp if (error)
7301 ad493afc 2019-08-03 stsp goto done;
7302 ad493afc 2019-08-03 stsp
7303 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7304 ad493afc 2019-08-03 stsp if (error)
7305 ad493afc 2019-08-03 stsp goto done;
7306 ad493afc 2019-08-03 stsp
7307 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7308 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7309 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7310 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7311 715dc77e 2019-08-03 stsp done:
7312 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7313 2e1f37b0 2019-08-08 stsp error == NULL)
7314 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7315 0ebf8283 2019-07-24 stsp if (repo)
7316 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7317 715dc77e 2019-08-03 stsp if (worktree)
7318 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7319 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7320 715dc77e 2019-08-03 stsp free((char *)pe->path);
7321 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7322 715dc77e 2019-08-03 stsp free(cwd);
7323 01073a5d 2019-08-22 stsp return error;
7324 01073a5d 2019-08-22 stsp }
7325 01073a5d 2019-08-22 stsp
7326 01073a5d 2019-08-22 stsp __dead static void
7327 01073a5d 2019-08-22 stsp usage_cat(void)
7328 01073a5d 2019-08-22 stsp {
7329 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7330 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7331 01073a5d 2019-08-22 stsp exit(1);
7332 01073a5d 2019-08-22 stsp }
7333 01073a5d 2019-08-22 stsp
7334 01073a5d 2019-08-22 stsp static const struct got_error *
7335 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7336 01073a5d 2019-08-22 stsp {
7337 01073a5d 2019-08-22 stsp const struct got_error *err;
7338 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7339 01073a5d 2019-08-22 stsp
7340 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7341 01073a5d 2019-08-22 stsp if (err)
7342 01073a5d 2019-08-22 stsp return err;
7343 01073a5d 2019-08-22 stsp
7344 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7345 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7346 01073a5d 2019-08-22 stsp return err;
7347 01073a5d 2019-08-22 stsp }
7348 01073a5d 2019-08-22 stsp
7349 01073a5d 2019-08-22 stsp static const struct got_error *
7350 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7351 01073a5d 2019-08-22 stsp {
7352 01073a5d 2019-08-22 stsp const struct got_error *err;
7353 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7354 56e0773d 2019-11-28 stsp int nentries, i;
7355 01073a5d 2019-08-22 stsp
7356 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7357 01073a5d 2019-08-22 stsp if (err)
7358 01073a5d 2019-08-22 stsp return err;
7359 01073a5d 2019-08-22 stsp
7360 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7361 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7362 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7363 01073a5d 2019-08-22 stsp char *id_str;
7364 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7365 01073a5d 2019-08-22 stsp break;
7366 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7367 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7368 01073a5d 2019-08-22 stsp if (err)
7369 01073a5d 2019-08-22 stsp break;
7370 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7371 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7372 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7373 01073a5d 2019-08-22 stsp free(id_str);
7374 01073a5d 2019-08-22 stsp }
7375 01073a5d 2019-08-22 stsp
7376 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7377 01073a5d 2019-08-22 stsp return err;
7378 01073a5d 2019-08-22 stsp }
7379 01073a5d 2019-08-22 stsp
7380 01073a5d 2019-08-22 stsp static const struct got_error *
7381 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7382 01073a5d 2019-08-22 stsp {
7383 01073a5d 2019-08-22 stsp const struct got_error *err;
7384 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7385 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7386 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7387 01073a5d 2019-08-22 stsp char *id_str = NULL;
7388 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7389 01073a5d 2019-08-22 stsp
7390 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7391 01073a5d 2019-08-22 stsp if (err)
7392 01073a5d 2019-08-22 stsp return err;
7393 01073a5d 2019-08-22 stsp
7394 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7395 01073a5d 2019-08-22 stsp if (err)
7396 01073a5d 2019-08-22 stsp goto done;
7397 01073a5d 2019-08-22 stsp
7398 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7399 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7400 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7401 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7402 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7403 01073a5d 2019-08-22 stsp char *pid_str;
7404 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7405 01073a5d 2019-08-22 stsp if (err)
7406 01073a5d 2019-08-22 stsp goto done;
7407 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7408 01073a5d 2019-08-22 stsp free(pid_str);
7409 01073a5d 2019-08-22 stsp }
7410 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7411 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7412 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7413 01073a5d 2019-08-22 stsp
7414 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7415 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7416 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7417 01073a5d 2019-08-22 stsp
7418 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7419 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7420 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7421 01073a5d 2019-08-22 stsp done:
7422 01073a5d 2019-08-22 stsp free(id_str);
7423 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7424 01073a5d 2019-08-22 stsp return err;
7425 01073a5d 2019-08-22 stsp }
7426 01073a5d 2019-08-22 stsp
7427 01073a5d 2019-08-22 stsp static const struct got_error *
7428 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7429 01073a5d 2019-08-22 stsp {
7430 01073a5d 2019-08-22 stsp const struct got_error *err;
7431 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7432 01073a5d 2019-08-22 stsp char *id_str = NULL;
7433 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7434 01073a5d 2019-08-22 stsp
7435 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7436 01073a5d 2019-08-22 stsp if (err)
7437 01073a5d 2019-08-22 stsp return err;
7438 01073a5d 2019-08-22 stsp
7439 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7440 01073a5d 2019-08-22 stsp if (err)
7441 01073a5d 2019-08-22 stsp goto done;
7442 01073a5d 2019-08-22 stsp
7443 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7444 e15d5241 2019-08-22 stsp
7445 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7446 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7447 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7448 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7449 01073a5d 2019-08-22 stsp break;
7450 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7451 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7452 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7453 01073a5d 2019-08-22 stsp break;
7454 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7455 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7456 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7457 01073a5d 2019-08-22 stsp break;
7458 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7459 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7460 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7461 01073a5d 2019-08-22 stsp break;
7462 01073a5d 2019-08-22 stsp default:
7463 01073a5d 2019-08-22 stsp break;
7464 01073a5d 2019-08-22 stsp }
7465 01073a5d 2019-08-22 stsp
7466 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7467 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7468 e15d5241 2019-08-22 stsp
7469 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7470 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7471 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7472 01073a5d 2019-08-22 stsp
7473 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7474 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7475 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7476 01073a5d 2019-08-22 stsp done:
7477 01073a5d 2019-08-22 stsp free(id_str);
7478 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7479 01073a5d 2019-08-22 stsp return err;
7480 01073a5d 2019-08-22 stsp }
7481 01073a5d 2019-08-22 stsp
7482 01073a5d 2019-08-22 stsp static const struct got_error *
7483 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7484 01073a5d 2019-08-22 stsp {
7485 01073a5d 2019-08-22 stsp const struct got_error *error;
7486 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7487 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7488 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7489 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7490 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7491 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7492 01073a5d 2019-08-22 stsp
7493 01073a5d 2019-08-22 stsp #ifndef PROFILE
7494 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7495 01073a5d 2019-08-22 stsp NULL) == -1)
7496 01073a5d 2019-08-22 stsp err(1, "pledge");
7497 01073a5d 2019-08-22 stsp #endif
7498 01073a5d 2019-08-22 stsp
7499 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7500 01073a5d 2019-08-22 stsp switch (ch) {
7501 896e9b6f 2019-08-26 stsp case 'c':
7502 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7503 896e9b6f 2019-08-26 stsp break;
7504 01073a5d 2019-08-22 stsp case 'r':
7505 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7506 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7507 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7508 9ba1d308 2019-10-21 stsp optarg);
7509 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7510 01073a5d 2019-08-22 stsp break;
7511 896e9b6f 2019-08-26 stsp case 'P':
7512 896e9b6f 2019-08-26 stsp force_path = 1;
7513 896e9b6f 2019-08-26 stsp break;
7514 01073a5d 2019-08-22 stsp default:
7515 01073a5d 2019-08-22 stsp usage_cat();
7516 01073a5d 2019-08-22 stsp /* NOTREACHED */
7517 01073a5d 2019-08-22 stsp }
7518 01073a5d 2019-08-22 stsp }
7519 01073a5d 2019-08-22 stsp
7520 01073a5d 2019-08-22 stsp argc -= optind;
7521 01073a5d 2019-08-22 stsp argv += optind;
7522 01073a5d 2019-08-22 stsp
7523 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7524 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7525 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7526 01073a5d 2019-08-22 stsp goto done;
7527 01073a5d 2019-08-22 stsp }
7528 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7529 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7530 01073a5d 2019-08-22 stsp goto done;
7531 01073a5d 2019-08-22 stsp if (worktree) {
7532 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7533 01073a5d 2019-08-22 stsp repo_path = strdup(
7534 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7535 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7536 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7537 01073a5d 2019-08-22 stsp goto done;
7538 01073a5d 2019-08-22 stsp }
7539 01073a5d 2019-08-22 stsp }
7540 01073a5d 2019-08-22 stsp }
7541 01073a5d 2019-08-22 stsp
7542 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7543 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7544 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7545 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7546 01073a5d 2019-08-22 stsp }
7547 01073a5d 2019-08-22 stsp
7548 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7549 01073a5d 2019-08-22 stsp free(repo_path);
7550 01073a5d 2019-08-22 stsp if (error != NULL)
7551 01073a5d 2019-08-22 stsp goto done;
7552 01073a5d 2019-08-22 stsp
7553 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7554 896e9b6f 2019-08-26 stsp if (error)
7555 896e9b6f 2019-08-26 stsp goto done;
7556 896e9b6f 2019-08-26 stsp
7557 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7558 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7559 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7560 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7561 01073a5d 2019-08-22 stsp if (error)
7562 01073a5d 2019-08-22 stsp goto done;
7563 01073a5d 2019-08-22 stsp
7564 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7565 896e9b6f 2019-08-26 stsp if (force_path) {
7566 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7567 896e9b6f 2019-08-26 stsp argv[i]);
7568 896e9b6f 2019-08-26 stsp if (error)
7569 896e9b6f 2019-08-26 stsp break;
7570 896e9b6f 2019-08-26 stsp } else {
7571 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7572 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7573 896e9b6f 2019-08-26 stsp if (error) {
7574 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7575 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7576 896e9b6f 2019-08-26 stsp break;
7577 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7578 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7579 896e9b6f 2019-08-26 stsp if (error)
7580 896e9b6f 2019-08-26 stsp break;
7581 896e9b6f 2019-08-26 stsp }
7582 896e9b6f 2019-08-26 stsp }
7583 01073a5d 2019-08-22 stsp
7584 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7585 01073a5d 2019-08-22 stsp if (error)
7586 01073a5d 2019-08-22 stsp break;
7587 01073a5d 2019-08-22 stsp
7588 01073a5d 2019-08-22 stsp switch (obj_type) {
7589 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7590 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7591 01073a5d 2019-08-22 stsp break;
7592 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7593 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7594 01073a5d 2019-08-22 stsp break;
7595 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7596 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7597 01073a5d 2019-08-22 stsp break;
7598 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7599 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7600 01073a5d 2019-08-22 stsp break;
7601 01073a5d 2019-08-22 stsp default:
7602 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7603 01073a5d 2019-08-22 stsp break;
7604 01073a5d 2019-08-22 stsp }
7605 01073a5d 2019-08-22 stsp if (error)
7606 01073a5d 2019-08-22 stsp break;
7607 01073a5d 2019-08-22 stsp free(label);
7608 01073a5d 2019-08-22 stsp label = NULL;
7609 01073a5d 2019-08-22 stsp free(id);
7610 01073a5d 2019-08-22 stsp id = NULL;
7611 01073a5d 2019-08-22 stsp }
7612 01073a5d 2019-08-22 stsp done:
7613 01073a5d 2019-08-22 stsp free(label);
7614 01073a5d 2019-08-22 stsp free(id);
7615 896e9b6f 2019-08-26 stsp free(commit_id);
7616 01073a5d 2019-08-22 stsp if (worktree)
7617 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7618 01073a5d 2019-08-22 stsp if (repo) {
7619 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7620 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7621 01073a5d 2019-08-22 stsp if (error == NULL)
7622 01073a5d 2019-08-22 stsp error = repo_error;
7623 01073a5d 2019-08-22 stsp }
7624 0ebf8283 2019-07-24 stsp return error;
7625 0ebf8283 2019-07-24 stsp }