Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
23 f42b1b34 2018-03-12 stsp
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 6841bf13 2019-11-29 kn #include <regex.h>
38 5c860e29 2018-03-12 stsp
39 53ccebc2 2019-07-30 stsp #include "got_version.h"
40 f42b1b34 2018-03-12 stsp #include "got_error.h"
41 f42b1b34 2018-03-12 stsp #include "got_object.h"
42 5261c201 2018-04-01 stsp #include "got_reference.h"
43 f42b1b34 2018-03-12 stsp #include "got_repository.h"
44 1dd54920 2019-05-11 stsp #include "got_path.h"
45 e6209546 2019-08-22 stsp #include "got_cancel.h"
46 c09a553d 2018-03-12 stsp #include "got_worktree.h"
47 79109fed 2018-03-27 stsp #include "got_diff.h"
48 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
49 404c43c4 2018-06-21 stsp #include "got_blame.h"
50 63219cd2 2019-01-04 stsp #include "got_privsep.h"
51 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
52 5c860e29 2018-03-12 stsp
53 5c860e29 2018-03-12 stsp #ifndef nitems
54 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 5c860e29 2018-03-12 stsp #endif
56 99437157 2018-11-11 stsp
57 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
58 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
59 99437157 2018-11-11 stsp
60 99437157 2018-11-11 stsp static void
61 99437157 2018-11-11 stsp catch_sigint(int signo)
62 99437157 2018-11-11 stsp {
63 99437157 2018-11-11 stsp sigint_received = 1;
64 99437157 2018-11-11 stsp }
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static void
67 99437157 2018-11-11 stsp catch_sigpipe(int signo)
68 99437157 2018-11-11 stsp {
69 99437157 2018-11-11 stsp sigpipe_received = 1;
70 99437157 2018-11-11 stsp }
71 5c860e29 2018-03-12 stsp
72 99437157 2018-11-11 stsp
73 8cfb4057 2019-07-09 stsp struct got_cmd {
74 5c860e29 2018-03-12 stsp const char *cmd_name;
75 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
77 97b3a7be 2019-07-09 stsp const char *cmd_alias;
78 5c860e29 2018-03-12 stsp };
79 5c860e29 2018-03-12 stsp
80 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
81 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
82 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
84 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
88 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
89 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
90 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
91 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
92 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
93 d00136be 2019-03-26 stsp __dead static void usage_add(void);
94 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
95 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
96 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
97 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
98 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
99 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
100 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
101 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
102 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
103 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
104 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
105 5c860e29 2018-03-12 stsp
106 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
107 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
108 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
109 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
111 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
112 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
113 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
114 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
115 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
116 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
117 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
118 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
119 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
120 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
121 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
122 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
123 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
124 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
125 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
126 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
127 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
128 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
129 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
130 5c860e29 2018-03-12 stsp
131 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
132 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
133 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
134 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
135 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
136 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
137 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
138 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
139 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
140 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
141 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
142 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
143 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
144 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
145 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
146 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
147 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
148 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
149 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
150 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
151 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
152 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
153 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
154 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
155 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
156 5c860e29 2018-03-12 stsp };
157 ce5b7c56 2019-07-09 stsp
158 ce5b7c56 2019-07-09 stsp static void
159 ce5b7c56 2019-07-09 stsp list_commands(void)
160 ce5b7c56 2019-07-09 stsp {
161 ce5b7c56 2019-07-09 stsp int i;
162 ce5b7c56 2019-07-09 stsp
163 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
164 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
165 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
166 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
167 ce5b7c56 2019-07-09 stsp }
168 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
169 ce5b7c56 2019-07-09 stsp }
170 5c860e29 2018-03-12 stsp
171 5c860e29 2018-03-12 stsp int
172 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
173 5c860e29 2018-03-12 stsp {
174 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
175 5c860e29 2018-03-12 stsp unsigned int i;
176 5c860e29 2018-03-12 stsp int ch;
177 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
178 5c860e29 2018-03-12 stsp
179 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
180 5c860e29 2018-03-12 stsp
181 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
182 5c860e29 2018-03-12 stsp switch (ch) {
183 1b6b95a8 2018-03-12 stsp case 'h':
184 1b6b95a8 2018-03-12 stsp hflag = 1;
185 53ccebc2 2019-07-30 stsp break;
186 53ccebc2 2019-07-30 stsp case 'V':
187 53ccebc2 2019-07-30 stsp Vflag = 1;
188 1b6b95a8 2018-03-12 stsp break;
189 5c860e29 2018-03-12 stsp default:
190 ce5b7c56 2019-07-09 stsp usage(hflag);
191 5c860e29 2018-03-12 stsp /* NOTREACHED */
192 5c860e29 2018-03-12 stsp }
193 5c860e29 2018-03-12 stsp }
194 5c860e29 2018-03-12 stsp
195 5c860e29 2018-03-12 stsp argc -= optind;
196 5c860e29 2018-03-12 stsp argv += optind;
197 1e70621d 2018-03-27 stsp optind = 0;
198 53ccebc2 2019-07-30 stsp
199 53ccebc2 2019-07-30 stsp if (Vflag) {
200 53ccebc2 2019-07-30 stsp got_version_print_str();
201 53ccebc2 2019-07-30 stsp return 1;
202 53ccebc2 2019-07-30 stsp }
203 5c860e29 2018-03-12 stsp
204 5c860e29 2018-03-12 stsp if (argc <= 0)
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp
207 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
208 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
209 99437157 2018-11-11 stsp
210 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
211 d7d4f210 2018-03-12 stsp const struct got_error *error;
212 d7d4f210 2018-03-12 stsp
213 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
214 5c860e29 2018-03-12 stsp
215 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
216 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
217 5c860e29 2018-03-12 stsp continue;
218 5c860e29 2018-03-12 stsp
219 1b6b95a8 2018-03-12 stsp if (hflag)
220 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
221 1b6b95a8 2018-03-12 stsp
222 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
223 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
224 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
225 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
226 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
227 70015d7a 2019-11-08 stsp !(sigint_received &&
228 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
229 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
230 d7d4f210 2018-03-12 stsp return 1;
231 d7d4f210 2018-03-12 stsp }
232 d7d4f210 2018-03-12 stsp
233 d7d4f210 2018-03-12 stsp return 0;
234 5c860e29 2018-03-12 stsp }
235 5c860e29 2018-03-12 stsp
236 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
237 ce5b7c56 2019-07-09 stsp list_commands();
238 5c860e29 2018-03-12 stsp return 1;
239 5c860e29 2018-03-12 stsp }
240 5c860e29 2018-03-12 stsp
241 4ed7e80c 2018-05-20 stsp __dead static void
242 ce5b7c56 2019-07-09 stsp usage(int hflag)
243 5c860e29 2018-03-12 stsp {
244 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
245 53ccebc2 2019-07-30 stsp getprogname());
246 ce5b7c56 2019-07-09 stsp if (hflag)
247 ce5b7c56 2019-07-09 stsp list_commands();
248 5c860e29 2018-03-12 stsp exit(1);
249 5c860e29 2018-03-12 stsp }
250 5c860e29 2018-03-12 stsp
251 0266afb7 2019-01-04 stsp static const struct got_error *
252 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
253 e2ba3d07 2019-05-13 stsp {
254 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
255 e2ba3d07 2019-05-13 stsp const char *editor;
256 8920fa04 2019-08-18 stsp
257 8920fa04 2019-08-18 stsp *abspath = NULL;
258 e2ba3d07 2019-05-13 stsp
259 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
260 e2ba3d07 2019-05-13 stsp if (editor == NULL)
261 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
262 e2ba3d07 2019-05-13 stsp
263 0ee7065d 2019-05-13 stsp if (editor) {
264 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
265 0ee7065d 2019-05-13 stsp if (err)
266 0ee7065d 2019-05-13 stsp return err;
267 0ee7065d 2019-05-13 stsp }
268 e2ba3d07 2019-05-13 stsp
269 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
270 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
271 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
272 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
273 0ee7065d 2019-05-13 stsp }
274 0ee7065d 2019-05-13 stsp
275 e2ba3d07 2019-05-13 stsp return NULL;
276 e2ba3d07 2019-05-13 stsp }
277 e2ba3d07 2019-05-13 stsp
278 e2ba3d07 2019-05-13 stsp static const struct got_error *
279 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
280 c530dc23 2019-07-23 stsp const char *worktree_path)
281 0266afb7 2019-01-04 stsp {
282 163ce85a 2019-05-13 stsp const struct got_error *err;
283 0266afb7 2019-01-04 stsp
284 37c06ea4 2019-07-15 stsp #ifdef PROFILE
285 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
286 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
287 37c06ea4 2019-07-15 stsp #endif
288 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
289 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
290 0266afb7 2019-01-04 stsp
291 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
292 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
293 0266afb7 2019-01-04 stsp
294 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
295 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
296 0266afb7 2019-01-04 stsp
297 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
298 163ce85a 2019-05-13 stsp if (err != NULL)
299 163ce85a 2019-05-13 stsp return err;
300 0266afb7 2019-01-04 stsp
301 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
302 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
303 0266afb7 2019-01-04 stsp
304 0266afb7 2019-01-04 stsp return NULL;
305 2c7829a4 2019-06-17 stsp }
306 2c7829a4 2019-06-17 stsp
307 2c7829a4 2019-06-17 stsp __dead static void
308 2c7829a4 2019-06-17 stsp usage_init(void)
309 2c7829a4 2019-06-17 stsp {
310 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
311 2c7829a4 2019-06-17 stsp exit(1);
312 2c7829a4 2019-06-17 stsp }
313 2c7829a4 2019-06-17 stsp
314 2c7829a4 2019-06-17 stsp static const struct got_error *
315 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
316 2c7829a4 2019-06-17 stsp {
317 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
318 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
319 2c7829a4 2019-06-17 stsp int ch;
320 2c7829a4 2019-06-17 stsp
321 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
322 2c7829a4 2019-06-17 stsp switch (ch) {
323 2c7829a4 2019-06-17 stsp default:
324 2c7829a4 2019-06-17 stsp usage_init();
325 2c7829a4 2019-06-17 stsp /* NOTREACHED */
326 2c7829a4 2019-06-17 stsp }
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp
329 2c7829a4 2019-06-17 stsp argc -= optind;
330 2c7829a4 2019-06-17 stsp argv += optind;
331 2c7829a4 2019-06-17 stsp
332 2c7829a4 2019-06-17 stsp #ifndef PROFILE
333 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
334 2c7829a4 2019-06-17 stsp err(1, "pledge");
335 2c7829a4 2019-06-17 stsp #endif
336 2c7829a4 2019-06-17 stsp if (argc != 1)
337 bc20e173 2019-06-17 stsp usage_init();
338 2c7829a4 2019-06-17 stsp
339 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
340 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
341 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
342 2c7829a4 2019-06-17 stsp
343 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
344 2c7829a4 2019-06-17 stsp
345 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
346 2c7829a4 2019-06-17 stsp if (error &&
347 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
348 2c7829a4 2019-06-17 stsp goto done;
349 2c7829a4 2019-06-17 stsp
350 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
351 2c7829a4 2019-06-17 stsp if (error)
352 2c7829a4 2019-06-17 stsp goto done;
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
355 2c7829a4 2019-06-17 stsp if (error != NULL)
356 3ce1b845 2019-07-15 stsp goto done;
357 3ce1b845 2019-07-15 stsp
358 3ce1b845 2019-07-15 stsp done:
359 3ce1b845 2019-07-15 stsp free(repo_path);
360 3ce1b845 2019-07-15 stsp return error;
361 3ce1b845 2019-07-15 stsp }
362 3ce1b845 2019-07-15 stsp
363 3ce1b845 2019-07-15 stsp __dead static void
364 3ce1b845 2019-07-15 stsp usage_import(void)
365 3ce1b845 2019-07-15 stsp {
366 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
367 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
368 3ce1b845 2019-07-15 stsp exit(1);
369 3ce1b845 2019-07-15 stsp }
370 3ce1b845 2019-07-15 stsp
371 3ce1b845 2019-07-15 stsp int
372 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
373 3ce1b845 2019-07-15 stsp {
374 3ce1b845 2019-07-15 stsp pid_t pid;
375 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
376 3ce1b845 2019-07-15 stsp int st = -1;
377 3ce1b845 2019-07-15 stsp
378 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
379 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
380 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
381 3ce1b845 2019-07-15 stsp
382 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
383 3ce1b845 2019-07-15 stsp case -1:
384 3ce1b845 2019-07-15 stsp goto doneediting;
385 3ce1b845 2019-07-15 stsp case 0:
386 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
387 3ce1b845 2019-07-15 stsp _exit(127);
388 3ce1b845 2019-07-15 stsp }
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
391 3ce1b845 2019-07-15 stsp if (errno != EINTR)
392 3ce1b845 2019-07-15 stsp break;
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp doneediting:
395 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
396 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
397 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
400 3ce1b845 2019-07-15 stsp errno = EINTR;
401 3ce1b845 2019-07-15 stsp return -1;
402 3ce1b845 2019-07-15 stsp }
403 3ce1b845 2019-07-15 stsp
404 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
405 3ce1b845 2019-07-15 stsp }
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp static const struct got_error *
408 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
409 3ce1b845 2019-07-15 stsp const char *initial_content)
410 3ce1b845 2019-07-15 stsp {
411 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
412 3ce1b845 2019-07-15 stsp char buf[1024];
413 3ce1b845 2019-07-15 stsp struct stat st, st2;
414 3ce1b845 2019-07-15 stsp FILE *fp;
415 3ce1b845 2019-07-15 stsp int content_changed = 0;
416 3ce1b845 2019-07-15 stsp size_t len;
417 3ce1b845 2019-07-15 stsp
418 3ce1b845 2019-07-15 stsp *logmsg = NULL;
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
421 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
424 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
427 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
430 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
431 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
432 3ce1b845 2019-07-15 stsp
433 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
434 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
435 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
436 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
437 3ce1b845 2019-07-15 stsp len = 0;
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
440 3ce1b845 2019-07-15 stsp if (fp == NULL) {
441 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
442 3ce1b845 2019-07-15 stsp goto done;
443 3ce1b845 2019-07-15 stsp }
444 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
445 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
446 3ce1b845 2019-07-15 stsp content_changed = 1;
447 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
448 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
449 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
450 3ce1b845 2019-07-15 stsp }
451 3ce1b845 2019-07-15 stsp fclose(fp);
452 3ce1b845 2019-07-15 stsp
453 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
454 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
455 3ce1b845 2019-07-15 stsp len--;
456 3ce1b845 2019-07-15 stsp }
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
459 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
461 3ce1b845 2019-07-15 stsp done:
462 3ce1b845 2019-07-15 stsp if (err) {
463 3ce1b845 2019-07-15 stsp free(*logmsg);
464 3ce1b845 2019-07-15 stsp *logmsg = NULL;
465 3ce1b845 2019-07-15 stsp }
466 3ce1b845 2019-07-15 stsp return err;
467 3ce1b845 2019-07-15 stsp }
468 3ce1b845 2019-07-15 stsp
469 3ce1b845 2019-07-15 stsp static const struct got_error *
470 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
471 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
472 3ce1b845 2019-07-15 stsp {
473 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
474 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
475 3ce1b845 2019-07-15 stsp int fd;
476 3ce1b845 2019-07-15 stsp
477 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
478 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
479 3ce1b845 2019-07-15 stsp branch_name) == -1)
480 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
481 3ce1b845 2019-07-15 stsp
482 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
483 3ce1b845 2019-07-15 stsp if (err)
484 3ce1b845 2019-07-15 stsp goto done;
485 3ce1b845 2019-07-15 stsp
486 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
487 3ce1b845 2019-07-15 stsp close(fd);
488 3ce1b845 2019-07-15 stsp
489 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
490 3ce1b845 2019-07-15 stsp done:
491 3ce1b845 2019-07-15 stsp free(initial_content);
492 3ce1b845 2019-07-15 stsp return err;
493 3ce1b845 2019-07-15 stsp }
494 3ce1b845 2019-07-15 stsp
495 3ce1b845 2019-07-15 stsp static const struct got_error *
496 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
497 3ce1b845 2019-07-15 stsp {
498 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
499 3ce1b845 2019-07-15 stsp return NULL;
500 3ce1b845 2019-07-15 stsp }
501 3ce1b845 2019-07-15 stsp
502 3ce1b845 2019-07-15 stsp static const struct got_error *
503 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
504 84792843 2019-08-09 stsp {
505 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
506 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
507 84792843 2019-08-09 stsp
508 84792843 2019-08-09 stsp *author = NULL;
509 aba9c984 2019-09-08 stsp
510 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
511 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
512 c9956ddf 2019-09-08 stsp if (name && email) {
513 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
514 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
515 aba9c984 2019-09-08 stsp return NULL;
516 aba9c984 2019-09-08 stsp }
517 84792843 2019-08-09 stsp
518 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
519 84792843 2019-08-09 stsp if (got_author == NULL) {
520 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
521 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
522 c9956ddf 2019-09-08 stsp if (name && email) {
523 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
524 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
525 c9956ddf 2019-09-08 stsp return NULL;
526 c9956ddf 2019-09-08 stsp }
527 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
528 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
529 84792843 2019-08-09 stsp }
530 84792843 2019-08-09 stsp
531 aba9c984 2019-09-08 stsp *author = strdup(got_author);
532 aba9c984 2019-09-08 stsp if (*author == NULL)
533 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
534 84792843 2019-08-09 stsp
535 84792843 2019-08-09 stsp /*
536 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
537 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
538 84792843 2019-08-09 stsp */
539 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
540 84792843 2019-08-09 stsp got_author++;
541 aba9c984 2019-09-08 stsp if (*got_author != '<') {
542 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
543 aba9c984 2019-09-08 stsp goto done;
544 aba9c984 2019-09-08 stsp }
545 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
546 84792843 2019-08-09 stsp got_author++;
547 aba9c984 2019-09-08 stsp if (*got_author != '@') {
548 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 aba9c984 2019-09-08 stsp goto done;
550 aba9c984 2019-09-08 stsp }
551 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
552 84792843 2019-08-09 stsp got_author++;
553 84792843 2019-08-09 stsp if (*got_author != '>')
554 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 aba9c984 2019-09-08 stsp done:
556 aba9c984 2019-09-08 stsp if (err) {
557 aba9c984 2019-09-08 stsp free(*author);
558 aba9c984 2019-09-08 stsp *author = NULL;
559 aba9c984 2019-09-08 stsp }
560 aba9c984 2019-09-08 stsp return err;
561 c9956ddf 2019-09-08 stsp }
562 c9956ddf 2019-09-08 stsp
563 c9956ddf 2019-09-08 stsp static const struct got_error *
564 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
565 c9956ddf 2019-09-08 stsp {
566 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
567 c9956ddf 2019-09-08 stsp
568 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
569 c9956ddf 2019-09-08 stsp if (homedir) {
570 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
571 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
572 c9956ddf 2019-09-08 stsp
573 c9956ddf 2019-09-08 stsp }
574 c9956ddf 2019-09-08 stsp return NULL;
575 84792843 2019-08-09 stsp }
576 84792843 2019-08-09 stsp
577 84792843 2019-08-09 stsp static const struct got_error *
578 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
579 3ce1b845 2019-07-15 stsp {
580 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
581 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
582 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
583 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
584 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
585 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
586 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
587 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
588 3ce1b845 2019-07-15 stsp int ch;
589 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
590 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
591 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
592 3ce1b845 2019-07-15 stsp
593 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
594 3ce1b845 2019-07-15 stsp
595 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
596 3ce1b845 2019-07-15 stsp switch (ch) {
597 3ce1b845 2019-07-15 stsp case 'b':
598 3ce1b845 2019-07-15 stsp branch_name = optarg;
599 3ce1b845 2019-07-15 stsp break;
600 3ce1b845 2019-07-15 stsp case 'm':
601 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
602 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
603 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
604 3ce1b845 2019-07-15 stsp goto done;
605 3ce1b845 2019-07-15 stsp }
606 3ce1b845 2019-07-15 stsp break;
607 3ce1b845 2019-07-15 stsp case 'r':
608 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
609 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
610 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
611 9ba1d308 2019-10-21 stsp optarg);
612 3ce1b845 2019-07-15 stsp goto done;
613 3ce1b845 2019-07-15 stsp }
614 3ce1b845 2019-07-15 stsp break;
615 3ce1b845 2019-07-15 stsp case 'I':
616 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
617 3ce1b845 2019-07-15 stsp break;
618 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
619 3ce1b845 2019-07-15 stsp NULL);
620 3ce1b845 2019-07-15 stsp if (error)
621 3ce1b845 2019-07-15 stsp goto done;
622 3ce1b845 2019-07-15 stsp break;
623 3ce1b845 2019-07-15 stsp default:
624 b2b65d18 2019-08-22 stsp usage_import();
625 3ce1b845 2019-07-15 stsp /* NOTREACHED */
626 3ce1b845 2019-07-15 stsp }
627 3ce1b845 2019-07-15 stsp }
628 3ce1b845 2019-07-15 stsp
629 3ce1b845 2019-07-15 stsp argc -= optind;
630 3ce1b845 2019-07-15 stsp argv += optind;
631 3ce1b845 2019-07-15 stsp
632 3ce1b845 2019-07-15 stsp #ifndef PROFILE
633 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
634 aba9c984 2019-09-08 stsp "unveil",
635 3ce1b845 2019-07-15 stsp NULL) == -1)
636 3ce1b845 2019-07-15 stsp err(1, "pledge");
637 3ce1b845 2019-07-15 stsp #endif
638 3ce1b845 2019-07-15 stsp if (argc != 1)
639 3ce1b845 2019-07-15 stsp usage_import();
640 2c7829a4 2019-06-17 stsp
641 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
642 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
643 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
644 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
645 3ce1b845 2019-07-15 stsp }
646 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
647 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
648 c9956ddf 2019-09-08 stsp if (error)
649 c9956ddf 2019-09-08 stsp goto done;
650 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
651 3ce1b845 2019-07-15 stsp if (error)
652 3ce1b845 2019-07-15 stsp goto done;
653 aba9c984 2019-09-08 stsp
654 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
655 aba9c984 2019-09-08 stsp if (error)
656 aba9c984 2019-09-08 stsp return error;
657 e560b7e0 2019-11-28 stsp
658 e560b7e0 2019-11-28 stsp /*
659 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
660 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
661 e560b7e0 2019-11-28 stsp * an unintended typo.
662 e560b7e0 2019-11-28 stsp */
663 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
664 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
665 3ce1b845 2019-07-15 stsp
666 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
667 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
668 3ce1b845 2019-07-15 stsp goto done;
669 3ce1b845 2019-07-15 stsp }
670 3ce1b845 2019-07-15 stsp
671 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
672 3ce1b845 2019-07-15 stsp if (error) {
673 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
674 3ce1b845 2019-07-15 stsp goto done;
675 3ce1b845 2019-07-15 stsp } else {
676 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
677 3ce1b845 2019-07-15 stsp "import target branch already exists");
678 3ce1b845 2019-07-15 stsp goto done;
679 3ce1b845 2019-07-15 stsp }
680 3ce1b845 2019-07-15 stsp
681 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
682 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
683 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
684 3ce1b845 2019-07-15 stsp goto done;
685 3ce1b845 2019-07-15 stsp }
686 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
687 3ce1b845 2019-07-15 stsp
688 3ce1b845 2019-07-15 stsp /*
689 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
690 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
691 3ce1b845 2019-07-15 stsp */
692 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
693 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
694 3ce1b845 2019-07-15 stsp if (error)
695 3ce1b845 2019-07-15 stsp goto done;
696 8e158b01 2019-09-22 stsp free(logmsg);
697 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
698 ef293bdd 2019-10-21 stsp path_dir, refname);
699 ef293bdd 2019-10-21 stsp if (error) {
700 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
701 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
702 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
703 3ce1b845 2019-07-15 stsp goto done;
704 ef293bdd 2019-10-21 stsp }
705 3ce1b845 2019-07-15 stsp }
706 3ce1b845 2019-07-15 stsp
707 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
708 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
709 ef293bdd 2019-10-21 stsp if (logmsg_path)
710 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
711 3ce1b845 2019-07-15 stsp goto done;
712 ef293bdd 2019-10-21 stsp }
713 3ce1b845 2019-07-15 stsp
714 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
715 ef293bdd 2019-10-21 stsp if (error) {
716 ef293bdd 2019-10-21 stsp if (logmsg_path)
717 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
718 ef293bdd 2019-10-21 stsp goto done;
719 ef293bdd 2019-10-21 stsp }
720 ef293bdd 2019-10-21 stsp
721 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
722 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
723 ef293bdd 2019-10-21 stsp if (error) {
724 ef293bdd 2019-10-21 stsp if (logmsg_path)
725 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
726 3ce1b845 2019-07-15 stsp goto done;
727 ef293bdd 2019-10-21 stsp }
728 3ce1b845 2019-07-15 stsp
729 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
730 ef293bdd 2019-10-21 stsp if (error) {
731 ef293bdd 2019-10-21 stsp if (logmsg_path)
732 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
733 3ce1b845 2019-07-15 stsp goto done;
734 ef293bdd 2019-10-21 stsp }
735 3ce1b845 2019-07-15 stsp
736 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
737 ef293bdd 2019-10-21 stsp if (error) {
738 ef293bdd 2019-10-21 stsp if (logmsg_path)
739 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
740 3ce1b845 2019-07-15 stsp goto done;
741 ef293bdd 2019-10-21 stsp }
742 3ce1b845 2019-07-15 stsp
743 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
744 ef293bdd 2019-10-21 stsp if (error) {
745 ef293bdd 2019-10-21 stsp if (logmsg_path)
746 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
747 3ce1b845 2019-07-15 stsp goto done;
748 ef293bdd 2019-10-21 stsp }
749 3ce1b845 2019-07-15 stsp
750 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
751 3ce1b845 2019-07-15 stsp if (error) {
752 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
753 ef293bdd 2019-10-21 stsp if (logmsg_path)
754 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
755 3ce1b845 2019-07-15 stsp goto done;
756 ef293bdd 2019-10-21 stsp }
757 3ce1b845 2019-07-15 stsp
758 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
759 3ce1b845 2019-07-15 stsp branch_ref);
760 ef293bdd 2019-10-21 stsp if (error) {
761 ef293bdd 2019-10-21 stsp if (logmsg_path)
762 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
763 3ce1b845 2019-07-15 stsp goto done;
764 ef293bdd 2019-10-21 stsp }
765 3ce1b845 2019-07-15 stsp
766 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
767 ef293bdd 2019-10-21 stsp if (error) {
768 ef293bdd 2019-10-21 stsp if (logmsg_path)
769 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
770 3ce1b845 2019-07-15 stsp goto done;
771 ef293bdd 2019-10-21 stsp }
772 3ce1b845 2019-07-15 stsp }
773 3ce1b845 2019-07-15 stsp
774 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
775 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
776 2c7829a4 2019-06-17 stsp done:
777 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
778 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
779 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
780 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
781 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
782 8e158b01 2019-09-22 stsp free(logmsg);
783 ef293bdd 2019-10-21 stsp free(logmsg_path);
784 2c7829a4 2019-06-17 stsp free(repo_path);
785 3ce1b845 2019-07-15 stsp free(editor);
786 3ce1b845 2019-07-15 stsp free(refname);
787 3ce1b845 2019-07-15 stsp free(new_commit_id);
788 3ce1b845 2019-07-15 stsp free(id_str);
789 aba9c984 2019-09-08 stsp free(author);
790 c9956ddf 2019-09-08 stsp free(gitconfig_path);
791 3ce1b845 2019-07-15 stsp if (branch_ref)
792 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
793 3ce1b845 2019-07-15 stsp if (head_ref)
794 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
795 2c7829a4 2019-06-17 stsp return error;
796 0266afb7 2019-01-04 stsp }
797 0266afb7 2019-01-04 stsp
798 4ed7e80c 2018-05-20 stsp __dead static void
799 c09a553d 2018-03-12 stsp usage_checkout(void)
800 c09a553d 2018-03-12 stsp {
801 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
802 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
803 c09a553d 2018-03-12 stsp exit(1);
804 92a684f4 2018-03-12 stsp }
805 92a684f4 2018-03-12 stsp
806 1ee397ad 2019-07-12 stsp static const struct got_error *
807 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
808 92a684f4 2018-03-12 stsp {
809 92a684f4 2018-03-12 stsp char *worktree_path = arg;
810 a484d721 2019-06-10 stsp
811 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
812 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
813 1ee397ad 2019-07-12 stsp return NULL;
814 92a684f4 2018-03-12 stsp
815 92a684f4 2018-03-12 stsp while (path[0] == '/')
816 92a684f4 2018-03-12 stsp path++;
817 92a684f4 2018-03-12 stsp
818 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
819 1ee397ad 2019-07-12 stsp return NULL;
820 99437157 2018-11-11 stsp }
821 99437157 2018-11-11 stsp
822 99437157 2018-11-11 stsp static const struct got_error *
823 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
824 99437157 2018-11-11 stsp {
825 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
826 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
827 99437157 2018-11-11 stsp return NULL;
828 8069f636 2019-01-12 stsp }
829 8069f636 2019-01-12 stsp
830 8069f636 2019-01-12 stsp static const struct got_error *
831 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
832 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
833 3aef623b 2019-10-15 stsp struct got_repository *repo)
834 8069f636 2019-01-12 stsp {
835 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
836 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
837 8069f636 2019-01-12 stsp
838 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
839 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
840 36a38700 2019-05-10 stsp if (err)
841 36a38700 2019-05-10 stsp return err;
842 8069f636 2019-01-12 stsp
843 d5bea539 2019-05-13 stsp if (yca_id == NULL)
844 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
845 8069f636 2019-01-12 stsp
846 d5bea539 2019-05-13 stsp /*
847 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
848 d5bea539 2019-05-13 stsp * and the work tree's base commit.
849 d5bea539 2019-05-13 stsp *
850 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
851 d5bea539 2019-05-13 stsp *
852 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
853 d5bea539 2019-05-13 stsp * \ /
854 d5bea539 2019-05-13 stsp * C E
855 d5bea539 2019-05-13 stsp * \ /
856 d5bea539 2019-05-13 stsp * B (yca)
857 d5bea539 2019-05-13 stsp * |
858 d5bea539 2019-05-13 stsp * A
859 d5bea539 2019-05-13 stsp *
860 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
861 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
862 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
863 d5bea539 2019-05-13 stsp */
864 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
865 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
866 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
867 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
868 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
869 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
870 8069f636 2019-01-12 stsp
871 d5bea539 2019-05-13 stsp free(yca_id);
872 d5bea539 2019-05-13 stsp return NULL;
873 c09a553d 2018-03-12 stsp }
874 a367ff0f 2019-05-14 stsp
875 a367ff0f 2019-05-14 stsp static const struct got_error *
876 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
877 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
878 a51a74b3 2019-07-27 stsp struct got_repository *repo)
879 a367ff0f 2019-05-14 stsp {
880 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
881 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
882 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
883 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
884 a367ff0f 2019-05-14 stsp
885 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
886 a367ff0f 2019-05-14 stsp if (err)
887 a51a74b3 2019-07-27 stsp goto done;
888 a51a74b3 2019-07-27 stsp
889 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
890 a51a74b3 2019-07-27 stsp is_same_branch = 1;
891 a51a74b3 2019-07-27 stsp goto done;
892 a51a74b3 2019-07-27 stsp }
893 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
894 a51a74b3 2019-07-27 stsp is_same_branch = 1;
895 a367ff0f 2019-05-14 stsp goto done;
896 a51a74b3 2019-07-27 stsp }
897 a367ff0f 2019-05-14 stsp
898 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
899 a367ff0f 2019-05-14 stsp if (err)
900 a367ff0f 2019-05-14 stsp goto done;
901 a367ff0f 2019-05-14 stsp
902 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
903 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
904 a367ff0f 2019-05-14 stsp if (err)
905 a367ff0f 2019-05-14 stsp goto done;
906 a367ff0f 2019-05-14 stsp
907 a367ff0f 2019-05-14 stsp for (;;) {
908 a367ff0f 2019-05-14 stsp struct got_object_id *id;
909 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
910 a367ff0f 2019-05-14 stsp if (err) {
911 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
912 a367ff0f 2019-05-14 stsp err = NULL;
913 a367ff0f 2019-05-14 stsp break;
914 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
915 a367ff0f 2019-05-14 stsp break;
916 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
917 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
918 a367ff0f 2019-05-14 stsp if (err)
919 a367ff0f 2019-05-14 stsp break;
920 a367ff0f 2019-05-14 stsp }
921 c09a553d 2018-03-12 stsp
922 a367ff0f 2019-05-14 stsp if (id) {
923 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
924 a51a74b3 2019-07-27 stsp break;
925 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
926 a367ff0f 2019-05-14 stsp is_same_branch = 1;
927 a367ff0f 2019-05-14 stsp break;
928 a367ff0f 2019-05-14 stsp }
929 a367ff0f 2019-05-14 stsp }
930 a367ff0f 2019-05-14 stsp }
931 a367ff0f 2019-05-14 stsp done:
932 a367ff0f 2019-05-14 stsp if (graph)
933 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
934 a367ff0f 2019-05-14 stsp free(head_commit_id);
935 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
936 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
937 04f57cb3 2019-07-25 stsp return err;
938 04f57cb3 2019-07-25 stsp }
939 04f57cb3 2019-07-25 stsp
940 04f57cb3 2019-07-25 stsp static const struct got_error *
941 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
942 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
943 04f57cb3 2019-07-25 stsp {
944 04f57cb3 2019-07-25 stsp const struct got_error *err;
945 04f57cb3 2019-07-25 stsp struct got_reference *ref;
946 303e2782 2019-08-09 stsp struct got_tag_object *tag;
947 04f57cb3 2019-07-25 stsp
948 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
949 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
950 303e2782 2019-08-09 stsp if (err == NULL) {
951 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
952 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
953 303e2782 2019-08-09 stsp if (*commit_id == NULL)
954 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
955 303e2782 2019-08-09 stsp got_object_tag_close(tag);
956 303e2782 2019-08-09 stsp return err;
957 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
958 303e2782 2019-08-09 stsp return err;
959 303e2782 2019-08-09 stsp
960 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
961 04f57cb3 2019-07-25 stsp if (err == NULL) {
962 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
963 04f57cb3 2019-07-25 stsp got_ref_close(ref);
964 04f57cb3 2019-07-25 stsp } else {
965 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
966 04f57cb3 2019-07-25 stsp return err;
967 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
968 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
969 04f57cb3 2019-07-25 stsp }
970 a367ff0f 2019-05-14 stsp return err;
971 a367ff0f 2019-05-14 stsp }
972 8069f636 2019-01-12 stsp
973 4ed7e80c 2018-05-20 stsp static const struct got_error *
974 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
975 c09a553d 2018-03-12 stsp {
976 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
977 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
978 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
979 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
980 c09a553d 2018-03-12 stsp char *repo_path = NULL;
981 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
982 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
983 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
984 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
985 72151b04 2019-05-11 stsp int ch, same_path_prefix;
986 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
987 f2ea84fa 2019-07-27 stsp
988 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
989 c09a553d 2018-03-12 stsp
990 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
991 0bb8a95e 2018-03-12 stsp switch (ch) {
992 08573d5b 2019-05-14 stsp case 'b':
993 08573d5b 2019-05-14 stsp branch_name = optarg;
994 08573d5b 2019-05-14 stsp break;
995 8069f636 2019-01-12 stsp case 'c':
996 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
997 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
998 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
999 8069f636 2019-01-12 stsp break;
1000 0bb8a95e 2018-03-12 stsp case 'p':
1001 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1002 0bb8a95e 2018-03-12 stsp break;
1003 0bb8a95e 2018-03-12 stsp default:
1004 2deda0b9 2019-03-07 stsp usage_checkout();
1005 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1006 0bb8a95e 2018-03-12 stsp }
1007 0bb8a95e 2018-03-12 stsp }
1008 0bb8a95e 2018-03-12 stsp
1009 0bb8a95e 2018-03-12 stsp argc -= optind;
1010 0bb8a95e 2018-03-12 stsp argv += optind;
1011 0bb8a95e 2018-03-12 stsp
1012 6715a751 2018-03-16 stsp #ifndef PROFILE
1013 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1014 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1015 c09a553d 2018-03-12 stsp err(1, "pledge");
1016 6715a751 2018-03-16 stsp #endif
1017 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1018 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1019 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1020 76089277 2018-04-01 stsp if (repo_path == NULL)
1021 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1022 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1023 76089277 2018-04-01 stsp if (cwd == NULL) {
1024 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1025 76089277 2018-04-01 stsp goto done;
1026 76089277 2018-04-01 stsp }
1027 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1028 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1029 230a42bd 2019-05-11 jcs if (base == NULL) {
1030 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1031 230a42bd 2019-05-11 jcs path_prefix);
1032 230a42bd 2019-05-11 jcs goto done;
1033 230a42bd 2019-05-11 jcs }
1034 230a42bd 2019-05-11 jcs } else {
1035 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1036 230a42bd 2019-05-11 jcs if (base == NULL) {
1037 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1038 230a42bd 2019-05-11 jcs repo_path);
1039 230a42bd 2019-05-11 jcs goto done;
1040 230a42bd 2019-05-11 jcs }
1041 76089277 2018-04-01 stsp }
1042 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1043 c09a553d 2018-03-12 stsp if (dotgit)
1044 c09a553d 2018-03-12 stsp *dotgit = '\0';
1045 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1046 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1047 c09a553d 2018-03-12 stsp free(cwd);
1048 76089277 2018-04-01 stsp goto done;
1049 c09a553d 2018-03-12 stsp }
1050 c09a553d 2018-03-12 stsp free(cwd);
1051 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1052 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1053 76089277 2018-04-01 stsp if (repo_path == NULL) {
1054 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1055 76089277 2018-04-01 stsp goto done;
1056 76089277 2018-04-01 stsp }
1057 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1058 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1059 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1060 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1061 b4b3a7dd 2019-07-22 stsp argv[1]);
1062 b4b3a7dd 2019-07-22 stsp goto done;
1063 b4b3a7dd 2019-07-22 stsp }
1064 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1065 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1066 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1067 b4b3a7dd 2019-07-22 stsp goto done;
1068 b4b3a7dd 2019-07-22 stsp }
1069 76089277 2018-04-01 stsp }
1070 c09a553d 2018-03-12 stsp } else
1071 c09a553d 2018-03-12 stsp usage_checkout();
1072 c09a553d 2018-03-12 stsp
1073 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1074 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1075 13bfb272 2019-05-10 jcs
1076 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1077 c09a553d 2018-03-12 stsp if (error != NULL)
1078 c02c541e 2019-03-29 stsp goto done;
1079 c02c541e 2019-03-29 stsp
1080 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1081 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1082 c530dc23 2019-07-23 stsp if (error) {
1083 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1084 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1085 c530dc23 2019-07-23 stsp goto done;
1086 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
1087 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1088 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1089 c530dc23 2019-07-23 stsp goto done;
1090 c530dc23 2019-07-23 stsp }
1091 c530dc23 2019-07-23 stsp }
1092 c530dc23 2019-07-23 stsp
1093 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1094 c02c541e 2019-03-29 stsp if (error)
1095 c09a553d 2018-03-12 stsp goto done;
1096 8069f636 2019-01-12 stsp
1097 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1098 c09a553d 2018-03-12 stsp if (error != NULL)
1099 c09a553d 2018-03-12 stsp goto done;
1100 c09a553d 2018-03-12 stsp
1101 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1102 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1103 c09a553d 2018-03-12 stsp goto done;
1104 c09a553d 2018-03-12 stsp
1105 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1106 c09a553d 2018-03-12 stsp if (error != NULL)
1107 c09a553d 2018-03-12 stsp goto done;
1108 c09a553d 2018-03-12 stsp
1109 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1110 e5dc7198 2018-12-29 stsp path_prefix);
1111 e5dc7198 2018-12-29 stsp if (error != NULL)
1112 e5dc7198 2018-12-29 stsp goto done;
1113 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1114 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1115 49520a32 2018-12-29 stsp goto done;
1116 49520a32 2018-12-29 stsp }
1117 49520a32 2018-12-29 stsp
1118 8069f636 2019-01-12 stsp if (commit_id_str) {
1119 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1120 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1121 30837e32 2019-07-25 stsp if (error)
1122 8069f636 2019-01-12 stsp goto done;
1123 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1124 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1125 8069f636 2019-01-12 stsp if (error != NULL) {
1126 8069f636 2019-01-12 stsp free(commit_id);
1127 8069f636 2019-01-12 stsp goto done;
1128 8069f636 2019-01-12 stsp }
1129 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1130 45d344f6 2019-05-14 stsp if (error)
1131 45d344f6 2019-05-14 stsp goto done;
1132 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1133 8069f636 2019-01-12 stsp commit_id);
1134 8069f636 2019-01-12 stsp free(commit_id);
1135 8069f636 2019-01-12 stsp if (error)
1136 8069f636 2019-01-12 stsp goto done;
1137 8069f636 2019-01-12 stsp }
1138 8069f636 2019-01-12 stsp
1139 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1140 f2ea84fa 2019-07-27 stsp if (error)
1141 f2ea84fa 2019-07-27 stsp goto done;
1142 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1143 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1144 c09a553d 2018-03-12 stsp if (error != NULL)
1145 c09a553d 2018-03-12 stsp goto done;
1146 c09a553d 2018-03-12 stsp
1147 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1148 c09a553d 2018-03-12 stsp
1149 c09a553d 2018-03-12 stsp done:
1150 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1151 8069f636 2019-01-12 stsp free(commit_id_str);
1152 76089277 2018-04-01 stsp free(repo_path);
1153 507dc3bb 2018-12-29 stsp free(worktree_path);
1154 507dc3bb 2018-12-29 stsp return error;
1155 507dc3bb 2018-12-29 stsp }
1156 507dc3bb 2018-12-29 stsp
1157 507dc3bb 2018-12-29 stsp __dead static void
1158 507dc3bb 2018-12-29 stsp usage_update(void)
1159 507dc3bb 2018-12-29 stsp {
1160 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1161 507dc3bb 2018-12-29 stsp getprogname());
1162 507dc3bb 2018-12-29 stsp exit(1);
1163 507dc3bb 2018-12-29 stsp }
1164 507dc3bb 2018-12-29 stsp
1165 1ee397ad 2019-07-12 stsp static const struct got_error *
1166 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1167 507dc3bb 2018-12-29 stsp {
1168 784955db 2019-01-12 stsp int *did_something = arg;
1169 784955db 2019-01-12 stsp
1170 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1171 1ee397ad 2019-07-12 stsp return NULL;
1172 507dc3bb 2018-12-29 stsp
1173 1545c615 2019-02-10 stsp *did_something = 1;
1174 a484d721 2019-06-10 stsp
1175 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1176 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1177 1ee397ad 2019-07-12 stsp return NULL;
1178 a484d721 2019-06-10 stsp
1179 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1180 507dc3bb 2018-12-29 stsp path++;
1181 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1182 1ee397ad 2019-07-12 stsp return NULL;
1183 be7061eb 2018-12-30 stsp }
1184 be7061eb 2018-12-30 stsp
1185 be7061eb 2018-12-30 stsp static const struct got_error *
1186 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1187 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1188 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1189 a1fb16d8 2019-05-24 stsp {
1190 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1191 a1fb16d8 2019-05-24 stsp char *base_id_str;
1192 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1193 a1fb16d8 2019-05-24 stsp
1194 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1195 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1196 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1197 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1198 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1199 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1200 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1201 a1fb16d8 2019-05-24 stsp }
1202 a1fb16d8 2019-05-24 stsp
1203 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1204 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1205 a1fb16d8 2019-05-24 stsp if (err) {
1206 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1207 a1fb16d8 2019-05-24 stsp return err;
1208 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1209 a1fb16d8 2019-05-24 stsp }
1210 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1211 a1fb16d8 2019-05-24 stsp return NULL;
1212 a1fb16d8 2019-05-24 stsp
1213 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1214 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1215 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1216 a1fb16d8 2019-05-24 stsp if (err)
1217 a1fb16d8 2019-05-24 stsp return err;
1218 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1219 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1220 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1221 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1222 0ebf8283 2019-07-24 stsp return NULL;
1223 0ebf8283 2019-07-24 stsp }
1224 0ebf8283 2019-07-24 stsp
1225 0ebf8283 2019-07-24 stsp static const struct got_error *
1226 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1227 0ebf8283 2019-07-24 stsp {
1228 0ebf8283 2019-07-24 stsp const struct got_error *err;
1229 0ebf8283 2019-07-24 stsp int in_progress;
1230 0ebf8283 2019-07-24 stsp
1231 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1232 0ebf8283 2019-07-24 stsp if (err)
1233 0ebf8283 2019-07-24 stsp return err;
1234 0ebf8283 2019-07-24 stsp if (in_progress)
1235 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1236 0ebf8283 2019-07-24 stsp
1237 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1238 0ebf8283 2019-07-24 stsp if (err)
1239 0ebf8283 2019-07-24 stsp return err;
1240 0ebf8283 2019-07-24 stsp if (in_progress)
1241 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1242 0ebf8283 2019-07-24 stsp
1243 a1fb16d8 2019-05-24 stsp return NULL;
1244 a5edda0a 2019-07-27 stsp }
1245 a5edda0a 2019-07-27 stsp
1246 a5edda0a 2019-07-27 stsp static const struct got_error *
1247 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1248 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1249 a5edda0a 2019-07-27 stsp {
1250 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1251 a5edda0a 2019-07-27 stsp char *path;
1252 a5edda0a 2019-07-27 stsp int i;
1253 a5edda0a 2019-07-27 stsp
1254 a5edda0a 2019-07-27 stsp if (argc == 0) {
1255 a5edda0a 2019-07-27 stsp path = strdup("");
1256 a5edda0a 2019-07-27 stsp if (path == NULL)
1257 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1258 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1259 a5edda0a 2019-07-27 stsp }
1260 a5edda0a 2019-07-27 stsp
1261 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1262 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1263 a5edda0a 2019-07-27 stsp if (err)
1264 a5edda0a 2019-07-27 stsp break;
1265 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1266 a5edda0a 2019-07-27 stsp if (err) {
1267 a5edda0a 2019-07-27 stsp free(path);
1268 a5edda0a 2019-07-27 stsp break;
1269 a5edda0a 2019-07-27 stsp }
1270 a5edda0a 2019-07-27 stsp }
1271 a5edda0a 2019-07-27 stsp
1272 a5edda0a 2019-07-27 stsp return err;
1273 a1fb16d8 2019-05-24 stsp }
1274 a1fb16d8 2019-05-24 stsp
1275 a1fb16d8 2019-05-24 stsp static const struct got_error *
1276 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1277 507dc3bb 2018-12-29 stsp {
1278 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1279 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1280 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1281 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1282 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1283 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1284 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1285 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1286 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1287 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1288 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1289 507dc3bb 2018-12-29 stsp
1290 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1291 f2ea84fa 2019-07-27 stsp
1292 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1293 507dc3bb 2018-12-29 stsp switch (ch) {
1294 024e9686 2019-05-14 stsp case 'b':
1295 024e9686 2019-05-14 stsp branch_name = optarg;
1296 024e9686 2019-05-14 stsp break;
1297 507dc3bb 2018-12-29 stsp case 'c':
1298 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1299 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1300 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1301 507dc3bb 2018-12-29 stsp break;
1302 507dc3bb 2018-12-29 stsp default:
1303 2deda0b9 2019-03-07 stsp usage_update();
1304 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1305 507dc3bb 2018-12-29 stsp }
1306 507dc3bb 2018-12-29 stsp }
1307 507dc3bb 2018-12-29 stsp
1308 507dc3bb 2018-12-29 stsp argc -= optind;
1309 507dc3bb 2018-12-29 stsp argv += optind;
1310 507dc3bb 2018-12-29 stsp
1311 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1312 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1313 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1314 507dc3bb 2018-12-29 stsp err(1, "pledge");
1315 507dc3bb 2018-12-29 stsp #endif
1316 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1317 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1318 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1319 c4cdcb68 2019-04-03 stsp goto done;
1320 c4cdcb68 2019-04-03 stsp }
1321 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1322 7d5807f4 2019-07-11 stsp if (error)
1323 7d5807f4 2019-07-11 stsp goto done;
1324 7d5807f4 2019-07-11 stsp
1325 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1326 f2ea84fa 2019-07-27 stsp if (error)
1327 f2ea84fa 2019-07-27 stsp goto done;
1328 507dc3bb 2018-12-29 stsp
1329 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1330 c9956ddf 2019-09-08 stsp NULL);
1331 507dc3bb 2018-12-29 stsp if (error != NULL)
1332 507dc3bb 2018-12-29 stsp goto done;
1333 507dc3bb 2018-12-29 stsp
1334 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1335 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1336 087fb88c 2019-08-04 stsp if (error)
1337 087fb88c 2019-08-04 stsp goto done;
1338 087fb88c 2019-08-04 stsp
1339 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1340 0266afb7 2019-01-04 stsp if (error)
1341 0266afb7 2019-01-04 stsp goto done;
1342 0266afb7 2019-01-04 stsp
1343 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1344 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1345 024e9686 2019-05-14 stsp if (error != NULL)
1346 024e9686 2019-05-14 stsp goto done;
1347 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1348 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1349 9c4b8182 2019-01-02 stsp if (error != NULL)
1350 9c4b8182 2019-01-02 stsp goto done;
1351 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1352 507dc3bb 2018-12-29 stsp if (error != NULL)
1353 507dc3bb 2018-12-29 stsp goto done;
1354 507dc3bb 2018-12-29 stsp } else {
1355 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1356 04f57cb3 2019-07-25 stsp free(commit_id_str);
1357 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1358 30837e32 2019-07-25 stsp if (error)
1359 a297e751 2019-07-11 stsp goto done;
1360 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1361 a297e751 2019-07-11 stsp if (error)
1362 507dc3bb 2018-12-29 stsp goto done;
1363 507dc3bb 2018-12-29 stsp }
1364 35c965b2 2018-12-29 stsp
1365 a1fb16d8 2019-05-24 stsp if (branch_name) {
1366 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1367 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1368 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1369 f2ea84fa 2019-07-27 stsp continue;
1370 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1371 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1372 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1373 024e9686 2019-05-14 stsp goto done;
1374 024e9686 2019-05-14 stsp }
1375 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1376 024e9686 2019-05-14 stsp if (error)
1377 024e9686 2019-05-14 stsp goto done;
1378 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1379 3aef623b 2019-10-15 stsp repo);
1380 a367ff0f 2019-05-14 stsp free(head_commit_id);
1381 024e9686 2019-05-14 stsp if (error != NULL)
1382 024e9686 2019-05-14 stsp goto done;
1383 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1384 a367ff0f 2019-05-14 stsp if (error)
1385 a367ff0f 2019-05-14 stsp goto done;
1386 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1387 024e9686 2019-05-14 stsp if (error)
1388 024e9686 2019-05-14 stsp goto done;
1389 024e9686 2019-05-14 stsp } else {
1390 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1391 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1392 a367ff0f 2019-05-14 stsp if (error != NULL) {
1393 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1394 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1395 024e9686 2019-05-14 stsp goto done;
1396 a367ff0f 2019-05-14 stsp }
1397 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1398 a367ff0f 2019-05-14 stsp if (error)
1399 a367ff0f 2019-05-14 stsp goto done;
1400 024e9686 2019-05-14 stsp }
1401 507dc3bb 2018-12-29 stsp
1402 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1403 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1404 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1405 507dc3bb 2018-12-29 stsp commit_id);
1406 507dc3bb 2018-12-29 stsp if (error)
1407 507dc3bb 2018-12-29 stsp goto done;
1408 507dc3bb 2018-12-29 stsp }
1409 507dc3bb 2018-12-29 stsp
1410 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1411 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1412 507dc3bb 2018-12-29 stsp if (error != NULL)
1413 507dc3bb 2018-12-29 stsp goto done;
1414 9c4b8182 2019-01-02 stsp
1415 784955db 2019-01-12 stsp if (did_something)
1416 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1417 784955db 2019-01-12 stsp else
1418 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1419 507dc3bb 2018-12-29 stsp done:
1420 c09a553d 2018-03-12 stsp free(worktree_path);
1421 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1422 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1423 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1424 507dc3bb 2018-12-29 stsp free(commit_id);
1425 9c4b8182 2019-01-02 stsp free(commit_id_str);
1426 c09a553d 2018-03-12 stsp return error;
1427 c09a553d 2018-03-12 stsp }
1428 c09a553d 2018-03-12 stsp
1429 f42b1b34 2018-03-12 stsp static const struct got_error *
1430 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1431 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1432 63035f9f 2019-10-06 stsp struct got_repository *repo)
1433 5c860e29 2018-03-12 stsp {
1434 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1435 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1436 79109fed 2018-03-27 stsp
1437 44392932 2019-08-25 stsp if (blob_id1) {
1438 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1439 44392932 2019-08-25 stsp if (err)
1440 44392932 2019-08-25 stsp goto done;
1441 44392932 2019-08-25 stsp }
1442 79109fed 2018-03-27 stsp
1443 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1444 44392932 2019-08-25 stsp if (err)
1445 44392932 2019-08-25 stsp goto done;
1446 79109fed 2018-03-27 stsp
1447 44392932 2019-08-25 stsp while (path[0] == '/')
1448 44392932 2019-08-25 stsp path++;
1449 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1450 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1451 44392932 2019-08-25 stsp done:
1452 44392932 2019-08-25 stsp if (blob1)
1453 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1454 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1455 44392932 2019-08-25 stsp return err;
1456 44392932 2019-08-25 stsp }
1457 44392932 2019-08-25 stsp
1458 44392932 2019-08-25 stsp static const struct got_error *
1459 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1460 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1461 63035f9f 2019-10-06 stsp struct got_repository *repo)
1462 44392932 2019-08-25 stsp {
1463 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1464 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1465 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1466 44392932 2019-08-25 stsp
1467 44392932 2019-08-25 stsp if (tree_id1) {
1468 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1469 79109fed 2018-03-27 stsp if (err)
1470 44392932 2019-08-25 stsp goto done;
1471 79109fed 2018-03-27 stsp }
1472 79109fed 2018-03-27 stsp
1473 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1474 0f2b3dca 2018-12-22 stsp if (err)
1475 0f2b3dca 2018-12-22 stsp goto done;
1476 0f2b3dca 2018-12-22 stsp
1477 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1478 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1479 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1480 44392932 2019-08-25 stsp while (path[0] == '/')
1481 44392932 2019-08-25 stsp path++;
1482 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1483 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1484 0f2b3dca 2018-12-22 stsp done:
1485 79109fed 2018-03-27 stsp if (tree1)
1486 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1487 366e0a5f 2019-10-10 stsp if (tree2)
1488 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1489 44392932 2019-08-25 stsp return err;
1490 44392932 2019-08-25 stsp }
1491 44392932 2019-08-25 stsp
1492 44392932 2019-08-25 stsp static const struct got_error *
1493 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1494 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1495 44392932 2019-08-25 stsp {
1496 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1497 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1498 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1499 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1500 44392932 2019-08-25 stsp struct got_object_qid *qid;
1501 44392932 2019-08-25 stsp
1502 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1503 44392932 2019-08-25 stsp if (qid != NULL) {
1504 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1505 44392932 2019-08-25 stsp qid->id);
1506 44392932 2019-08-25 stsp if (err)
1507 44392932 2019-08-25 stsp return err;
1508 44392932 2019-08-25 stsp }
1509 44392932 2019-08-25 stsp
1510 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1511 44392932 2019-08-25 stsp int obj_type;
1512 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1513 44392932 2019-08-25 stsp if (err)
1514 44392932 2019-08-25 stsp goto done;
1515 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1516 44392932 2019-08-25 stsp if (err) {
1517 44392932 2019-08-25 stsp free(obj_id2);
1518 44392932 2019-08-25 stsp goto done;
1519 44392932 2019-08-25 stsp }
1520 44392932 2019-08-25 stsp if (pcommit) {
1521 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1522 44392932 2019-08-25 stsp qid->id, path);
1523 44392932 2019-08-25 stsp if (err) {
1524 44392932 2019-08-25 stsp free(obj_id2);
1525 44392932 2019-08-25 stsp goto done;
1526 44392932 2019-08-25 stsp }
1527 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1528 44392932 2019-08-25 stsp if (err) {
1529 44392932 2019-08-25 stsp free(obj_id2);
1530 44392932 2019-08-25 stsp goto done;
1531 44392932 2019-08-25 stsp }
1532 44392932 2019-08-25 stsp }
1533 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1534 44392932 2019-08-25 stsp if (err) {
1535 44392932 2019-08-25 stsp free(obj_id2);
1536 44392932 2019-08-25 stsp goto done;
1537 44392932 2019-08-25 stsp }
1538 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1539 44392932 2019-08-25 stsp switch (obj_type) {
1540 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1541 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1542 63035f9f 2019-10-06 stsp 0, repo);
1543 44392932 2019-08-25 stsp break;
1544 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1545 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1546 63035f9f 2019-10-06 stsp 0, repo);
1547 44392932 2019-08-25 stsp break;
1548 44392932 2019-08-25 stsp default:
1549 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1550 44392932 2019-08-25 stsp break;
1551 44392932 2019-08-25 stsp }
1552 44392932 2019-08-25 stsp free(obj_id1);
1553 44392932 2019-08-25 stsp free(obj_id2);
1554 44392932 2019-08-25 stsp } else {
1555 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1556 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1557 44392932 2019-08-25 stsp if (err)
1558 44392932 2019-08-25 stsp goto done;
1559 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1560 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1561 44392932 2019-08-25 stsp if (err)
1562 44392932 2019-08-25 stsp goto done;
1563 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1564 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1565 44392932 2019-08-25 stsp }
1566 44392932 2019-08-25 stsp
1567 44392932 2019-08-25 stsp done:
1568 0f2b3dca 2018-12-22 stsp free(id_str1);
1569 0f2b3dca 2018-12-22 stsp free(id_str2);
1570 44392932 2019-08-25 stsp if (pcommit)
1571 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1572 79109fed 2018-03-27 stsp return err;
1573 79109fed 2018-03-27 stsp }
1574 79109fed 2018-03-27 stsp
1575 4bb494d5 2018-06-16 stsp static char *
1576 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1577 6c281f94 2018-06-11 stsp {
1578 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1579 09867e48 2019-08-13 stsp char *p, *s;
1580 09867e48 2019-08-13 stsp
1581 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1582 09867e48 2019-08-13 stsp if (tm == NULL)
1583 09867e48 2019-08-13 stsp return NULL;
1584 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1585 09867e48 2019-08-13 stsp if (s == NULL)
1586 09867e48 2019-08-13 stsp return NULL;
1587 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1588 6c281f94 2018-06-11 stsp if (p)
1589 6c281f94 2018-06-11 stsp *p = '\0';
1590 6c281f94 2018-06-11 stsp return s;
1591 6c281f94 2018-06-11 stsp }
1592 dc424a06 2019-08-07 stsp
1593 6841bf13 2019-11-29 kn static const struct got_error *
1594 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1595 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1596 6841bf13 2019-11-29 kn {
1597 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1598 6841bf13 2019-11-29 kn regmatch_t regmatch;
1599 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1600 6841bf13 2019-11-29 kn
1601 6841bf13 2019-11-29 kn *have_match = 0;
1602 6841bf13 2019-11-29 kn
1603 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1604 6841bf13 2019-11-29 kn if (err)
1605 6841bf13 2019-11-29 kn return err;
1606 6841bf13 2019-11-29 kn
1607 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1608 6841bf13 2019-11-29 kn if (err)
1609 6841bf13 2019-11-29 kn goto done;
1610 6841bf13 2019-11-29 kn
1611 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1612 6841bf13 2019-11-29 kn *have_match = 1;
1613 6841bf13 2019-11-29 kn done:
1614 6841bf13 2019-11-29 kn free(id_str);
1615 6841bf13 2019-11-29 kn free(logmsg);
1616 6841bf13 2019-11-29 kn return err;
1617 6841bf13 2019-11-29 kn }
1618 6841bf13 2019-11-29 kn
1619 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1620 6c281f94 2018-06-11 stsp
1621 79109fed 2018-03-27 stsp static const struct got_error *
1622 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1623 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1624 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1625 79109fed 2018-03-27 stsp {
1626 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1627 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1628 6c281f94 2018-06-11 stsp char datebuf[26];
1629 45d799e2 2018-12-23 stsp time_t committer_time;
1630 45d799e2 2018-12-23 stsp const char *author, *committer;
1631 199a4027 2019-02-02 stsp char *refs_str = NULL;
1632 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1633 5c860e29 2018-03-12 stsp
1634 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1635 199a4027 2019-02-02 stsp char *s;
1636 199a4027 2019-02-02 stsp const char *name;
1637 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1638 a436ad14 2019-08-13 stsp int cmp;
1639 a436ad14 2019-08-13 stsp
1640 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1641 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1642 d9498b20 2019-02-05 stsp continue;
1643 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1644 199a4027 2019-02-02 stsp name += 5;
1645 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1646 7143d404 2019-03-12 stsp continue;
1647 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1648 e34f9ed6 2019-02-02 stsp name += 6;
1649 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1650 141c2bff 2019-02-04 stsp name += 8;
1651 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1652 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1653 5d844a1e 2019-08-13 stsp if (err) {
1654 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1655 5d844a1e 2019-08-13 stsp return err;
1656 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1657 5d844a1e 2019-08-13 stsp err = NULL;
1658 5d844a1e 2019-08-13 stsp tag = NULL;
1659 5d844a1e 2019-08-13 stsp }
1660 a436ad14 2019-08-13 stsp }
1661 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1662 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1663 a436ad14 2019-08-13 stsp if (tag)
1664 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1665 a436ad14 2019-08-13 stsp if (cmp != 0)
1666 a436ad14 2019-08-13 stsp continue;
1667 199a4027 2019-02-02 stsp s = refs_str;
1668 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1669 199a4027 2019-02-02 stsp name) == -1) {
1670 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1671 199a4027 2019-02-02 stsp free(s);
1672 34ca4898 2019-08-13 stsp return err;
1673 199a4027 2019-02-02 stsp }
1674 199a4027 2019-02-02 stsp free(s);
1675 199a4027 2019-02-02 stsp }
1676 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1677 8bf5b3c9 2018-03-17 stsp if (err)
1678 8bf5b3c9 2018-03-17 stsp return err;
1679 788c352e 2018-06-16 stsp
1680 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1681 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1682 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1683 832c249c 2018-06-10 stsp free(id_str);
1684 d3d493d7 2019-02-21 stsp id_str = NULL;
1685 d3d493d7 2019-02-21 stsp free(refs_str);
1686 d3d493d7 2019-02-21 stsp refs_str = NULL;
1687 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1688 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1689 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1690 09867e48 2019-08-13 stsp if (datestr)
1691 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1692 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1693 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1694 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1695 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1696 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1697 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1698 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1699 3fe1abad 2018-06-10 stsp int n = 1;
1700 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1701 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1702 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1703 a0603db2 2018-06-10 stsp if (err)
1704 a0603db2 2018-06-10 stsp return err;
1705 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1706 a0603db2 2018-06-10 stsp free(id_str);
1707 a0603db2 2018-06-10 stsp }
1708 a0603db2 2018-06-10 stsp }
1709 832c249c 2018-06-10 stsp
1710 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1711 5943eee2 2019-08-13 stsp if (err)
1712 5943eee2 2019-08-13 stsp return err;
1713 8bf5b3c9 2018-03-17 stsp
1714 621015ac 2018-07-23 stsp logmsg = logmsg0;
1715 832c249c 2018-06-10 stsp do {
1716 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1717 832c249c 2018-06-10 stsp if (line)
1718 832c249c 2018-06-10 stsp printf(" %s\n", line);
1719 832c249c 2018-06-10 stsp } while (line);
1720 621015ac 2018-07-23 stsp free(logmsg0);
1721 832c249c 2018-06-10 stsp
1722 971751ac 2018-03-27 stsp if (show_patch) {
1723 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1724 971751ac 2018-03-27 stsp if (err == 0)
1725 971751ac 2018-03-27 stsp printf("\n");
1726 971751ac 2018-03-27 stsp }
1727 07862c20 2018-09-15 stsp
1728 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1729 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1730 07862c20 2018-09-15 stsp return err;
1731 f42b1b34 2018-03-12 stsp }
1732 5c860e29 2018-03-12 stsp
1733 f42b1b34 2018-03-12 stsp static const struct got_error *
1734 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1735 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1736 463a997a 2019-12-08 kn int diff_context, int limit, int first_parent_traversal,
1737 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1738 f42b1b34 2018-03-12 stsp {
1739 f42b1b34 2018-03-12 stsp const struct got_error *err;
1740 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1741 6841bf13 2019-11-29 kn regex_t regex;
1742 6841bf13 2019-11-29 kn int have_match;
1743 372ccdbb 2018-06-10 stsp
1744 6841bf13 2019-11-29 kn if (search_pattern &&
1745 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1746 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1747 6841bf13 2019-11-29 kn
1748 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1749 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1750 f42b1b34 2018-03-12 stsp if (err)
1751 f42b1b34 2018-03-12 stsp return err;
1752 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1753 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1754 372ccdbb 2018-06-10 stsp if (err)
1755 fcc85cad 2018-07-22 stsp goto done;
1756 656b1f76 2019-05-11 jcs for (;;) {
1757 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1758 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1759 8bf5b3c9 2018-03-17 stsp
1760 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1761 84453469 2018-11-11 stsp break;
1762 84453469 2018-11-11 stsp
1763 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1764 372ccdbb 2018-06-10 stsp if (err) {
1765 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1766 9ba79e04 2018-06-11 stsp err = NULL;
1767 9ba79e04 2018-06-11 stsp break;
1768 9ba79e04 2018-06-11 stsp }
1769 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1770 372ccdbb 2018-06-10 stsp break;
1771 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1772 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1773 372ccdbb 2018-06-10 stsp if (err)
1774 372ccdbb 2018-06-10 stsp break;
1775 372ccdbb 2018-06-10 stsp else
1776 372ccdbb 2018-06-10 stsp continue;
1777 7e665116 2018-04-02 stsp }
1778 b43fbaa0 2018-06-11 stsp if (id == NULL)
1779 7e665116 2018-04-02 stsp break;
1780 b43fbaa0 2018-06-11 stsp
1781 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1782 b43fbaa0 2018-06-11 stsp if (err)
1783 fcc85cad 2018-07-22 stsp break;
1784 6841bf13 2019-11-29 kn
1785 6841bf13 2019-11-29 kn if (search_pattern) {
1786 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1787 6841bf13 2019-11-29 kn if (err) {
1788 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1789 6841bf13 2019-11-29 kn break;
1790 6841bf13 2019-11-29 kn }
1791 6841bf13 2019-11-29 kn if (have_match == 0) {
1792 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1793 6841bf13 2019-11-29 kn continue;
1794 6841bf13 2019-11-29 kn }
1795 6841bf13 2019-11-29 kn }
1796 6841bf13 2019-11-29 kn
1797 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1798 44392932 2019-08-25 stsp diff_context, refs);
1799 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1800 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1801 7e665116 2018-04-02 stsp break;
1802 31cedeaf 2018-09-15 stsp }
1803 fcc85cad 2018-07-22 stsp done:
1804 6841bf13 2019-11-29 kn if (search_pattern)
1805 6841bf13 2019-11-29 kn regfree(&regex);
1806 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1807 f42b1b34 2018-03-12 stsp return err;
1808 f42b1b34 2018-03-12 stsp }
1809 5c860e29 2018-03-12 stsp
1810 4ed7e80c 2018-05-20 stsp __dead static void
1811 6f3d1eb0 2018-03-12 stsp usage_log(void)
1812 6f3d1eb0 2018-03-12 stsp {
1813 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1814 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1815 6f3d1eb0 2018-03-12 stsp exit(1);
1816 b1ebc001 2019-08-13 stsp }
1817 b1ebc001 2019-08-13 stsp
1818 b1ebc001 2019-08-13 stsp static int
1819 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1820 b1ebc001 2019-08-13 stsp {
1821 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1822 b1ebc001 2019-08-13 stsp long long n;
1823 b1ebc001 2019-08-13 stsp const char *errstr;
1824 b1ebc001 2019-08-13 stsp
1825 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1826 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1827 b1ebc001 2019-08-13 stsp return 0;
1828 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1829 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1830 b1ebc001 2019-08-13 stsp return 0;
1831 b1ebc001 2019-08-13 stsp return n;
1832 6f3d1eb0 2018-03-12 stsp }
1833 6f3d1eb0 2018-03-12 stsp
1834 4ed7e80c 2018-05-20 stsp static const struct got_error *
1835 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1836 f42b1b34 2018-03-12 stsp {
1837 f42b1b34 2018-03-12 stsp const struct got_error *error;
1838 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1839 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1840 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1841 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1842 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1843 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1844 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1845 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1846 64a96a6d 2018-04-01 stsp const char *errstr;
1847 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1848 1b3893a2 2019-03-18 stsp
1849 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1850 5c860e29 2018-03-12 stsp
1851 6715a751 2018-03-16 stsp #ifndef PROFILE
1852 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1853 6098196c 2019-01-04 stsp NULL)
1854 ad242220 2018-09-08 stsp == -1)
1855 f42b1b34 2018-03-12 stsp err(1, "pledge");
1856 6715a751 2018-03-16 stsp #endif
1857 79109fed 2018-03-27 stsp
1858 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1859 b1ebc001 2019-08-13 stsp
1860 6841bf13 2019-11-29 kn while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1861 79109fed 2018-03-27 stsp switch (ch) {
1862 79109fed 2018-03-27 stsp case 'p':
1863 79109fed 2018-03-27 stsp show_patch = 1;
1864 d142fc45 2018-04-01 stsp break;
1865 d142fc45 2018-04-01 stsp case 'c':
1866 d142fc45 2018-04-01 stsp start_commit = optarg;
1867 64a96a6d 2018-04-01 stsp break;
1868 c0cc5c62 2018-10-18 stsp case 'C':
1869 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1870 4a8520aa 2018-10-18 stsp &errstr);
1871 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1872 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1873 c0cc5c62 2018-10-18 stsp break;
1874 64a96a6d 2018-04-01 stsp case 'l':
1875 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1876 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1877 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1878 cc54c501 2019-07-15 stsp break;
1879 cc54c501 2019-07-15 stsp case 'f':
1880 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1881 a0603db2 2018-06-10 stsp break;
1882 04ca23f4 2018-07-16 stsp case 'r':
1883 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1884 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1885 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1886 9ba1d308 2019-10-21 stsp optarg);
1887 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1888 04ca23f4 2018-07-16 stsp break;
1889 6841bf13 2019-11-29 kn case 's':
1890 6841bf13 2019-11-29 kn search_pattern = optarg;
1891 6841bf13 2019-11-29 kn break;
1892 79109fed 2018-03-27 stsp default:
1893 2deda0b9 2019-03-07 stsp usage_log();
1894 79109fed 2018-03-27 stsp /* NOTREACHED */
1895 79109fed 2018-03-27 stsp }
1896 79109fed 2018-03-27 stsp }
1897 79109fed 2018-03-27 stsp
1898 79109fed 2018-03-27 stsp argc -= optind;
1899 79109fed 2018-03-27 stsp argv += optind;
1900 f42b1b34 2018-03-12 stsp
1901 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1902 dc1edbfa 2019-11-29 kn diff_context = 3;
1903 dc1edbfa 2019-11-29 kn else if (!show_patch)
1904 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1905 dc1edbfa 2019-11-29 kn
1906 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1907 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1908 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1909 04ca23f4 2018-07-16 stsp goto done;
1910 04ca23f4 2018-07-16 stsp }
1911 cffc0aa4 2019-02-05 stsp
1912 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1913 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1914 cffc0aa4 2019-02-05 stsp goto done;
1915 cffc0aa4 2019-02-05 stsp error = NULL;
1916 cffc0aa4 2019-02-05 stsp
1917 e7301579 2019-03-18 stsp if (argc == 0) {
1918 cbd1af7a 2019-03-18 stsp path = strdup("");
1919 e7301579 2019-03-18 stsp if (path == NULL) {
1920 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1921 cbd1af7a 2019-03-18 stsp goto done;
1922 e7301579 2019-03-18 stsp }
1923 e7301579 2019-03-18 stsp } else if (argc == 1) {
1924 e7301579 2019-03-18 stsp if (worktree) {
1925 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1926 e7301579 2019-03-18 stsp argv[0]);
1927 e7301579 2019-03-18 stsp if (error)
1928 e7301579 2019-03-18 stsp goto done;
1929 e7301579 2019-03-18 stsp } else {
1930 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1931 e7301579 2019-03-18 stsp if (path == NULL) {
1932 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1933 e7301579 2019-03-18 stsp goto done;
1934 e7301579 2019-03-18 stsp }
1935 e7301579 2019-03-18 stsp }
1936 cbd1af7a 2019-03-18 stsp } else
1937 cbd1af7a 2019-03-18 stsp usage_log();
1938 cbd1af7a 2019-03-18 stsp
1939 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1940 5486daa2 2019-05-11 stsp repo_path = worktree ?
1941 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1942 5486daa2 2019-05-11 stsp }
1943 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1944 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1945 cffc0aa4 2019-02-05 stsp goto done;
1946 04ca23f4 2018-07-16 stsp }
1947 6098196c 2019-01-04 stsp
1948 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1949 d7d4f210 2018-03-12 stsp if (error != NULL)
1950 04ca23f4 2018-07-16 stsp goto done;
1951 f42b1b34 2018-03-12 stsp
1952 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1953 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1954 c02c541e 2019-03-29 stsp if (error)
1955 c02c541e 2019-03-29 stsp goto done;
1956 c02c541e 2019-03-29 stsp
1957 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1958 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1959 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1960 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1961 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1962 3235492e 2018-04-01 stsp if (error != NULL)
1963 3235492e 2018-04-01 stsp return error;
1964 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1965 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1966 3235492e 2018-04-01 stsp if (error != NULL)
1967 3235492e 2018-04-01 stsp return error;
1968 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1969 3235492e 2018-04-01 stsp } else {
1970 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1971 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1972 3235492e 2018-04-01 stsp if (error == NULL) {
1973 1caad61b 2019-02-01 stsp int obj_type;
1974 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1975 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1976 d1f2edc9 2018-06-13 stsp if (error != NULL)
1977 1caad61b 2019-02-01 stsp goto done;
1978 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1979 1caad61b 2019-02-01 stsp if (error != NULL)
1980 1caad61b 2019-02-01 stsp goto done;
1981 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1982 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1983 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1984 1caad61b 2019-02-01 stsp if (error != NULL)
1985 1caad61b 2019-02-01 stsp goto done;
1986 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1987 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1988 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1989 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1990 1caad61b 2019-02-01 stsp goto done;
1991 1caad61b 2019-02-01 stsp }
1992 1caad61b 2019-02-01 stsp free(id);
1993 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1994 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1995 1caad61b 2019-02-01 stsp if (id == NULL)
1996 638f9024 2019-05-13 stsp error = got_error_from_errno(
1997 230a42bd 2019-05-11 jcs "got_object_id_dup");
1998 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1999 1caad61b 2019-02-01 stsp if (error)
2000 1caad61b 2019-02-01 stsp goto done;
2001 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2002 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2003 1caad61b 2019-02-01 stsp goto done;
2004 1caad61b 2019-02-01 stsp }
2005 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2006 d1f2edc9 2018-06-13 stsp if (error != NULL)
2007 1caad61b 2019-02-01 stsp goto done;
2008 d1f2edc9 2018-06-13 stsp }
2009 15a94983 2018-12-23 stsp if (commit == NULL) {
2010 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2011 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2012 d1f2edc9 2018-06-13 stsp if (error != NULL)
2013 d1f2edc9 2018-06-13 stsp return error;
2014 3235492e 2018-04-01 stsp }
2015 3235492e 2018-04-01 stsp }
2016 d7d4f210 2018-03-12 stsp if (error != NULL)
2017 04ca23f4 2018-07-16 stsp goto done;
2018 04ca23f4 2018-07-16 stsp
2019 deeabeae 2019-08-27 stsp if (worktree) {
2020 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2021 deeabeae 2019-08-27 stsp char *p;
2022 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2023 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2024 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2025 deeabeae 2019-08-27 stsp goto done;
2026 deeabeae 2019-08-27 stsp }
2027 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2028 deeabeae 2019-08-27 stsp free(p);
2029 deeabeae 2019-08-27 stsp } else
2030 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2031 04ca23f4 2018-07-16 stsp if (error != NULL)
2032 04ca23f4 2018-07-16 stsp goto done;
2033 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2034 04ca23f4 2018-07-16 stsp free(path);
2035 04ca23f4 2018-07-16 stsp path = in_repo_path;
2036 04ca23f4 2018-07-16 stsp }
2037 199a4027 2019-02-02 stsp
2038 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2039 199a4027 2019-02-02 stsp if (error)
2040 199a4027 2019-02-02 stsp goto done;
2041 04ca23f4 2018-07-16 stsp
2042 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2043 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
2044 04ca23f4 2018-07-16 stsp done:
2045 04ca23f4 2018-07-16 stsp free(path);
2046 04ca23f4 2018-07-16 stsp free(repo_path);
2047 04ca23f4 2018-07-16 stsp free(cwd);
2048 f42b1b34 2018-03-12 stsp free(id);
2049 cffc0aa4 2019-02-05 stsp if (worktree)
2050 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2051 ad242220 2018-09-08 stsp if (repo) {
2052 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2053 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2054 ad242220 2018-09-08 stsp if (error == NULL)
2055 ad242220 2018-09-08 stsp error = repo_error;
2056 ad242220 2018-09-08 stsp }
2057 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2058 8bf5b3c9 2018-03-17 stsp return error;
2059 5c860e29 2018-03-12 stsp }
2060 5c860e29 2018-03-12 stsp
2061 4ed7e80c 2018-05-20 stsp __dead static void
2062 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2063 3f8b7d6a 2018-04-01 stsp {
2064 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2065 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2066 3f8b7d6a 2018-04-01 stsp exit(1);
2067 b00d56cd 2018-04-01 stsp }
2068 b00d56cd 2018-04-01 stsp
2069 b72f483a 2019-02-05 stsp struct print_diff_arg {
2070 b72f483a 2019-02-05 stsp struct got_repository *repo;
2071 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2072 b72f483a 2019-02-05 stsp int diff_context;
2073 3fc0c068 2019-02-10 stsp const char *id_str;
2074 3fc0c068 2019-02-10 stsp int header_shown;
2075 98eaaa12 2019-08-03 stsp int diff_staged;
2076 63035f9f 2019-10-06 stsp int ignore_whitespace;
2077 b72f483a 2019-02-05 stsp };
2078 b72f483a 2019-02-05 stsp
2079 4ed7e80c 2018-05-20 stsp static const struct got_error *
2080 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2081 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2082 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2083 b72f483a 2019-02-05 stsp {
2084 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2085 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2086 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2087 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2088 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2089 b72f483a 2019-02-05 stsp struct stat sb;
2090 b72f483a 2019-02-05 stsp
2091 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2092 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2093 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2094 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2095 98eaaa12 2019-08-03 stsp return NULL;
2096 98eaaa12 2019-08-03 stsp } else {
2097 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2098 98eaaa12 2019-08-03 stsp return NULL;
2099 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2100 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2101 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2102 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2103 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2104 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2105 98eaaa12 2019-08-03 stsp return NULL;
2106 98eaaa12 2019-08-03 stsp }
2107 3fc0c068 2019-02-10 stsp
2108 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2109 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2110 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2111 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2112 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2113 3fc0c068 2019-02-10 stsp }
2114 b72f483a 2019-02-05 stsp
2115 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2116 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2117 98eaaa12 2019-08-03 stsp switch (staged_status) {
2118 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2119 98eaaa12 2019-08-03 stsp label1 = path;
2120 98eaaa12 2019-08-03 stsp label2 = path;
2121 98eaaa12 2019-08-03 stsp break;
2122 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2123 98eaaa12 2019-08-03 stsp label2 = path;
2124 98eaaa12 2019-08-03 stsp break;
2125 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2126 98eaaa12 2019-08-03 stsp label1 = path;
2127 98eaaa12 2019-08-03 stsp break;
2128 98eaaa12 2019-08-03 stsp default:
2129 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2130 98eaaa12 2019-08-03 stsp }
2131 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2132 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2133 63035f9f 2019-10-06 stsp a->repo, stdout);
2134 98eaaa12 2019-08-03 stsp }
2135 98eaaa12 2019-08-03 stsp
2136 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2137 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2138 4ce46740 2019-08-08 stsp char *id_str;
2139 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2140 408b4ebc 2019-08-03 stsp 8192);
2141 4ce46740 2019-08-08 stsp if (err)
2142 4ce46740 2019-08-08 stsp goto done;
2143 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2144 4ce46740 2019-08-08 stsp if (err)
2145 4ce46740 2019-08-08 stsp goto done;
2146 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2147 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2148 4ce46740 2019-08-08 stsp free(id_str);
2149 4ce46740 2019-08-08 stsp goto done;
2150 4ce46740 2019-08-08 stsp }
2151 4ce46740 2019-08-08 stsp free(id_str);
2152 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2153 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2154 4ce46740 2019-08-08 stsp if (err)
2155 4ce46740 2019-08-08 stsp goto done;
2156 4ce46740 2019-08-08 stsp }
2157 d00136be 2019-03-26 stsp
2158 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2159 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2160 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2161 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2162 2ec1f75b 2019-03-26 stsp goto done;
2163 2ec1f75b 2019-03-26 stsp }
2164 b72f483a 2019-02-05 stsp
2165 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
2166 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
2167 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
2168 2ec1f75b 2019-03-26 stsp goto done;
2169 2ec1f75b 2019-03-26 stsp }
2170 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
2171 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
2172 2ec1f75b 2019-03-26 stsp goto done;
2173 2ec1f75b 2019-03-26 stsp }
2174 2ec1f75b 2019-03-26 stsp } else
2175 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2176 b72f483a 2019-02-05 stsp
2177 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2178 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2179 b72f483a 2019-02-05 stsp done:
2180 b72f483a 2019-02-05 stsp if (blob1)
2181 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2182 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
2183 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2184 b72f483a 2019-02-05 stsp free(abspath);
2185 927df6b7 2019-02-10 stsp return err;
2186 927df6b7 2019-02-10 stsp }
2187 927df6b7 2019-02-10 stsp
2188 927df6b7 2019-02-10 stsp static const struct got_error *
2189 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2190 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2191 01073a5d 2019-08-22 stsp struct got_repository *repo)
2192 0adb7196 2019-08-11 stsp {
2193 0adb7196 2019-08-11 stsp const struct got_error *err;
2194 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2195 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2196 0adb7196 2019-08-11 stsp
2197 0adb7196 2019-08-11 stsp *id = NULL;
2198 0adb7196 2019-08-11 stsp *label = NULL;
2199 d24820bf 2019-08-11 stsp
2200 01073a5d 2019-08-22 stsp if (resolve_tags) {
2201 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2202 01073a5d 2019-08-22 stsp repo);
2203 01073a5d 2019-08-22 stsp if (err == NULL) {
2204 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2205 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2206 01073a5d 2019-08-22 stsp if (*id == NULL)
2207 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2208 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2209 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2210 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2211 32f0ab81 2019-08-28 hiltjo free(*id);
2212 01073a5d 2019-08-22 stsp *id = NULL;
2213 01073a5d 2019-08-22 stsp }
2214 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2215 01073a5d 2019-08-22 stsp return err;
2216 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2217 01073a5d 2019-08-22 stsp return err;
2218 01073a5d 2019-08-22 stsp }
2219 0adb7196 2019-08-11 stsp
2220 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2221 0adb7196 2019-08-11 stsp if (err) {
2222 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2223 0adb7196 2019-08-11 stsp return err;
2224 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2225 0adb7196 2019-08-11 stsp if (err != NULL)
2226 0adb7196 2019-08-11 stsp goto done;
2227 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2228 0adb7196 2019-08-11 stsp if (*label == NULL) {
2229 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2230 0adb7196 2019-08-11 stsp goto done;
2231 0adb7196 2019-08-11 stsp }
2232 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2233 0adb7196 2019-08-11 stsp } else {
2234 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2235 0adb7196 2019-08-11 stsp if (*label == NULL) {
2236 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2237 0adb7196 2019-08-11 stsp goto done;
2238 0adb7196 2019-08-11 stsp }
2239 0adb7196 2019-08-11 stsp }
2240 0adb7196 2019-08-11 stsp done:
2241 0adb7196 2019-08-11 stsp if (ref)
2242 0adb7196 2019-08-11 stsp got_ref_close(ref);
2243 0adb7196 2019-08-11 stsp return err;
2244 0adb7196 2019-08-11 stsp }
2245 0adb7196 2019-08-11 stsp
2246 0adb7196 2019-08-11 stsp
2247 0adb7196 2019-08-11 stsp static const struct got_error *
2248 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2249 b00d56cd 2018-04-01 stsp {
2250 b00d56cd 2018-04-01 stsp const struct got_error *error;
2251 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2252 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2253 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2254 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2255 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2256 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2257 15a94983 2018-12-23 stsp int type1, type2;
2258 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2259 c0cc5c62 2018-10-18 stsp const char *errstr;
2260 927df6b7 2019-02-10 stsp char *path = NULL;
2261 b00d56cd 2018-04-01 stsp
2262 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2263 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2264 25eccc22 2019-01-04 stsp NULL) == -1)
2265 b00d56cd 2018-04-01 stsp err(1, "pledge");
2266 b00d56cd 2018-04-01 stsp #endif
2267 b00d56cd 2018-04-01 stsp
2268 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2269 b00d56cd 2018-04-01 stsp switch (ch) {
2270 c0cc5c62 2018-10-18 stsp case 'C':
2271 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2272 dfcab68b 2019-11-29 kn &errstr);
2273 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2274 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2275 c0cc5c62 2018-10-18 stsp break;
2276 b72f483a 2019-02-05 stsp case 'r':
2277 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2278 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2279 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2280 9ba1d308 2019-10-21 stsp optarg);
2281 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2282 b72f483a 2019-02-05 stsp break;
2283 98eaaa12 2019-08-03 stsp case 's':
2284 98eaaa12 2019-08-03 stsp diff_staged = 1;
2285 63035f9f 2019-10-06 stsp break;
2286 63035f9f 2019-10-06 stsp case 'w':
2287 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2288 98eaaa12 2019-08-03 stsp break;
2289 b00d56cd 2018-04-01 stsp default:
2290 2deda0b9 2019-03-07 stsp usage_diff();
2291 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2292 b00d56cd 2018-04-01 stsp }
2293 b00d56cd 2018-04-01 stsp }
2294 b00d56cd 2018-04-01 stsp
2295 b00d56cd 2018-04-01 stsp argc -= optind;
2296 b00d56cd 2018-04-01 stsp argv += optind;
2297 b00d56cd 2018-04-01 stsp
2298 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2299 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2300 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2301 b72f483a 2019-02-05 stsp goto done;
2302 b72f483a 2019-02-05 stsp }
2303 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2304 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2305 927df6b7 2019-02-10 stsp goto done;
2306 927df6b7 2019-02-10 stsp if (argc <= 1) {
2307 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2308 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2309 927df6b7 2019-02-10 stsp goto done;
2310 927df6b7 2019-02-10 stsp }
2311 b72f483a 2019-02-05 stsp if (repo_path)
2312 b72f483a 2019-02-05 stsp errx(1,
2313 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2314 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2315 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2316 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2317 927df6b7 2019-02-10 stsp goto done;
2318 927df6b7 2019-02-10 stsp }
2319 927df6b7 2019-02-10 stsp if (argc == 1) {
2320 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2321 cbd1af7a 2019-03-18 stsp argv[0]);
2322 927df6b7 2019-02-10 stsp if (error)
2323 927df6b7 2019-02-10 stsp goto done;
2324 927df6b7 2019-02-10 stsp } else {
2325 927df6b7 2019-02-10 stsp path = strdup("");
2326 927df6b7 2019-02-10 stsp if (path == NULL) {
2327 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2328 927df6b7 2019-02-10 stsp goto done;
2329 927df6b7 2019-02-10 stsp }
2330 927df6b7 2019-02-10 stsp }
2331 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2332 98eaaa12 2019-08-03 stsp if (diff_staged)
2333 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2334 98eaaa12 2019-08-03 stsp "objects in repository");
2335 15a94983 2018-12-23 stsp id_str1 = argv[0];
2336 15a94983 2018-12-23 stsp id_str2 = argv[1];
2337 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2338 30db809c 2019-06-05 stsp repo_path =
2339 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2340 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2341 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2342 30db809c 2019-06-05 stsp goto done;
2343 30db809c 2019-06-05 stsp }
2344 30db809c 2019-06-05 stsp }
2345 b00d56cd 2018-04-01 stsp } else
2346 b00d56cd 2018-04-01 stsp usage_diff();
2347 25eccc22 2019-01-04 stsp
2348 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2349 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2350 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2351 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2352 b72f483a 2019-02-05 stsp }
2353 b00d56cd 2018-04-01 stsp
2354 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2355 76089277 2018-04-01 stsp free(repo_path);
2356 b00d56cd 2018-04-01 stsp if (error != NULL)
2357 b00d56cd 2018-04-01 stsp goto done;
2358 b00d56cd 2018-04-01 stsp
2359 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2360 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2361 c02c541e 2019-03-29 stsp if (error)
2362 c02c541e 2019-03-29 stsp goto done;
2363 c02c541e 2019-03-29 stsp
2364 30db809c 2019-06-05 stsp if (argc <= 1) {
2365 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2366 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2367 b72f483a 2019-02-05 stsp char *id_str;
2368 72ea6654 2019-07-27 stsp
2369 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2370 72ea6654 2019-07-27 stsp
2371 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2372 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2373 b72f483a 2019-02-05 stsp if (error)
2374 b72f483a 2019-02-05 stsp goto done;
2375 b72f483a 2019-02-05 stsp arg.repo = repo;
2376 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2377 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2378 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2379 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2380 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2381 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2382 b72f483a 2019-02-05 stsp
2383 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2384 72ea6654 2019-07-27 stsp if (error)
2385 72ea6654 2019-07-27 stsp goto done;
2386 72ea6654 2019-07-27 stsp
2387 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2388 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2389 b72f483a 2019-02-05 stsp free(id_str);
2390 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2391 b72f483a 2019-02-05 stsp goto done;
2392 b72f483a 2019-02-05 stsp }
2393 b72f483a 2019-02-05 stsp
2394 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2395 01073a5d 2019-08-22 stsp repo);
2396 0adb7196 2019-08-11 stsp if (error)
2397 0adb7196 2019-08-11 stsp goto done;
2398 b00d56cd 2018-04-01 stsp
2399 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2400 01073a5d 2019-08-22 stsp repo);
2401 0adb7196 2019-08-11 stsp if (error)
2402 0adb7196 2019-08-11 stsp goto done;
2403 b00d56cd 2018-04-01 stsp
2404 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2405 15a94983 2018-12-23 stsp if (error)
2406 15a94983 2018-12-23 stsp goto done;
2407 15a94983 2018-12-23 stsp
2408 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2409 15a94983 2018-12-23 stsp if (error)
2410 15a94983 2018-12-23 stsp goto done;
2411 15a94983 2018-12-23 stsp
2412 15a94983 2018-12-23 stsp if (type1 != type2) {
2413 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2414 b00d56cd 2018-04-01 stsp goto done;
2415 b00d56cd 2018-04-01 stsp }
2416 b00d56cd 2018-04-01 stsp
2417 15a94983 2018-12-23 stsp switch (type1) {
2418 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2419 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2420 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2421 b00d56cd 2018-04-01 stsp break;
2422 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2423 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2424 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2425 b00d56cd 2018-04-01 stsp break;
2426 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2427 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2428 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2429 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2430 b00d56cd 2018-04-01 stsp break;
2431 b00d56cd 2018-04-01 stsp default:
2432 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2433 b00d56cd 2018-04-01 stsp }
2434 b00d56cd 2018-04-01 stsp
2435 b00d56cd 2018-04-01 stsp done:
2436 5e70831e 2019-05-28 stsp free(label1);
2437 5e70831e 2019-05-28 stsp free(label2);
2438 15a94983 2018-12-23 stsp free(id1);
2439 15a94983 2018-12-23 stsp free(id2);
2440 927df6b7 2019-02-10 stsp free(path);
2441 b72f483a 2019-02-05 stsp if (worktree)
2442 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2443 ad242220 2018-09-08 stsp if (repo) {
2444 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2445 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2446 ad242220 2018-09-08 stsp if (error == NULL)
2447 ad242220 2018-09-08 stsp error = repo_error;
2448 ad242220 2018-09-08 stsp }
2449 b00d56cd 2018-04-01 stsp return error;
2450 404c43c4 2018-06-21 stsp }
2451 404c43c4 2018-06-21 stsp
2452 404c43c4 2018-06-21 stsp __dead static void
2453 404c43c4 2018-06-21 stsp usage_blame(void)
2454 404c43c4 2018-06-21 stsp {
2455 9270e621 2019-02-05 stsp fprintf(stderr,
2456 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2457 404c43c4 2018-06-21 stsp getprogname());
2458 404c43c4 2018-06-21 stsp exit(1);
2459 b00d56cd 2018-04-01 stsp }
2460 b00d56cd 2018-04-01 stsp
2461 28315671 2019-08-14 stsp struct blame_line {
2462 28315671 2019-08-14 stsp int annotated;
2463 28315671 2019-08-14 stsp char *id_str;
2464 82f6abb8 2019-08-14 stsp char *committer;
2465 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2466 28315671 2019-08-14 stsp };
2467 28315671 2019-08-14 stsp
2468 28315671 2019-08-14 stsp struct blame_cb_args {
2469 28315671 2019-08-14 stsp struct blame_line *lines;
2470 28315671 2019-08-14 stsp int nlines;
2471 7ef28ff8 2019-08-14 stsp int nlines_prec;
2472 28315671 2019-08-14 stsp int lineno_cur;
2473 28315671 2019-08-14 stsp off_t *line_offsets;
2474 28315671 2019-08-14 stsp FILE *f;
2475 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2476 28315671 2019-08-14 stsp };
2477 28315671 2019-08-14 stsp
2478 404c43c4 2018-06-21 stsp static const struct got_error *
2479 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2480 28315671 2019-08-14 stsp {
2481 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2482 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2483 28315671 2019-08-14 stsp struct blame_line *bline;
2484 82f6abb8 2019-08-14 stsp char *line = NULL;
2485 28315671 2019-08-14 stsp size_t linesize = 0;
2486 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2487 28315671 2019-08-14 stsp off_t offset;
2488 bcb49d15 2019-08-14 stsp struct tm tm;
2489 bcb49d15 2019-08-14 stsp time_t committer_time;
2490 28315671 2019-08-14 stsp
2491 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2492 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2493 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2494 28315671 2019-08-14 stsp
2495 28315671 2019-08-14 stsp if (sigint_received)
2496 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2497 28315671 2019-08-14 stsp
2498 2839f8b9 2019-08-18 stsp if (lineno == -1)
2499 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2500 2839f8b9 2019-08-18 stsp
2501 28315671 2019-08-14 stsp /* Annotate this line. */
2502 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2503 28315671 2019-08-14 stsp if (bline->annotated)
2504 28315671 2019-08-14 stsp return NULL;
2505 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2506 28315671 2019-08-14 stsp if (err)
2507 28315671 2019-08-14 stsp return err;
2508 82f6abb8 2019-08-14 stsp
2509 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2510 82f6abb8 2019-08-14 stsp if (err)
2511 82f6abb8 2019-08-14 stsp goto done;
2512 82f6abb8 2019-08-14 stsp
2513 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2514 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2515 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2516 bcb49d15 2019-08-14 stsp goto done;
2517 bcb49d15 2019-08-14 stsp }
2518 bcb49d15 2019-08-14 stsp
2519 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2520 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2521 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2522 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2523 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2524 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2525 82f6abb8 2019-08-14 stsp goto done;
2526 82f6abb8 2019-08-14 stsp }
2527 28315671 2019-08-14 stsp bline->annotated = 1;
2528 28315671 2019-08-14 stsp
2529 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2530 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2531 28315671 2019-08-14 stsp if (!bline->annotated)
2532 82f6abb8 2019-08-14 stsp goto done;
2533 28315671 2019-08-14 stsp
2534 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2535 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2536 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2537 82f6abb8 2019-08-14 stsp goto done;
2538 82f6abb8 2019-08-14 stsp }
2539 28315671 2019-08-14 stsp
2540 28315671 2019-08-14 stsp while (bline->annotated) {
2541 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2542 82f6abb8 2019-08-14 stsp size_t len;
2543 82f6abb8 2019-08-14 stsp
2544 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2545 28315671 2019-08-14 stsp if (ferror(a->f))
2546 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2547 28315671 2019-08-14 stsp break;
2548 28315671 2019-08-14 stsp }
2549 82f6abb8 2019-08-14 stsp
2550 82f6abb8 2019-08-14 stsp committer = bline->committer;
2551 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2552 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2553 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2554 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2555 82f6abb8 2019-08-14 stsp if (at)
2556 82f6abb8 2019-08-14 stsp *at = '\0';
2557 82f6abb8 2019-08-14 stsp len = strlen(committer);
2558 82f6abb8 2019-08-14 stsp if (len >= 9)
2559 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2560 28315671 2019-08-14 stsp
2561 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2562 28315671 2019-08-14 stsp if (nl)
2563 28315671 2019-08-14 stsp *nl = '\0';
2564 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2565 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2566 28315671 2019-08-14 stsp
2567 28315671 2019-08-14 stsp a->lineno_cur++;
2568 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2569 28315671 2019-08-14 stsp }
2570 82f6abb8 2019-08-14 stsp done:
2571 82f6abb8 2019-08-14 stsp if (commit)
2572 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2573 28315671 2019-08-14 stsp free(line);
2574 28315671 2019-08-14 stsp return err;
2575 28315671 2019-08-14 stsp }
2576 28315671 2019-08-14 stsp
2577 28315671 2019-08-14 stsp static const struct got_error *
2578 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2579 404c43c4 2018-06-21 stsp {
2580 404c43c4 2018-06-21 stsp const struct got_error *error;
2581 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2582 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2583 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2584 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2585 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2586 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2587 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2588 28315671 2019-08-14 stsp struct blame_cb_args bca;
2589 28315671 2019-08-14 stsp int ch, obj_type, i;
2590 b02560ec 2019-08-19 stsp size_t filesize;
2591 90f3c347 2019-08-19 stsp
2592 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2593 404c43c4 2018-06-21 stsp
2594 404c43c4 2018-06-21 stsp #ifndef PROFILE
2595 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2596 36e2fb66 2019-01-04 stsp NULL) == -1)
2597 404c43c4 2018-06-21 stsp err(1, "pledge");
2598 404c43c4 2018-06-21 stsp #endif
2599 404c43c4 2018-06-21 stsp
2600 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2601 404c43c4 2018-06-21 stsp switch (ch) {
2602 404c43c4 2018-06-21 stsp case 'c':
2603 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2604 404c43c4 2018-06-21 stsp break;
2605 66bea077 2018-08-02 stsp case 'r':
2606 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2607 66bea077 2018-08-02 stsp if (repo_path == NULL)
2608 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2609 9ba1d308 2019-10-21 stsp optarg);
2610 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2611 66bea077 2018-08-02 stsp break;
2612 404c43c4 2018-06-21 stsp default:
2613 2deda0b9 2019-03-07 stsp usage_blame();
2614 404c43c4 2018-06-21 stsp /* NOTREACHED */
2615 404c43c4 2018-06-21 stsp }
2616 404c43c4 2018-06-21 stsp }
2617 404c43c4 2018-06-21 stsp
2618 404c43c4 2018-06-21 stsp argc -= optind;
2619 404c43c4 2018-06-21 stsp argv += optind;
2620 404c43c4 2018-06-21 stsp
2621 a39318fd 2018-08-02 stsp if (argc == 1)
2622 404c43c4 2018-06-21 stsp path = argv[0];
2623 a39318fd 2018-08-02 stsp else
2624 404c43c4 2018-06-21 stsp usage_blame();
2625 404c43c4 2018-06-21 stsp
2626 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2627 66bea077 2018-08-02 stsp if (cwd == NULL) {
2628 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2629 66bea077 2018-08-02 stsp goto done;
2630 66bea077 2018-08-02 stsp }
2631 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2632 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2633 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2634 66bea077 2018-08-02 stsp goto done;
2635 0c06baac 2019-02-05 stsp else
2636 0c06baac 2019-02-05 stsp error = NULL;
2637 0c06baac 2019-02-05 stsp if (worktree) {
2638 0c06baac 2019-02-05 stsp repo_path =
2639 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2640 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2641 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2642 0c06baac 2019-02-05 stsp if (error)
2643 0c06baac 2019-02-05 stsp goto done;
2644 0c06baac 2019-02-05 stsp } else {
2645 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2646 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2647 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2648 0c06baac 2019-02-05 stsp goto done;
2649 0c06baac 2019-02-05 stsp }
2650 66bea077 2018-08-02 stsp }
2651 66bea077 2018-08-02 stsp }
2652 36e2fb66 2019-01-04 stsp
2653 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2654 c02c541e 2019-03-29 stsp if (error != NULL)
2655 36e2fb66 2019-01-04 stsp goto done;
2656 66bea077 2018-08-02 stsp
2657 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2658 c02c541e 2019-03-29 stsp if (error)
2659 404c43c4 2018-06-21 stsp goto done;
2660 404c43c4 2018-06-21 stsp
2661 0c06baac 2019-02-05 stsp if (worktree) {
2662 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2663 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2664 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2665 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2666 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2667 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2668 6efaaa2d 2019-02-05 stsp path) == -1) {
2669 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2670 0c06baac 2019-02-05 stsp goto done;
2671 0c06baac 2019-02-05 stsp }
2672 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2673 0c06baac 2019-02-05 stsp free(p);
2674 0c06baac 2019-02-05 stsp } else {
2675 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2676 0c06baac 2019-02-05 stsp }
2677 0c06baac 2019-02-05 stsp if (error)
2678 66bea077 2018-08-02 stsp goto done;
2679 66bea077 2018-08-02 stsp
2680 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2681 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2682 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2683 404c43c4 2018-06-21 stsp if (error != NULL)
2684 66bea077 2018-08-02 stsp goto done;
2685 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2686 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2687 404c43c4 2018-06-21 stsp if (error != NULL)
2688 66bea077 2018-08-02 stsp goto done;
2689 404c43c4 2018-06-21 stsp } else {
2690 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2691 30837e32 2019-07-25 stsp if (error)
2692 66bea077 2018-08-02 stsp goto done;
2693 404c43c4 2018-06-21 stsp }
2694 404c43c4 2018-06-21 stsp
2695 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2696 28315671 2019-08-14 stsp if (error)
2697 28315671 2019-08-14 stsp goto done;
2698 28315671 2019-08-14 stsp if (obj_id == NULL) {
2699 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2700 28315671 2019-08-14 stsp goto done;
2701 28315671 2019-08-14 stsp }
2702 28315671 2019-08-14 stsp
2703 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2704 28315671 2019-08-14 stsp if (error)
2705 28315671 2019-08-14 stsp goto done;
2706 28315671 2019-08-14 stsp
2707 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2708 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2709 28315671 2019-08-14 stsp goto done;
2710 28315671 2019-08-14 stsp }
2711 28315671 2019-08-14 stsp
2712 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2713 28315671 2019-08-14 stsp if (error)
2714 28315671 2019-08-14 stsp goto done;
2715 28315671 2019-08-14 stsp bca.f = got_opentemp();
2716 28315671 2019-08-14 stsp if (bca.f == NULL) {
2717 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2718 28315671 2019-08-14 stsp goto done;
2719 28315671 2019-08-14 stsp }
2720 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2721 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2722 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2723 28315671 2019-08-14 stsp goto done;
2724 28315671 2019-08-14 stsp
2725 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2726 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2727 b02560ec 2019-08-19 stsp bca.nlines--;
2728 b02560ec 2019-08-19 stsp
2729 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2730 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2731 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2732 28315671 2019-08-14 stsp goto done;
2733 28315671 2019-08-14 stsp }
2734 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2735 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2736 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2737 7ef28ff8 2019-08-14 stsp while (i > 0) {
2738 7ef28ff8 2019-08-14 stsp i /= 10;
2739 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2740 7ef28ff8 2019-08-14 stsp }
2741 82f6abb8 2019-08-14 stsp bca.repo = repo;
2742 7ef28ff8 2019-08-14 stsp
2743 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2744 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2745 28315671 2019-08-14 stsp if (error)
2746 28315671 2019-08-14 stsp goto done;
2747 404c43c4 2018-06-21 stsp done:
2748 66bea077 2018-08-02 stsp free(in_repo_path);
2749 66bea077 2018-08-02 stsp free(repo_path);
2750 66bea077 2018-08-02 stsp free(cwd);
2751 404c43c4 2018-06-21 stsp free(commit_id);
2752 28315671 2019-08-14 stsp free(obj_id);
2753 28315671 2019-08-14 stsp if (blob)
2754 28315671 2019-08-14 stsp got_object_blob_close(blob);
2755 0c06baac 2019-02-05 stsp if (worktree)
2756 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2757 ad242220 2018-09-08 stsp if (repo) {
2758 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2759 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2760 ad242220 2018-09-08 stsp if (error == NULL)
2761 ad242220 2018-09-08 stsp error = repo_error;
2762 ad242220 2018-09-08 stsp }
2763 3affba96 2019-09-22 stsp if (bca.lines) {
2764 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2765 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2766 3affba96 2019-09-22 stsp free(bline->id_str);
2767 3affba96 2019-09-22 stsp free(bline->committer);
2768 3affba96 2019-09-22 stsp }
2769 3affba96 2019-09-22 stsp free(bca.lines);
2770 28315671 2019-08-14 stsp }
2771 28315671 2019-08-14 stsp free(bca.line_offsets);
2772 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2773 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2774 404c43c4 2018-06-21 stsp return error;
2775 5de5890b 2018-10-18 stsp }
2776 5de5890b 2018-10-18 stsp
2777 5de5890b 2018-10-18 stsp __dead static void
2778 5de5890b 2018-10-18 stsp usage_tree(void)
2779 5de5890b 2018-10-18 stsp {
2780 c1669e2e 2019-01-09 stsp fprintf(stderr,
2781 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2782 5de5890b 2018-10-18 stsp getprogname());
2783 5de5890b 2018-10-18 stsp exit(1);
2784 5de5890b 2018-10-18 stsp }
2785 5de5890b 2018-10-18 stsp
2786 c1669e2e 2019-01-09 stsp static void
2787 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2788 c1669e2e 2019-01-09 stsp const char *root_path)
2789 c1669e2e 2019-01-09 stsp {
2790 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2791 848d6979 2019-08-12 stsp const char *modestr = "";
2792 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2793 5de5890b 2018-10-18 stsp
2794 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2795 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2796 c1669e2e 2019-01-09 stsp path++;
2797 c1669e2e 2019-01-09 stsp
2798 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2799 63c5ca5d 2019-08-24 stsp modestr = "$";
2800 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2801 848d6979 2019-08-12 stsp modestr = "@";
2802 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2803 848d6979 2019-08-12 stsp modestr = "/";
2804 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2805 848d6979 2019-08-12 stsp modestr = "*";
2806 848d6979 2019-08-12 stsp
2807 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2808 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2809 c1669e2e 2019-01-09 stsp }
2810 c1669e2e 2019-01-09 stsp
2811 5de5890b 2018-10-18 stsp static const struct got_error *
2812 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2813 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2814 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2815 5de5890b 2018-10-18 stsp {
2816 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2817 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2818 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2819 56e0773d 2019-11-28 stsp int nentries, i;
2820 5de5890b 2018-10-18 stsp
2821 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2822 5de5890b 2018-10-18 stsp if (err)
2823 5de5890b 2018-10-18 stsp goto done;
2824 5de5890b 2018-10-18 stsp
2825 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2826 5de5890b 2018-10-18 stsp if (err)
2827 5de5890b 2018-10-18 stsp goto done;
2828 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2829 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2830 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2831 5de5890b 2018-10-18 stsp char *id = NULL;
2832 84453469 2018-11-11 stsp
2833 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2834 84453469 2018-11-11 stsp break;
2835 84453469 2018-11-11 stsp
2836 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2837 5de5890b 2018-10-18 stsp if (show_ids) {
2838 5de5890b 2018-10-18 stsp char *id_str;
2839 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2840 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2841 5de5890b 2018-10-18 stsp if (err)
2842 5de5890b 2018-10-18 stsp goto done;
2843 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2844 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2845 5de5890b 2018-10-18 stsp free(id_str);
2846 5de5890b 2018-10-18 stsp goto done;
2847 5de5890b 2018-10-18 stsp }
2848 5de5890b 2018-10-18 stsp free(id_str);
2849 5de5890b 2018-10-18 stsp }
2850 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2851 5de5890b 2018-10-18 stsp free(id);
2852 c1669e2e 2019-01-09 stsp
2853 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2854 c1669e2e 2019-01-09 stsp char *child_path;
2855 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2856 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2857 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2858 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2859 c1669e2e 2019-01-09 stsp goto done;
2860 c1669e2e 2019-01-09 stsp }
2861 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2862 c1669e2e 2019-01-09 stsp root_path, repo);
2863 c1669e2e 2019-01-09 stsp free(child_path);
2864 c1669e2e 2019-01-09 stsp if (err)
2865 c1669e2e 2019-01-09 stsp goto done;
2866 c1669e2e 2019-01-09 stsp }
2867 5de5890b 2018-10-18 stsp }
2868 5de5890b 2018-10-18 stsp done:
2869 5de5890b 2018-10-18 stsp if (tree)
2870 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2871 5de5890b 2018-10-18 stsp free(tree_id);
2872 5de5890b 2018-10-18 stsp return err;
2873 404c43c4 2018-06-21 stsp }
2874 404c43c4 2018-06-21 stsp
2875 5de5890b 2018-10-18 stsp static const struct got_error *
2876 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2877 5de5890b 2018-10-18 stsp {
2878 5de5890b 2018-10-18 stsp const struct got_error *error;
2879 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2880 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2881 7a2c19d6 2019-02-05 stsp const char *path;
2882 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2883 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2884 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2885 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2886 5de5890b 2018-10-18 stsp int ch;
2887 5de5890b 2018-10-18 stsp
2888 5de5890b 2018-10-18 stsp #ifndef PROFILE
2889 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2890 0f8d269b 2019-01-04 stsp NULL) == -1)
2891 5de5890b 2018-10-18 stsp err(1, "pledge");
2892 5de5890b 2018-10-18 stsp #endif
2893 5de5890b 2018-10-18 stsp
2894 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2895 5de5890b 2018-10-18 stsp switch (ch) {
2896 5de5890b 2018-10-18 stsp case 'c':
2897 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2898 5de5890b 2018-10-18 stsp break;
2899 5de5890b 2018-10-18 stsp case 'r':
2900 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2901 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2902 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2903 9ba1d308 2019-10-21 stsp optarg);
2904 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2905 5de5890b 2018-10-18 stsp break;
2906 5de5890b 2018-10-18 stsp case 'i':
2907 5de5890b 2018-10-18 stsp show_ids = 1;
2908 5de5890b 2018-10-18 stsp break;
2909 c1669e2e 2019-01-09 stsp case 'R':
2910 c1669e2e 2019-01-09 stsp recurse = 1;
2911 c1669e2e 2019-01-09 stsp break;
2912 5de5890b 2018-10-18 stsp default:
2913 2deda0b9 2019-03-07 stsp usage_tree();
2914 5de5890b 2018-10-18 stsp /* NOTREACHED */
2915 5de5890b 2018-10-18 stsp }
2916 5de5890b 2018-10-18 stsp }
2917 5de5890b 2018-10-18 stsp
2918 5de5890b 2018-10-18 stsp argc -= optind;
2919 5de5890b 2018-10-18 stsp argv += optind;
2920 5de5890b 2018-10-18 stsp
2921 5de5890b 2018-10-18 stsp if (argc == 1)
2922 5de5890b 2018-10-18 stsp path = argv[0];
2923 5de5890b 2018-10-18 stsp else if (argc > 1)
2924 5de5890b 2018-10-18 stsp usage_tree();
2925 5de5890b 2018-10-18 stsp else
2926 7a2c19d6 2019-02-05 stsp path = NULL;
2927 7a2c19d6 2019-02-05 stsp
2928 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2929 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2930 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2931 9bf7a39b 2019-02-05 stsp goto done;
2932 9bf7a39b 2019-02-05 stsp }
2933 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2934 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2935 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2936 7a2c19d6 2019-02-05 stsp goto done;
2937 7a2c19d6 2019-02-05 stsp else
2938 7a2c19d6 2019-02-05 stsp error = NULL;
2939 7a2c19d6 2019-02-05 stsp if (worktree) {
2940 7a2c19d6 2019-02-05 stsp repo_path =
2941 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2942 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2943 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2944 7a2c19d6 2019-02-05 stsp if (error)
2945 7a2c19d6 2019-02-05 stsp goto done;
2946 7a2c19d6 2019-02-05 stsp } else {
2947 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2948 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2949 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2950 7a2c19d6 2019-02-05 stsp goto done;
2951 7a2c19d6 2019-02-05 stsp }
2952 5de5890b 2018-10-18 stsp }
2953 5de5890b 2018-10-18 stsp }
2954 5de5890b 2018-10-18 stsp
2955 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2956 5de5890b 2018-10-18 stsp if (error != NULL)
2957 c02c541e 2019-03-29 stsp goto done;
2958 c02c541e 2019-03-29 stsp
2959 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2960 c02c541e 2019-03-29 stsp if (error)
2961 5de5890b 2018-10-18 stsp goto done;
2962 5de5890b 2018-10-18 stsp
2963 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2964 9bf7a39b 2019-02-05 stsp if (worktree) {
2965 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2966 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2967 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2968 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2969 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2970 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2971 9bf7a39b 2019-02-05 stsp goto done;
2972 9bf7a39b 2019-02-05 stsp }
2973 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2974 9bf7a39b 2019-02-05 stsp free(p);
2975 9bf7a39b 2019-02-05 stsp if (error)
2976 9bf7a39b 2019-02-05 stsp goto done;
2977 9bf7a39b 2019-02-05 stsp } else
2978 9bf7a39b 2019-02-05 stsp path = "/";
2979 9bf7a39b 2019-02-05 stsp }
2980 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2981 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2982 9bf7a39b 2019-02-05 stsp if (error != NULL)
2983 9bf7a39b 2019-02-05 stsp goto done;
2984 9bf7a39b 2019-02-05 stsp }
2985 5de5890b 2018-10-18 stsp
2986 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2987 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2988 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2989 5de5890b 2018-10-18 stsp if (error != NULL)
2990 5de5890b 2018-10-18 stsp goto done;
2991 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2992 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2993 5de5890b 2018-10-18 stsp if (error != NULL)
2994 5de5890b 2018-10-18 stsp goto done;
2995 5de5890b 2018-10-18 stsp } else {
2996 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2997 30837e32 2019-07-25 stsp if (error)
2998 5de5890b 2018-10-18 stsp goto done;
2999 5de5890b 2018-10-18 stsp }
3000 5de5890b 2018-10-18 stsp
3001 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3002 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3003 5de5890b 2018-10-18 stsp done:
3004 5de5890b 2018-10-18 stsp free(in_repo_path);
3005 5de5890b 2018-10-18 stsp free(repo_path);
3006 5de5890b 2018-10-18 stsp free(cwd);
3007 5de5890b 2018-10-18 stsp free(commit_id);
3008 7a2c19d6 2019-02-05 stsp if (worktree)
3009 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3010 5de5890b 2018-10-18 stsp if (repo) {
3011 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3012 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3013 5de5890b 2018-10-18 stsp if (error == NULL)
3014 5de5890b 2018-10-18 stsp error = repo_error;
3015 5de5890b 2018-10-18 stsp }
3016 5de5890b 2018-10-18 stsp return error;
3017 5de5890b 2018-10-18 stsp }
3018 5de5890b 2018-10-18 stsp
3019 6bad629b 2019-02-04 stsp __dead static void
3020 6bad629b 2019-02-04 stsp usage_status(void)
3021 6bad629b 2019-02-04 stsp {
3022 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3023 6bad629b 2019-02-04 stsp exit(1);
3024 6bad629b 2019-02-04 stsp }
3025 5c860e29 2018-03-12 stsp
3026 b72f483a 2019-02-05 stsp static const struct got_error *
3027 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3028 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3029 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3030 6bad629b 2019-02-04 stsp {
3031 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3032 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3033 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3034 b72f483a 2019-02-05 stsp return NULL;
3035 6bad629b 2019-02-04 stsp }
3036 5c860e29 2018-03-12 stsp
3037 6bad629b 2019-02-04 stsp static const struct got_error *
3038 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3039 6bad629b 2019-02-04 stsp {
3040 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3041 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3042 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3043 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3044 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3045 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3046 a5edda0a 2019-07-27 stsp int ch;
3047 5c860e29 2018-03-12 stsp
3048 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3049 72ea6654 2019-07-27 stsp
3050 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3051 6bad629b 2019-02-04 stsp switch (ch) {
3052 5c860e29 2018-03-12 stsp default:
3053 2deda0b9 2019-03-07 stsp usage_status();
3054 6bad629b 2019-02-04 stsp /* NOTREACHED */
3055 5c860e29 2018-03-12 stsp }
3056 5c860e29 2018-03-12 stsp }
3057 5c860e29 2018-03-12 stsp
3058 6bad629b 2019-02-04 stsp argc -= optind;
3059 6bad629b 2019-02-04 stsp argv += optind;
3060 5c860e29 2018-03-12 stsp
3061 6bad629b 2019-02-04 stsp #ifndef PROFILE
3062 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3063 6bad629b 2019-02-04 stsp NULL) == -1)
3064 6bad629b 2019-02-04 stsp err(1, "pledge");
3065 f42b1b34 2018-03-12 stsp #endif
3066 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3067 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3068 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3069 927df6b7 2019-02-10 stsp goto done;
3070 927df6b7 2019-02-10 stsp }
3071 927df6b7 2019-02-10 stsp
3072 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3073 927df6b7 2019-02-10 stsp if (error != NULL)
3074 a5edda0a 2019-07-27 stsp goto done;
3075 6bad629b 2019-02-04 stsp
3076 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3077 c9956ddf 2019-09-08 stsp NULL);
3078 6bad629b 2019-02-04 stsp if (error != NULL)
3079 6bad629b 2019-02-04 stsp goto done;
3080 6bad629b 2019-02-04 stsp
3081 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3082 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3083 087fb88c 2019-08-04 stsp if (error)
3084 087fb88c 2019-08-04 stsp goto done;
3085 087fb88c 2019-08-04 stsp
3086 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3087 6bad629b 2019-02-04 stsp if (error)
3088 6bad629b 2019-02-04 stsp goto done;
3089 6bad629b 2019-02-04 stsp
3090 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3091 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3092 6bad629b 2019-02-04 stsp done:
3093 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3094 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3095 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3096 927df6b7 2019-02-10 stsp free(cwd);
3097 d0eebce4 2019-03-11 stsp return error;
3098 d0eebce4 2019-03-11 stsp }
3099 d0eebce4 2019-03-11 stsp
3100 d0eebce4 2019-03-11 stsp __dead static void
3101 d0eebce4 2019-03-11 stsp usage_ref(void)
3102 d0eebce4 2019-03-11 stsp {
3103 d0eebce4 2019-03-11 stsp fprintf(stderr,
3104 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3105 d0eebce4 2019-03-11 stsp getprogname());
3106 d0eebce4 2019-03-11 stsp exit(1);
3107 d0eebce4 2019-03-11 stsp }
3108 d0eebce4 2019-03-11 stsp
3109 d0eebce4 2019-03-11 stsp static const struct got_error *
3110 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3111 d0eebce4 2019-03-11 stsp {
3112 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3113 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3114 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3115 d0eebce4 2019-03-11 stsp
3116 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3117 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3118 d0eebce4 2019-03-11 stsp if (err)
3119 d0eebce4 2019-03-11 stsp return err;
3120 d0eebce4 2019-03-11 stsp
3121 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3122 d0eebce4 2019-03-11 stsp char *refstr;
3123 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3124 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3125 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3126 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3127 d0eebce4 2019-03-11 stsp free(refstr);
3128 d0eebce4 2019-03-11 stsp }
3129 d0eebce4 2019-03-11 stsp
3130 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3131 d0eebce4 2019-03-11 stsp return NULL;
3132 d0eebce4 2019-03-11 stsp }
3133 d0eebce4 2019-03-11 stsp
3134 d0eebce4 2019-03-11 stsp static const struct got_error *
3135 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3136 d0eebce4 2019-03-11 stsp {
3137 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3138 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3139 d0eebce4 2019-03-11 stsp
3140 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3141 d0eebce4 2019-03-11 stsp if (err)
3142 d0eebce4 2019-03-11 stsp return err;
3143 d0eebce4 2019-03-11 stsp
3144 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3145 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3146 d0eebce4 2019-03-11 stsp return err;
3147 d0eebce4 2019-03-11 stsp }
3148 d0eebce4 2019-03-11 stsp
3149 d0eebce4 2019-03-11 stsp static const struct got_error *
3150 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3151 d0eebce4 2019-03-11 stsp {
3152 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3153 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3154 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3155 d1644381 2019-07-14 stsp
3156 d1644381 2019-07-14 stsp /*
3157 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3158 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3159 d1644381 2019-07-14 stsp * an unintended typo.
3160 d1644381 2019-07-14 stsp */
3161 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3162 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3163 d0eebce4 2019-03-11 stsp
3164 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3165 dd88155e 2019-06-29 stsp repo);
3166 d83d9d5c 2019-05-13 stsp if (err) {
3167 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3168 d0eebce4 2019-03-11 stsp
3169 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3170 d83d9d5c 2019-05-13 stsp return err;
3171 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3172 d83d9d5c 2019-05-13 stsp if (err)
3173 d83d9d5c 2019-05-13 stsp return err;
3174 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3175 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3176 d83d9d5c 2019-05-13 stsp if (err)
3177 d83d9d5c 2019-05-13 stsp return err;
3178 d83d9d5c 2019-05-13 stsp }
3179 d83d9d5c 2019-05-13 stsp
3180 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3181 d0eebce4 2019-03-11 stsp if (err)
3182 d0eebce4 2019-03-11 stsp goto done;
3183 d0eebce4 2019-03-11 stsp
3184 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3185 d0eebce4 2019-03-11 stsp done:
3186 d0eebce4 2019-03-11 stsp if (ref)
3187 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3188 d0eebce4 2019-03-11 stsp free(id);
3189 d1c1ae5f 2019-08-12 stsp return err;
3190 d1c1ae5f 2019-08-12 stsp }
3191 d1c1ae5f 2019-08-12 stsp
3192 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3193 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3194 d1c1ae5f 2019-08-12 stsp {
3195 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3196 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3197 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3198 d1c1ae5f 2019-08-12 stsp
3199 d1c1ae5f 2019-08-12 stsp /*
3200 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3201 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3202 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3203 d1c1ae5f 2019-08-12 stsp */
3204 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3205 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3206 d1c1ae5f 2019-08-12 stsp
3207 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3208 d1c1ae5f 2019-08-12 stsp if (err)
3209 d1c1ae5f 2019-08-12 stsp return err;
3210 d1c1ae5f 2019-08-12 stsp
3211 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3212 d1c1ae5f 2019-08-12 stsp if (err)
3213 d1c1ae5f 2019-08-12 stsp goto done;
3214 d1c1ae5f 2019-08-12 stsp
3215 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3216 d1c1ae5f 2019-08-12 stsp done:
3217 d1c1ae5f 2019-08-12 stsp if (target_ref)
3218 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3219 d1c1ae5f 2019-08-12 stsp if (ref)
3220 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3221 d0eebce4 2019-03-11 stsp return err;
3222 d0eebce4 2019-03-11 stsp }
3223 d0eebce4 2019-03-11 stsp
3224 d0eebce4 2019-03-11 stsp static const struct got_error *
3225 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3226 d0eebce4 2019-03-11 stsp {
3227 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3228 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3229 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3230 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3231 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3232 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3233 d0eebce4 2019-03-11 stsp
3234 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3235 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3236 d0eebce4 2019-03-11 stsp switch (ch) {
3237 d0eebce4 2019-03-11 stsp case 'd':
3238 d0eebce4 2019-03-11 stsp delref = optarg;
3239 d0eebce4 2019-03-11 stsp break;
3240 d0eebce4 2019-03-11 stsp case 'r':
3241 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3242 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3243 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3244 9ba1d308 2019-10-21 stsp optarg);
3245 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3246 d0eebce4 2019-03-11 stsp break;
3247 d0eebce4 2019-03-11 stsp case 'l':
3248 d0eebce4 2019-03-11 stsp do_list = 1;
3249 d1c1ae5f 2019-08-12 stsp break;
3250 d1c1ae5f 2019-08-12 stsp case 's':
3251 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3252 d0eebce4 2019-03-11 stsp break;
3253 d0eebce4 2019-03-11 stsp default:
3254 d0eebce4 2019-03-11 stsp usage_ref();
3255 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3256 d0eebce4 2019-03-11 stsp }
3257 d0eebce4 2019-03-11 stsp }
3258 d0eebce4 2019-03-11 stsp
3259 d0eebce4 2019-03-11 stsp if (do_list && delref)
3260 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3261 d0eebce4 2019-03-11 stsp
3262 d0eebce4 2019-03-11 stsp argc -= optind;
3263 d0eebce4 2019-03-11 stsp argv += optind;
3264 d0eebce4 2019-03-11 stsp
3265 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3266 d1c1ae5f 2019-08-12 stsp if (create_symref)
3267 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3268 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3269 d0eebce4 2019-03-11 stsp if (argc > 0)
3270 d0eebce4 2019-03-11 stsp usage_ref();
3271 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3272 d0eebce4 2019-03-11 stsp usage_ref();
3273 d0eebce4 2019-03-11 stsp
3274 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3275 e0b57350 2019-03-12 stsp if (do_list) {
3276 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3277 e0b57350 2019-03-12 stsp NULL) == -1)
3278 e0b57350 2019-03-12 stsp err(1, "pledge");
3279 e0b57350 2019-03-12 stsp } else {
3280 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3281 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3282 e0b57350 2019-03-12 stsp err(1, "pledge");
3283 e0b57350 2019-03-12 stsp }
3284 d0eebce4 2019-03-11 stsp #endif
3285 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3286 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3287 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3288 d0eebce4 2019-03-11 stsp goto done;
3289 d0eebce4 2019-03-11 stsp }
3290 d0eebce4 2019-03-11 stsp
3291 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3292 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3293 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3294 d0eebce4 2019-03-11 stsp goto done;
3295 d0eebce4 2019-03-11 stsp else
3296 d0eebce4 2019-03-11 stsp error = NULL;
3297 d0eebce4 2019-03-11 stsp if (worktree) {
3298 d0eebce4 2019-03-11 stsp repo_path =
3299 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3300 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3301 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3302 d0eebce4 2019-03-11 stsp if (error)
3303 d0eebce4 2019-03-11 stsp goto done;
3304 d0eebce4 2019-03-11 stsp } else {
3305 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3306 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3307 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3308 d0eebce4 2019-03-11 stsp goto done;
3309 d0eebce4 2019-03-11 stsp }
3310 d0eebce4 2019-03-11 stsp }
3311 d0eebce4 2019-03-11 stsp }
3312 d0eebce4 2019-03-11 stsp
3313 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3314 d0eebce4 2019-03-11 stsp if (error != NULL)
3315 d0eebce4 2019-03-11 stsp goto done;
3316 d0eebce4 2019-03-11 stsp
3317 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3318 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3319 c02c541e 2019-03-29 stsp if (error)
3320 c02c541e 2019-03-29 stsp goto done;
3321 c02c541e 2019-03-29 stsp
3322 d0eebce4 2019-03-11 stsp if (do_list)
3323 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3324 d0eebce4 2019-03-11 stsp else if (delref)
3325 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3326 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3327 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3328 d0eebce4 2019-03-11 stsp else
3329 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3330 4e759de4 2019-06-26 stsp done:
3331 4e759de4 2019-06-26 stsp if (repo)
3332 4e759de4 2019-06-26 stsp got_repo_close(repo);
3333 4e759de4 2019-06-26 stsp if (worktree)
3334 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3335 4e759de4 2019-06-26 stsp free(cwd);
3336 4e759de4 2019-06-26 stsp free(repo_path);
3337 4e759de4 2019-06-26 stsp return error;
3338 4e759de4 2019-06-26 stsp }
3339 4e759de4 2019-06-26 stsp
3340 4e759de4 2019-06-26 stsp __dead static void
3341 4e759de4 2019-06-26 stsp usage_branch(void)
3342 4e759de4 2019-06-26 stsp {
3343 4e759de4 2019-06-26 stsp fprintf(stderr,
3344 a74f7e83 2019-11-10 stsp "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3345 a74f7e83 2019-11-10 stsp "[name]\n", getprogname());
3346 4e759de4 2019-06-26 stsp exit(1);
3347 4e759de4 2019-06-26 stsp }
3348 4e759de4 2019-06-26 stsp
3349 4e759de4 2019-06-26 stsp static const struct got_error *
3350 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3351 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3352 4e99b47e 2019-10-04 stsp {
3353 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3354 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3355 4e99b47e 2019-10-04 stsp char *refstr;
3356 4e99b47e 2019-10-04 stsp
3357 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3358 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3359 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3360 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3361 4e99b47e 2019-10-04 stsp
3362 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3363 4e99b47e 2019-10-04 stsp if (err)
3364 4e99b47e 2019-10-04 stsp return err;
3365 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3366 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3367 4e99b47e 2019-10-04 stsp marker = "* ";
3368 4e99b47e 2019-10-04 stsp else
3369 4e99b47e 2019-10-04 stsp marker = "~ ";
3370 4e99b47e 2019-10-04 stsp free(id);
3371 4e99b47e 2019-10-04 stsp }
3372 4e99b47e 2019-10-04 stsp
3373 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3374 4e99b47e 2019-10-04 stsp refname += 11;
3375 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3376 4e99b47e 2019-10-04 stsp refname += 18;
3377 4e99b47e 2019-10-04 stsp
3378 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3379 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3380 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3381 4e99b47e 2019-10-04 stsp
3382 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3383 4e99b47e 2019-10-04 stsp free(refstr);
3384 4e99b47e 2019-10-04 stsp return NULL;
3385 4e99b47e 2019-10-04 stsp }
3386 4e99b47e 2019-10-04 stsp
3387 4e99b47e 2019-10-04 stsp static const struct got_error *
3388 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3389 ad89fa31 2019-10-04 stsp {
3390 ad89fa31 2019-10-04 stsp const char *refname;
3391 ad89fa31 2019-10-04 stsp
3392 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3393 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3394 ad89fa31 2019-10-04 stsp
3395 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3396 ad89fa31 2019-10-04 stsp
3397 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3398 ad89fa31 2019-10-04 stsp refname += 11;
3399 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3400 ad89fa31 2019-10-04 stsp refname += 18;
3401 ad89fa31 2019-10-04 stsp
3402 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3403 ad89fa31 2019-10-04 stsp
3404 ad89fa31 2019-10-04 stsp return NULL;
3405 ad89fa31 2019-10-04 stsp }
3406 ad89fa31 2019-10-04 stsp
3407 ad89fa31 2019-10-04 stsp static const struct got_error *
3408 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3409 4e759de4 2019-06-26 stsp {
3410 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3411 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3412 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3413 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3414 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3415 4e759de4 2019-06-26 stsp
3416 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3417 ba882ee3 2019-07-11 stsp
3418 4e99b47e 2019-10-04 stsp if (worktree) {
3419 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3420 4e99b47e 2019-10-04 stsp worktree);
3421 4e99b47e 2019-10-04 stsp if (err)
3422 4e99b47e 2019-10-04 stsp return err;
3423 4e99b47e 2019-10-04 stsp
3424 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3425 4e99b47e 2019-10-04 stsp worktree);
3426 4e99b47e 2019-10-04 stsp if (err)
3427 4e99b47e 2019-10-04 stsp return err;
3428 4e99b47e 2019-10-04 stsp
3429 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3430 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3431 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3432 4e99b47e 2019-10-04 stsp if (err)
3433 4e99b47e 2019-10-04 stsp return err;
3434 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3435 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3436 4e99b47e 2019-10-04 stsp }
3437 4e99b47e 2019-10-04 stsp }
3438 4e99b47e 2019-10-04 stsp
3439 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3440 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3441 4e759de4 2019-06-26 stsp if (err)
3442 4e759de4 2019-06-26 stsp return err;
3443 4e759de4 2019-06-26 stsp
3444 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3445 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3446 4e759de4 2019-06-26 stsp
3447 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3448 4e759de4 2019-06-26 stsp return NULL;
3449 4e759de4 2019-06-26 stsp }
3450 4e759de4 2019-06-26 stsp
3451 4e759de4 2019-06-26 stsp static const struct got_error *
3452 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3453 45cd4e47 2019-08-25 stsp const char *branch_name)
3454 4e759de4 2019-06-26 stsp {
3455 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3456 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3457 4e759de4 2019-06-26 stsp char *refname;
3458 4e759de4 2019-06-26 stsp
3459 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3460 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3461 4e759de4 2019-06-26 stsp
3462 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3463 4e759de4 2019-06-26 stsp if (err)
3464 4e759de4 2019-06-26 stsp goto done;
3465 4e759de4 2019-06-26 stsp
3466 45cd4e47 2019-08-25 stsp if (worktree &&
3467 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3468 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3469 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3470 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3471 45cd4e47 2019-08-25 stsp goto done;
3472 45cd4e47 2019-08-25 stsp }
3473 45cd4e47 2019-08-25 stsp
3474 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3475 4e759de4 2019-06-26 stsp done:
3476 45cd4e47 2019-08-25 stsp if (ref)
3477 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3478 4e759de4 2019-06-26 stsp free(refname);
3479 4e759de4 2019-06-26 stsp return err;
3480 4e759de4 2019-06-26 stsp }
3481 4e759de4 2019-06-26 stsp
3482 4e759de4 2019-06-26 stsp static const struct got_error *
3483 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3484 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3485 4e759de4 2019-06-26 stsp {
3486 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3487 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3488 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3489 d3f84d51 2019-07-11 stsp
3490 d3f84d51 2019-07-11 stsp /*
3491 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3492 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3493 d3f84d51 2019-07-11 stsp * an unintended typo.
3494 d3f84d51 2019-07-11 stsp */
3495 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3496 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3497 4e759de4 2019-06-26 stsp
3498 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3499 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3500 4e759de4 2019-06-26 stsp goto done;
3501 4e759de4 2019-06-26 stsp }
3502 4e759de4 2019-06-26 stsp
3503 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3504 4e759de4 2019-06-26 stsp if (err == NULL) {
3505 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3506 4e759de4 2019-06-26 stsp goto done;
3507 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3508 4e759de4 2019-06-26 stsp goto done;
3509 4e759de4 2019-06-26 stsp
3510 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3511 4e759de4 2019-06-26 stsp if (err)
3512 4e759de4 2019-06-26 stsp goto done;
3513 4e759de4 2019-06-26 stsp
3514 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3515 d0eebce4 2019-03-11 stsp done:
3516 4e759de4 2019-06-26 stsp if (ref)
3517 4e759de4 2019-06-26 stsp got_ref_close(ref);
3518 4e759de4 2019-06-26 stsp free(base_refname);
3519 4e759de4 2019-06-26 stsp free(refname);
3520 4e759de4 2019-06-26 stsp return err;
3521 4e759de4 2019-06-26 stsp }
3522 4e759de4 2019-06-26 stsp
3523 4e759de4 2019-06-26 stsp static const struct got_error *
3524 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3525 4e759de4 2019-06-26 stsp {
3526 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3527 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3528 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3529 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3530 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3531 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3532 4e759de4 2019-06-26 stsp
3533 a74f7e83 2019-11-10 stsp while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3534 4e759de4 2019-06-26 stsp switch (ch) {
3535 a74f7e83 2019-11-10 stsp case 'c':
3536 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3537 a74f7e83 2019-11-10 stsp break;
3538 4e759de4 2019-06-26 stsp case 'd':
3539 4e759de4 2019-06-26 stsp delref = optarg;
3540 4e759de4 2019-06-26 stsp break;
3541 4e759de4 2019-06-26 stsp case 'r':
3542 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3543 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3544 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3545 9ba1d308 2019-10-21 stsp optarg);
3546 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3547 4e759de4 2019-06-26 stsp break;
3548 4e759de4 2019-06-26 stsp case 'l':
3549 4e759de4 2019-06-26 stsp do_list = 1;
3550 4e759de4 2019-06-26 stsp break;
3551 4e759de4 2019-06-26 stsp default:
3552 4e759de4 2019-06-26 stsp usage_branch();
3553 4e759de4 2019-06-26 stsp /* NOTREACHED */
3554 4e759de4 2019-06-26 stsp }
3555 4e759de4 2019-06-26 stsp }
3556 4e759de4 2019-06-26 stsp
3557 4e759de4 2019-06-26 stsp if (do_list && delref)
3558 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3559 4e759de4 2019-06-26 stsp
3560 4e759de4 2019-06-26 stsp argc -= optind;
3561 4e759de4 2019-06-26 stsp argv += optind;
3562 ad89fa31 2019-10-04 stsp
3563 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3564 ad89fa31 2019-10-04 stsp do_show = 1;
3565 4e759de4 2019-06-26 stsp
3566 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3567 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3568 a74f7e83 2019-11-10 stsp
3569 4e759de4 2019-06-26 stsp if (do_list || delref) {
3570 4e759de4 2019-06-26 stsp if (argc > 0)
3571 4e759de4 2019-06-26 stsp usage_branch();
3572 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3573 4e759de4 2019-06-26 stsp usage_branch();
3574 4e759de4 2019-06-26 stsp
3575 4e759de4 2019-06-26 stsp #ifndef PROFILE
3576 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3577 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3578 4e759de4 2019-06-26 stsp NULL) == -1)
3579 4e759de4 2019-06-26 stsp err(1, "pledge");
3580 4e759de4 2019-06-26 stsp } else {
3581 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3582 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3583 4e759de4 2019-06-26 stsp err(1, "pledge");
3584 4e759de4 2019-06-26 stsp }
3585 4e759de4 2019-06-26 stsp #endif
3586 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3587 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3588 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3589 4e759de4 2019-06-26 stsp goto done;
3590 4e759de4 2019-06-26 stsp }
3591 4e759de4 2019-06-26 stsp
3592 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3593 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3594 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3595 4e759de4 2019-06-26 stsp goto done;
3596 4e759de4 2019-06-26 stsp else
3597 4e759de4 2019-06-26 stsp error = NULL;
3598 4e759de4 2019-06-26 stsp if (worktree) {
3599 4e759de4 2019-06-26 stsp repo_path =
3600 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3601 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3602 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3603 4e759de4 2019-06-26 stsp if (error)
3604 4e759de4 2019-06-26 stsp goto done;
3605 4e759de4 2019-06-26 stsp } else {
3606 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3607 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3608 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3609 4e759de4 2019-06-26 stsp goto done;
3610 4e759de4 2019-06-26 stsp }
3611 4e759de4 2019-06-26 stsp }
3612 4e759de4 2019-06-26 stsp }
3613 4e759de4 2019-06-26 stsp
3614 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3615 4e759de4 2019-06-26 stsp if (error != NULL)
3616 4e759de4 2019-06-26 stsp goto done;
3617 4e759de4 2019-06-26 stsp
3618 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3619 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3620 4e759de4 2019-06-26 stsp if (error)
3621 4e759de4 2019-06-26 stsp goto done;
3622 4e759de4 2019-06-26 stsp
3623 ad89fa31 2019-10-04 stsp if (do_show)
3624 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3625 ad89fa31 2019-10-04 stsp else if (do_list)
3626 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3627 4e759de4 2019-06-26 stsp else if (delref)
3628 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3629 4e759de4 2019-06-26 stsp else {
3630 a74f7e83 2019-11-10 stsp struct got_object_id *commit_id;
3631 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3632 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3633 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3634 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3635 a74f7e83 2019-11-10 stsp error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3636 a74f7e83 2019-11-10 stsp if (error)
3637 a74f7e83 2019-11-10 stsp goto done;
3638 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3639 a74f7e83 2019-11-10 stsp free(commit_id);
3640 4e759de4 2019-06-26 stsp }
3641 4e759de4 2019-06-26 stsp done:
3642 d0eebce4 2019-03-11 stsp if (repo)
3643 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3644 d0eebce4 2019-03-11 stsp if (worktree)
3645 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3646 d0eebce4 2019-03-11 stsp free(cwd);
3647 d0eebce4 2019-03-11 stsp free(repo_path);
3648 d00136be 2019-03-26 stsp return error;
3649 d00136be 2019-03-26 stsp }
3650 d00136be 2019-03-26 stsp
3651 8e7bd50a 2019-08-22 stsp
3652 d00136be 2019-03-26 stsp __dead static void
3653 8e7bd50a 2019-08-22 stsp usage_tag(void)
3654 8e7bd50a 2019-08-22 stsp {
3655 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3656 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3657 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3658 8e7bd50a 2019-08-22 stsp exit(1);
3659 b8bad2ba 2019-08-23 stsp }
3660 b8bad2ba 2019-08-23 stsp
3661 b8bad2ba 2019-08-23 stsp #if 0
3662 b8bad2ba 2019-08-23 stsp static const struct got_error *
3663 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3664 b8bad2ba 2019-08-23 stsp {
3665 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3666 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3667 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3668 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3669 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3670 b8bad2ba 2019-08-23 stsp
3671 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3672 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3673 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3674 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3675 b8bad2ba 2019-08-23 stsp if (err)
3676 b8bad2ba 2019-08-23 stsp return err;
3677 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3678 b8bad2ba 2019-08-23 stsp continue;
3679 b8bad2ba 2019-08-23 stsp } else {
3680 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3681 b8bad2ba 2019-08-23 stsp if (err)
3682 b8bad2ba 2019-08-23 stsp break;
3683 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3684 b8bad2ba 2019-08-23 stsp free(re_id);
3685 b8bad2ba 2019-08-23 stsp if (err)
3686 b8bad2ba 2019-08-23 stsp break;
3687 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3688 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3689 b8bad2ba 2019-08-23 stsp }
3690 b8bad2ba 2019-08-23 stsp
3691 b8bad2ba 2019-08-23 stsp while (se) {
3692 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3693 b8bad2ba 2019-08-23 stsp if (err)
3694 b8bad2ba 2019-08-23 stsp break;
3695 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3696 b8bad2ba 2019-08-23 stsp free(se_id);
3697 b8bad2ba 2019-08-23 stsp if (err)
3698 b8bad2ba 2019-08-23 stsp break;
3699 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3700 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3701 b8bad2ba 2019-08-23 stsp
3702 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3703 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3704 b8bad2ba 2019-08-23 stsp if (err)
3705 b8bad2ba 2019-08-23 stsp return err;
3706 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3707 b8bad2ba 2019-08-23 stsp break;
3708 b8bad2ba 2019-08-23 stsp }
3709 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3710 b8bad2ba 2019-08-23 stsp continue;
3711 b8bad2ba 2019-08-23 stsp }
3712 b8bad2ba 2019-08-23 stsp }
3713 b8bad2ba 2019-08-23 stsp done:
3714 b8bad2ba 2019-08-23 stsp return err;
3715 8e7bd50a 2019-08-22 stsp }
3716 b8bad2ba 2019-08-23 stsp #endif
3717 8e7bd50a 2019-08-22 stsp
3718 8e7bd50a 2019-08-22 stsp static const struct got_error *
3719 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3720 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3721 b8bad2ba 2019-08-23 stsp {
3722 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3723 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3724 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3725 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3726 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3727 b8bad2ba 2019-08-23 stsp
3728 b8bad2ba 2019-08-23 stsp *cmp = 0;
3729 b8bad2ba 2019-08-23 stsp
3730 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3731 b8bad2ba 2019-08-23 stsp if (err)
3732 b8bad2ba 2019-08-23 stsp return err;
3733 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3734 b8bad2ba 2019-08-23 stsp if (err)
3735 b8bad2ba 2019-08-23 stsp goto done;
3736 b8bad2ba 2019-08-23 stsp
3737 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3738 b8bad2ba 2019-08-23 stsp if (err)
3739 b8bad2ba 2019-08-23 stsp goto done;
3740 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3741 b8bad2ba 2019-08-23 stsp if (err)
3742 b8bad2ba 2019-08-23 stsp goto done;
3743 b8bad2ba 2019-08-23 stsp
3744 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3745 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3746 b8bad2ba 2019-08-23 stsp
3747 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3748 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3749 b8bad2ba 2019-08-23 stsp *cmp = 1;
3750 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3751 b8bad2ba 2019-08-23 stsp *cmp = -1;
3752 b8bad2ba 2019-08-23 stsp else
3753 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3754 b8bad2ba 2019-08-23 stsp done:
3755 b8bad2ba 2019-08-23 stsp free(id1);
3756 b8bad2ba 2019-08-23 stsp free(id2);
3757 b8bad2ba 2019-08-23 stsp if (tag1)
3758 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3759 b8bad2ba 2019-08-23 stsp if (tag2)
3760 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3761 b8bad2ba 2019-08-23 stsp return err;
3762 b8bad2ba 2019-08-23 stsp }
3763 b8bad2ba 2019-08-23 stsp
3764 b8bad2ba 2019-08-23 stsp static const struct got_error *
3765 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3766 8e7bd50a 2019-08-22 stsp {
3767 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3768 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3769 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3770 8e7bd50a 2019-08-22 stsp
3771 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3772 8e7bd50a 2019-08-22 stsp
3773 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3774 8e7bd50a 2019-08-22 stsp if (err)
3775 8e7bd50a 2019-08-22 stsp return err;
3776 8e7bd50a 2019-08-22 stsp
3777 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3778 8e7bd50a 2019-08-22 stsp const char *refname;
3779 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3780 8e7bd50a 2019-08-22 stsp char datebuf[26];
3781 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3782 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3783 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3784 8e7bd50a 2019-08-22 stsp
3785 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3786 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3787 8e7bd50a 2019-08-22 stsp continue;
3788 8e7bd50a 2019-08-22 stsp refname += 10;
3789 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3790 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3791 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3792 8e7bd50a 2019-08-22 stsp break;
3793 8e7bd50a 2019-08-22 stsp }
3794 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3795 8e7bd50a 2019-08-22 stsp free(refstr);
3796 8e7bd50a 2019-08-22 stsp
3797 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3798 8e7bd50a 2019-08-22 stsp if (err)
3799 8e7bd50a 2019-08-22 stsp break;
3800 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3801 8e7bd50a 2019-08-22 stsp free(id);
3802 8e7bd50a 2019-08-22 stsp if (err)
3803 8e7bd50a 2019-08-22 stsp break;
3804 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3805 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3806 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3807 2417344c 2019-08-23 stsp if (datestr)
3808 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3809 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3810 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3811 8e7bd50a 2019-08-22 stsp if (err)
3812 8e7bd50a 2019-08-22 stsp break;
3813 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3814 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3815 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3816 8e7bd50a 2019-08-22 stsp break;
3817 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3818 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3819 8e7bd50a 2019-08-22 stsp break;
3820 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3821 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3822 8e7bd50a 2019-08-22 stsp break;
3823 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3824 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3825 8e7bd50a 2019-08-22 stsp break;
3826 8e7bd50a 2019-08-22 stsp default:
3827 8e7bd50a 2019-08-22 stsp break;
3828 8e7bd50a 2019-08-22 stsp }
3829 8e7bd50a 2019-08-22 stsp free(id_str);
3830 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3831 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3832 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3833 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3834 8e7bd50a 2019-08-22 stsp break;
3835 8e7bd50a 2019-08-22 stsp }
3836 8e7bd50a 2019-08-22 stsp
3837 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3838 8e7bd50a 2019-08-22 stsp do {
3839 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3840 8e7bd50a 2019-08-22 stsp if (line)
3841 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3842 8e7bd50a 2019-08-22 stsp } while (line);
3843 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3844 8e7bd50a 2019-08-22 stsp }
3845 8e7bd50a 2019-08-22 stsp
3846 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3847 8e7bd50a 2019-08-22 stsp return NULL;
3848 8e7bd50a 2019-08-22 stsp }
3849 8e7bd50a 2019-08-22 stsp
3850 8e7bd50a 2019-08-22 stsp static const struct got_error *
3851 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3852 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3853 8e7bd50a 2019-08-22 stsp {
3854 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3855 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3856 f372d5cd 2019-10-21 stsp char *editor = NULL;
3857 8e7bd50a 2019-08-22 stsp int fd = -1;
3858 8e7bd50a 2019-08-22 stsp
3859 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3860 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3861 8e7bd50a 2019-08-22 stsp goto done;
3862 8e7bd50a 2019-08-22 stsp }
3863 8e7bd50a 2019-08-22 stsp
3864 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3865 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3866 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3867 8e7bd50a 2019-08-22 stsp goto done;
3868 8e7bd50a 2019-08-22 stsp }
3869 8e7bd50a 2019-08-22 stsp
3870 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3871 8e7bd50a 2019-08-22 stsp if (err)
3872 8e7bd50a 2019-08-22 stsp goto done;
3873 8e7bd50a 2019-08-22 stsp
3874 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3875 8e7bd50a 2019-08-22 stsp close(fd);
3876 8e7bd50a 2019-08-22 stsp
3877 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3878 8e7bd50a 2019-08-22 stsp if (err)
3879 8e7bd50a 2019-08-22 stsp goto done;
3880 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3881 8e7bd50a 2019-08-22 stsp done:
3882 8e7bd50a 2019-08-22 stsp free(initial_content);
3883 8e7bd50a 2019-08-22 stsp free(template);
3884 8e7bd50a 2019-08-22 stsp free(editor);
3885 8e7bd50a 2019-08-22 stsp
3886 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3887 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3888 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3889 8e7bd50a 2019-08-22 stsp if (err) {
3890 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3891 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3892 8e7bd50a 2019-08-22 stsp }
3893 8e7bd50a 2019-08-22 stsp }
3894 8e7bd50a 2019-08-22 stsp return err;
3895 8e7bd50a 2019-08-22 stsp }
3896 8e7bd50a 2019-08-22 stsp
3897 8e7bd50a 2019-08-22 stsp static const struct got_error *
3898 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3899 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3900 8e7bd50a 2019-08-22 stsp {
3901 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3902 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3903 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3904 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3905 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3906 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3907 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3908 8e7bd50a 2019-08-22 stsp
3909 8e7bd50a 2019-08-22 stsp /*
3910 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3911 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3912 8e7bd50a 2019-08-22 stsp * an unintended typo.
3913 8e7bd50a 2019-08-22 stsp */
3914 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3915 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3916 8e7bd50a 2019-08-22 stsp
3917 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3918 8e7bd50a 2019-08-22 stsp if (err)
3919 8e7bd50a 2019-08-22 stsp return err;
3920 8e7bd50a 2019-08-22 stsp
3921 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3922 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3923 8e7bd50a 2019-08-22 stsp if (err)
3924 8e7bd50a 2019-08-22 stsp goto done;
3925 8e7bd50a 2019-08-22 stsp
3926 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3927 8e7bd50a 2019-08-22 stsp if (err)
3928 8e7bd50a 2019-08-22 stsp goto done;
3929 8e7bd50a 2019-08-22 stsp
3930 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3931 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3932 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3933 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3934 8e7bd50a 2019-08-22 stsp goto done;
3935 8e7bd50a 2019-08-22 stsp }
3936 8e7bd50a 2019-08-22 stsp tag_name += 10;
3937 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3938 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3939 8e7bd50a 2019-08-22 stsp goto done;
3940 8e7bd50a 2019-08-22 stsp }
3941 8e7bd50a 2019-08-22 stsp
3942 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3943 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3944 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3945 8e7bd50a 2019-08-22 stsp goto done;
3946 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3947 8e7bd50a 2019-08-22 stsp goto done;
3948 8e7bd50a 2019-08-22 stsp
3949 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3950 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3951 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3952 f372d5cd 2019-10-21 stsp if (err) {
3953 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3954 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3955 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3956 8e7bd50a 2019-08-22 stsp goto done;
3957 f372d5cd 2019-10-21 stsp }
3958 8e7bd50a 2019-08-22 stsp }
3959 8e7bd50a 2019-08-22 stsp
3960 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3961 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3962 f372d5cd 2019-10-21 stsp if (err) {
3963 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3964 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3965 8e7bd50a 2019-08-22 stsp goto done;
3966 f372d5cd 2019-10-21 stsp }
3967 8e7bd50a 2019-08-22 stsp
3968 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3969 f372d5cd 2019-10-21 stsp if (err) {
3970 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3971 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3972 8e7bd50a 2019-08-22 stsp goto done;
3973 f372d5cd 2019-10-21 stsp }
3974 8e7bd50a 2019-08-22 stsp
3975 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3976 f372d5cd 2019-10-21 stsp if (err) {
3977 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3978 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3979 f372d5cd 2019-10-21 stsp goto done;
3980 f372d5cd 2019-10-21 stsp }
3981 8e7bd50a 2019-08-22 stsp
3982 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
3983 f372d5cd 2019-10-21 stsp if (err) {
3984 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3985 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3986 f372d5cd 2019-10-21 stsp goto done;
3987 8e7bd50a 2019-08-22 stsp }
3988 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
3989 8e7bd50a 2019-08-22 stsp done:
3990 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
3991 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
3992 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
3993 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3994 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
3995 f372d5cd 2019-10-21 stsp free(tag_id_str);
3996 8e7bd50a 2019-08-22 stsp if (ref)
3997 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3998 8e7bd50a 2019-08-22 stsp free(commit_id);
3999 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4000 8e7bd50a 2019-08-22 stsp free(refname);
4001 8e7bd50a 2019-08-22 stsp free(tagmsg);
4002 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4003 aba9c984 2019-09-08 stsp free(tagger);
4004 8e7bd50a 2019-08-22 stsp return err;
4005 8e7bd50a 2019-08-22 stsp }
4006 8e7bd50a 2019-08-22 stsp
4007 8e7bd50a 2019-08-22 stsp static const struct got_error *
4008 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4009 8e7bd50a 2019-08-22 stsp {
4010 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4011 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4012 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4013 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4014 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4015 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4016 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4017 8e7bd50a 2019-08-22 stsp
4018 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4019 8e7bd50a 2019-08-22 stsp switch (ch) {
4020 8e7bd50a 2019-08-22 stsp case 'm':
4021 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4022 8e7bd50a 2019-08-22 stsp break;
4023 8e7bd50a 2019-08-22 stsp case 'r':
4024 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4025 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4026 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4027 9ba1d308 2019-10-21 stsp optarg);
4028 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4029 8e7bd50a 2019-08-22 stsp break;
4030 8e7bd50a 2019-08-22 stsp case 'l':
4031 8e7bd50a 2019-08-22 stsp do_list = 1;
4032 8e7bd50a 2019-08-22 stsp break;
4033 8e7bd50a 2019-08-22 stsp default:
4034 8e7bd50a 2019-08-22 stsp usage_tag();
4035 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4036 8e7bd50a 2019-08-22 stsp }
4037 8e7bd50a 2019-08-22 stsp }
4038 8e7bd50a 2019-08-22 stsp
4039 8e7bd50a 2019-08-22 stsp argc -= optind;
4040 8e7bd50a 2019-08-22 stsp argv += optind;
4041 8e7bd50a 2019-08-22 stsp
4042 8e7bd50a 2019-08-22 stsp if (do_list) {
4043 8e7bd50a 2019-08-22 stsp if (tagmsg)
4044 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
4045 8e7bd50a 2019-08-22 stsp if (argc > 0)
4046 8e7bd50a 2019-08-22 stsp usage_tag();
4047 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
4048 8e7bd50a 2019-08-22 stsp usage_tag();
4049 a2887370 2019-08-23 stsp else if (argc > 1)
4050 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
4051 a2887370 2019-08-23 stsp tag_name = argv[0];
4052 8e7bd50a 2019-08-22 stsp
4053 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4054 8e7bd50a 2019-08-22 stsp if (do_list) {
4055 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4056 8e7bd50a 2019-08-22 stsp NULL) == -1)
4057 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4058 8e7bd50a 2019-08-22 stsp } else {
4059 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4060 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4061 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4062 8e7bd50a 2019-08-22 stsp }
4063 8e7bd50a 2019-08-22 stsp #endif
4064 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4065 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4066 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4067 8e7bd50a 2019-08-22 stsp goto done;
4068 8e7bd50a 2019-08-22 stsp }
4069 8e7bd50a 2019-08-22 stsp
4070 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4071 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4072 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4073 8e7bd50a 2019-08-22 stsp goto done;
4074 8e7bd50a 2019-08-22 stsp else
4075 8e7bd50a 2019-08-22 stsp error = NULL;
4076 8e7bd50a 2019-08-22 stsp if (worktree) {
4077 8e7bd50a 2019-08-22 stsp repo_path =
4078 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4079 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4080 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4081 8e7bd50a 2019-08-22 stsp if (error)
4082 8e7bd50a 2019-08-22 stsp goto done;
4083 8e7bd50a 2019-08-22 stsp } else {
4084 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4085 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4086 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4087 8e7bd50a 2019-08-22 stsp goto done;
4088 8e7bd50a 2019-08-22 stsp }
4089 8e7bd50a 2019-08-22 stsp }
4090 8e7bd50a 2019-08-22 stsp }
4091 8e7bd50a 2019-08-22 stsp
4092 8e7bd50a 2019-08-22 stsp if (do_list) {
4093 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4094 c9956ddf 2019-09-08 stsp if (error != NULL)
4095 c9956ddf 2019-09-08 stsp goto done;
4096 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4097 8e7bd50a 2019-08-22 stsp if (error)
4098 8e7bd50a 2019-08-22 stsp goto done;
4099 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4100 8e7bd50a 2019-08-22 stsp } else {
4101 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4102 c9956ddf 2019-09-08 stsp if (error)
4103 c9956ddf 2019-09-08 stsp goto done;
4104 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4105 c9956ddf 2019-09-08 stsp if (error != NULL)
4106 c9956ddf 2019-09-08 stsp goto done;
4107 c9956ddf 2019-09-08 stsp
4108 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4109 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4110 8e7bd50a 2019-08-22 stsp if (error)
4111 8e7bd50a 2019-08-22 stsp goto done;
4112 8e7bd50a 2019-08-22 stsp }
4113 8e7bd50a 2019-08-22 stsp
4114 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4115 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4116 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4117 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4118 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4119 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4120 8e7bd50a 2019-08-22 stsp if (error)
4121 8e7bd50a 2019-08-22 stsp goto done;
4122 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4123 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4124 8e7bd50a 2019-08-22 stsp if (error)
4125 8e7bd50a 2019-08-22 stsp goto done;
4126 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4127 8e7bd50a 2019-08-22 stsp free(commit_id);
4128 8e7bd50a 2019-08-22 stsp if (error)
4129 8e7bd50a 2019-08-22 stsp goto done;
4130 8e7bd50a 2019-08-22 stsp }
4131 8e7bd50a 2019-08-22 stsp
4132 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4133 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4134 8e7bd50a 2019-08-22 stsp }
4135 8e7bd50a 2019-08-22 stsp done:
4136 8e7bd50a 2019-08-22 stsp if (repo)
4137 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4138 8e7bd50a 2019-08-22 stsp if (worktree)
4139 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4140 8e7bd50a 2019-08-22 stsp free(cwd);
4141 8e7bd50a 2019-08-22 stsp free(repo_path);
4142 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4143 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4144 8e7bd50a 2019-08-22 stsp return error;
4145 8e7bd50a 2019-08-22 stsp }
4146 8e7bd50a 2019-08-22 stsp
4147 8e7bd50a 2019-08-22 stsp __dead static void
4148 d00136be 2019-03-26 stsp usage_add(void)
4149 d00136be 2019-03-26 stsp {
4150 022fae89 2019-12-06 tracey fprintf(stderr, "usage: %s add [-R] [-I] file-path ...\n",
4151 022fae89 2019-12-06 tracey getprogname());
4152 d00136be 2019-03-26 stsp exit(1);
4153 d00136be 2019-03-26 stsp }
4154 d00136be 2019-03-26 stsp
4155 d00136be 2019-03-26 stsp static const struct got_error *
4156 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4157 4e68cba3 2019-11-23 stsp {
4158 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4159 4e68cba3 2019-11-23 stsp path++;
4160 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4161 4e68cba3 2019-11-23 stsp return NULL;
4162 4e68cba3 2019-11-23 stsp }
4163 4e68cba3 2019-11-23 stsp
4164 4e68cba3 2019-11-23 stsp static const struct got_error *
4165 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4166 d00136be 2019-03-26 stsp {
4167 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4168 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4169 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4170 1dd54920 2019-05-11 stsp char *cwd = NULL;
4171 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4172 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4173 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4174 1dd54920 2019-05-11 stsp
4175 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4176 d00136be 2019-03-26 stsp
4177 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4178 d00136be 2019-03-26 stsp switch (ch) {
4179 022fae89 2019-12-06 tracey case 'I':
4180 022fae89 2019-12-06 tracey no_ignores = 1;
4181 022fae89 2019-12-06 tracey break;
4182 4e68cba3 2019-11-23 stsp case 'R':
4183 4e68cba3 2019-11-23 stsp can_recurse = 1;
4184 4e68cba3 2019-11-23 stsp break;
4185 d00136be 2019-03-26 stsp default:
4186 d00136be 2019-03-26 stsp usage_add();
4187 d00136be 2019-03-26 stsp /* NOTREACHED */
4188 d00136be 2019-03-26 stsp }
4189 d00136be 2019-03-26 stsp }
4190 d00136be 2019-03-26 stsp
4191 d00136be 2019-03-26 stsp argc -= optind;
4192 d00136be 2019-03-26 stsp argv += optind;
4193 d00136be 2019-03-26 stsp
4194 43012d58 2019-07-14 stsp #ifndef PROFILE
4195 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4196 43012d58 2019-07-14 stsp NULL) == -1)
4197 43012d58 2019-07-14 stsp err(1, "pledge");
4198 43012d58 2019-07-14 stsp #endif
4199 723c305c 2019-05-11 jcs if (argc < 1)
4200 d00136be 2019-03-26 stsp usage_add();
4201 d00136be 2019-03-26 stsp
4202 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4203 d00136be 2019-03-26 stsp if (cwd == NULL) {
4204 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4205 d00136be 2019-03-26 stsp goto done;
4206 d00136be 2019-03-26 stsp }
4207 723c305c 2019-05-11 jcs
4208 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4209 d00136be 2019-03-26 stsp if (error)
4210 d00136be 2019-03-26 stsp goto done;
4211 d00136be 2019-03-26 stsp
4212 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4213 c9956ddf 2019-09-08 stsp NULL);
4214 031a5338 2019-03-26 stsp if (error != NULL)
4215 031a5338 2019-03-26 stsp goto done;
4216 031a5338 2019-03-26 stsp
4217 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4218 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4219 d00136be 2019-03-26 stsp if (error)
4220 d00136be 2019-03-26 stsp goto done;
4221 d00136be 2019-03-26 stsp
4222 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4223 6d022e97 2019-08-04 stsp if (error)
4224 022fae89 2019-12-06 tracey goto done;
4225 022fae89 2019-12-06 tracey
4226 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4227 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4228 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4229 6d022e97 2019-08-04 stsp goto done;
4230 723c305c 2019-05-11 jcs
4231 022fae89 2019-12-06 tracey }
4232 022fae89 2019-12-06 tracey
4233 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4234 4e68cba3 2019-11-23 stsp char *ondisk_path;
4235 4e68cba3 2019-11-23 stsp struct stat sb;
4236 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4237 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4238 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4239 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4240 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4241 4e68cba3 2019-11-23 stsp goto done;
4242 4e68cba3 2019-11-23 stsp }
4243 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4244 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4245 4e68cba3 2019-11-23 stsp free(ondisk_path);
4246 4e68cba3 2019-11-23 stsp continue;
4247 4e68cba3 2019-11-23 stsp }
4248 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4249 4e68cba3 2019-11-23 stsp ondisk_path);
4250 4e68cba3 2019-11-23 stsp free(ondisk_path);
4251 4e68cba3 2019-11-23 stsp goto done;
4252 4e68cba3 2019-11-23 stsp }
4253 4e68cba3 2019-11-23 stsp free(ondisk_path);
4254 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4255 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4256 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4257 4e68cba3 2019-11-23 stsp goto done;
4258 4e68cba3 2019-11-23 stsp }
4259 4e68cba3 2019-11-23 stsp }
4260 4e68cba3 2019-11-23 stsp }
4261 022fae89 2019-12-06 tracey
4262 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4263 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4264 d00136be 2019-03-26 stsp done:
4265 031a5338 2019-03-26 stsp if (repo)
4266 031a5338 2019-03-26 stsp got_repo_close(repo);
4267 d00136be 2019-03-26 stsp if (worktree)
4268 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4269 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4270 1dd54920 2019-05-11 stsp free((char *)pe->path);
4271 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4272 2ec1f75b 2019-03-26 stsp free(cwd);
4273 2ec1f75b 2019-03-26 stsp return error;
4274 2ec1f75b 2019-03-26 stsp }
4275 2ec1f75b 2019-03-26 stsp
4276 2ec1f75b 2019-03-26 stsp __dead static void
4277 648e4ef7 2019-07-09 stsp usage_remove(void)
4278 2ec1f75b 2019-03-26 stsp {
4279 f2a9dc41 2019-12-13 tracey fprintf(stderr, "usage: %s remove [-f] [-R] file-path ...\n",
4280 f2a9dc41 2019-12-13 tracey getprogname());
4281 2ec1f75b 2019-03-26 stsp exit(1);
4282 2a06fe5f 2019-08-24 stsp }
4283 2a06fe5f 2019-08-24 stsp
4284 2a06fe5f 2019-08-24 stsp static const struct got_error *
4285 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4286 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4287 2a06fe5f 2019-08-24 stsp {
4288 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4289 f2a9dc41 2019-12-13 tracey path++;
4290 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4291 2a06fe5f 2019-08-24 stsp return NULL;
4292 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4293 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4294 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4295 2a06fe5f 2019-08-24 stsp return NULL;
4296 2ec1f75b 2019-03-26 stsp }
4297 2ec1f75b 2019-03-26 stsp
4298 2ec1f75b 2019-03-26 stsp static const struct got_error *
4299 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4300 2ec1f75b 2019-03-26 stsp {
4301 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4302 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4303 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4304 17ed4618 2019-06-02 stsp char *cwd = NULL;
4305 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4306 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4307 f2a9dc41 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0;
4308 2ec1f75b 2019-03-26 stsp
4309 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4310 17ed4618 2019-06-02 stsp
4311 f2a9dc41 2019-12-13 tracey while ((ch = getopt(argc, argv, "fR")) != -1) {
4312 2ec1f75b 2019-03-26 stsp switch (ch) {
4313 2ec1f75b 2019-03-26 stsp case 'f':
4314 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4315 2ec1f75b 2019-03-26 stsp break;
4316 f2a9dc41 2019-12-13 tracey case 'R':
4317 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4318 f2a9dc41 2019-12-13 tracey break;
4319 2ec1f75b 2019-03-26 stsp default:
4320 f2a9dc41 2019-12-13 tracey usage_remove();
4321 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4322 2ec1f75b 2019-03-26 stsp }
4323 2ec1f75b 2019-03-26 stsp }
4324 2ec1f75b 2019-03-26 stsp
4325 2ec1f75b 2019-03-26 stsp argc -= optind;
4326 2ec1f75b 2019-03-26 stsp argv += optind;
4327 2ec1f75b 2019-03-26 stsp
4328 43012d58 2019-07-14 stsp #ifndef PROFILE
4329 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4330 43012d58 2019-07-14 stsp NULL) == -1)
4331 43012d58 2019-07-14 stsp err(1, "pledge");
4332 43012d58 2019-07-14 stsp #endif
4333 17ed4618 2019-06-02 stsp if (argc < 1)
4334 648e4ef7 2019-07-09 stsp usage_remove();
4335 2ec1f75b 2019-03-26 stsp
4336 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4337 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4338 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4339 2ec1f75b 2019-03-26 stsp goto done;
4340 2ec1f75b 2019-03-26 stsp }
4341 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4342 2ec1f75b 2019-03-26 stsp if (error)
4343 2ec1f75b 2019-03-26 stsp goto done;
4344 2ec1f75b 2019-03-26 stsp
4345 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4346 c9956ddf 2019-09-08 stsp NULL);
4347 2af4a041 2019-05-11 jcs if (error)
4348 2ec1f75b 2019-03-26 stsp goto done;
4349 2ec1f75b 2019-03-26 stsp
4350 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4351 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4352 2ec1f75b 2019-03-26 stsp if (error)
4353 2ec1f75b 2019-03-26 stsp goto done;
4354 2ec1f75b 2019-03-26 stsp
4355 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4356 6d022e97 2019-08-04 stsp if (error)
4357 6d022e97 2019-08-04 stsp goto done;
4358 17ed4618 2019-06-02 stsp
4359 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4360 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4361 f2a9dc41 2019-12-13 tracey struct stat sb;
4362 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4363 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4364 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4365 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4366 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4367 f2a9dc41 2019-12-13 tracey goto done;
4368 f2a9dc41 2019-12-13 tracey }
4369 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4370 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4371 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4372 f2a9dc41 2019-12-13 tracey continue;
4373 f2a9dc41 2019-12-13 tracey }
4374 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4375 f2a9dc41 2019-12-13 tracey ondisk_path);
4376 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4377 f2a9dc41 2019-12-13 tracey goto done;
4378 f2a9dc41 2019-12-13 tracey }
4379 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4380 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4381 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4382 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4383 f2a9dc41 2019-12-13 tracey goto done;
4384 f2a9dc41 2019-12-13 tracey }
4385 f2a9dc41 2019-12-13 tracey }
4386 f2a9dc41 2019-12-13 tracey }
4387 f2a9dc41 2019-12-13 tracey
4388 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4389 2a06fe5f 2019-08-24 stsp delete_local_mods, print_remove_status, NULL, repo);
4390 a129376b 2019-03-28 stsp if (error)
4391 a129376b 2019-03-28 stsp goto done;
4392 a129376b 2019-03-28 stsp done:
4393 a129376b 2019-03-28 stsp if (repo)
4394 a129376b 2019-03-28 stsp got_repo_close(repo);
4395 a129376b 2019-03-28 stsp if (worktree)
4396 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4397 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4398 17ed4618 2019-06-02 stsp free((char *)pe->path);
4399 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4400 a129376b 2019-03-28 stsp free(cwd);
4401 a129376b 2019-03-28 stsp return error;
4402 a129376b 2019-03-28 stsp }
4403 a129376b 2019-03-28 stsp
4404 a129376b 2019-03-28 stsp __dead static void
4405 a129376b 2019-03-28 stsp usage_revert(void)
4406 a129376b 2019-03-28 stsp {
4407 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4408 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4409 a129376b 2019-03-28 stsp exit(1);
4410 a129376b 2019-03-28 stsp }
4411 a129376b 2019-03-28 stsp
4412 1ee397ad 2019-07-12 stsp static const struct got_error *
4413 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4414 a129376b 2019-03-28 stsp {
4415 a129376b 2019-03-28 stsp while (path[0] == '/')
4416 a129376b 2019-03-28 stsp path++;
4417 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4418 33aa809d 2019-08-08 stsp return NULL;
4419 33aa809d 2019-08-08 stsp }
4420 33aa809d 2019-08-08 stsp
4421 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4422 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4423 33aa809d 2019-08-08 stsp const char *action;
4424 33aa809d 2019-08-08 stsp };
4425 33aa809d 2019-08-08 stsp
4426 33aa809d 2019-08-08 stsp static const struct got_error *
4427 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4428 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4429 33aa809d 2019-08-08 stsp {
4430 33aa809d 2019-08-08 stsp char *line = NULL;
4431 33aa809d 2019-08-08 stsp size_t linesize = 0;
4432 33aa809d 2019-08-08 stsp ssize_t linelen;
4433 33aa809d 2019-08-08 stsp
4434 33aa809d 2019-08-08 stsp switch (status) {
4435 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4436 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4437 33aa809d 2019-08-08 stsp break;
4438 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4439 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4440 33aa809d 2019-08-08 stsp break;
4441 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4442 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4443 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4444 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4445 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4446 33aa809d 2019-08-08 stsp printf("%s", line);
4447 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4448 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4449 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4450 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4451 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4452 33aa809d 2019-08-08 stsp break;
4453 33aa809d 2019-08-08 stsp default:
4454 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4455 33aa809d 2019-08-08 stsp }
4456 33aa809d 2019-08-08 stsp
4457 33aa809d 2019-08-08 stsp return NULL;
4458 33aa809d 2019-08-08 stsp }
4459 33aa809d 2019-08-08 stsp
4460 33aa809d 2019-08-08 stsp static const struct got_error *
4461 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4462 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4463 33aa809d 2019-08-08 stsp {
4464 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4465 33aa809d 2019-08-08 stsp char *line = NULL;
4466 33aa809d 2019-08-08 stsp size_t linesize = 0;
4467 33aa809d 2019-08-08 stsp ssize_t linelen;
4468 33aa809d 2019-08-08 stsp int resp = ' ';
4469 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4470 33aa809d 2019-08-08 stsp
4471 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4472 33aa809d 2019-08-08 stsp
4473 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4474 33aa809d 2019-08-08 stsp char *nl;
4475 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4476 33aa809d 2019-08-08 stsp a->action);
4477 33aa809d 2019-08-08 stsp if (err)
4478 33aa809d 2019-08-08 stsp return err;
4479 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4480 33aa809d 2019-08-08 stsp if (linelen == -1) {
4481 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4482 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4483 33aa809d 2019-08-08 stsp return NULL;
4484 33aa809d 2019-08-08 stsp }
4485 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4486 33aa809d 2019-08-08 stsp if (nl)
4487 33aa809d 2019-08-08 stsp *nl = '\0';
4488 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4489 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4490 33aa809d 2019-08-08 stsp printf("y\n");
4491 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4492 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4493 33aa809d 2019-08-08 stsp printf("n\n");
4494 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4495 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4496 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4497 33aa809d 2019-08-08 stsp printf("q\n");
4498 33aa809d 2019-08-08 stsp } else
4499 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4500 33aa809d 2019-08-08 stsp free(line);
4501 33aa809d 2019-08-08 stsp return NULL;
4502 33aa809d 2019-08-08 stsp }
4503 33aa809d 2019-08-08 stsp
4504 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4505 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4506 33aa809d 2019-08-08 stsp a->action);
4507 33aa809d 2019-08-08 stsp if (err)
4508 33aa809d 2019-08-08 stsp return err;
4509 33aa809d 2019-08-08 stsp resp = getchar();
4510 33aa809d 2019-08-08 stsp if (resp == '\n')
4511 33aa809d 2019-08-08 stsp resp = getchar();
4512 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4513 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4514 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4515 33aa809d 2019-08-08 stsp resp = ' ';
4516 33aa809d 2019-08-08 stsp }
4517 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4518 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4519 33aa809d 2019-08-08 stsp resp = ' ';
4520 33aa809d 2019-08-08 stsp }
4521 33aa809d 2019-08-08 stsp }
4522 33aa809d 2019-08-08 stsp
4523 33aa809d 2019-08-08 stsp if (resp == 'y')
4524 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4525 33aa809d 2019-08-08 stsp else if (resp == 'n')
4526 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4527 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4528 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4529 33aa809d 2019-08-08 stsp
4530 1ee397ad 2019-07-12 stsp return NULL;
4531 a129376b 2019-03-28 stsp }
4532 a129376b 2019-03-28 stsp
4533 33aa809d 2019-08-08 stsp
4534 a129376b 2019-03-28 stsp static const struct got_error *
4535 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4536 a129376b 2019-03-28 stsp {
4537 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4538 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4539 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4540 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4541 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4542 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4543 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4544 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4545 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4546 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4547 a129376b 2019-03-28 stsp
4548 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4549 e20a8b6f 2019-06-04 stsp
4550 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4551 a129376b 2019-03-28 stsp switch (ch) {
4552 33aa809d 2019-08-08 stsp case 'p':
4553 33aa809d 2019-08-08 stsp pflag = 1;
4554 33aa809d 2019-08-08 stsp break;
4555 33aa809d 2019-08-08 stsp case 'F':
4556 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4557 33aa809d 2019-08-08 stsp break;
4558 0f6d7415 2019-08-08 stsp case 'R':
4559 0f6d7415 2019-08-08 stsp can_recurse = 1;
4560 0f6d7415 2019-08-08 stsp break;
4561 a129376b 2019-03-28 stsp default:
4562 a129376b 2019-03-28 stsp usage_revert();
4563 a129376b 2019-03-28 stsp /* NOTREACHED */
4564 a129376b 2019-03-28 stsp }
4565 a129376b 2019-03-28 stsp }
4566 a129376b 2019-03-28 stsp
4567 a129376b 2019-03-28 stsp argc -= optind;
4568 a129376b 2019-03-28 stsp argv += optind;
4569 a129376b 2019-03-28 stsp
4570 43012d58 2019-07-14 stsp #ifndef PROFILE
4571 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4572 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4573 43012d58 2019-07-14 stsp err(1, "pledge");
4574 43012d58 2019-07-14 stsp #endif
4575 e20a8b6f 2019-06-04 stsp if (argc < 1)
4576 a129376b 2019-03-28 stsp usage_revert();
4577 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4578 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4579 a129376b 2019-03-28 stsp
4580 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4581 a129376b 2019-03-28 stsp if (cwd == NULL) {
4582 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4583 a129376b 2019-03-28 stsp goto done;
4584 a129376b 2019-03-28 stsp }
4585 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4586 2ec1f75b 2019-03-26 stsp if (error)
4587 2ec1f75b 2019-03-26 stsp goto done;
4588 a129376b 2019-03-28 stsp
4589 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4590 c9956ddf 2019-09-08 stsp NULL);
4591 a129376b 2019-03-28 stsp if (error != NULL)
4592 a129376b 2019-03-28 stsp goto done;
4593 a129376b 2019-03-28 stsp
4594 33aa809d 2019-08-08 stsp if (patch_script_path) {
4595 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4596 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4597 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4598 33aa809d 2019-08-08 stsp patch_script_path);
4599 33aa809d 2019-08-08 stsp goto done;
4600 33aa809d 2019-08-08 stsp }
4601 33aa809d 2019-08-08 stsp }
4602 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4603 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4604 a129376b 2019-03-28 stsp if (error)
4605 a129376b 2019-03-28 stsp goto done;
4606 a129376b 2019-03-28 stsp
4607 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4608 6d022e97 2019-08-04 stsp if (error)
4609 6d022e97 2019-08-04 stsp goto done;
4610 00db391e 2019-08-03 stsp
4611 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4612 0f6d7415 2019-08-08 stsp char *ondisk_path;
4613 0f6d7415 2019-08-08 stsp struct stat sb;
4614 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4615 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4616 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4617 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4618 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4619 0f6d7415 2019-08-08 stsp goto done;
4620 0f6d7415 2019-08-08 stsp }
4621 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4622 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4623 0f6d7415 2019-08-08 stsp free(ondisk_path);
4624 0f6d7415 2019-08-08 stsp continue;
4625 0f6d7415 2019-08-08 stsp }
4626 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4627 0f6d7415 2019-08-08 stsp ondisk_path);
4628 0f6d7415 2019-08-08 stsp free(ondisk_path);
4629 0f6d7415 2019-08-08 stsp goto done;
4630 0f6d7415 2019-08-08 stsp }
4631 0f6d7415 2019-08-08 stsp free(ondisk_path);
4632 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4633 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4634 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4635 0f6d7415 2019-08-08 stsp goto done;
4636 0f6d7415 2019-08-08 stsp }
4637 0f6d7415 2019-08-08 stsp }
4638 0f6d7415 2019-08-08 stsp }
4639 0f6d7415 2019-08-08 stsp
4640 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4641 33aa809d 2019-08-08 stsp cpa.action = "revert";
4642 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4643 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4644 a129376b 2019-03-28 stsp if (error)
4645 a129376b 2019-03-28 stsp goto done;
4646 2ec1f75b 2019-03-26 stsp done:
4647 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4648 33aa809d 2019-08-08 stsp error == NULL)
4649 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4650 2ec1f75b 2019-03-26 stsp if (repo)
4651 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4652 2ec1f75b 2019-03-26 stsp if (worktree)
4653 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4654 2ec1f75b 2019-03-26 stsp free(path);
4655 d00136be 2019-03-26 stsp free(cwd);
4656 6bad629b 2019-02-04 stsp return error;
4657 c4296144 2019-05-09 stsp }
4658 c4296144 2019-05-09 stsp
4659 c4296144 2019-05-09 stsp __dead static void
4660 c4296144 2019-05-09 stsp usage_commit(void)
4661 c4296144 2019-05-09 stsp {
4662 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4663 5c1e53bc 2019-07-28 stsp getprogname());
4664 c4296144 2019-05-09 stsp exit(1);
4665 33ad4cbe 2019-05-12 jcs }
4666 33ad4cbe 2019-05-12 jcs
4667 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4668 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4669 e2ba3d07 2019-05-13 stsp const char *editor;
4670 e0870e44 2019-05-13 stsp const char *worktree_path;
4671 76d98825 2019-06-03 stsp const char *branch_name;
4672 314a6357 2019-05-13 stsp const char *repo_path;
4673 e0870e44 2019-05-13 stsp char *logmsg_path;
4674 e2ba3d07 2019-05-13 stsp
4675 e2ba3d07 2019-05-13 stsp };
4676 e0870e44 2019-05-13 stsp
4677 33ad4cbe 2019-05-12 jcs static const struct got_error *
4678 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4679 33ad4cbe 2019-05-12 jcs void *arg)
4680 33ad4cbe 2019-05-12 jcs {
4681 76d98825 2019-06-03 stsp char *initial_content = NULL;
4682 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4683 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4684 e0870e44 2019-05-13 stsp char *template = NULL;
4685 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4686 3ce1b845 2019-07-15 stsp int fd;
4687 33ad4cbe 2019-05-12 jcs size_t len;
4688 33ad4cbe 2019-05-12 jcs
4689 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4690 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4691 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4692 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4693 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4694 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4695 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4696 33ad4cbe 2019-05-12 jcs return NULL;
4697 33ad4cbe 2019-05-12 jcs }
4698 33ad4cbe 2019-05-12 jcs
4699 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4700 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4701 76d98825 2019-06-03 stsp
4702 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4703 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4704 76d98825 2019-06-03 stsp a->branch_name) == -1)
4705 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4706 e0870e44 2019-05-13 stsp
4707 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4708 793c30b5 2019-05-13 stsp if (err)
4709 e0870e44 2019-05-13 stsp goto done;
4710 33ad4cbe 2019-05-12 jcs
4711 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4712 33ad4cbe 2019-05-12 jcs
4713 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4714 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4715 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4716 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4717 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4718 33ad4cbe 2019-05-12 jcs }
4719 33ad4cbe 2019-05-12 jcs close(fd);
4720 33ad4cbe 2019-05-12 jcs
4721 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4722 33ad4cbe 2019-05-12 jcs done:
4723 76d98825 2019-06-03 stsp free(initial_content);
4724 e0870e44 2019-05-13 stsp free(template);
4725 314a6357 2019-05-13 stsp
4726 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4727 3ce1b845 2019-07-15 stsp if (err == NULL) {
4728 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4729 3ce1b845 2019-07-15 stsp if (err) {
4730 3ce1b845 2019-07-15 stsp free(*logmsg);
4731 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4732 3ce1b845 2019-07-15 stsp }
4733 3ce1b845 2019-07-15 stsp }
4734 33ad4cbe 2019-05-12 jcs return err;
4735 6bad629b 2019-02-04 stsp }
4736 c4296144 2019-05-09 stsp
4737 c4296144 2019-05-09 stsp static const struct got_error *
4738 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4739 c4296144 2019-05-09 stsp {
4740 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4741 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4742 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4743 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4744 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4745 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4746 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4747 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4748 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4749 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4750 5c1e53bc 2019-07-28 stsp
4751 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4752 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4753 c4296144 2019-05-09 stsp
4754 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4755 c4296144 2019-05-09 stsp switch (ch) {
4756 c4296144 2019-05-09 stsp case 'm':
4757 c4296144 2019-05-09 stsp logmsg = optarg;
4758 c4296144 2019-05-09 stsp break;
4759 c4296144 2019-05-09 stsp default:
4760 c4296144 2019-05-09 stsp usage_commit();
4761 c4296144 2019-05-09 stsp /* NOTREACHED */
4762 c4296144 2019-05-09 stsp }
4763 c4296144 2019-05-09 stsp }
4764 c4296144 2019-05-09 stsp
4765 c4296144 2019-05-09 stsp argc -= optind;
4766 c4296144 2019-05-09 stsp argv += optind;
4767 c4296144 2019-05-09 stsp
4768 43012d58 2019-07-14 stsp #ifndef PROFILE
4769 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4770 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4771 43012d58 2019-07-14 stsp err(1, "pledge");
4772 43012d58 2019-07-14 stsp #endif
4773 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4774 c4296144 2019-05-09 stsp if (cwd == NULL) {
4775 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4776 c4296144 2019-05-09 stsp goto done;
4777 c4296144 2019-05-09 stsp }
4778 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4779 7d5807f4 2019-07-11 stsp if (error)
4780 7d5807f4 2019-07-11 stsp goto done;
4781 7d5807f4 2019-07-11 stsp
4782 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4783 c4296144 2019-05-09 stsp if (error)
4784 a698f62e 2019-07-25 stsp goto done;
4785 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4786 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4787 c4296144 2019-05-09 stsp goto done;
4788 a698f62e 2019-07-25 stsp }
4789 5c1e53bc 2019-07-28 stsp
4790 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4791 916f288c 2019-07-30 stsp worktree);
4792 5c1e53bc 2019-07-28 stsp if (error)
4793 5c1e53bc 2019-07-28 stsp goto done;
4794 c4296144 2019-05-09 stsp
4795 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4796 c9956ddf 2019-09-08 stsp if (error)
4797 c9956ddf 2019-09-08 stsp goto done;
4798 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4799 c9956ddf 2019-09-08 stsp gitconfig_path);
4800 c4296144 2019-05-09 stsp if (error != NULL)
4801 c4296144 2019-05-09 stsp goto done;
4802 c4296144 2019-05-09 stsp
4803 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4804 aba9c984 2019-09-08 stsp if (error)
4805 aba9c984 2019-09-08 stsp return error;
4806 aba9c984 2019-09-08 stsp
4807 314a6357 2019-05-13 stsp /*
4808 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4809 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4810 314a6357 2019-05-13 stsp */
4811 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4812 314a6357 2019-05-13 stsp error = get_editor(&editor);
4813 314a6357 2019-05-13 stsp else
4814 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4815 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4816 c4296144 2019-05-09 stsp if (error)
4817 c4296144 2019-05-09 stsp goto done;
4818 c4296144 2019-05-09 stsp
4819 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4820 087fb88c 2019-08-04 stsp if (error)
4821 087fb88c 2019-08-04 stsp goto done;
4822 087fb88c 2019-08-04 stsp
4823 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4824 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4825 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4826 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4827 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4828 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4829 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4830 916f288c 2019-07-30 stsp goto done;
4831 916f288c 2019-07-30 stsp }
4832 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4833 916f288c 2019-07-30 stsp }
4834 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4835 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4836 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4837 e0870e44 2019-05-13 stsp if (error) {
4838 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4839 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4840 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4841 c4296144 2019-05-09 stsp goto done;
4842 e0870e44 2019-05-13 stsp }
4843 c4296144 2019-05-09 stsp
4844 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4845 c4296144 2019-05-09 stsp if (error)
4846 c4296144 2019-05-09 stsp goto done;
4847 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4848 c4296144 2019-05-09 stsp done:
4849 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4850 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4851 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4852 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4853 7266f21f 2019-10-21 stsp error == NULL)
4854 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4855 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4856 c4296144 2019-05-09 stsp if (repo)
4857 c4296144 2019-05-09 stsp got_repo_close(repo);
4858 c4296144 2019-05-09 stsp if (worktree)
4859 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4860 c4296144 2019-05-09 stsp free(cwd);
4861 c4296144 2019-05-09 stsp free(id_str);
4862 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4863 e2ba3d07 2019-05-13 stsp free(editor);
4864 aba9c984 2019-09-08 stsp free(author);
4865 234035bc 2019-06-01 stsp return error;
4866 234035bc 2019-06-01 stsp }
4867 234035bc 2019-06-01 stsp
4868 234035bc 2019-06-01 stsp __dead static void
4869 234035bc 2019-06-01 stsp usage_cherrypick(void)
4870 234035bc 2019-06-01 stsp {
4871 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4872 234035bc 2019-06-01 stsp exit(1);
4873 234035bc 2019-06-01 stsp }
4874 234035bc 2019-06-01 stsp
4875 234035bc 2019-06-01 stsp static const struct got_error *
4876 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4877 234035bc 2019-06-01 stsp {
4878 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4879 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4880 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4881 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4882 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4883 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4884 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4885 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4886 234035bc 2019-06-01 stsp int ch, did_something = 0;
4887 234035bc 2019-06-01 stsp
4888 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4889 234035bc 2019-06-01 stsp switch (ch) {
4890 234035bc 2019-06-01 stsp default:
4891 234035bc 2019-06-01 stsp usage_cherrypick();
4892 234035bc 2019-06-01 stsp /* NOTREACHED */
4893 234035bc 2019-06-01 stsp }
4894 234035bc 2019-06-01 stsp }
4895 234035bc 2019-06-01 stsp
4896 234035bc 2019-06-01 stsp argc -= optind;
4897 234035bc 2019-06-01 stsp argv += optind;
4898 234035bc 2019-06-01 stsp
4899 43012d58 2019-07-14 stsp #ifndef PROFILE
4900 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4901 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4902 43012d58 2019-07-14 stsp err(1, "pledge");
4903 43012d58 2019-07-14 stsp #endif
4904 234035bc 2019-06-01 stsp if (argc != 1)
4905 234035bc 2019-06-01 stsp usage_cherrypick();
4906 234035bc 2019-06-01 stsp
4907 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4908 234035bc 2019-06-01 stsp if (cwd == NULL) {
4909 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4910 234035bc 2019-06-01 stsp goto done;
4911 234035bc 2019-06-01 stsp }
4912 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4913 234035bc 2019-06-01 stsp if (error)
4914 234035bc 2019-06-01 stsp goto done;
4915 234035bc 2019-06-01 stsp
4916 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4917 c9956ddf 2019-09-08 stsp NULL);
4918 234035bc 2019-06-01 stsp if (error != NULL)
4919 234035bc 2019-06-01 stsp goto done;
4920 234035bc 2019-06-01 stsp
4921 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4922 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4923 234035bc 2019-06-01 stsp if (error)
4924 234035bc 2019-06-01 stsp goto done;
4925 234035bc 2019-06-01 stsp
4926 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4927 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4928 234035bc 2019-06-01 stsp if (error != NULL) {
4929 234035bc 2019-06-01 stsp struct got_reference *ref;
4930 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4931 234035bc 2019-06-01 stsp goto done;
4932 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4933 234035bc 2019-06-01 stsp if (error != NULL)
4934 234035bc 2019-06-01 stsp goto done;
4935 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4936 234035bc 2019-06-01 stsp got_ref_close(ref);
4937 234035bc 2019-06-01 stsp if (error != NULL)
4938 234035bc 2019-06-01 stsp goto done;
4939 234035bc 2019-06-01 stsp }
4940 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4941 234035bc 2019-06-01 stsp if (error)
4942 234035bc 2019-06-01 stsp goto done;
4943 234035bc 2019-06-01 stsp
4944 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4945 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4946 234035bc 2019-06-01 stsp if (error != NULL)
4947 234035bc 2019-06-01 stsp goto done;
4948 234035bc 2019-06-01 stsp
4949 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4950 234035bc 2019-06-01 stsp if (error) {
4951 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4952 234035bc 2019-06-01 stsp goto done;
4953 234035bc 2019-06-01 stsp error = NULL;
4954 234035bc 2019-06-01 stsp } else {
4955 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4956 234035bc 2019-06-01 stsp goto done;
4957 234035bc 2019-06-01 stsp }
4958 234035bc 2019-06-01 stsp
4959 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4960 234035bc 2019-06-01 stsp if (error)
4961 234035bc 2019-06-01 stsp goto done;
4962 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4963 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4964 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4965 03415a1a 2019-06-02 stsp NULL);
4966 234035bc 2019-06-01 stsp if (error != NULL)
4967 234035bc 2019-06-01 stsp goto done;
4968 234035bc 2019-06-01 stsp
4969 234035bc 2019-06-01 stsp if (did_something)
4970 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4971 234035bc 2019-06-01 stsp done:
4972 234035bc 2019-06-01 stsp if (commit)
4973 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4974 234035bc 2019-06-01 stsp free(commit_id_str);
4975 234035bc 2019-06-01 stsp if (head_ref)
4976 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4977 234035bc 2019-06-01 stsp if (worktree)
4978 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4979 234035bc 2019-06-01 stsp if (repo)
4980 234035bc 2019-06-01 stsp got_repo_close(repo);
4981 c4296144 2019-05-09 stsp return error;
4982 c4296144 2019-05-09 stsp }
4983 5ef14e63 2019-06-02 stsp
4984 5ef14e63 2019-06-02 stsp __dead static void
4985 5ef14e63 2019-06-02 stsp usage_backout(void)
4986 5ef14e63 2019-06-02 stsp {
4987 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4988 5ef14e63 2019-06-02 stsp exit(1);
4989 5ef14e63 2019-06-02 stsp }
4990 5ef14e63 2019-06-02 stsp
4991 5ef14e63 2019-06-02 stsp static const struct got_error *
4992 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4993 5ef14e63 2019-06-02 stsp {
4994 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4995 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4996 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4997 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4998 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4999 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5000 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5001 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5002 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5003 5ef14e63 2019-06-02 stsp
5004 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5005 5ef14e63 2019-06-02 stsp switch (ch) {
5006 5ef14e63 2019-06-02 stsp default:
5007 5ef14e63 2019-06-02 stsp usage_backout();
5008 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5009 5ef14e63 2019-06-02 stsp }
5010 5ef14e63 2019-06-02 stsp }
5011 5ef14e63 2019-06-02 stsp
5012 5ef14e63 2019-06-02 stsp argc -= optind;
5013 5ef14e63 2019-06-02 stsp argv += optind;
5014 5ef14e63 2019-06-02 stsp
5015 43012d58 2019-07-14 stsp #ifndef PROFILE
5016 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5017 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5018 43012d58 2019-07-14 stsp err(1, "pledge");
5019 43012d58 2019-07-14 stsp #endif
5020 5ef14e63 2019-06-02 stsp if (argc != 1)
5021 5ef14e63 2019-06-02 stsp usage_backout();
5022 5ef14e63 2019-06-02 stsp
5023 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5024 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5025 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5026 5ef14e63 2019-06-02 stsp goto done;
5027 5ef14e63 2019-06-02 stsp }
5028 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5029 5ef14e63 2019-06-02 stsp if (error)
5030 5ef14e63 2019-06-02 stsp goto done;
5031 5ef14e63 2019-06-02 stsp
5032 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5033 c9956ddf 2019-09-08 stsp NULL);
5034 5ef14e63 2019-06-02 stsp if (error != NULL)
5035 5ef14e63 2019-06-02 stsp goto done;
5036 5ef14e63 2019-06-02 stsp
5037 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5038 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5039 5ef14e63 2019-06-02 stsp if (error)
5040 5ef14e63 2019-06-02 stsp goto done;
5041 5ef14e63 2019-06-02 stsp
5042 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5043 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5044 5ef14e63 2019-06-02 stsp if (error != NULL) {
5045 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5046 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5047 5ef14e63 2019-06-02 stsp goto done;
5048 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5049 5ef14e63 2019-06-02 stsp if (error != NULL)
5050 5ef14e63 2019-06-02 stsp goto done;
5051 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5052 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5053 5ef14e63 2019-06-02 stsp if (error != NULL)
5054 5ef14e63 2019-06-02 stsp goto done;
5055 5ef14e63 2019-06-02 stsp }
5056 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5057 5ef14e63 2019-06-02 stsp if (error)
5058 5ef14e63 2019-06-02 stsp goto done;
5059 5ef14e63 2019-06-02 stsp
5060 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5061 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5062 5ef14e63 2019-06-02 stsp if (error != NULL)
5063 5ef14e63 2019-06-02 stsp goto done;
5064 5ef14e63 2019-06-02 stsp
5065 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5066 5ef14e63 2019-06-02 stsp if (error)
5067 5ef14e63 2019-06-02 stsp goto done;
5068 5ef14e63 2019-06-02 stsp
5069 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5070 5ef14e63 2019-06-02 stsp if (error)
5071 5ef14e63 2019-06-02 stsp goto done;
5072 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5073 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5074 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5075 5ef14e63 2019-06-02 stsp goto done;
5076 5ef14e63 2019-06-02 stsp }
5077 5ef14e63 2019-06-02 stsp
5078 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5079 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5080 5ef14e63 2019-06-02 stsp if (error != NULL)
5081 5ef14e63 2019-06-02 stsp goto done;
5082 5ef14e63 2019-06-02 stsp
5083 5ef14e63 2019-06-02 stsp if (did_something)
5084 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5085 5ef14e63 2019-06-02 stsp done:
5086 5ef14e63 2019-06-02 stsp if (commit)
5087 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5088 5ef14e63 2019-06-02 stsp free(commit_id_str);
5089 5ef14e63 2019-06-02 stsp if (head_ref)
5090 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5091 818c7501 2019-07-11 stsp if (worktree)
5092 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5093 818c7501 2019-07-11 stsp if (repo)
5094 818c7501 2019-07-11 stsp got_repo_close(repo);
5095 818c7501 2019-07-11 stsp return error;
5096 818c7501 2019-07-11 stsp }
5097 818c7501 2019-07-11 stsp
5098 818c7501 2019-07-11 stsp __dead static void
5099 818c7501 2019-07-11 stsp usage_rebase(void)
5100 818c7501 2019-07-11 stsp {
5101 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5102 af54c8f8 2019-07-11 stsp getprogname());
5103 818c7501 2019-07-11 stsp exit(1);
5104 0ebf8283 2019-07-24 stsp }
5105 0ebf8283 2019-07-24 stsp
5106 0ebf8283 2019-07-24 stsp void
5107 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5108 0ebf8283 2019-07-24 stsp {
5109 0ebf8283 2019-07-24 stsp char *nl;
5110 0ebf8283 2019-07-24 stsp size_t len;
5111 0ebf8283 2019-07-24 stsp
5112 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5113 0ebf8283 2019-07-24 stsp if (len > limit)
5114 0ebf8283 2019-07-24 stsp len = limit;
5115 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5116 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5117 0ebf8283 2019-07-24 stsp if (nl)
5118 0ebf8283 2019-07-24 stsp *nl = '\0';
5119 0ebf8283 2019-07-24 stsp }
5120 0ebf8283 2019-07-24 stsp
5121 0ebf8283 2019-07-24 stsp static const struct got_error *
5122 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5123 0ebf8283 2019-07-24 stsp {
5124 5943eee2 2019-08-13 stsp const struct got_error *err;
5125 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5126 5943eee2 2019-08-13 stsp const char *s;
5127 0ebf8283 2019-07-24 stsp
5128 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5129 5943eee2 2019-08-13 stsp if (err)
5130 5943eee2 2019-08-13 stsp return err;
5131 0ebf8283 2019-07-24 stsp
5132 5943eee2 2019-08-13 stsp s = logmsg0;
5133 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5134 5943eee2 2019-08-13 stsp s++;
5135 5943eee2 2019-08-13 stsp
5136 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5137 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5138 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5139 5943eee2 2019-08-13 stsp goto done;
5140 5943eee2 2019-08-13 stsp }
5141 0ebf8283 2019-07-24 stsp
5142 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5143 5943eee2 2019-08-13 stsp done:
5144 5943eee2 2019-08-13 stsp free(logmsg0);
5145 5943eee2 2019-08-13 stsp return err;
5146 818c7501 2019-07-11 stsp }
5147 818c7501 2019-07-11 stsp
5148 818c7501 2019-07-11 stsp static const struct got_error *
5149 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5150 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5151 818c7501 2019-07-11 stsp {
5152 818c7501 2019-07-11 stsp const struct got_error *err;
5153 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5154 818c7501 2019-07-11 stsp
5155 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5156 818c7501 2019-07-11 stsp if (err)
5157 818c7501 2019-07-11 stsp goto done;
5158 818c7501 2019-07-11 stsp
5159 ff0d2220 2019-07-11 stsp if (new_id) {
5160 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5161 ff0d2220 2019-07-11 stsp if (err)
5162 ff0d2220 2019-07-11 stsp goto done;
5163 ff0d2220 2019-07-11 stsp }
5164 818c7501 2019-07-11 stsp
5165 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5166 ff0d2220 2019-07-11 stsp if (new_id_str)
5167 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5168 0ebf8283 2019-07-24 stsp
5169 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5170 0ebf8283 2019-07-24 stsp if (err)
5171 0ebf8283 2019-07-24 stsp goto done;
5172 0ebf8283 2019-07-24 stsp
5173 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5174 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5175 818c7501 2019-07-11 stsp done:
5176 818c7501 2019-07-11 stsp free(old_id_str);
5177 818c7501 2019-07-11 stsp free(new_id_str);
5178 818c7501 2019-07-11 stsp return err;
5179 818c7501 2019-07-11 stsp }
5180 818c7501 2019-07-11 stsp
5181 1ee397ad 2019-07-12 stsp static const struct got_error *
5182 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5183 818c7501 2019-07-11 stsp {
5184 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5185 818c7501 2019-07-11 stsp
5186 818c7501 2019-07-11 stsp while (path[0] == '/')
5187 818c7501 2019-07-11 stsp path++;
5188 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5189 818c7501 2019-07-11 stsp
5190 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5191 1ee397ad 2019-07-12 stsp return NULL;
5192 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5193 818c7501 2019-07-11 stsp *rebase_status = status;
5194 1ee397ad 2019-07-12 stsp return NULL;
5195 818c7501 2019-07-11 stsp }
5196 818c7501 2019-07-11 stsp
5197 818c7501 2019-07-11 stsp static const struct got_error *
5198 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5199 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5200 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5201 818c7501 2019-07-11 stsp {
5202 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5203 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5204 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5205 818c7501 2019-07-11 stsp }
5206 818c7501 2019-07-11 stsp
5207 818c7501 2019-07-11 stsp static const struct got_error *
5208 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5209 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5210 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5211 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5212 ff0d2220 2019-07-11 stsp {
5213 ff0d2220 2019-07-11 stsp const struct got_error *error;
5214 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5215 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5216 ff0d2220 2019-07-11 stsp
5217 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5218 ff0d2220 2019-07-11 stsp if (error)
5219 ff0d2220 2019-07-11 stsp return error;
5220 ff0d2220 2019-07-11 stsp
5221 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5222 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5223 ff0d2220 2019-07-11 stsp if (error) {
5224 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5225 ff0d2220 2019-07-11 stsp goto done;
5226 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5227 ff0d2220 2019-07-11 stsp } else {
5228 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5229 ff0d2220 2019-07-11 stsp free(new_commit_id);
5230 ff0d2220 2019-07-11 stsp }
5231 ff0d2220 2019-07-11 stsp done:
5232 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5233 ff0d2220 2019-07-11 stsp return error;
5234 64c6d990 2019-07-11 stsp }
5235 64c6d990 2019-07-11 stsp
5236 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5237 64c6d990 2019-07-11 stsp const char *path_prefix;
5238 64c6d990 2019-07-11 stsp size_t len;
5239 8ca9bd68 2019-07-25 stsp int errcode;
5240 64c6d990 2019-07-11 stsp };
5241 64c6d990 2019-07-11 stsp
5242 64c6d990 2019-07-11 stsp static const struct got_error *
5243 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5244 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5245 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5246 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5247 64c6d990 2019-07-11 stsp {
5248 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5249 64c6d990 2019-07-11 stsp
5250 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5251 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5252 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5253 64c6d990 2019-07-11 stsp
5254 64c6d990 2019-07-11 stsp return NULL;
5255 64c6d990 2019-07-11 stsp }
5256 64c6d990 2019-07-11 stsp
5257 64c6d990 2019-07-11 stsp static const struct got_error *
5258 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5259 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5260 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5261 64c6d990 2019-07-11 stsp {
5262 64c6d990 2019-07-11 stsp const struct got_error *err;
5263 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5264 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5265 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5266 64c6d990 2019-07-11 stsp
5267 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5268 64c6d990 2019-07-11 stsp return NULL;
5269 64c6d990 2019-07-11 stsp
5270 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5271 64c6d990 2019-07-11 stsp if (err)
5272 64c6d990 2019-07-11 stsp goto done;
5273 64c6d990 2019-07-11 stsp
5274 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5275 64c6d990 2019-07-11 stsp if (err)
5276 64c6d990 2019-07-11 stsp goto done;
5277 64c6d990 2019-07-11 stsp
5278 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5279 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5280 64c6d990 2019-07-11 stsp if (err)
5281 64c6d990 2019-07-11 stsp goto done;
5282 64c6d990 2019-07-11 stsp
5283 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5284 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5285 64c6d990 2019-07-11 stsp if (err)
5286 64c6d990 2019-07-11 stsp goto done;
5287 64c6d990 2019-07-11 stsp
5288 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5289 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5290 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5291 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5292 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5293 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5294 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5295 64c6d990 2019-07-11 stsp done:
5296 64c6d990 2019-07-11 stsp if (tree1)
5297 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5298 64c6d990 2019-07-11 stsp if (tree2)
5299 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5300 64c6d990 2019-07-11 stsp if (commit)
5301 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5302 64c6d990 2019-07-11 stsp if (parent_commit)
5303 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5304 64c6d990 2019-07-11 stsp return err;
5305 ff0d2220 2019-07-11 stsp }
5306 ff0d2220 2019-07-11 stsp
5307 ff0d2220 2019-07-11 stsp static const struct got_error *
5308 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5309 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5310 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5311 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5312 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5313 af61c510 2019-07-19 stsp {
5314 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5315 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5316 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5317 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5318 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5319 af61c510 2019-07-19 stsp
5320 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5321 af61c510 2019-07-19 stsp if (err)
5322 af61c510 2019-07-19 stsp return err;
5323 af61c510 2019-07-19 stsp
5324 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5325 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5326 af61c510 2019-07-19 stsp if (err)
5327 af61c510 2019-07-19 stsp goto done;
5328 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5329 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
5330 af61c510 2019-07-19 stsp if (err) {
5331 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5332 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5333 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5334 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5335 af61c510 2019-07-19 stsp "been reached?!?");
5336 af61c510 2019-07-19 stsp goto done;
5337 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5338 af61c510 2019-07-19 stsp goto done;
5339 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
5340 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5341 af61c510 2019-07-19 stsp if (err)
5342 af61c510 2019-07-19 stsp goto done;
5343 af61c510 2019-07-19 stsp } else {
5344 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5345 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5346 af61c510 2019-07-19 stsp if (err)
5347 af61c510 2019-07-19 stsp goto done;
5348 af61c510 2019-07-19 stsp
5349 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5350 af61c510 2019-07-19 stsp if (err)
5351 af61c510 2019-07-19 stsp goto done;
5352 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5353 af61c510 2019-07-19 stsp commit_id = parent_id;
5354 af61c510 2019-07-19 stsp }
5355 af61c510 2019-07-19 stsp }
5356 af61c510 2019-07-19 stsp done:
5357 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5358 af61c510 2019-07-19 stsp return err;
5359 af61c510 2019-07-19 stsp }
5360 af61c510 2019-07-19 stsp
5361 af61c510 2019-07-19 stsp static const struct got_error *
5362 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5363 818c7501 2019-07-11 stsp {
5364 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5365 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5366 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5367 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5368 818c7501 2019-07-11 stsp char *cwd = NULL;
5369 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5370 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5371 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5372 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5373 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5374 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5375 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5376 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5377 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5378 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5379 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5380 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5381 818c7501 2019-07-11 stsp
5382 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5383 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5384 818c7501 2019-07-11 stsp
5385 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5386 818c7501 2019-07-11 stsp switch (ch) {
5387 818c7501 2019-07-11 stsp case 'a':
5388 818c7501 2019-07-11 stsp abort_rebase = 1;
5389 818c7501 2019-07-11 stsp break;
5390 818c7501 2019-07-11 stsp case 'c':
5391 818c7501 2019-07-11 stsp continue_rebase = 1;
5392 818c7501 2019-07-11 stsp break;
5393 818c7501 2019-07-11 stsp default:
5394 818c7501 2019-07-11 stsp usage_rebase();
5395 818c7501 2019-07-11 stsp /* NOTREACHED */
5396 818c7501 2019-07-11 stsp }
5397 818c7501 2019-07-11 stsp }
5398 818c7501 2019-07-11 stsp
5399 818c7501 2019-07-11 stsp argc -= optind;
5400 818c7501 2019-07-11 stsp argv += optind;
5401 818c7501 2019-07-11 stsp
5402 43012d58 2019-07-14 stsp #ifndef PROFILE
5403 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5404 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5405 43012d58 2019-07-14 stsp err(1, "pledge");
5406 43012d58 2019-07-14 stsp #endif
5407 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5408 818c7501 2019-07-11 stsp usage_rebase();
5409 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5410 818c7501 2019-07-11 stsp if (argc != 0)
5411 818c7501 2019-07-11 stsp usage_rebase();
5412 818c7501 2019-07-11 stsp } else if (argc != 1)
5413 818c7501 2019-07-11 stsp usage_rebase();
5414 818c7501 2019-07-11 stsp
5415 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5416 818c7501 2019-07-11 stsp if (cwd == NULL) {
5417 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5418 818c7501 2019-07-11 stsp goto done;
5419 818c7501 2019-07-11 stsp }
5420 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5421 818c7501 2019-07-11 stsp if (error)
5422 818c7501 2019-07-11 stsp goto done;
5423 818c7501 2019-07-11 stsp
5424 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5425 c9956ddf 2019-09-08 stsp NULL);
5426 818c7501 2019-07-11 stsp if (error != NULL)
5427 818c7501 2019-07-11 stsp goto done;
5428 818c7501 2019-07-11 stsp
5429 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5430 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5431 818c7501 2019-07-11 stsp if (error)
5432 818c7501 2019-07-11 stsp goto done;
5433 818c7501 2019-07-11 stsp
5434 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5435 818c7501 2019-07-11 stsp if (error)
5436 818c7501 2019-07-11 stsp goto done;
5437 818c7501 2019-07-11 stsp
5438 f6794adc 2019-07-23 stsp if (abort_rebase) {
5439 818c7501 2019-07-11 stsp int did_something;
5440 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5441 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5442 f6794adc 2019-07-23 stsp goto done;
5443 f6794adc 2019-07-23 stsp }
5444 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5445 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5446 3e3a69f1 2019-07-25 stsp worktree, repo);
5447 818c7501 2019-07-11 stsp if (error)
5448 818c7501 2019-07-11 stsp goto done;
5449 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5450 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5451 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5452 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5453 818c7501 2019-07-11 stsp if (error)
5454 818c7501 2019-07-11 stsp goto done;
5455 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5456 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5457 818c7501 2019-07-11 stsp }
5458 818c7501 2019-07-11 stsp
5459 818c7501 2019-07-11 stsp if (continue_rebase) {
5460 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5461 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5462 f6794adc 2019-07-23 stsp goto done;
5463 f6794adc 2019-07-23 stsp }
5464 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5465 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5466 3e3a69f1 2019-07-25 stsp worktree, repo);
5467 818c7501 2019-07-11 stsp if (error)
5468 818c7501 2019-07-11 stsp goto done;
5469 818c7501 2019-07-11 stsp
5470 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5471 01757395 2019-07-12 stsp resume_commit_id, repo);
5472 818c7501 2019-07-11 stsp if (error)
5473 818c7501 2019-07-11 stsp goto done;
5474 818c7501 2019-07-11 stsp
5475 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5476 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5477 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5478 818c7501 2019-07-11 stsp goto done;
5479 818c7501 2019-07-11 stsp }
5480 818c7501 2019-07-11 stsp } else {
5481 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5482 818c7501 2019-07-11 stsp if (error != NULL)
5483 818c7501 2019-07-11 stsp goto done;
5484 ff0d2220 2019-07-11 stsp }
5485 818c7501 2019-07-11 stsp
5486 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5487 ff0d2220 2019-07-11 stsp if (error)
5488 ff0d2220 2019-07-11 stsp goto done;
5489 ff0d2220 2019-07-11 stsp
5490 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5491 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5492 a51a74b3 2019-07-27 stsp
5493 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5494 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5495 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5496 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5497 818c7501 2019-07-11 stsp if (error)
5498 818c7501 2019-07-11 stsp goto done;
5499 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5500 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5501 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5502 818c7501 2019-07-11 stsp "with work tree's branch");
5503 818c7501 2019-07-11 stsp goto done;
5504 818c7501 2019-07-11 stsp }
5505 818c7501 2019-07-11 stsp
5506 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5507 a51a74b3 2019-07-27 stsp if (error) {
5508 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5509 a51a74b3 2019-07-27 stsp goto done;
5510 a51a74b3 2019-07-27 stsp error = NULL;
5511 a51a74b3 2019-07-27 stsp } else {
5512 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5513 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5514 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5515 a51a74b3 2019-07-27 stsp goto done;
5516 a51a74b3 2019-07-27 stsp }
5517 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5518 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5519 818c7501 2019-07-11 stsp if (error)
5520 818c7501 2019-07-11 stsp goto done;
5521 818c7501 2019-07-11 stsp }
5522 818c7501 2019-07-11 stsp
5523 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5524 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5525 818c7501 2019-07-11 stsp if (error)
5526 818c7501 2019-07-11 stsp goto done;
5527 818c7501 2019-07-11 stsp
5528 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5529 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5530 fc66b545 2019-08-12 stsp if (pid == NULL) {
5531 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5532 fc66b545 2019-08-12 stsp int did_something;
5533 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5534 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5535 fc66b545 2019-08-12 stsp &did_something);
5536 fc66b545 2019-08-12 stsp if (error)
5537 fc66b545 2019-08-12 stsp goto done;
5538 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5539 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5540 fc66b545 2019-08-12 stsp }
5541 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5542 fc66b545 2019-08-12 stsp goto done;
5543 fc66b545 2019-08-12 stsp }
5544 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5545 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5546 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5547 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5548 818c7501 2019-07-11 stsp commit = NULL;
5549 818c7501 2019-07-11 stsp if (error)
5550 818c7501 2019-07-11 stsp goto done;
5551 64c6d990 2019-07-11 stsp
5552 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5553 38b0338b 2019-11-29 stsp if (continue_rebase) {
5554 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5555 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5556 38b0338b 2019-11-29 stsp goto done;
5557 38b0338b 2019-11-29 stsp } else {
5558 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5559 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5560 38b0338b 2019-11-29 stsp char *id_str;
5561 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5562 38b0338b 2019-11-29 stsp new_base_branch);
5563 38b0338b 2019-11-29 stsp if (error)
5564 38b0338b 2019-11-29 stsp goto done;
5565 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5566 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5567 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5568 38b0338b 2019-11-29 stsp free(id_str);
5569 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5570 38b0338b 2019-11-29 stsp new_head_commit_id);
5571 38b0338b 2019-11-29 stsp if (error)
5572 38b0338b 2019-11-29 stsp goto done;
5573 38b0338b 2019-11-29 stsp }
5574 818c7501 2019-07-11 stsp }
5575 818c7501 2019-07-11 stsp
5576 818c7501 2019-07-11 stsp pid = NULL;
5577 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5578 818c7501 2019-07-11 stsp commit_id = qid->id;
5579 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5580 818c7501 2019-07-11 stsp pid = qid;
5581 818c7501 2019-07-11 stsp
5582 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5583 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5584 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5585 818c7501 2019-07-11 stsp if (error)
5586 818c7501 2019-07-11 stsp goto done;
5587 818c7501 2019-07-11 stsp
5588 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5589 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5590 818c7501 2019-07-11 stsp break;
5591 01757395 2019-07-12 stsp }
5592 818c7501 2019-07-11 stsp
5593 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5594 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5595 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5596 818c7501 2019-07-11 stsp if (error)
5597 818c7501 2019-07-11 stsp goto done;
5598 818c7501 2019-07-11 stsp }
5599 818c7501 2019-07-11 stsp
5600 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5601 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5602 818c7501 2019-07-11 stsp if (error)
5603 818c7501 2019-07-11 stsp goto done;
5604 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5605 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5606 818c7501 2019-07-11 stsp } else
5607 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5608 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5609 818c7501 2019-07-11 stsp done:
5610 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5611 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5612 818c7501 2019-07-11 stsp free(resume_commit_id);
5613 818c7501 2019-07-11 stsp free(yca_id);
5614 818c7501 2019-07-11 stsp if (commit)
5615 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5616 818c7501 2019-07-11 stsp if (branch)
5617 818c7501 2019-07-11 stsp got_ref_close(branch);
5618 818c7501 2019-07-11 stsp if (new_base_branch)
5619 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5620 818c7501 2019-07-11 stsp if (tmp_branch)
5621 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5622 5ef14e63 2019-06-02 stsp if (worktree)
5623 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5624 5ef14e63 2019-06-02 stsp if (repo)
5625 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5626 5ef14e63 2019-06-02 stsp return error;
5627 0ebf8283 2019-07-24 stsp }
5628 0ebf8283 2019-07-24 stsp
5629 0ebf8283 2019-07-24 stsp __dead static void
5630 0ebf8283 2019-07-24 stsp usage_histedit(void)
5631 0ebf8283 2019-07-24 stsp {
5632 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5633 0ebf8283 2019-07-24 stsp getprogname());
5634 0ebf8283 2019-07-24 stsp exit(1);
5635 0ebf8283 2019-07-24 stsp }
5636 0ebf8283 2019-07-24 stsp
5637 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5638 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5639 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5640 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5641 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5642 0ebf8283 2019-07-24 stsp
5643 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5644 0ebf8283 2019-07-24 stsp unsigned char code;
5645 0ebf8283 2019-07-24 stsp const char *name;
5646 0ebf8283 2019-07-24 stsp const char *desc;
5647 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5648 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5649 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5650 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5651 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5652 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5653 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5654 0ebf8283 2019-07-24 stsp };
5655 0ebf8283 2019-07-24 stsp
5656 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5657 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5658 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5659 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5660 0ebf8283 2019-07-24 stsp char *logmsg;
5661 0ebf8283 2019-07-24 stsp };
5662 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5663 0ebf8283 2019-07-24 stsp
5664 0ebf8283 2019-07-24 stsp static const struct got_error *
5665 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5666 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5667 0ebf8283 2019-07-24 stsp {
5668 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5669 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5670 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5671 8138f3e1 2019-08-11 stsp int n;
5672 0ebf8283 2019-07-24 stsp
5673 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5674 0ebf8283 2019-07-24 stsp if (err)
5675 0ebf8283 2019-07-24 stsp goto done;
5676 0ebf8283 2019-07-24 stsp
5677 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5678 0ebf8283 2019-07-24 stsp if (err)
5679 0ebf8283 2019-07-24 stsp goto done;
5680 0ebf8283 2019-07-24 stsp
5681 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5682 0ebf8283 2019-07-24 stsp if (err)
5683 0ebf8283 2019-07-24 stsp goto done;
5684 0ebf8283 2019-07-24 stsp
5685 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5686 0ebf8283 2019-07-24 stsp if (n < 0)
5687 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5688 0ebf8283 2019-07-24 stsp done:
5689 0ebf8283 2019-07-24 stsp if (commit)
5690 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5691 0ebf8283 2019-07-24 stsp free(id_str);
5692 0ebf8283 2019-07-24 stsp free(logmsg);
5693 0ebf8283 2019-07-24 stsp return err;
5694 0ebf8283 2019-07-24 stsp }
5695 0ebf8283 2019-07-24 stsp
5696 0ebf8283 2019-07-24 stsp static const struct got_error *
5697 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5698 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5699 0ebf8283 2019-07-24 stsp {
5700 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5701 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5702 0ebf8283 2019-07-24 stsp
5703 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5704 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5705 0ebf8283 2019-07-24 stsp
5706 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5707 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5708 0ebf8283 2019-07-24 stsp f, repo);
5709 0ebf8283 2019-07-24 stsp if (err)
5710 0ebf8283 2019-07-24 stsp break;
5711 0ebf8283 2019-07-24 stsp }
5712 0ebf8283 2019-07-24 stsp
5713 0ebf8283 2019-07-24 stsp return err;
5714 0ebf8283 2019-07-24 stsp }
5715 0ebf8283 2019-07-24 stsp
5716 0ebf8283 2019-07-24 stsp static const struct got_error *
5717 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5718 0ebf8283 2019-07-24 stsp {
5719 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5720 0ebf8283 2019-07-24 stsp int n, i;
5721 0ebf8283 2019-07-24 stsp
5722 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5723 0ebf8283 2019-07-24 stsp if (n < 0)
5724 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5725 0ebf8283 2019-07-24 stsp
5726 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5727 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5728 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5729 0ebf8283 2019-07-24 stsp cmd->desc);
5730 0ebf8283 2019-07-24 stsp if (n < 0) {
5731 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5732 0ebf8283 2019-07-24 stsp break;
5733 0ebf8283 2019-07-24 stsp }
5734 0ebf8283 2019-07-24 stsp }
5735 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5736 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5737 0ebf8283 2019-07-24 stsp if (n < 0)
5738 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5739 0ebf8283 2019-07-24 stsp return err;
5740 0ebf8283 2019-07-24 stsp }
5741 0ebf8283 2019-07-24 stsp
5742 0ebf8283 2019-07-24 stsp static const struct got_error *
5743 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5744 0ebf8283 2019-07-24 stsp {
5745 0ebf8283 2019-07-24 stsp static char msg[42];
5746 0ebf8283 2019-07-24 stsp int ret;
5747 0ebf8283 2019-07-24 stsp
5748 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5749 0ebf8283 2019-07-24 stsp lineno);
5750 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5751 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5752 0ebf8283 2019-07-24 stsp
5753 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5754 0ebf8283 2019-07-24 stsp }
5755 0ebf8283 2019-07-24 stsp
5756 0ebf8283 2019-07-24 stsp static const struct got_error *
5757 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5758 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5759 0ebf8283 2019-07-24 stsp {
5760 0ebf8283 2019-07-24 stsp const struct got_error *err;
5761 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5762 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5763 0ebf8283 2019-07-24 stsp
5764 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5765 0ebf8283 2019-07-24 stsp if (err)
5766 0ebf8283 2019-07-24 stsp return err;
5767 0ebf8283 2019-07-24 stsp
5768 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5769 0ebf8283 2019-07-24 stsp if (err)
5770 0ebf8283 2019-07-24 stsp goto done;
5771 0ebf8283 2019-07-24 stsp
5772 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5773 5943eee2 2019-08-13 stsp if (err)
5774 5943eee2 2019-08-13 stsp goto done;
5775 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5776 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5777 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5778 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5779 0ebf8283 2019-07-24 stsp goto done;
5780 0ebf8283 2019-07-24 stsp }
5781 0ebf8283 2019-07-24 stsp done:
5782 0ebf8283 2019-07-24 stsp if (folded_commit)
5783 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5784 0ebf8283 2019-07-24 stsp free(id_str);
5785 5943eee2 2019-08-13 stsp free(folded_logmsg);
5786 0ebf8283 2019-07-24 stsp return err;
5787 0ebf8283 2019-07-24 stsp }
5788 0ebf8283 2019-07-24 stsp
5789 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5790 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5791 0ebf8283 2019-07-24 stsp {
5792 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5793 0ebf8283 2019-07-24 stsp
5794 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5795 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5796 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5797 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5798 3f9de99f 2019-07-24 stsp folded = prev;
5799 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5800 0ebf8283 2019-07-24 stsp }
5801 0ebf8283 2019-07-24 stsp
5802 0ebf8283 2019-07-24 stsp return folded;
5803 0ebf8283 2019-07-24 stsp }
5804 0ebf8283 2019-07-24 stsp
5805 0ebf8283 2019-07-24 stsp static const struct got_error *
5806 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5807 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5808 0ebf8283 2019-07-24 stsp {
5809 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5810 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5811 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5812 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5813 0ebf8283 2019-07-24 stsp int fd;
5814 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5815 0ebf8283 2019-07-24 stsp
5816 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5817 0ebf8283 2019-07-24 stsp if (err)
5818 0ebf8283 2019-07-24 stsp return err;
5819 0ebf8283 2019-07-24 stsp
5820 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5821 0ebf8283 2019-07-24 stsp if (folded) {
5822 0ebf8283 2019-07-24 stsp while (folded != hle) {
5823 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5824 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5825 3f9de99f 2019-07-24 stsp continue;
5826 3f9de99f 2019-07-24 stsp }
5827 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5828 0ebf8283 2019-07-24 stsp logmsg, repo);
5829 0ebf8283 2019-07-24 stsp if (err)
5830 0ebf8283 2019-07-24 stsp goto done;
5831 0ebf8283 2019-07-24 stsp free(logmsg);
5832 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5833 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5834 0ebf8283 2019-07-24 stsp }
5835 0ebf8283 2019-07-24 stsp }
5836 0ebf8283 2019-07-24 stsp
5837 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5838 0ebf8283 2019-07-24 stsp if (err)
5839 0ebf8283 2019-07-24 stsp goto done;
5840 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5841 5943eee2 2019-08-13 stsp if (err)
5842 5943eee2 2019-08-13 stsp goto done;
5843 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5844 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5845 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5846 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5847 0ebf8283 2019-07-24 stsp goto done;
5848 0ebf8283 2019-07-24 stsp }
5849 0ebf8283 2019-07-24 stsp free(logmsg);
5850 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5851 0ebf8283 2019-07-24 stsp
5852 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5853 0ebf8283 2019-07-24 stsp if (err)
5854 0ebf8283 2019-07-24 stsp goto done;
5855 0ebf8283 2019-07-24 stsp
5856 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5857 0ebf8283 2019-07-24 stsp if (err)
5858 0ebf8283 2019-07-24 stsp goto done;
5859 0ebf8283 2019-07-24 stsp
5860 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5861 0ebf8283 2019-07-24 stsp close(fd);
5862 0ebf8283 2019-07-24 stsp
5863 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5864 0ebf8283 2019-07-24 stsp if (err)
5865 0ebf8283 2019-07-24 stsp goto done;
5866 0ebf8283 2019-07-24 stsp
5867 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5868 0ebf8283 2019-07-24 stsp if (err) {
5869 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5870 0ebf8283 2019-07-24 stsp goto done;
5871 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5872 0ebf8283 2019-07-24 stsp }
5873 0ebf8283 2019-07-24 stsp done:
5874 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5875 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5876 0ebf8283 2019-07-24 stsp free(logmsg_path);
5877 0ebf8283 2019-07-24 stsp free(logmsg);
5878 5943eee2 2019-08-13 stsp free(orig_logmsg);
5879 0ebf8283 2019-07-24 stsp free(editor);
5880 0ebf8283 2019-07-24 stsp if (commit)
5881 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5882 0ebf8283 2019-07-24 stsp return err;
5883 5ef14e63 2019-06-02 stsp }
5884 0ebf8283 2019-07-24 stsp
5885 0ebf8283 2019-07-24 stsp static const struct got_error *
5886 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5887 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5888 0ebf8283 2019-07-24 stsp {
5889 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5890 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5891 0ebf8283 2019-07-24 stsp size_t size;
5892 0ebf8283 2019-07-24 stsp ssize_t len;
5893 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5894 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5895 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5896 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5897 0ebf8283 2019-07-24 stsp
5898 0ebf8283 2019-07-24 stsp for (;;) {
5899 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5900 0ebf8283 2019-07-24 stsp if (len == -1) {
5901 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5902 0ebf8283 2019-07-24 stsp if (feof(f))
5903 0ebf8283 2019-07-24 stsp break;
5904 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5905 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5906 0ebf8283 2019-07-24 stsp break;
5907 0ebf8283 2019-07-24 stsp }
5908 0ebf8283 2019-07-24 stsp lineno++;
5909 0ebf8283 2019-07-24 stsp p = line;
5910 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5911 0ebf8283 2019-07-24 stsp p++;
5912 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5913 0ebf8283 2019-07-24 stsp free(line);
5914 0ebf8283 2019-07-24 stsp line = NULL;
5915 0ebf8283 2019-07-24 stsp continue;
5916 0ebf8283 2019-07-24 stsp }
5917 0ebf8283 2019-07-24 stsp cmd = NULL;
5918 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5919 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5920 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5921 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5922 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5923 0ebf8283 2019-07-24 stsp break;
5924 0ebf8283 2019-07-24 stsp }
5925 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5926 0ebf8283 2019-07-24 stsp p++;
5927 0ebf8283 2019-07-24 stsp break;
5928 0ebf8283 2019-07-24 stsp }
5929 0ebf8283 2019-07-24 stsp }
5930 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5931 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5932 0ebf8283 2019-07-24 stsp break;
5933 0ebf8283 2019-07-24 stsp }
5934 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5935 0ebf8283 2019-07-24 stsp p++;
5936 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5937 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5938 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5939 0ebf8283 2019-07-24 stsp break;
5940 0ebf8283 2019-07-24 stsp }
5941 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5942 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5943 0ebf8283 2019-07-24 stsp if (err)
5944 0ebf8283 2019-07-24 stsp break;
5945 0ebf8283 2019-07-24 stsp } else {
5946 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5947 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5948 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5949 0ebf8283 2019-07-24 stsp break;
5950 0ebf8283 2019-07-24 stsp }
5951 0ebf8283 2019-07-24 stsp }
5952 0ebf8283 2019-07-24 stsp free(line);
5953 0ebf8283 2019-07-24 stsp line = NULL;
5954 0ebf8283 2019-07-24 stsp continue;
5955 0ebf8283 2019-07-24 stsp } else {
5956 0ebf8283 2019-07-24 stsp end = p;
5957 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5958 0ebf8283 2019-07-24 stsp end++;
5959 0ebf8283 2019-07-24 stsp *end = '\0';
5960 0ebf8283 2019-07-24 stsp
5961 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5962 0ebf8283 2019-07-24 stsp if (err) {
5963 0ebf8283 2019-07-24 stsp /* override error code */
5964 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5965 0ebf8283 2019-07-24 stsp break;
5966 0ebf8283 2019-07-24 stsp }
5967 0ebf8283 2019-07-24 stsp }
5968 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5969 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5970 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5971 0ebf8283 2019-07-24 stsp break;
5972 0ebf8283 2019-07-24 stsp }
5973 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5974 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5975 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5976 0ebf8283 2019-07-24 stsp commit_id = NULL;
5977 0ebf8283 2019-07-24 stsp free(line);
5978 0ebf8283 2019-07-24 stsp line = NULL;
5979 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5980 0ebf8283 2019-07-24 stsp }
5981 0ebf8283 2019-07-24 stsp
5982 0ebf8283 2019-07-24 stsp free(line);
5983 0ebf8283 2019-07-24 stsp free(commit_id);
5984 0ebf8283 2019-07-24 stsp return err;
5985 0ebf8283 2019-07-24 stsp }
5986 0ebf8283 2019-07-24 stsp
5987 0ebf8283 2019-07-24 stsp static const struct got_error *
5988 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5989 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5990 bfce7f83 2019-07-27 stsp {
5991 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5992 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5993 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5994 bfce7f83 2019-07-27 stsp static char msg[80];
5995 bfce7f83 2019-07-27 stsp char *id_str;
5996 bfce7f83 2019-07-27 stsp
5997 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5998 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5999 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6000 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6001 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6002 bfce7f83 2019-07-27 stsp
6003 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6004 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6005 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6006 bfce7f83 2019-07-27 stsp break;
6007 bfce7f83 2019-07-27 stsp }
6008 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6009 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6010 bfce7f83 2019-07-27 stsp if (err)
6011 bfce7f83 2019-07-27 stsp return err;
6012 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6013 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6014 bfce7f83 2019-07-27 stsp free(id_str);
6015 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6016 bfce7f83 2019-07-27 stsp }
6017 bfce7f83 2019-07-27 stsp }
6018 bfce7f83 2019-07-27 stsp
6019 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6020 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6021 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6022 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6023 bfce7f83 2019-07-27 stsp
6024 bfce7f83 2019-07-27 stsp return NULL;
6025 bfce7f83 2019-07-27 stsp }
6026 bfce7f83 2019-07-27 stsp
6027 bfce7f83 2019-07-27 stsp static const struct got_error *
6028 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6029 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6030 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6031 0ebf8283 2019-07-24 stsp {
6032 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6033 0ebf8283 2019-07-24 stsp char *editor;
6034 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6035 0ebf8283 2019-07-24 stsp
6036 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6037 0ebf8283 2019-07-24 stsp if (err)
6038 0ebf8283 2019-07-24 stsp return err;
6039 0ebf8283 2019-07-24 stsp
6040 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6041 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6042 0ebf8283 2019-07-24 stsp goto done;
6043 0ebf8283 2019-07-24 stsp }
6044 0ebf8283 2019-07-24 stsp
6045 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6046 0ebf8283 2019-07-24 stsp if (f == NULL) {
6047 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6048 0ebf8283 2019-07-24 stsp goto done;
6049 0ebf8283 2019-07-24 stsp }
6050 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6051 bfce7f83 2019-07-27 stsp if (err)
6052 bfce7f83 2019-07-27 stsp goto done;
6053 bfce7f83 2019-07-27 stsp
6054 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6055 0ebf8283 2019-07-24 stsp done:
6056 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6057 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6058 0ebf8283 2019-07-24 stsp free(editor);
6059 0ebf8283 2019-07-24 stsp return err;
6060 0ebf8283 2019-07-24 stsp }
6061 0ebf8283 2019-07-24 stsp
6062 0ebf8283 2019-07-24 stsp static const struct got_error *
6063 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6064 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
6065 0ebf8283 2019-07-24 stsp
6066 0ebf8283 2019-07-24 stsp static const struct got_error *
6067 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6068 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6069 0ebf8283 2019-07-24 stsp {
6070 0ebf8283 2019-07-24 stsp const struct got_error *err;
6071 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6072 0ebf8283 2019-07-24 stsp char *path = NULL;
6073 0ebf8283 2019-07-24 stsp
6074 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6075 0ebf8283 2019-07-24 stsp if (err)
6076 0ebf8283 2019-07-24 stsp return err;
6077 0ebf8283 2019-07-24 stsp
6078 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
6079 0ebf8283 2019-07-24 stsp if (err)
6080 0ebf8283 2019-07-24 stsp goto done;
6081 0ebf8283 2019-07-24 stsp
6082 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
6083 0ebf8283 2019-07-24 stsp if (err)
6084 0ebf8283 2019-07-24 stsp goto done;
6085 0ebf8283 2019-07-24 stsp
6086 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
6087 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6088 0ebf8283 2019-07-24 stsp goto done;
6089 0ebf8283 2019-07-24 stsp }
6090 0ebf8283 2019-07-24 stsp f = NULL;
6091 0ebf8283 2019-07-24 stsp
6092 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6093 0ebf8283 2019-07-24 stsp if (err) {
6094 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6095 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6096 0ebf8283 2019-07-24 stsp goto done;
6097 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6098 0ebf8283 2019-07-24 stsp commits, path, repo);
6099 0ebf8283 2019-07-24 stsp }
6100 0ebf8283 2019-07-24 stsp done:
6101 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6102 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6103 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6104 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6105 0ebf8283 2019-07-24 stsp free(path);
6106 0ebf8283 2019-07-24 stsp return err;
6107 0ebf8283 2019-07-24 stsp }
6108 0ebf8283 2019-07-24 stsp
6109 0ebf8283 2019-07-24 stsp static const struct got_error *
6110 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6111 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6112 0ebf8283 2019-07-24 stsp {
6113 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6114 0ebf8283 2019-07-24 stsp char *path = NULL;
6115 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6116 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6117 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6118 0ebf8283 2019-07-24 stsp
6119 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6120 0ebf8283 2019-07-24 stsp if (err)
6121 0ebf8283 2019-07-24 stsp return err;
6122 0ebf8283 2019-07-24 stsp
6123 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6124 0ebf8283 2019-07-24 stsp if (f == NULL) {
6125 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6126 0ebf8283 2019-07-24 stsp goto done;
6127 0ebf8283 2019-07-24 stsp }
6128 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6129 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6130 0ebf8283 2019-07-24 stsp repo);
6131 0ebf8283 2019-07-24 stsp if (err)
6132 0ebf8283 2019-07-24 stsp break;
6133 0ebf8283 2019-07-24 stsp
6134 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6135 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6136 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6137 0ebf8283 2019-07-24 stsp if (n < 0) {
6138 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6139 0ebf8283 2019-07-24 stsp break;
6140 0ebf8283 2019-07-24 stsp }
6141 0ebf8283 2019-07-24 stsp }
6142 0ebf8283 2019-07-24 stsp }
6143 0ebf8283 2019-07-24 stsp done:
6144 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6145 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6146 0ebf8283 2019-07-24 stsp free(path);
6147 0ebf8283 2019-07-24 stsp if (commit)
6148 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6149 0ebf8283 2019-07-24 stsp return err;
6150 0ebf8283 2019-07-24 stsp }
6151 0ebf8283 2019-07-24 stsp
6152 bfce7f83 2019-07-27 stsp void
6153 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6154 bfce7f83 2019-07-27 stsp {
6155 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6156 bfce7f83 2019-07-27 stsp
6157 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6158 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6159 bfce7f83 2019-07-27 stsp free(hle);
6160 bfce7f83 2019-07-27 stsp }
6161 bfce7f83 2019-07-27 stsp }
6162 bfce7f83 2019-07-27 stsp
6163 0ebf8283 2019-07-24 stsp static const struct got_error *
6164 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6165 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6166 0ebf8283 2019-07-24 stsp {
6167 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6168 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6169 0ebf8283 2019-07-24 stsp
6170 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6171 0ebf8283 2019-07-24 stsp if (f == NULL) {
6172 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6173 0ebf8283 2019-07-24 stsp goto done;
6174 0ebf8283 2019-07-24 stsp }
6175 0ebf8283 2019-07-24 stsp
6176 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6177 0ebf8283 2019-07-24 stsp done:
6178 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6179 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6180 0ebf8283 2019-07-24 stsp return err;
6181 0ebf8283 2019-07-24 stsp }
6182 0ebf8283 2019-07-24 stsp
6183 0ebf8283 2019-07-24 stsp static const struct got_error *
6184 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6185 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6186 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6187 0ebf8283 2019-07-24 stsp {
6188 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6189 0ebf8283 2019-07-24 stsp int resp = ' ';
6190 0ebf8283 2019-07-24 stsp
6191 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6192 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6193 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6194 0ebf8283 2019-07-24 stsp resp = getchar();
6195 a61a4414 2019-08-07 stsp if (resp == '\n')
6196 a61a4414 2019-08-07 stsp resp = getchar();
6197 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6198 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6199 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6200 bfce7f83 2019-07-27 stsp repo);
6201 426ebf2e 2019-07-25 stsp if (err) {
6202 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6203 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6204 426ebf2e 2019-07-25 stsp break;
6205 bfce7f83 2019-07-27 stsp prev_err = err;
6206 426ebf2e 2019-07-25 stsp resp = ' ';
6207 426ebf2e 2019-07-25 stsp continue;
6208 426ebf2e 2019-07-25 stsp }
6209 bfce7f83 2019-07-27 stsp break;
6210 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6211 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6212 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6213 0ebf8283 2019-07-24 stsp commits, repo);
6214 426ebf2e 2019-07-25 stsp if (err) {
6215 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6216 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6217 426ebf2e 2019-07-25 stsp break;
6218 bfce7f83 2019-07-27 stsp prev_err = err;
6219 426ebf2e 2019-07-25 stsp resp = ' ';
6220 426ebf2e 2019-07-25 stsp continue;
6221 426ebf2e 2019-07-25 stsp }
6222 bfce7f83 2019-07-27 stsp break;
6223 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6224 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6225 0ebf8283 2019-07-24 stsp break;
6226 426ebf2e 2019-07-25 stsp } else
6227 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6228 0ebf8283 2019-07-24 stsp }
6229 0ebf8283 2019-07-24 stsp
6230 0ebf8283 2019-07-24 stsp return err;
6231 0ebf8283 2019-07-24 stsp }
6232 0ebf8283 2019-07-24 stsp
6233 0ebf8283 2019-07-24 stsp static const struct got_error *
6234 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6235 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6236 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6237 0ebf8283 2019-07-24 stsp {
6238 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6239 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6240 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6241 3e3a69f1 2019-07-25 stsp branch, repo);
6242 0ebf8283 2019-07-24 stsp }
6243 0ebf8283 2019-07-24 stsp
6244 0ebf8283 2019-07-24 stsp static const struct got_error *
6245 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6246 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6247 0ebf8283 2019-07-24 stsp {
6248 0ebf8283 2019-07-24 stsp const struct got_error *err;
6249 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6250 0ebf8283 2019-07-24 stsp
6251 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6252 0ebf8283 2019-07-24 stsp if (err)
6253 0ebf8283 2019-07-24 stsp goto done;
6254 0ebf8283 2019-07-24 stsp
6255 0ebf8283 2019-07-24 stsp if (new_id) {
6256 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6257 0ebf8283 2019-07-24 stsp if (err)
6258 0ebf8283 2019-07-24 stsp goto done;
6259 0ebf8283 2019-07-24 stsp }
6260 0ebf8283 2019-07-24 stsp
6261 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6262 0ebf8283 2019-07-24 stsp if (new_id_str)
6263 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6264 0ebf8283 2019-07-24 stsp
6265 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6266 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6267 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6268 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6269 0ebf8283 2019-07-24 stsp goto done;
6270 0ebf8283 2019-07-24 stsp }
6271 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6272 0ebf8283 2019-07-24 stsp } else {
6273 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6274 0ebf8283 2019-07-24 stsp if (err)
6275 0ebf8283 2019-07-24 stsp goto done;
6276 0ebf8283 2019-07-24 stsp }
6277 0ebf8283 2019-07-24 stsp
6278 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6279 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6280 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6281 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6282 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6283 0ebf8283 2019-07-24 stsp break;
6284 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6285 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6286 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6287 0ebf8283 2019-07-24 stsp logmsg);
6288 0ebf8283 2019-07-24 stsp break;
6289 0ebf8283 2019-07-24 stsp default:
6290 0ebf8283 2019-07-24 stsp break;
6291 0ebf8283 2019-07-24 stsp }
6292 0ebf8283 2019-07-24 stsp
6293 0ebf8283 2019-07-24 stsp done:
6294 0ebf8283 2019-07-24 stsp free(old_id_str);
6295 0ebf8283 2019-07-24 stsp free(new_id_str);
6296 0ebf8283 2019-07-24 stsp return err;
6297 0ebf8283 2019-07-24 stsp }
6298 0ebf8283 2019-07-24 stsp
6299 0ebf8283 2019-07-24 stsp static const struct got_error *
6300 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6301 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6302 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6303 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6304 0ebf8283 2019-07-24 stsp {
6305 0ebf8283 2019-07-24 stsp const struct got_error *err;
6306 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6307 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6308 0ebf8283 2019-07-24 stsp
6309 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6310 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6311 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6312 0ebf8283 2019-07-24 stsp if (err)
6313 0ebf8283 2019-07-24 stsp return err;
6314 0ebf8283 2019-07-24 stsp }
6315 0ebf8283 2019-07-24 stsp
6316 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6317 0ebf8283 2019-07-24 stsp if (err)
6318 0ebf8283 2019-07-24 stsp return err;
6319 0ebf8283 2019-07-24 stsp
6320 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6321 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6322 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6323 0ebf8283 2019-07-24 stsp if (err) {
6324 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6325 0ebf8283 2019-07-24 stsp goto done;
6326 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6327 0ebf8283 2019-07-24 stsp } else {
6328 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6329 0ebf8283 2019-07-24 stsp free(new_commit_id);
6330 0ebf8283 2019-07-24 stsp }
6331 0ebf8283 2019-07-24 stsp done:
6332 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6333 0ebf8283 2019-07-24 stsp return err;
6334 0ebf8283 2019-07-24 stsp }
6335 0ebf8283 2019-07-24 stsp
6336 0ebf8283 2019-07-24 stsp static const struct got_error *
6337 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6338 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6339 0ebf8283 2019-07-24 stsp {
6340 0ebf8283 2019-07-24 stsp const struct got_error *error;
6341 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6342 0ebf8283 2019-07-24 stsp
6343 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6344 0ebf8283 2019-07-24 stsp repo);
6345 0ebf8283 2019-07-24 stsp if (error)
6346 0ebf8283 2019-07-24 stsp return error;
6347 0ebf8283 2019-07-24 stsp
6348 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6349 0ebf8283 2019-07-24 stsp if (error)
6350 0ebf8283 2019-07-24 stsp return error;
6351 0ebf8283 2019-07-24 stsp
6352 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6353 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6354 0ebf8283 2019-07-24 stsp return error;
6355 0ebf8283 2019-07-24 stsp }
6356 0ebf8283 2019-07-24 stsp
6357 0ebf8283 2019-07-24 stsp static const struct got_error *
6358 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6359 0ebf8283 2019-07-24 stsp {
6360 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6361 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6362 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6363 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6364 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6365 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6366 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6367 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6368 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6369 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6370 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6371 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6372 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6373 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6374 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6375 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6376 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6377 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6378 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6379 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6380 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6381 0ebf8283 2019-07-24 stsp
6382 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6383 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6384 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6385 0ebf8283 2019-07-24 stsp
6386 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6387 0ebf8283 2019-07-24 stsp switch (ch) {
6388 0ebf8283 2019-07-24 stsp case 'a':
6389 0ebf8283 2019-07-24 stsp abort_edit = 1;
6390 0ebf8283 2019-07-24 stsp break;
6391 0ebf8283 2019-07-24 stsp case 'c':
6392 0ebf8283 2019-07-24 stsp continue_edit = 1;
6393 0ebf8283 2019-07-24 stsp break;
6394 0ebf8283 2019-07-24 stsp case 'F':
6395 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6396 0ebf8283 2019-07-24 stsp break;
6397 0ebf8283 2019-07-24 stsp default:
6398 0ebf8283 2019-07-24 stsp usage_histedit();
6399 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6400 0ebf8283 2019-07-24 stsp }
6401 0ebf8283 2019-07-24 stsp }
6402 0ebf8283 2019-07-24 stsp
6403 0ebf8283 2019-07-24 stsp argc -= optind;
6404 0ebf8283 2019-07-24 stsp argv += optind;
6405 0ebf8283 2019-07-24 stsp
6406 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6407 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6408 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6409 0ebf8283 2019-07-24 stsp err(1, "pledge");
6410 0ebf8283 2019-07-24 stsp #endif
6411 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6412 0ebf8283 2019-07-24 stsp usage_histedit();
6413 0ebf8283 2019-07-24 stsp if (argc != 0)
6414 0ebf8283 2019-07-24 stsp usage_histedit();
6415 0ebf8283 2019-07-24 stsp
6416 0ebf8283 2019-07-24 stsp /*
6417 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6418 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6419 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6420 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6421 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6422 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6423 0ebf8283 2019-07-24 stsp */
6424 0ebf8283 2019-07-24 stsp
6425 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6426 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6427 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6428 0ebf8283 2019-07-24 stsp goto done;
6429 0ebf8283 2019-07-24 stsp }
6430 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6431 0ebf8283 2019-07-24 stsp if (error)
6432 0ebf8283 2019-07-24 stsp goto done;
6433 0ebf8283 2019-07-24 stsp
6434 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6435 c9956ddf 2019-09-08 stsp NULL);
6436 0ebf8283 2019-07-24 stsp if (error != NULL)
6437 0ebf8283 2019-07-24 stsp goto done;
6438 0ebf8283 2019-07-24 stsp
6439 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6440 0ebf8283 2019-07-24 stsp if (error)
6441 0ebf8283 2019-07-24 stsp goto done;
6442 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6443 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6444 0ebf8283 2019-07-24 stsp goto done;
6445 0ebf8283 2019-07-24 stsp }
6446 0ebf8283 2019-07-24 stsp
6447 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6448 0ebf8283 2019-07-24 stsp if (error)
6449 0ebf8283 2019-07-24 stsp goto done;
6450 0ebf8283 2019-07-24 stsp
6451 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6452 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6453 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6454 3e3a69f1 2019-07-25 stsp worktree, repo);
6455 0ebf8283 2019-07-24 stsp if (error)
6456 0ebf8283 2019-07-24 stsp goto done;
6457 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6458 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6459 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6460 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6461 0ebf8283 2019-07-24 stsp if (error)
6462 0ebf8283 2019-07-24 stsp goto done;
6463 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6464 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6465 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6466 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6467 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6468 0ebf8283 2019-07-24 stsp goto done;
6469 0ebf8283 2019-07-24 stsp }
6470 0ebf8283 2019-07-24 stsp
6471 0ebf8283 2019-07-24 stsp if (continue_edit) {
6472 0ebf8283 2019-07-24 stsp char *path;
6473 0ebf8283 2019-07-24 stsp
6474 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6475 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6476 0ebf8283 2019-07-24 stsp goto done;
6477 0ebf8283 2019-07-24 stsp }
6478 0ebf8283 2019-07-24 stsp
6479 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6480 0ebf8283 2019-07-24 stsp if (error)
6481 0ebf8283 2019-07-24 stsp goto done;
6482 0ebf8283 2019-07-24 stsp
6483 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6484 0ebf8283 2019-07-24 stsp free(path);
6485 0ebf8283 2019-07-24 stsp if (error)
6486 0ebf8283 2019-07-24 stsp goto done;
6487 0ebf8283 2019-07-24 stsp
6488 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6489 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6490 3e3a69f1 2019-07-25 stsp worktree, repo);
6491 0ebf8283 2019-07-24 stsp if (error)
6492 0ebf8283 2019-07-24 stsp goto done;
6493 0ebf8283 2019-07-24 stsp
6494 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6495 0ebf8283 2019-07-24 stsp if (error)
6496 0ebf8283 2019-07-24 stsp goto done;
6497 0ebf8283 2019-07-24 stsp
6498 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6499 0ebf8283 2019-07-24 stsp head_commit_id);
6500 0ebf8283 2019-07-24 stsp if (error)
6501 0ebf8283 2019-07-24 stsp goto done;
6502 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6503 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6504 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6505 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6506 3aac7cf7 2019-07-25 stsp goto done;
6507 3aac7cf7 2019-07-25 stsp }
6508 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6509 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6510 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6511 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6512 0ebf8283 2019-07-24 stsp commit = NULL;
6513 0ebf8283 2019-07-24 stsp if (error)
6514 0ebf8283 2019-07-24 stsp goto done;
6515 0ebf8283 2019-07-24 stsp } else {
6516 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6517 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6518 0ebf8283 2019-07-24 stsp goto done;
6519 0ebf8283 2019-07-24 stsp }
6520 0ebf8283 2019-07-24 stsp
6521 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6522 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6523 0ebf8283 2019-07-24 stsp if (error != NULL)
6524 0ebf8283 2019-07-24 stsp goto done;
6525 0ebf8283 2019-07-24 stsp
6526 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6527 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6528 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6529 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6530 c7d20a3f 2019-07-30 stsp goto done;
6531 c7d20a3f 2019-07-30 stsp }
6532 c7d20a3f 2019-07-30 stsp
6533 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6534 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6535 bfce7f83 2019-07-27 stsp branch = NULL;
6536 0ebf8283 2019-07-24 stsp if (error)
6537 0ebf8283 2019-07-24 stsp goto done;
6538 0ebf8283 2019-07-24 stsp
6539 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6540 0ebf8283 2019-07-24 stsp head_commit_id);
6541 0ebf8283 2019-07-24 stsp if (error)
6542 0ebf8283 2019-07-24 stsp goto done;
6543 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6544 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6545 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6546 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6547 3aac7cf7 2019-07-25 stsp goto done;
6548 3aac7cf7 2019-07-25 stsp }
6549 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6550 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6551 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6552 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6553 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6554 0ebf8283 2019-07-24 stsp commit = NULL;
6555 0ebf8283 2019-07-24 stsp if (error)
6556 0ebf8283 2019-07-24 stsp goto done;
6557 0ebf8283 2019-07-24 stsp
6558 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6559 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6560 bfce7f83 2019-07-27 stsp if (error)
6561 bfce7f83 2019-07-27 stsp goto done;
6562 bfce7f83 2019-07-27 stsp
6563 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6564 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6565 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6566 6ba2bea2 2019-07-27 stsp if (error) {
6567 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6568 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6569 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6570 0ebf8283 2019-07-24 stsp goto done;
6571 6ba2bea2 2019-07-27 stsp }
6572 0ebf8283 2019-07-24 stsp } else {
6573 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6574 0ebf8283 2019-07-24 stsp repo);
6575 6ba2bea2 2019-07-27 stsp if (error) {
6576 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6577 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6578 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6579 0ebf8283 2019-07-24 stsp goto done;
6580 6ba2bea2 2019-07-27 stsp }
6581 0ebf8283 2019-07-24 stsp
6582 0ebf8283 2019-07-24 stsp }
6583 0ebf8283 2019-07-24 stsp
6584 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6585 0ebf8283 2019-07-24 stsp repo);
6586 6ba2bea2 2019-07-27 stsp if (error) {
6587 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6588 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6589 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6590 0ebf8283 2019-07-24 stsp goto done;
6591 6ba2bea2 2019-07-27 stsp }
6592 0ebf8283 2019-07-24 stsp
6593 0ebf8283 2019-07-24 stsp }
6594 0ebf8283 2019-07-24 stsp
6595 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6596 4ec14e60 2019-08-28 hiltjo if (error)
6597 0ebf8283 2019-07-24 stsp goto done;
6598 0ebf8283 2019-07-24 stsp
6599 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6600 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6601 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6602 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6603 0ebf8283 2019-07-24 stsp continue;
6604 0ebf8283 2019-07-24 stsp
6605 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6606 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6607 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6608 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6609 0ebf8283 2019-07-24 stsp repo);
6610 0ebf8283 2019-07-24 stsp } else {
6611 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6612 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6613 0ebf8283 2019-07-24 stsp }
6614 0ebf8283 2019-07-24 stsp if (error)
6615 0ebf8283 2019-07-24 stsp goto done;
6616 0ebf8283 2019-07-24 stsp continue;
6617 0ebf8283 2019-07-24 stsp }
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6620 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6621 0ebf8283 2019-07-24 stsp if (error)
6622 0ebf8283 2019-07-24 stsp goto done;
6623 0ebf8283 2019-07-24 stsp continue;
6624 0ebf8283 2019-07-24 stsp }
6625 0ebf8283 2019-07-24 stsp
6626 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6627 0ebf8283 2019-07-24 stsp hle->commit_id);
6628 0ebf8283 2019-07-24 stsp if (error)
6629 0ebf8283 2019-07-24 stsp goto done;
6630 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6631 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6632 0ebf8283 2019-07-24 stsp
6633 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6634 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6635 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6636 0ebf8283 2019-07-24 stsp if (error)
6637 0ebf8283 2019-07-24 stsp goto done;
6638 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6639 0ebf8283 2019-07-24 stsp commit = NULL;
6640 0ebf8283 2019-07-24 stsp
6641 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6642 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6643 0ebf8283 2019-07-24 stsp break;
6644 0ebf8283 2019-07-24 stsp }
6645 0ebf8283 2019-07-24 stsp
6646 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6647 0ebf8283 2019-07-24 stsp char *id_str;
6648 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6649 0ebf8283 2019-07-24 stsp if (error)
6650 0ebf8283 2019-07-24 stsp goto done;
6651 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6652 0ebf8283 2019-07-24 stsp id_str);
6653 0ebf8283 2019-07-24 stsp free(id_str);
6654 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6655 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6656 3e3a69f1 2019-07-25 stsp fileindex);
6657 0ebf8283 2019-07-24 stsp goto done;
6658 0ebf8283 2019-07-24 stsp }
6659 0ebf8283 2019-07-24 stsp
6660 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6661 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6662 0ebf8283 2019-07-24 stsp if (error)
6663 0ebf8283 2019-07-24 stsp goto done;
6664 0ebf8283 2019-07-24 stsp continue;
6665 0ebf8283 2019-07-24 stsp }
6666 0ebf8283 2019-07-24 stsp
6667 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6668 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6669 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6670 0ebf8283 2019-07-24 stsp if (error)
6671 0ebf8283 2019-07-24 stsp goto done;
6672 0ebf8283 2019-07-24 stsp }
6673 0ebf8283 2019-07-24 stsp
6674 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6675 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6676 0ebf8283 2019-07-24 stsp if (error)
6677 0ebf8283 2019-07-24 stsp goto done;
6678 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6679 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6680 0ebf8283 2019-07-24 stsp } else
6681 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6682 3e3a69f1 2019-07-25 stsp branch, repo);
6683 0ebf8283 2019-07-24 stsp done:
6684 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6685 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6686 0ebf8283 2019-07-24 stsp free(head_commit_id);
6687 0ebf8283 2019-07-24 stsp free(base_commit_id);
6688 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6689 0ebf8283 2019-07-24 stsp if (commit)
6690 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6691 0ebf8283 2019-07-24 stsp if (branch)
6692 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6693 0ebf8283 2019-07-24 stsp if (tmp_branch)
6694 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6695 0ebf8283 2019-07-24 stsp if (worktree)
6696 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6697 2822a352 2019-10-15 stsp if (repo)
6698 2822a352 2019-10-15 stsp got_repo_close(repo);
6699 2822a352 2019-10-15 stsp return error;
6700 2822a352 2019-10-15 stsp }
6701 2822a352 2019-10-15 stsp
6702 2822a352 2019-10-15 stsp __dead static void
6703 2822a352 2019-10-15 stsp usage_integrate(void)
6704 2822a352 2019-10-15 stsp {
6705 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6706 2822a352 2019-10-15 stsp exit(1);
6707 2822a352 2019-10-15 stsp }
6708 2822a352 2019-10-15 stsp
6709 2822a352 2019-10-15 stsp static const struct got_error *
6710 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6711 2822a352 2019-10-15 stsp {
6712 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6713 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6714 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6715 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6716 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6717 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6718 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6719 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6720 2822a352 2019-10-15 stsp int ch, did_something = 0;
6721 2822a352 2019-10-15 stsp
6722 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6723 2822a352 2019-10-15 stsp switch (ch) {
6724 2822a352 2019-10-15 stsp default:
6725 2822a352 2019-10-15 stsp usage_integrate();
6726 2822a352 2019-10-15 stsp /* NOTREACHED */
6727 2822a352 2019-10-15 stsp }
6728 2822a352 2019-10-15 stsp }
6729 2822a352 2019-10-15 stsp
6730 2822a352 2019-10-15 stsp argc -= optind;
6731 2822a352 2019-10-15 stsp argv += optind;
6732 2822a352 2019-10-15 stsp
6733 2822a352 2019-10-15 stsp if (argc != 1)
6734 2822a352 2019-10-15 stsp usage_integrate();
6735 2822a352 2019-10-15 stsp branch_arg = argv[0];
6736 2822a352 2019-10-15 stsp
6737 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6738 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6739 2822a352 2019-10-15 stsp err(1, "pledge");
6740 2822a352 2019-10-15 stsp
6741 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6742 2822a352 2019-10-15 stsp if (cwd == NULL) {
6743 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6744 2822a352 2019-10-15 stsp goto done;
6745 2822a352 2019-10-15 stsp }
6746 2822a352 2019-10-15 stsp
6747 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6748 2822a352 2019-10-15 stsp if (error)
6749 2822a352 2019-10-15 stsp goto done;
6750 2822a352 2019-10-15 stsp
6751 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6752 2822a352 2019-10-15 stsp if (error)
6753 2822a352 2019-10-15 stsp goto done;
6754 2822a352 2019-10-15 stsp
6755 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6756 2822a352 2019-10-15 stsp NULL);
6757 2822a352 2019-10-15 stsp if (error != NULL)
6758 2822a352 2019-10-15 stsp goto done;
6759 2822a352 2019-10-15 stsp
6760 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6761 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6762 2822a352 2019-10-15 stsp if (error)
6763 2822a352 2019-10-15 stsp goto done;
6764 2822a352 2019-10-15 stsp
6765 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6766 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6767 2822a352 2019-10-15 stsp goto done;
6768 2822a352 2019-10-15 stsp }
6769 2822a352 2019-10-15 stsp
6770 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6771 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6772 2822a352 2019-10-15 stsp if (error)
6773 2822a352 2019-10-15 stsp goto done;
6774 2822a352 2019-10-15 stsp
6775 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6776 2822a352 2019-10-15 stsp if (refname == NULL) {
6777 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6778 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6779 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6780 2822a352 2019-10-15 stsp goto done;
6781 2822a352 2019-10-15 stsp }
6782 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6783 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6784 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6785 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6786 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6787 2822a352 2019-10-15 stsp goto done;
6788 2822a352 2019-10-15 stsp }
6789 2822a352 2019-10-15 stsp
6790 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6791 2822a352 2019-10-15 stsp if (error)
6792 2822a352 2019-10-15 stsp goto done;
6793 2822a352 2019-10-15 stsp
6794 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6795 2822a352 2019-10-15 stsp if (error)
6796 2822a352 2019-10-15 stsp goto done;
6797 2822a352 2019-10-15 stsp
6798 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6799 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6800 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6801 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6802 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6803 2822a352 2019-10-15 stsp goto done;
6804 2822a352 2019-10-15 stsp }
6805 2822a352 2019-10-15 stsp
6806 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6807 2822a352 2019-10-15 stsp if (error) {
6808 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6809 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6810 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6811 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6812 2822a352 2019-10-15 stsp goto done;
6813 2822a352 2019-10-15 stsp }
6814 2822a352 2019-10-15 stsp
6815 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6816 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6817 2822a352 2019-10-15 stsp check_cancelled, NULL);
6818 2822a352 2019-10-15 stsp if (error)
6819 2822a352 2019-10-15 stsp goto done;
6820 2822a352 2019-10-15 stsp
6821 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6822 2822a352 2019-10-15 stsp done:
6823 715dc77e 2019-08-03 stsp if (repo)
6824 715dc77e 2019-08-03 stsp got_repo_close(repo);
6825 2822a352 2019-10-15 stsp if (worktree)
6826 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6827 2822a352 2019-10-15 stsp free(cwd);
6828 2822a352 2019-10-15 stsp free(base_commit_id);
6829 2822a352 2019-10-15 stsp free(commit_id);
6830 2822a352 2019-10-15 stsp free(refname);
6831 2822a352 2019-10-15 stsp free(base_refname);
6832 715dc77e 2019-08-03 stsp return error;
6833 715dc77e 2019-08-03 stsp }
6834 715dc77e 2019-08-03 stsp
6835 715dc77e 2019-08-03 stsp __dead static void
6836 715dc77e 2019-08-03 stsp usage_stage(void)
6837 715dc77e 2019-08-03 stsp {
6838 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6839 f3055044 2019-08-07 stsp "[file-path ...]\n",
6840 715dc77e 2019-08-03 stsp getprogname());
6841 715dc77e 2019-08-03 stsp exit(1);
6842 715dc77e 2019-08-03 stsp }
6843 715dc77e 2019-08-03 stsp
6844 715dc77e 2019-08-03 stsp static const struct got_error *
6845 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6846 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6847 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6848 408b4ebc 2019-08-03 stsp {
6849 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6850 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6851 408b4ebc 2019-08-03 stsp
6852 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6853 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6854 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6855 408b4ebc 2019-08-03 stsp return NULL;
6856 408b4ebc 2019-08-03 stsp
6857 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6858 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6859 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6860 408b4ebc 2019-08-03 stsp else
6861 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6862 408b4ebc 2019-08-03 stsp if (err)
6863 408b4ebc 2019-08-03 stsp return err;
6864 408b4ebc 2019-08-03 stsp
6865 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6866 408b4ebc 2019-08-03 stsp free(id_str);
6867 dc424a06 2019-08-07 stsp return NULL;
6868 dc424a06 2019-08-07 stsp }
6869 2e1f37b0 2019-08-08 stsp
6870 dc424a06 2019-08-07 stsp static const struct got_error *
6871 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6872 715dc77e 2019-08-03 stsp {
6873 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6874 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6875 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6876 715dc77e 2019-08-03 stsp char *cwd = NULL;
6877 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6878 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6879 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6880 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6881 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6882 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6883 715dc77e 2019-08-03 stsp
6884 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6885 715dc77e 2019-08-03 stsp
6886 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6887 715dc77e 2019-08-03 stsp switch (ch) {
6888 408b4ebc 2019-08-03 stsp case 'l':
6889 408b4ebc 2019-08-03 stsp list_stage = 1;
6890 408b4ebc 2019-08-03 stsp break;
6891 dc424a06 2019-08-07 stsp case 'p':
6892 dc424a06 2019-08-07 stsp pflag = 1;
6893 dc424a06 2019-08-07 stsp break;
6894 dc424a06 2019-08-07 stsp case 'F':
6895 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6896 dc424a06 2019-08-07 stsp break;
6897 715dc77e 2019-08-03 stsp default:
6898 715dc77e 2019-08-03 stsp usage_stage();
6899 715dc77e 2019-08-03 stsp /* NOTREACHED */
6900 715dc77e 2019-08-03 stsp }
6901 715dc77e 2019-08-03 stsp }
6902 715dc77e 2019-08-03 stsp
6903 715dc77e 2019-08-03 stsp argc -= optind;
6904 715dc77e 2019-08-03 stsp argv += optind;
6905 715dc77e 2019-08-03 stsp
6906 715dc77e 2019-08-03 stsp #ifndef PROFILE
6907 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6908 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6909 715dc77e 2019-08-03 stsp err(1, "pledge");
6910 715dc77e 2019-08-03 stsp #endif
6911 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6912 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6913 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6914 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6915 715dc77e 2019-08-03 stsp
6916 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6917 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6918 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6919 715dc77e 2019-08-03 stsp goto done;
6920 715dc77e 2019-08-03 stsp }
6921 715dc77e 2019-08-03 stsp
6922 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6923 715dc77e 2019-08-03 stsp if (error)
6924 715dc77e 2019-08-03 stsp goto done;
6925 715dc77e 2019-08-03 stsp
6926 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6927 c9956ddf 2019-09-08 stsp NULL);
6928 715dc77e 2019-08-03 stsp if (error != NULL)
6929 715dc77e 2019-08-03 stsp goto done;
6930 715dc77e 2019-08-03 stsp
6931 dc424a06 2019-08-07 stsp if (patch_script_path) {
6932 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6933 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6934 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6935 dc424a06 2019-08-07 stsp patch_script_path);
6936 dc424a06 2019-08-07 stsp goto done;
6937 dc424a06 2019-08-07 stsp }
6938 dc424a06 2019-08-07 stsp }
6939 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6940 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6941 715dc77e 2019-08-03 stsp if (error)
6942 715dc77e 2019-08-03 stsp goto done;
6943 715dc77e 2019-08-03 stsp
6944 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6945 6d022e97 2019-08-04 stsp if (error)
6946 6d022e97 2019-08-04 stsp goto done;
6947 715dc77e 2019-08-03 stsp
6948 6d022e97 2019-08-04 stsp if (list_stage)
6949 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6950 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6951 2e1f37b0 2019-08-08 stsp else {
6952 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6953 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6954 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6955 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6956 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6957 2e1f37b0 2019-08-08 stsp }
6958 ad493afc 2019-08-03 stsp done:
6959 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6960 2e1f37b0 2019-08-08 stsp error == NULL)
6961 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6962 ad493afc 2019-08-03 stsp if (repo)
6963 ad493afc 2019-08-03 stsp got_repo_close(repo);
6964 ad493afc 2019-08-03 stsp if (worktree)
6965 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6966 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6967 ad493afc 2019-08-03 stsp free((char *)pe->path);
6968 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6969 ad493afc 2019-08-03 stsp free(cwd);
6970 ad493afc 2019-08-03 stsp return error;
6971 ad493afc 2019-08-03 stsp }
6972 ad493afc 2019-08-03 stsp
6973 ad493afc 2019-08-03 stsp __dead static void
6974 ad493afc 2019-08-03 stsp usage_unstage(void)
6975 ad493afc 2019-08-03 stsp {
6976 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6977 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6978 ad493afc 2019-08-03 stsp getprogname());
6979 ad493afc 2019-08-03 stsp exit(1);
6980 ad493afc 2019-08-03 stsp }
6981 ad493afc 2019-08-03 stsp
6982 ad493afc 2019-08-03 stsp
6983 ad493afc 2019-08-03 stsp static const struct got_error *
6984 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6985 ad493afc 2019-08-03 stsp {
6986 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6987 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6988 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6989 ad493afc 2019-08-03 stsp char *cwd = NULL;
6990 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6991 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6992 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6993 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6994 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6995 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6996 ad493afc 2019-08-03 stsp
6997 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6998 ad493afc 2019-08-03 stsp
6999 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7000 ad493afc 2019-08-03 stsp switch (ch) {
7001 2e1f37b0 2019-08-08 stsp case 'p':
7002 2e1f37b0 2019-08-08 stsp pflag = 1;
7003 2e1f37b0 2019-08-08 stsp break;
7004 2e1f37b0 2019-08-08 stsp case 'F':
7005 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7006 2e1f37b0 2019-08-08 stsp break;
7007 ad493afc 2019-08-03 stsp default:
7008 ad493afc 2019-08-03 stsp usage_unstage();
7009 ad493afc 2019-08-03 stsp /* NOTREACHED */
7010 ad493afc 2019-08-03 stsp }
7011 ad493afc 2019-08-03 stsp }
7012 ad493afc 2019-08-03 stsp
7013 ad493afc 2019-08-03 stsp argc -= optind;
7014 ad493afc 2019-08-03 stsp argv += optind;
7015 ad493afc 2019-08-03 stsp
7016 ad493afc 2019-08-03 stsp #ifndef PROFILE
7017 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7018 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7019 ad493afc 2019-08-03 stsp err(1, "pledge");
7020 ad493afc 2019-08-03 stsp #endif
7021 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7022 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7023 2e1f37b0 2019-08-08 stsp
7024 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7025 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7026 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7027 ad493afc 2019-08-03 stsp goto done;
7028 ad493afc 2019-08-03 stsp }
7029 ad493afc 2019-08-03 stsp
7030 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7031 ad493afc 2019-08-03 stsp if (error)
7032 ad493afc 2019-08-03 stsp goto done;
7033 ad493afc 2019-08-03 stsp
7034 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7035 c9956ddf 2019-09-08 stsp NULL);
7036 ad493afc 2019-08-03 stsp if (error != NULL)
7037 ad493afc 2019-08-03 stsp goto done;
7038 ad493afc 2019-08-03 stsp
7039 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7040 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7041 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7042 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7043 2e1f37b0 2019-08-08 stsp patch_script_path);
7044 2e1f37b0 2019-08-08 stsp goto done;
7045 2e1f37b0 2019-08-08 stsp }
7046 2e1f37b0 2019-08-08 stsp }
7047 2e1f37b0 2019-08-08 stsp
7048 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7049 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7050 ad493afc 2019-08-03 stsp if (error)
7051 ad493afc 2019-08-03 stsp goto done;
7052 ad493afc 2019-08-03 stsp
7053 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7054 ad493afc 2019-08-03 stsp if (error)
7055 ad493afc 2019-08-03 stsp goto done;
7056 ad493afc 2019-08-03 stsp
7057 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7058 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7059 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7060 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7061 715dc77e 2019-08-03 stsp done:
7062 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7063 2e1f37b0 2019-08-08 stsp error == NULL)
7064 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7065 0ebf8283 2019-07-24 stsp if (repo)
7066 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7067 715dc77e 2019-08-03 stsp if (worktree)
7068 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7069 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7070 715dc77e 2019-08-03 stsp free((char *)pe->path);
7071 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7072 715dc77e 2019-08-03 stsp free(cwd);
7073 01073a5d 2019-08-22 stsp return error;
7074 01073a5d 2019-08-22 stsp }
7075 01073a5d 2019-08-22 stsp
7076 01073a5d 2019-08-22 stsp __dead static void
7077 01073a5d 2019-08-22 stsp usage_cat(void)
7078 01073a5d 2019-08-22 stsp {
7079 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7080 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7081 01073a5d 2019-08-22 stsp exit(1);
7082 01073a5d 2019-08-22 stsp }
7083 01073a5d 2019-08-22 stsp
7084 01073a5d 2019-08-22 stsp static const struct got_error *
7085 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7086 01073a5d 2019-08-22 stsp {
7087 01073a5d 2019-08-22 stsp const struct got_error *err;
7088 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7089 01073a5d 2019-08-22 stsp
7090 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7091 01073a5d 2019-08-22 stsp if (err)
7092 01073a5d 2019-08-22 stsp return err;
7093 01073a5d 2019-08-22 stsp
7094 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7095 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7096 01073a5d 2019-08-22 stsp return err;
7097 01073a5d 2019-08-22 stsp }
7098 01073a5d 2019-08-22 stsp
7099 01073a5d 2019-08-22 stsp static const struct got_error *
7100 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7101 01073a5d 2019-08-22 stsp {
7102 01073a5d 2019-08-22 stsp const struct got_error *err;
7103 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7104 56e0773d 2019-11-28 stsp int nentries, i;
7105 01073a5d 2019-08-22 stsp
7106 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7107 01073a5d 2019-08-22 stsp if (err)
7108 01073a5d 2019-08-22 stsp return err;
7109 01073a5d 2019-08-22 stsp
7110 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7111 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7112 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7113 01073a5d 2019-08-22 stsp char *id_str;
7114 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7115 01073a5d 2019-08-22 stsp break;
7116 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7117 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7118 01073a5d 2019-08-22 stsp if (err)
7119 01073a5d 2019-08-22 stsp break;
7120 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7121 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7122 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7123 01073a5d 2019-08-22 stsp free(id_str);
7124 01073a5d 2019-08-22 stsp }
7125 01073a5d 2019-08-22 stsp
7126 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7127 01073a5d 2019-08-22 stsp return err;
7128 01073a5d 2019-08-22 stsp }
7129 01073a5d 2019-08-22 stsp
7130 01073a5d 2019-08-22 stsp static const struct got_error *
7131 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7132 01073a5d 2019-08-22 stsp {
7133 01073a5d 2019-08-22 stsp const struct got_error *err;
7134 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7135 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7136 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7137 01073a5d 2019-08-22 stsp char *id_str = NULL;
7138 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7139 01073a5d 2019-08-22 stsp
7140 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7141 01073a5d 2019-08-22 stsp if (err)
7142 01073a5d 2019-08-22 stsp return err;
7143 01073a5d 2019-08-22 stsp
7144 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7145 01073a5d 2019-08-22 stsp if (err)
7146 01073a5d 2019-08-22 stsp goto done;
7147 01073a5d 2019-08-22 stsp
7148 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7149 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7150 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7151 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7152 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7153 01073a5d 2019-08-22 stsp char *pid_str;
7154 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7155 01073a5d 2019-08-22 stsp if (err)
7156 01073a5d 2019-08-22 stsp goto done;
7157 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7158 01073a5d 2019-08-22 stsp free(pid_str);
7159 01073a5d 2019-08-22 stsp }
7160 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7161 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7162 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7163 01073a5d 2019-08-22 stsp
7164 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7165 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7166 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7167 01073a5d 2019-08-22 stsp
7168 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7169 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7170 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7171 01073a5d 2019-08-22 stsp done:
7172 01073a5d 2019-08-22 stsp free(id_str);
7173 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7174 01073a5d 2019-08-22 stsp return err;
7175 01073a5d 2019-08-22 stsp }
7176 01073a5d 2019-08-22 stsp
7177 01073a5d 2019-08-22 stsp static const struct got_error *
7178 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7179 01073a5d 2019-08-22 stsp {
7180 01073a5d 2019-08-22 stsp const struct got_error *err;
7181 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7182 01073a5d 2019-08-22 stsp char *id_str = NULL;
7183 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7184 01073a5d 2019-08-22 stsp
7185 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7186 01073a5d 2019-08-22 stsp if (err)
7187 01073a5d 2019-08-22 stsp return err;
7188 01073a5d 2019-08-22 stsp
7189 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7190 01073a5d 2019-08-22 stsp if (err)
7191 01073a5d 2019-08-22 stsp goto done;
7192 01073a5d 2019-08-22 stsp
7193 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7194 e15d5241 2019-08-22 stsp
7195 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7196 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7197 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7198 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7199 01073a5d 2019-08-22 stsp break;
7200 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7201 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7202 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7203 01073a5d 2019-08-22 stsp break;
7204 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7205 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7206 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7207 01073a5d 2019-08-22 stsp break;
7208 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7209 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7210 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7211 01073a5d 2019-08-22 stsp break;
7212 01073a5d 2019-08-22 stsp default:
7213 01073a5d 2019-08-22 stsp break;
7214 01073a5d 2019-08-22 stsp }
7215 01073a5d 2019-08-22 stsp
7216 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7217 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7218 e15d5241 2019-08-22 stsp
7219 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7220 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7221 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7222 01073a5d 2019-08-22 stsp
7223 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7224 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7225 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7226 01073a5d 2019-08-22 stsp done:
7227 01073a5d 2019-08-22 stsp free(id_str);
7228 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7229 01073a5d 2019-08-22 stsp return err;
7230 01073a5d 2019-08-22 stsp }
7231 01073a5d 2019-08-22 stsp
7232 01073a5d 2019-08-22 stsp static const struct got_error *
7233 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7234 01073a5d 2019-08-22 stsp {
7235 01073a5d 2019-08-22 stsp const struct got_error *error;
7236 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7237 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7238 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7239 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7240 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7241 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7242 01073a5d 2019-08-22 stsp
7243 01073a5d 2019-08-22 stsp #ifndef PROFILE
7244 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7245 01073a5d 2019-08-22 stsp NULL) == -1)
7246 01073a5d 2019-08-22 stsp err(1, "pledge");
7247 01073a5d 2019-08-22 stsp #endif
7248 01073a5d 2019-08-22 stsp
7249 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7250 01073a5d 2019-08-22 stsp switch (ch) {
7251 896e9b6f 2019-08-26 stsp case 'c':
7252 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7253 896e9b6f 2019-08-26 stsp break;
7254 01073a5d 2019-08-22 stsp case 'r':
7255 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7256 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7257 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7258 9ba1d308 2019-10-21 stsp optarg);
7259 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7260 01073a5d 2019-08-22 stsp break;
7261 896e9b6f 2019-08-26 stsp case 'P':
7262 896e9b6f 2019-08-26 stsp force_path = 1;
7263 896e9b6f 2019-08-26 stsp break;
7264 01073a5d 2019-08-22 stsp default:
7265 01073a5d 2019-08-22 stsp usage_cat();
7266 01073a5d 2019-08-22 stsp /* NOTREACHED */
7267 01073a5d 2019-08-22 stsp }
7268 01073a5d 2019-08-22 stsp }
7269 01073a5d 2019-08-22 stsp
7270 01073a5d 2019-08-22 stsp argc -= optind;
7271 01073a5d 2019-08-22 stsp argv += optind;
7272 01073a5d 2019-08-22 stsp
7273 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7274 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7275 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7276 01073a5d 2019-08-22 stsp goto done;
7277 01073a5d 2019-08-22 stsp }
7278 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7279 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7280 01073a5d 2019-08-22 stsp goto done;
7281 01073a5d 2019-08-22 stsp if (worktree) {
7282 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7283 01073a5d 2019-08-22 stsp repo_path = strdup(
7284 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7285 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7286 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7287 01073a5d 2019-08-22 stsp goto done;
7288 01073a5d 2019-08-22 stsp }
7289 01073a5d 2019-08-22 stsp }
7290 01073a5d 2019-08-22 stsp }
7291 01073a5d 2019-08-22 stsp
7292 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7293 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7294 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7295 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7296 01073a5d 2019-08-22 stsp }
7297 01073a5d 2019-08-22 stsp
7298 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7299 01073a5d 2019-08-22 stsp free(repo_path);
7300 01073a5d 2019-08-22 stsp if (error != NULL)
7301 01073a5d 2019-08-22 stsp goto done;
7302 01073a5d 2019-08-22 stsp
7303 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7304 896e9b6f 2019-08-26 stsp if (error)
7305 896e9b6f 2019-08-26 stsp goto done;
7306 896e9b6f 2019-08-26 stsp
7307 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7308 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7309 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7310 01073a5d 2019-08-22 stsp if (error)
7311 01073a5d 2019-08-22 stsp goto done;
7312 01073a5d 2019-08-22 stsp
7313 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7314 896e9b6f 2019-08-26 stsp if (force_path) {
7315 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7316 896e9b6f 2019-08-26 stsp argv[i]);
7317 896e9b6f 2019-08-26 stsp if (error)
7318 896e9b6f 2019-08-26 stsp break;
7319 896e9b6f 2019-08-26 stsp } else {
7320 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7321 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7322 896e9b6f 2019-08-26 stsp if (error) {
7323 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7324 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7325 896e9b6f 2019-08-26 stsp break;
7326 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7327 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7328 896e9b6f 2019-08-26 stsp if (error)
7329 896e9b6f 2019-08-26 stsp break;
7330 896e9b6f 2019-08-26 stsp }
7331 896e9b6f 2019-08-26 stsp }
7332 01073a5d 2019-08-22 stsp
7333 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7334 01073a5d 2019-08-22 stsp if (error)
7335 01073a5d 2019-08-22 stsp break;
7336 01073a5d 2019-08-22 stsp
7337 01073a5d 2019-08-22 stsp switch (obj_type) {
7338 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7339 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7340 01073a5d 2019-08-22 stsp break;
7341 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7342 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7343 01073a5d 2019-08-22 stsp break;
7344 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7345 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7346 01073a5d 2019-08-22 stsp break;
7347 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7348 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7349 01073a5d 2019-08-22 stsp break;
7350 01073a5d 2019-08-22 stsp default:
7351 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7352 01073a5d 2019-08-22 stsp break;
7353 01073a5d 2019-08-22 stsp }
7354 01073a5d 2019-08-22 stsp if (error)
7355 01073a5d 2019-08-22 stsp break;
7356 01073a5d 2019-08-22 stsp free(label);
7357 01073a5d 2019-08-22 stsp label = NULL;
7358 01073a5d 2019-08-22 stsp free(id);
7359 01073a5d 2019-08-22 stsp id = NULL;
7360 01073a5d 2019-08-22 stsp }
7361 01073a5d 2019-08-22 stsp
7362 01073a5d 2019-08-22 stsp done:
7363 01073a5d 2019-08-22 stsp free(label);
7364 01073a5d 2019-08-22 stsp free(id);
7365 896e9b6f 2019-08-26 stsp free(commit_id);
7366 01073a5d 2019-08-22 stsp if (worktree)
7367 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7368 01073a5d 2019-08-22 stsp if (repo) {
7369 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7370 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7371 01073a5d 2019-08-22 stsp if (error == NULL)
7372 01073a5d 2019-08-22 stsp error = repo_error;
7373 01073a5d 2019-08-22 stsp }
7374 0ebf8283 2019-07-24 stsp return error;
7375 0ebf8283 2019-07-24 stsp }