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 5c860e29 2018-03-12 stsp
38 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 c09a553d 2018-03-12 stsp #include "got_worktree.h"
46 79109fed 2018-03-27 stsp #include "got_diff.h"
47 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
48 404c43c4 2018-06-21 stsp #include "got_blame.h"
49 63219cd2 2019-01-04 stsp #include "got_privsep.h"
50 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
51 5c860e29 2018-03-12 stsp
52 5c860e29 2018-03-12 stsp #ifndef nitems
53 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 5c860e29 2018-03-12 stsp #endif
55 99437157 2018-11-11 stsp
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
57 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
58 99437157 2018-11-11 stsp
59 99437157 2018-11-11 stsp static void
60 99437157 2018-11-11 stsp catch_sigint(int signo)
61 99437157 2018-11-11 stsp {
62 99437157 2018-11-11 stsp sigint_received = 1;
63 99437157 2018-11-11 stsp }
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigpipe(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigpipe_received = 1;
69 99437157 2018-11-11 stsp }
70 5c860e29 2018-03-12 stsp
71 99437157 2018-11-11 stsp
72 8cfb4057 2019-07-09 stsp struct got_cmd {
73 5c860e29 2018-03-12 stsp const char *cmd_name;
74 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
76 97b3a7be 2019-07-09 stsp const char *cmd_alias;
77 5c860e29 2018-03-12 stsp };
78 5c860e29 2018-03-12 stsp
79 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
80 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
81 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
83 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
87 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
88 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
89 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
90 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
91 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
92 d00136be 2019-03-26 stsp __dead static void usage_add(void);
93 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
94 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
95 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
96 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
97 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
98 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
99 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
100 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
101 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
102 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
103 5c860e29 2018-03-12 stsp
104 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
105 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
106 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
107 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
108 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
110 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
111 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
112 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
113 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
114 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
115 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
116 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
117 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
118 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
119 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
120 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
121 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
122 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
123 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
124 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
125 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
126 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
127 5c860e29 2018-03-12 stsp
128 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
129 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
130 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
131 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
132 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
133 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
134 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
135 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
136 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
137 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
138 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
139 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
140 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
141 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
142 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
143 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
144 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
145 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
147 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
148 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
149 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
150 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
151 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
152 5c860e29 2018-03-12 stsp };
153 ce5b7c56 2019-07-09 stsp
154 ce5b7c56 2019-07-09 stsp static void
155 ce5b7c56 2019-07-09 stsp list_commands(void)
156 ce5b7c56 2019-07-09 stsp {
157 ce5b7c56 2019-07-09 stsp int i;
158 ce5b7c56 2019-07-09 stsp
159 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
160 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
161 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
162 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
163 ce5b7c56 2019-07-09 stsp }
164 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
165 ce5b7c56 2019-07-09 stsp }
166 5c860e29 2018-03-12 stsp
167 5c860e29 2018-03-12 stsp int
168 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
169 5c860e29 2018-03-12 stsp {
170 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
171 5c860e29 2018-03-12 stsp unsigned int i;
172 5c860e29 2018-03-12 stsp int ch;
173 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
174 5c860e29 2018-03-12 stsp
175 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
176 5c860e29 2018-03-12 stsp
177 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
178 5c860e29 2018-03-12 stsp switch (ch) {
179 1b6b95a8 2018-03-12 stsp case 'h':
180 1b6b95a8 2018-03-12 stsp hflag = 1;
181 53ccebc2 2019-07-30 stsp break;
182 53ccebc2 2019-07-30 stsp case 'V':
183 53ccebc2 2019-07-30 stsp Vflag = 1;
184 1b6b95a8 2018-03-12 stsp break;
185 5c860e29 2018-03-12 stsp default:
186 ce5b7c56 2019-07-09 stsp usage(hflag);
187 5c860e29 2018-03-12 stsp /* NOTREACHED */
188 5c860e29 2018-03-12 stsp }
189 5c860e29 2018-03-12 stsp }
190 5c860e29 2018-03-12 stsp
191 5c860e29 2018-03-12 stsp argc -= optind;
192 5c860e29 2018-03-12 stsp argv += optind;
193 1e70621d 2018-03-27 stsp optind = 0;
194 53ccebc2 2019-07-30 stsp
195 53ccebc2 2019-07-30 stsp if (Vflag) {
196 53ccebc2 2019-07-30 stsp got_version_print_str();
197 53ccebc2 2019-07-30 stsp return 1;
198 53ccebc2 2019-07-30 stsp }
199 5c860e29 2018-03-12 stsp
200 5c860e29 2018-03-12 stsp if (argc <= 0)
201 ce5b7c56 2019-07-09 stsp usage(hflag);
202 5c860e29 2018-03-12 stsp
203 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
204 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
205 99437157 2018-11-11 stsp
206 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
207 d7d4f210 2018-03-12 stsp const struct got_error *error;
208 d7d4f210 2018-03-12 stsp
209 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
210 5c860e29 2018-03-12 stsp
211 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
213 5c860e29 2018-03-12 stsp continue;
214 5c860e29 2018-03-12 stsp
215 1b6b95a8 2018-03-12 stsp if (hflag)
216 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
217 1b6b95a8 2018-03-12 stsp
218 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
219 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
220 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
221 d7d4f210 2018-03-12 stsp return 1;
222 d7d4f210 2018-03-12 stsp }
223 d7d4f210 2018-03-12 stsp
224 d7d4f210 2018-03-12 stsp return 0;
225 5c860e29 2018-03-12 stsp }
226 5c860e29 2018-03-12 stsp
227 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 ce5b7c56 2019-07-09 stsp list_commands();
229 5c860e29 2018-03-12 stsp return 1;
230 5c860e29 2018-03-12 stsp }
231 5c860e29 2018-03-12 stsp
232 4ed7e80c 2018-05-20 stsp __dead static void
233 ce5b7c56 2019-07-09 stsp usage(int hflag)
234 5c860e29 2018-03-12 stsp {
235 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
236 53ccebc2 2019-07-30 stsp getprogname());
237 ce5b7c56 2019-07-09 stsp if (hflag)
238 ce5b7c56 2019-07-09 stsp list_commands();
239 5c860e29 2018-03-12 stsp exit(1);
240 5c860e29 2018-03-12 stsp }
241 5c860e29 2018-03-12 stsp
242 0266afb7 2019-01-04 stsp static const struct got_error *
243 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
244 e2ba3d07 2019-05-13 stsp {
245 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
246 e2ba3d07 2019-05-13 stsp const char *editor;
247 8920fa04 2019-08-18 stsp
248 8920fa04 2019-08-18 stsp *abspath = NULL;
249 e2ba3d07 2019-05-13 stsp
250 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
251 e2ba3d07 2019-05-13 stsp if (editor == NULL)
252 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
253 e2ba3d07 2019-05-13 stsp
254 0ee7065d 2019-05-13 stsp if (editor) {
255 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
256 0ee7065d 2019-05-13 stsp if (err)
257 0ee7065d 2019-05-13 stsp return err;
258 0ee7065d 2019-05-13 stsp }
259 e2ba3d07 2019-05-13 stsp
260 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
261 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
262 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
263 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
264 0ee7065d 2019-05-13 stsp }
265 0ee7065d 2019-05-13 stsp
266 e2ba3d07 2019-05-13 stsp return NULL;
267 e2ba3d07 2019-05-13 stsp }
268 e2ba3d07 2019-05-13 stsp
269 e2ba3d07 2019-05-13 stsp static const struct got_error *
270 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
271 c530dc23 2019-07-23 stsp const char *worktree_path)
272 0266afb7 2019-01-04 stsp {
273 163ce85a 2019-05-13 stsp const struct got_error *err;
274 0266afb7 2019-01-04 stsp
275 37c06ea4 2019-07-15 stsp #ifdef PROFILE
276 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
277 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
278 37c06ea4 2019-07-15 stsp #endif
279 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
281 0266afb7 2019-01-04 stsp
282 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
284 0266afb7 2019-01-04 stsp
285 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
286 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
287 0266afb7 2019-01-04 stsp
288 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
289 163ce85a 2019-05-13 stsp if (err != NULL)
290 163ce85a 2019-05-13 stsp return err;
291 0266afb7 2019-01-04 stsp
292 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
293 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
294 0266afb7 2019-01-04 stsp
295 0266afb7 2019-01-04 stsp return NULL;
296 2c7829a4 2019-06-17 stsp }
297 2c7829a4 2019-06-17 stsp
298 2c7829a4 2019-06-17 stsp __dead static void
299 2c7829a4 2019-06-17 stsp usage_init(void)
300 2c7829a4 2019-06-17 stsp {
301 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
302 2c7829a4 2019-06-17 stsp exit(1);
303 2c7829a4 2019-06-17 stsp }
304 2c7829a4 2019-06-17 stsp
305 2c7829a4 2019-06-17 stsp static const struct got_error *
306 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
307 2c7829a4 2019-06-17 stsp {
308 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
309 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
310 2c7829a4 2019-06-17 stsp int ch;
311 2c7829a4 2019-06-17 stsp
312 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
313 2c7829a4 2019-06-17 stsp switch (ch) {
314 2c7829a4 2019-06-17 stsp default:
315 2c7829a4 2019-06-17 stsp usage_init();
316 2c7829a4 2019-06-17 stsp /* NOTREACHED */
317 2c7829a4 2019-06-17 stsp }
318 2c7829a4 2019-06-17 stsp }
319 2c7829a4 2019-06-17 stsp
320 2c7829a4 2019-06-17 stsp argc -= optind;
321 2c7829a4 2019-06-17 stsp argv += optind;
322 2c7829a4 2019-06-17 stsp
323 2c7829a4 2019-06-17 stsp #ifndef PROFILE
324 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
325 2c7829a4 2019-06-17 stsp err(1, "pledge");
326 2c7829a4 2019-06-17 stsp #endif
327 2c7829a4 2019-06-17 stsp if (argc != 1)
328 bc20e173 2019-06-17 stsp usage_init();
329 2c7829a4 2019-06-17 stsp
330 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
331 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
332 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
333 2c7829a4 2019-06-17 stsp
334 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
337 2c7829a4 2019-06-17 stsp if (error &&
338 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
339 2c7829a4 2019-06-17 stsp goto done;
340 2c7829a4 2019-06-17 stsp
341 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
342 2c7829a4 2019-06-17 stsp if (error)
343 2c7829a4 2019-06-17 stsp goto done;
344 2c7829a4 2019-06-17 stsp
345 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
346 2c7829a4 2019-06-17 stsp if (error != NULL)
347 3ce1b845 2019-07-15 stsp goto done;
348 3ce1b845 2019-07-15 stsp
349 3ce1b845 2019-07-15 stsp done:
350 3ce1b845 2019-07-15 stsp free(repo_path);
351 3ce1b845 2019-07-15 stsp return error;
352 3ce1b845 2019-07-15 stsp }
353 3ce1b845 2019-07-15 stsp
354 3ce1b845 2019-07-15 stsp __dead static void
355 3ce1b845 2019-07-15 stsp usage_import(void)
356 3ce1b845 2019-07-15 stsp {
357 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
359 3ce1b845 2019-07-15 stsp exit(1);
360 3ce1b845 2019-07-15 stsp }
361 3ce1b845 2019-07-15 stsp
362 3ce1b845 2019-07-15 stsp int
363 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
364 3ce1b845 2019-07-15 stsp {
365 3ce1b845 2019-07-15 stsp pid_t pid;
366 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
367 3ce1b845 2019-07-15 stsp int st = -1;
368 3ce1b845 2019-07-15 stsp
369 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
370 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
371 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
372 3ce1b845 2019-07-15 stsp
373 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
374 3ce1b845 2019-07-15 stsp case -1:
375 3ce1b845 2019-07-15 stsp goto doneediting;
376 3ce1b845 2019-07-15 stsp case 0:
377 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
378 3ce1b845 2019-07-15 stsp _exit(127);
379 3ce1b845 2019-07-15 stsp }
380 3ce1b845 2019-07-15 stsp
381 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
382 3ce1b845 2019-07-15 stsp if (errno != EINTR)
383 3ce1b845 2019-07-15 stsp break;
384 3ce1b845 2019-07-15 stsp
385 3ce1b845 2019-07-15 stsp doneediting:
386 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
387 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
388 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
391 3ce1b845 2019-07-15 stsp errno = EINTR;
392 3ce1b845 2019-07-15 stsp return -1;
393 3ce1b845 2019-07-15 stsp }
394 3ce1b845 2019-07-15 stsp
395 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
396 3ce1b845 2019-07-15 stsp }
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp static const struct got_error *
399 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 3ce1b845 2019-07-15 stsp const char *initial_content)
401 3ce1b845 2019-07-15 stsp {
402 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
403 3ce1b845 2019-07-15 stsp char buf[1024];
404 3ce1b845 2019-07-15 stsp struct stat st, st2;
405 3ce1b845 2019-07-15 stsp FILE *fp;
406 3ce1b845 2019-07-15 stsp int content_changed = 0;
407 3ce1b845 2019-07-15 stsp size_t len;
408 3ce1b845 2019-07-15 stsp
409 3ce1b845 2019-07-15 stsp *logmsg = NULL;
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
412 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
413 3ce1b845 2019-07-15 stsp
414 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
415 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
416 3ce1b845 2019-07-15 stsp
417 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
418 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
423 3ce1b845 2019-07-15 stsp
424 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
425 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
426 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
427 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
428 3ce1b845 2019-07-15 stsp len = 0;
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
431 3ce1b845 2019-07-15 stsp if (fp == NULL) {
432 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
433 3ce1b845 2019-07-15 stsp goto done;
434 3ce1b845 2019-07-15 stsp }
435 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
436 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
437 3ce1b845 2019-07-15 stsp content_changed = 1;
438 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
440 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
441 3ce1b845 2019-07-15 stsp }
442 3ce1b845 2019-07-15 stsp fclose(fp);
443 3ce1b845 2019-07-15 stsp
444 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
446 3ce1b845 2019-07-15 stsp len--;
447 3ce1b845 2019-07-15 stsp }
448 3ce1b845 2019-07-15 stsp
449 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
450 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
452 3ce1b845 2019-07-15 stsp done:
453 3ce1b845 2019-07-15 stsp if (err) {
454 3ce1b845 2019-07-15 stsp free(*logmsg);
455 3ce1b845 2019-07-15 stsp *logmsg = NULL;
456 3ce1b845 2019-07-15 stsp }
457 3ce1b845 2019-07-15 stsp return err;
458 3ce1b845 2019-07-15 stsp }
459 3ce1b845 2019-07-15 stsp
460 3ce1b845 2019-07-15 stsp static const struct got_error *
461 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 3ce1b845 2019-07-15 stsp const char *branch_name)
463 3ce1b845 2019-07-15 stsp {
464 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
465 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
466 3ce1b845 2019-07-15 stsp int fd;
467 3ce1b845 2019-07-15 stsp
468 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
469 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
470 3ce1b845 2019-07-15 stsp branch_name) == -1)
471 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
472 3ce1b845 2019-07-15 stsp
473 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
474 3ce1b845 2019-07-15 stsp if (err)
475 3ce1b845 2019-07-15 stsp goto done;
476 3ce1b845 2019-07-15 stsp
477 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
478 3ce1b845 2019-07-15 stsp close(fd);
479 3ce1b845 2019-07-15 stsp
480 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
481 3ce1b845 2019-07-15 stsp done:
482 3ce1b845 2019-07-15 stsp free(initial_content);
483 3ce1b845 2019-07-15 stsp free(logmsg_path);
484 3ce1b845 2019-07-15 stsp return err;
485 3ce1b845 2019-07-15 stsp }
486 3ce1b845 2019-07-15 stsp
487 3ce1b845 2019-07-15 stsp static const struct got_error *
488 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
489 3ce1b845 2019-07-15 stsp {
490 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
491 3ce1b845 2019-07-15 stsp return NULL;
492 3ce1b845 2019-07-15 stsp }
493 3ce1b845 2019-07-15 stsp
494 3ce1b845 2019-07-15 stsp static const struct got_error *
495 84792843 2019-08-09 stsp get_author(const char **author)
496 84792843 2019-08-09 stsp {
497 84792843 2019-08-09 stsp const char *got_author;
498 84792843 2019-08-09 stsp
499 84792843 2019-08-09 stsp *author = NULL;
500 84792843 2019-08-09 stsp
501 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
502 84792843 2019-08-09 stsp if (got_author == NULL) {
503 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
504 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
505 84792843 2019-08-09 stsp }
506 84792843 2019-08-09 stsp
507 84792843 2019-08-09 stsp *author = got_author;
508 84792843 2019-08-09 stsp
509 84792843 2019-08-09 stsp /*
510 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
511 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
512 84792843 2019-08-09 stsp */
513 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
514 84792843 2019-08-09 stsp got_author++;
515 84792843 2019-08-09 stsp if (*got_author != '<')
516 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
518 84792843 2019-08-09 stsp got_author++;
519 84792843 2019-08-09 stsp if (*got_author != '@')
520 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
521 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
522 84792843 2019-08-09 stsp got_author++;
523 84792843 2019-08-09 stsp if (*got_author != '>')
524 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
525 84792843 2019-08-09 stsp
526 84792843 2019-08-09 stsp return NULL;
527 84792843 2019-08-09 stsp }
528 84792843 2019-08-09 stsp
529 84792843 2019-08-09 stsp static const struct got_error *
530 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
531 3ce1b845 2019-07-15 stsp {
532 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
533 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
534 3ce1b845 2019-07-15 stsp char *editor = NULL;
535 84792843 2019-08-09 stsp const char *author;
536 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
537 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
538 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
539 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
540 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
541 3ce1b845 2019-07-15 stsp int ch;
542 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
543 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
544 3ce1b845 2019-07-15 stsp
545 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
546 3ce1b845 2019-07-15 stsp
547 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
548 3ce1b845 2019-07-15 stsp switch (ch) {
549 3ce1b845 2019-07-15 stsp case 'b':
550 3ce1b845 2019-07-15 stsp branch_name = optarg;
551 3ce1b845 2019-07-15 stsp break;
552 3ce1b845 2019-07-15 stsp case 'm':
553 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
554 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
555 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
556 3ce1b845 2019-07-15 stsp goto done;
557 3ce1b845 2019-07-15 stsp }
558 3ce1b845 2019-07-15 stsp break;
559 3ce1b845 2019-07-15 stsp case 'r':
560 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
561 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
562 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
563 3ce1b845 2019-07-15 stsp goto done;
564 3ce1b845 2019-07-15 stsp }
565 3ce1b845 2019-07-15 stsp break;
566 3ce1b845 2019-07-15 stsp case 'I':
567 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
568 3ce1b845 2019-07-15 stsp break;
569 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
570 3ce1b845 2019-07-15 stsp NULL);
571 3ce1b845 2019-07-15 stsp if (error)
572 3ce1b845 2019-07-15 stsp goto done;
573 3ce1b845 2019-07-15 stsp break;
574 3ce1b845 2019-07-15 stsp default:
575 b2b65d18 2019-08-22 stsp usage_import();
576 3ce1b845 2019-07-15 stsp /* NOTREACHED */
577 3ce1b845 2019-07-15 stsp }
578 3ce1b845 2019-07-15 stsp }
579 3ce1b845 2019-07-15 stsp
580 3ce1b845 2019-07-15 stsp argc -= optind;
581 3ce1b845 2019-07-15 stsp argv += optind;
582 3ce1b845 2019-07-15 stsp
583 3ce1b845 2019-07-15 stsp #ifndef PROFILE
584 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
585 3ce1b845 2019-07-15 stsp NULL) == -1)
586 3ce1b845 2019-07-15 stsp err(1, "pledge");
587 3ce1b845 2019-07-15 stsp #endif
588 3ce1b845 2019-07-15 stsp if (argc != 1)
589 3ce1b845 2019-07-15 stsp usage_import();
590 3ce1b845 2019-07-15 stsp
591 84792843 2019-08-09 stsp error = get_author(&author);
592 84792843 2019-08-09 stsp if (error)
593 84792843 2019-08-09 stsp return error;
594 2c7829a4 2019-06-17 stsp
595 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
596 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
597 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
598 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
599 3ce1b845 2019-07-15 stsp }
600 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
601 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
602 3ce1b845 2019-07-15 stsp if (error)
603 3ce1b845 2019-07-15 stsp goto done;
604 3ce1b845 2019-07-15 stsp
605 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
606 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
607 3ce1b845 2019-07-15 stsp goto done;
608 3ce1b845 2019-07-15 stsp }
609 3ce1b845 2019-07-15 stsp
610 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
611 3ce1b845 2019-07-15 stsp if (error) {
612 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
613 3ce1b845 2019-07-15 stsp goto done;
614 3ce1b845 2019-07-15 stsp } else {
615 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
616 3ce1b845 2019-07-15 stsp "import target branch already exists");
617 3ce1b845 2019-07-15 stsp goto done;
618 3ce1b845 2019-07-15 stsp }
619 3ce1b845 2019-07-15 stsp
620 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
621 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
622 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
623 3ce1b845 2019-07-15 stsp goto done;
624 3ce1b845 2019-07-15 stsp }
625 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
626 3ce1b845 2019-07-15 stsp
627 3ce1b845 2019-07-15 stsp /*
628 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
629 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
630 3ce1b845 2019-07-15 stsp */
631 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
632 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
633 3ce1b845 2019-07-15 stsp if (error)
634 3ce1b845 2019-07-15 stsp goto done;
635 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
636 3ce1b845 2019-07-15 stsp if (error)
637 3ce1b845 2019-07-15 stsp goto done;
638 3ce1b845 2019-07-15 stsp }
639 3ce1b845 2019-07-15 stsp
640 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
641 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
642 3ce1b845 2019-07-15 stsp
643 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
644 3ce1b845 2019-07-15 stsp if (error)
645 3ce1b845 2019-07-15 stsp goto done;
646 3ce1b845 2019-07-15 stsp
647 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
648 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
649 3ce1b845 2019-07-15 stsp if (error)
650 3ce1b845 2019-07-15 stsp goto done;
651 3ce1b845 2019-07-15 stsp
652 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
653 3ce1b845 2019-07-15 stsp if (error)
654 3ce1b845 2019-07-15 stsp goto done;
655 3ce1b845 2019-07-15 stsp
656 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
657 3ce1b845 2019-07-15 stsp if (error)
658 3ce1b845 2019-07-15 stsp goto done;
659 3ce1b845 2019-07-15 stsp
660 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
661 3ce1b845 2019-07-15 stsp if (error)
662 3ce1b845 2019-07-15 stsp goto done;
663 3ce1b845 2019-07-15 stsp
664 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
665 3ce1b845 2019-07-15 stsp if (error) {
666 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
667 3ce1b845 2019-07-15 stsp goto done;
668 3ce1b845 2019-07-15 stsp
669 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
670 3ce1b845 2019-07-15 stsp branch_ref);
671 3ce1b845 2019-07-15 stsp if (error)
672 3ce1b845 2019-07-15 stsp goto done;
673 3ce1b845 2019-07-15 stsp
674 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
675 3ce1b845 2019-07-15 stsp if (error)
676 3ce1b845 2019-07-15 stsp goto done;
677 3ce1b845 2019-07-15 stsp }
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
680 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
681 2c7829a4 2019-06-17 stsp done:
682 2c7829a4 2019-06-17 stsp free(repo_path);
683 3ce1b845 2019-07-15 stsp free(editor);
684 3ce1b845 2019-07-15 stsp free(refname);
685 3ce1b845 2019-07-15 stsp free(new_commit_id);
686 3ce1b845 2019-07-15 stsp free(id_str);
687 3ce1b845 2019-07-15 stsp if (branch_ref)
688 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
689 3ce1b845 2019-07-15 stsp if (head_ref)
690 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
691 2c7829a4 2019-06-17 stsp return error;
692 0266afb7 2019-01-04 stsp }
693 0266afb7 2019-01-04 stsp
694 4ed7e80c 2018-05-20 stsp __dead static void
695 c09a553d 2018-03-12 stsp usage_checkout(void)
696 c09a553d 2018-03-12 stsp {
697 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
698 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
699 c09a553d 2018-03-12 stsp exit(1);
700 92a684f4 2018-03-12 stsp }
701 92a684f4 2018-03-12 stsp
702 1ee397ad 2019-07-12 stsp static const struct got_error *
703 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
704 92a684f4 2018-03-12 stsp {
705 92a684f4 2018-03-12 stsp char *worktree_path = arg;
706 a484d721 2019-06-10 stsp
707 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
708 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
709 1ee397ad 2019-07-12 stsp return NULL;
710 92a684f4 2018-03-12 stsp
711 92a684f4 2018-03-12 stsp while (path[0] == '/')
712 92a684f4 2018-03-12 stsp path++;
713 92a684f4 2018-03-12 stsp
714 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
715 1ee397ad 2019-07-12 stsp return NULL;
716 99437157 2018-11-11 stsp }
717 99437157 2018-11-11 stsp
718 99437157 2018-11-11 stsp static const struct got_error *
719 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
720 99437157 2018-11-11 stsp {
721 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
722 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
723 99437157 2018-11-11 stsp return NULL;
724 8069f636 2019-01-12 stsp }
725 8069f636 2019-01-12 stsp
726 8069f636 2019-01-12 stsp static const struct got_error *
727 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
728 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
729 8069f636 2019-01-12 stsp {
730 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
731 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
732 8069f636 2019-01-12 stsp
733 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
734 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
735 36a38700 2019-05-10 stsp if (err)
736 36a38700 2019-05-10 stsp return err;
737 8069f636 2019-01-12 stsp
738 d5bea539 2019-05-13 stsp if (yca_id == NULL)
739 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
740 8069f636 2019-01-12 stsp
741 d5bea539 2019-05-13 stsp /*
742 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
743 d5bea539 2019-05-13 stsp * and the work tree's base commit.
744 d5bea539 2019-05-13 stsp *
745 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
746 d5bea539 2019-05-13 stsp *
747 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
748 d5bea539 2019-05-13 stsp * \ /
749 d5bea539 2019-05-13 stsp * C E
750 d5bea539 2019-05-13 stsp * \ /
751 d5bea539 2019-05-13 stsp * B (yca)
752 d5bea539 2019-05-13 stsp * |
753 d5bea539 2019-05-13 stsp * A
754 d5bea539 2019-05-13 stsp *
755 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
756 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
757 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
758 d5bea539 2019-05-13 stsp */
759 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
760 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
761 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
762 8069f636 2019-01-12 stsp
763 d5bea539 2019-05-13 stsp free(yca_id);
764 d5bea539 2019-05-13 stsp return NULL;
765 c09a553d 2018-03-12 stsp }
766 a367ff0f 2019-05-14 stsp
767 a367ff0f 2019-05-14 stsp static const struct got_error *
768 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
769 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
770 a51a74b3 2019-07-27 stsp struct got_repository *repo)
771 a367ff0f 2019-05-14 stsp {
772 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
773 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
774 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
775 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
776 a367ff0f 2019-05-14 stsp
777 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
778 a367ff0f 2019-05-14 stsp if (err)
779 a51a74b3 2019-07-27 stsp goto done;
780 a51a74b3 2019-07-27 stsp
781 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
782 a51a74b3 2019-07-27 stsp is_same_branch = 1;
783 a51a74b3 2019-07-27 stsp goto done;
784 a51a74b3 2019-07-27 stsp }
785 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
786 a51a74b3 2019-07-27 stsp is_same_branch = 1;
787 a367ff0f 2019-05-14 stsp goto done;
788 a51a74b3 2019-07-27 stsp }
789 a367ff0f 2019-05-14 stsp
790 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
791 a367ff0f 2019-05-14 stsp if (err)
792 a367ff0f 2019-05-14 stsp goto done;
793 a367ff0f 2019-05-14 stsp
794 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
795 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
796 a367ff0f 2019-05-14 stsp if (err)
797 a367ff0f 2019-05-14 stsp goto done;
798 a367ff0f 2019-05-14 stsp
799 a367ff0f 2019-05-14 stsp for (;;) {
800 a367ff0f 2019-05-14 stsp struct got_object_id *id;
801 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
802 a367ff0f 2019-05-14 stsp if (err) {
803 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
804 a367ff0f 2019-05-14 stsp err = NULL;
805 a367ff0f 2019-05-14 stsp break;
806 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
807 a367ff0f 2019-05-14 stsp break;
808 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
809 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
810 a367ff0f 2019-05-14 stsp if (err)
811 a367ff0f 2019-05-14 stsp break;
812 a367ff0f 2019-05-14 stsp }
813 c09a553d 2018-03-12 stsp
814 a367ff0f 2019-05-14 stsp if (id) {
815 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
816 a51a74b3 2019-07-27 stsp break;
817 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
818 a367ff0f 2019-05-14 stsp is_same_branch = 1;
819 a367ff0f 2019-05-14 stsp break;
820 a367ff0f 2019-05-14 stsp }
821 a367ff0f 2019-05-14 stsp }
822 a367ff0f 2019-05-14 stsp }
823 a367ff0f 2019-05-14 stsp done:
824 a367ff0f 2019-05-14 stsp if (graph)
825 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
826 a367ff0f 2019-05-14 stsp free(head_commit_id);
827 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
828 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
829 04f57cb3 2019-07-25 stsp return err;
830 04f57cb3 2019-07-25 stsp }
831 04f57cb3 2019-07-25 stsp
832 04f57cb3 2019-07-25 stsp static const struct got_error *
833 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
834 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
835 04f57cb3 2019-07-25 stsp {
836 04f57cb3 2019-07-25 stsp const struct got_error *err;
837 04f57cb3 2019-07-25 stsp struct got_reference *ref;
838 303e2782 2019-08-09 stsp struct got_tag_object *tag;
839 04f57cb3 2019-07-25 stsp
840 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
841 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
842 303e2782 2019-08-09 stsp if (err == NULL) {
843 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
844 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
845 303e2782 2019-08-09 stsp if (*commit_id == NULL)
846 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
847 303e2782 2019-08-09 stsp got_object_tag_close(tag);
848 303e2782 2019-08-09 stsp return err;
849 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
850 303e2782 2019-08-09 stsp return err;
851 303e2782 2019-08-09 stsp
852 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
853 04f57cb3 2019-07-25 stsp if (err == NULL) {
854 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
855 04f57cb3 2019-07-25 stsp got_ref_close(ref);
856 04f57cb3 2019-07-25 stsp } else {
857 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
858 04f57cb3 2019-07-25 stsp return err;
859 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
860 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
861 04f57cb3 2019-07-25 stsp }
862 a367ff0f 2019-05-14 stsp return err;
863 a367ff0f 2019-05-14 stsp }
864 8069f636 2019-01-12 stsp
865 4ed7e80c 2018-05-20 stsp static const struct got_error *
866 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
867 c09a553d 2018-03-12 stsp {
868 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
869 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
870 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
871 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
872 c09a553d 2018-03-12 stsp char *repo_path = NULL;
873 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
874 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
875 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
876 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
877 72151b04 2019-05-11 stsp int ch, same_path_prefix;
878 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
879 f2ea84fa 2019-07-27 stsp
880 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
881 c09a553d 2018-03-12 stsp
882 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
883 0bb8a95e 2018-03-12 stsp switch (ch) {
884 08573d5b 2019-05-14 stsp case 'b':
885 08573d5b 2019-05-14 stsp branch_name = optarg;
886 08573d5b 2019-05-14 stsp break;
887 8069f636 2019-01-12 stsp case 'c':
888 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
889 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
890 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
891 8069f636 2019-01-12 stsp break;
892 0bb8a95e 2018-03-12 stsp case 'p':
893 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
894 0bb8a95e 2018-03-12 stsp break;
895 0bb8a95e 2018-03-12 stsp default:
896 2deda0b9 2019-03-07 stsp usage_checkout();
897 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
898 0bb8a95e 2018-03-12 stsp }
899 0bb8a95e 2018-03-12 stsp }
900 0bb8a95e 2018-03-12 stsp
901 0bb8a95e 2018-03-12 stsp argc -= optind;
902 0bb8a95e 2018-03-12 stsp argv += optind;
903 0bb8a95e 2018-03-12 stsp
904 6715a751 2018-03-16 stsp #ifndef PROFILE
905 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
906 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
907 c09a553d 2018-03-12 stsp err(1, "pledge");
908 6715a751 2018-03-16 stsp #endif
909 0bb8a95e 2018-03-12 stsp if (argc == 1) {
910 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
911 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
912 76089277 2018-04-01 stsp if (repo_path == NULL)
913 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
914 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
915 76089277 2018-04-01 stsp if (cwd == NULL) {
916 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
917 76089277 2018-04-01 stsp goto done;
918 76089277 2018-04-01 stsp }
919 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
920 230a42bd 2019-05-11 jcs base = basename(path_prefix);
921 230a42bd 2019-05-11 jcs if (base == NULL) {
922 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
923 230a42bd 2019-05-11 jcs path_prefix);
924 230a42bd 2019-05-11 jcs goto done;
925 230a42bd 2019-05-11 jcs }
926 230a42bd 2019-05-11 jcs } else {
927 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
928 230a42bd 2019-05-11 jcs if (base == NULL) {
929 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
930 230a42bd 2019-05-11 jcs repo_path);
931 230a42bd 2019-05-11 jcs goto done;
932 230a42bd 2019-05-11 jcs }
933 76089277 2018-04-01 stsp }
934 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
935 c09a553d 2018-03-12 stsp if (dotgit)
936 c09a553d 2018-03-12 stsp *dotgit = '\0';
937 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
938 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
939 c09a553d 2018-03-12 stsp free(cwd);
940 76089277 2018-04-01 stsp goto done;
941 c09a553d 2018-03-12 stsp }
942 c09a553d 2018-03-12 stsp free(cwd);
943 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
944 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
945 76089277 2018-04-01 stsp if (repo_path == NULL) {
946 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
947 76089277 2018-04-01 stsp goto done;
948 76089277 2018-04-01 stsp }
949 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
950 76089277 2018-04-01 stsp if (worktree_path == NULL) {
951 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
952 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
953 b4b3a7dd 2019-07-22 stsp argv[1]);
954 b4b3a7dd 2019-07-22 stsp goto done;
955 b4b3a7dd 2019-07-22 stsp }
956 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
957 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
958 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
959 b4b3a7dd 2019-07-22 stsp goto done;
960 b4b3a7dd 2019-07-22 stsp }
961 76089277 2018-04-01 stsp }
962 c09a553d 2018-03-12 stsp } else
963 c09a553d 2018-03-12 stsp usage_checkout();
964 c09a553d 2018-03-12 stsp
965 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
966 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
967 13bfb272 2019-05-10 jcs
968 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
969 c09a553d 2018-03-12 stsp if (error != NULL)
970 c02c541e 2019-03-29 stsp goto done;
971 c02c541e 2019-03-29 stsp
972 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
973 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
974 c530dc23 2019-07-23 stsp if (error) {
975 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
976 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
977 c530dc23 2019-07-23 stsp goto done;
978 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
979 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
980 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
981 c530dc23 2019-07-23 stsp goto done;
982 c530dc23 2019-07-23 stsp }
983 c530dc23 2019-07-23 stsp }
984 c530dc23 2019-07-23 stsp
985 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
986 c02c541e 2019-03-29 stsp if (error)
987 c09a553d 2018-03-12 stsp goto done;
988 8069f636 2019-01-12 stsp
989 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
990 c09a553d 2018-03-12 stsp if (error != NULL)
991 c09a553d 2018-03-12 stsp goto done;
992 c09a553d 2018-03-12 stsp
993 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
994 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
995 c09a553d 2018-03-12 stsp goto done;
996 c09a553d 2018-03-12 stsp
997 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
998 c09a553d 2018-03-12 stsp if (error != NULL)
999 c09a553d 2018-03-12 stsp goto done;
1000 c09a553d 2018-03-12 stsp
1001 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1002 e5dc7198 2018-12-29 stsp path_prefix);
1003 e5dc7198 2018-12-29 stsp if (error != NULL)
1004 e5dc7198 2018-12-29 stsp goto done;
1005 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1006 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1007 49520a32 2018-12-29 stsp goto done;
1008 49520a32 2018-12-29 stsp }
1009 49520a32 2018-12-29 stsp
1010 8069f636 2019-01-12 stsp if (commit_id_str) {
1011 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1012 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1013 30837e32 2019-07-25 stsp if (error)
1014 8069f636 2019-01-12 stsp goto done;
1015 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1016 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1017 8069f636 2019-01-12 stsp if (error != NULL) {
1018 8069f636 2019-01-12 stsp free(commit_id);
1019 8069f636 2019-01-12 stsp goto done;
1020 8069f636 2019-01-12 stsp }
1021 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1022 45d344f6 2019-05-14 stsp if (error)
1023 45d344f6 2019-05-14 stsp goto done;
1024 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1025 8069f636 2019-01-12 stsp commit_id);
1026 8069f636 2019-01-12 stsp free(commit_id);
1027 8069f636 2019-01-12 stsp if (error)
1028 8069f636 2019-01-12 stsp goto done;
1029 8069f636 2019-01-12 stsp }
1030 8069f636 2019-01-12 stsp
1031 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1032 f2ea84fa 2019-07-27 stsp if (error)
1033 f2ea84fa 2019-07-27 stsp goto done;
1034 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1035 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1036 c09a553d 2018-03-12 stsp if (error != NULL)
1037 c09a553d 2018-03-12 stsp goto done;
1038 c09a553d 2018-03-12 stsp
1039 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1040 c09a553d 2018-03-12 stsp
1041 c09a553d 2018-03-12 stsp done:
1042 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1043 8069f636 2019-01-12 stsp free(commit_id_str);
1044 76089277 2018-04-01 stsp free(repo_path);
1045 507dc3bb 2018-12-29 stsp free(worktree_path);
1046 507dc3bb 2018-12-29 stsp return error;
1047 507dc3bb 2018-12-29 stsp }
1048 507dc3bb 2018-12-29 stsp
1049 507dc3bb 2018-12-29 stsp __dead static void
1050 507dc3bb 2018-12-29 stsp usage_update(void)
1051 507dc3bb 2018-12-29 stsp {
1052 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1053 507dc3bb 2018-12-29 stsp getprogname());
1054 507dc3bb 2018-12-29 stsp exit(1);
1055 507dc3bb 2018-12-29 stsp }
1056 507dc3bb 2018-12-29 stsp
1057 1ee397ad 2019-07-12 stsp static const struct got_error *
1058 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1059 507dc3bb 2018-12-29 stsp {
1060 784955db 2019-01-12 stsp int *did_something = arg;
1061 784955db 2019-01-12 stsp
1062 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1063 1ee397ad 2019-07-12 stsp return NULL;
1064 507dc3bb 2018-12-29 stsp
1065 1545c615 2019-02-10 stsp *did_something = 1;
1066 a484d721 2019-06-10 stsp
1067 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1068 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1069 1ee397ad 2019-07-12 stsp return NULL;
1070 a484d721 2019-06-10 stsp
1071 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1072 507dc3bb 2018-12-29 stsp path++;
1073 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1074 1ee397ad 2019-07-12 stsp return NULL;
1075 be7061eb 2018-12-30 stsp }
1076 be7061eb 2018-12-30 stsp
1077 be7061eb 2018-12-30 stsp static const struct got_error *
1078 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1079 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1080 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1081 a1fb16d8 2019-05-24 stsp {
1082 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1083 a1fb16d8 2019-05-24 stsp char *base_id_str;
1084 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1085 a1fb16d8 2019-05-24 stsp
1086 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1087 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1088 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1089 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1090 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1091 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1092 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1093 a1fb16d8 2019-05-24 stsp }
1094 a1fb16d8 2019-05-24 stsp
1095 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1096 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1097 a1fb16d8 2019-05-24 stsp if (err) {
1098 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1099 a1fb16d8 2019-05-24 stsp return err;
1100 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1101 a1fb16d8 2019-05-24 stsp }
1102 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1103 a1fb16d8 2019-05-24 stsp return NULL;
1104 a1fb16d8 2019-05-24 stsp
1105 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1106 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1107 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1108 a1fb16d8 2019-05-24 stsp if (err)
1109 a1fb16d8 2019-05-24 stsp return err;
1110 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1111 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1112 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1113 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1114 0ebf8283 2019-07-24 stsp return NULL;
1115 0ebf8283 2019-07-24 stsp }
1116 0ebf8283 2019-07-24 stsp
1117 0ebf8283 2019-07-24 stsp static const struct got_error *
1118 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1119 0ebf8283 2019-07-24 stsp {
1120 0ebf8283 2019-07-24 stsp const struct got_error *err;
1121 0ebf8283 2019-07-24 stsp int in_progress;
1122 0ebf8283 2019-07-24 stsp
1123 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1124 0ebf8283 2019-07-24 stsp if (err)
1125 0ebf8283 2019-07-24 stsp return err;
1126 0ebf8283 2019-07-24 stsp if (in_progress)
1127 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1128 0ebf8283 2019-07-24 stsp
1129 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1130 0ebf8283 2019-07-24 stsp if (err)
1131 0ebf8283 2019-07-24 stsp return err;
1132 0ebf8283 2019-07-24 stsp if (in_progress)
1133 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1134 0ebf8283 2019-07-24 stsp
1135 a1fb16d8 2019-05-24 stsp return NULL;
1136 a5edda0a 2019-07-27 stsp }
1137 a5edda0a 2019-07-27 stsp
1138 a5edda0a 2019-07-27 stsp static const struct got_error *
1139 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1140 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1141 a5edda0a 2019-07-27 stsp {
1142 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1143 a5edda0a 2019-07-27 stsp char *path;
1144 a5edda0a 2019-07-27 stsp int i;
1145 a5edda0a 2019-07-27 stsp
1146 a5edda0a 2019-07-27 stsp if (argc == 0) {
1147 a5edda0a 2019-07-27 stsp path = strdup("");
1148 a5edda0a 2019-07-27 stsp if (path == NULL)
1149 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1150 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1151 a5edda0a 2019-07-27 stsp }
1152 a5edda0a 2019-07-27 stsp
1153 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1154 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1155 a5edda0a 2019-07-27 stsp if (err)
1156 a5edda0a 2019-07-27 stsp break;
1157 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1158 a5edda0a 2019-07-27 stsp if (err) {
1159 a5edda0a 2019-07-27 stsp free(path);
1160 a5edda0a 2019-07-27 stsp break;
1161 a5edda0a 2019-07-27 stsp }
1162 a5edda0a 2019-07-27 stsp }
1163 a5edda0a 2019-07-27 stsp
1164 a5edda0a 2019-07-27 stsp return err;
1165 a1fb16d8 2019-05-24 stsp }
1166 a1fb16d8 2019-05-24 stsp
1167 a1fb16d8 2019-05-24 stsp static const struct got_error *
1168 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1169 507dc3bb 2018-12-29 stsp {
1170 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1171 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1172 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1173 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1174 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1175 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1176 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1177 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1178 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1179 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1180 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1181 507dc3bb 2018-12-29 stsp
1182 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1183 f2ea84fa 2019-07-27 stsp
1184 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1185 507dc3bb 2018-12-29 stsp switch (ch) {
1186 024e9686 2019-05-14 stsp case 'b':
1187 024e9686 2019-05-14 stsp branch_name = optarg;
1188 024e9686 2019-05-14 stsp break;
1189 507dc3bb 2018-12-29 stsp case 'c':
1190 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1191 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1192 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1193 507dc3bb 2018-12-29 stsp break;
1194 507dc3bb 2018-12-29 stsp default:
1195 2deda0b9 2019-03-07 stsp usage_update();
1196 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1197 507dc3bb 2018-12-29 stsp }
1198 507dc3bb 2018-12-29 stsp }
1199 507dc3bb 2018-12-29 stsp
1200 507dc3bb 2018-12-29 stsp argc -= optind;
1201 507dc3bb 2018-12-29 stsp argv += optind;
1202 507dc3bb 2018-12-29 stsp
1203 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1204 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1206 507dc3bb 2018-12-29 stsp err(1, "pledge");
1207 507dc3bb 2018-12-29 stsp #endif
1208 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1209 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1210 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1211 c4cdcb68 2019-04-03 stsp goto done;
1212 c4cdcb68 2019-04-03 stsp }
1213 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1214 7d5807f4 2019-07-11 stsp if (error)
1215 7d5807f4 2019-07-11 stsp goto done;
1216 7d5807f4 2019-07-11 stsp
1217 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1218 f2ea84fa 2019-07-27 stsp if (error)
1219 f2ea84fa 2019-07-27 stsp goto done;
1220 507dc3bb 2018-12-29 stsp
1221 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1222 507dc3bb 2018-12-29 stsp if (error != NULL)
1223 507dc3bb 2018-12-29 stsp goto done;
1224 507dc3bb 2018-12-29 stsp
1225 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1226 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1227 087fb88c 2019-08-04 stsp if (error)
1228 087fb88c 2019-08-04 stsp goto done;
1229 087fb88c 2019-08-04 stsp
1230 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1231 0266afb7 2019-01-04 stsp if (error)
1232 0266afb7 2019-01-04 stsp goto done;
1233 0266afb7 2019-01-04 stsp
1234 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1235 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1236 024e9686 2019-05-14 stsp if (error != NULL)
1237 024e9686 2019-05-14 stsp goto done;
1238 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1239 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1240 9c4b8182 2019-01-02 stsp if (error != NULL)
1241 9c4b8182 2019-01-02 stsp goto done;
1242 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1243 507dc3bb 2018-12-29 stsp if (error != NULL)
1244 507dc3bb 2018-12-29 stsp goto done;
1245 507dc3bb 2018-12-29 stsp } else {
1246 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1247 04f57cb3 2019-07-25 stsp free(commit_id_str);
1248 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1249 30837e32 2019-07-25 stsp if (error)
1250 a297e751 2019-07-11 stsp goto done;
1251 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1252 a297e751 2019-07-11 stsp if (error)
1253 507dc3bb 2018-12-29 stsp goto done;
1254 507dc3bb 2018-12-29 stsp }
1255 35c965b2 2018-12-29 stsp
1256 a1fb16d8 2019-05-24 stsp if (branch_name) {
1257 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1258 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1259 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1260 f2ea84fa 2019-07-27 stsp continue;
1261 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1262 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1263 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1264 024e9686 2019-05-14 stsp goto done;
1265 024e9686 2019-05-14 stsp }
1266 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1267 024e9686 2019-05-14 stsp if (error)
1268 024e9686 2019-05-14 stsp goto done;
1269 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1270 a367ff0f 2019-05-14 stsp free(head_commit_id);
1271 024e9686 2019-05-14 stsp if (error != NULL)
1272 024e9686 2019-05-14 stsp goto done;
1273 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1274 a367ff0f 2019-05-14 stsp if (error)
1275 a367ff0f 2019-05-14 stsp goto done;
1276 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1277 024e9686 2019-05-14 stsp if (error)
1278 024e9686 2019-05-14 stsp goto done;
1279 024e9686 2019-05-14 stsp } else {
1280 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1281 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1282 a367ff0f 2019-05-14 stsp if (error != NULL) {
1283 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1284 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1285 024e9686 2019-05-14 stsp goto done;
1286 a367ff0f 2019-05-14 stsp }
1287 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1288 a367ff0f 2019-05-14 stsp if (error)
1289 a367ff0f 2019-05-14 stsp goto done;
1290 024e9686 2019-05-14 stsp }
1291 507dc3bb 2018-12-29 stsp
1292 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1293 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1294 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1295 507dc3bb 2018-12-29 stsp commit_id);
1296 507dc3bb 2018-12-29 stsp if (error)
1297 507dc3bb 2018-12-29 stsp goto done;
1298 507dc3bb 2018-12-29 stsp }
1299 507dc3bb 2018-12-29 stsp
1300 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1301 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1302 507dc3bb 2018-12-29 stsp if (error != NULL)
1303 507dc3bb 2018-12-29 stsp goto done;
1304 9c4b8182 2019-01-02 stsp
1305 784955db 2019-01-12 stsp if (did_something)
1306 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1307 784955db 2019-01-12 stsp else
1308 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1309 507dc3bb 2018-12-29 stsp done:
1310 c09a553d 2018-03-12 stsp free(worktree_path);
1311 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1312 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1313 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1314 507dc3bb 2018-12-29 stsp free(commit_id);
1315 9c4b8182 2019-01-02 stsp free(commit_id_str);
1316 c09a553d 2018-03-12 stsp return error;
1317 c09a553d 2018-03-12 stsp }
1318 c09a553d 2018-03-12 stsp
1319 f42b1b34 2018-03-12 stsp static const struct got_error *
1320 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1321 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1322 5c860e29 2018-03-12 stsp {
1323 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1324 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1325 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1326 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1327 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1328 79109fed 2018-03-27 stsp
1329 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1330 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1331 79109fed 2018-03-27 stsp if (err)
1332 79109fed 2018-03-27 stsp return err;
1333 79109fed 2018-03-27 stsp
1334 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1335 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1336 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1337 79109fed 2018-03-27 stsp
1338 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1339 79109fed 2018-03-27 stsp if (err)
1340 79109fed 2018-03-27 stsp return err;
1341 79109fed 2018-03-27 stsp
1342 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1343 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1344 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1345 0f2b3dca 2018-12-22 stsp if (err)
1346 0f2b3dca 2018-12-22 stsp return err;
1347 0f2b3dca 2018-12-22 stsp
1348 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1349 79109fed 2018-03-27 stsp if (err)
1350 79109fed 2018-03-27 stsp return err;
1351 79109fed 2018-03-27 stsp }
1352 79109fed 2018-03-27 stsp
1353 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1354 0f2b3dca 2018-12-22 stsp if (err)
1355 0f2b3dca 2018-12-22 stsp goto done;
1356 0f2b3dca 2018-12-22 stsp
1357 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1358 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1359 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1360 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1361 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1362 0f2b3dca 2018-12-22 stsp done:
1363 79109fed 2018-03-27 stsp if (tree1)
1364 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1365 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1366 0f2b3dca 2018-12-22 stsp free(id_str1);
1367 0f2b3dca 2018-12-22 stsp free(id_str2);
1368 79109fed 2018-03-27 stsp return err;
1369 79109fed 2018-03-27 stsp }
1370 79109fed 2018-03-27 stsp
1371 4bb494d5 2018-06-16 stsp static char *
1372 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1373 6c281f94 2018-06-11 stsp {
1374 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1375 09867e48 2019-08-13 stsp char *p, *s;
1376 09867e48 2019-08-13 stsp
1377 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1378 09867e48 2019-08-13 stsp if (tm == NULL)
1379 09867e48 2019-08-13 stsp return NULL;
1380 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1381 09867e48 2019-08-13 stsp if (s == NULL)
1382 09867e48 2019-08-13 stsp return NULL;
1383 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1384 6c281f94 2018-06-11 stsp if (p)
1385 6c281f94 2018-06-11 stsp *p = '\0';
1386 6c281f94 2018-06-11 stsp return s;
1387 6c281f94 2018-06-11 stsp }
1388 dc424a06 2019-08-07 stsp
1389 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1390 6c281f94 2018-06-11 stsp
1391 79109fed 2018-03-27 stsp static const struct got_error *
1392 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1393 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1394 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1395 79109fed 2018-03-27 stsp {
1396 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1397 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1398 6c281f94 2018-06-11 stsp char datebuf[26];
1399 45d799e2 2018-12-23 stsp time_t committer_time;
1400 45d799e2 2018-12-23 stsp const char *author, *committer;
1401 199a4027 2019-02-02 stsp char *refs_str = NULL;
1402 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1403 5c860e29 2018-03-12 stsp
1404 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1405 199a4027 2019-02-02 stsp char *s;
1406 199a4027 2019-02-02 stsp const char *name;
1407 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1408 a436ad14 2019-08-13 stsp int cmp;
1409 a436ad14 2019-08-13 stsp
1410 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1411 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1412 d9498b20 2019-02-05 stsp continue;
1413 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1414 199a4027 2019-02-02 stsp name += 5;
1415 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1416 7143d404 2019-03-12 stsp continue;
1417 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1418 e34f9ed6 2019-02-02 stsp name += 6;
1419 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1420 141c2bff 2019-02-04 stsp name += 8;
1421 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1422 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1423 5d844a1e 2019-08-13 stsp if (err) {
1424 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1425 5d844a1e 2019-08-13 stsp return err;
1426 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1427 5d844a1e 2019-08-13 stsp err = NULL;
1428 5d844a1e 2019-08-13 stsp tag = NULL;
1429 5d844a1e 2019-08-13 stsp }
1430 a436ad14 2019-08-13 stsp }
1431 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1432 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1433 a436ad14 2019-08-13 stsp if (tag)
1434 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1435 a436ad14 2019-08-13 stsp if (cmp != 0)
1436 a436ad14 2019-08-13 stsp continue;
1437 199a4027 2019-02-02 stsp s = refs_str;
1438 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1439 199a4027 2019-02-02 stsp name) == -1) {
1440 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1441 199a4027 2019-02-02 stsp free(s);
1442 34ca4898 2019-08-13 stsp return err;
1443 199a4027 2019-02-02 stsp }
1444 199a4027 2019-02-02 stsp free(s);
1445 199a4027 2019-02-02 stsp }
1446 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1447 8bf5b3c9 2018-03-17 stsp if (err)
1448 8bf5b3c9 2018-03-17 stsp return err;
1449 788c352e 2018-06-16 stsp
1450 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1451 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1452 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1453 832c249c 2018-06-10 stsp free(id_str);
1454 d3d493d7 2019-02-21 stsp id_str = NULL;
1455 d3d493d7 2019-02-21 stsp free(refs_str);
1456 d3d493d7 2019-02-21 stsp refs_str = NULL;
1457 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1458 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1459 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1460 09867e48 2019-08-13 stsp if (datestr)
1461 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1462 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1463 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1464 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1465 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1466 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1467 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1468 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1469 3fe1abad 2018-06-10 stsp int n = 1;
1470 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1471 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1472 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1473 a0603db2 2018-06-10 stsp if (err)
1474 a0603db2 2018-06-10 stsp return err;
1475 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1476 a0603db2 2018-06-10 stsp free(id_str);
1477 a0603db2 2018-06-10 stsp }
1478 a0603db2 2018-06-10 stsp }
1479 832c249c 2018-06-10 stsp
1480 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1481 5943eee2 2019-08-13 stsp if (err)
1482 5943eee2 2019-08-13 stsp return err;
1483 8bf5b3c9 2018-03-17 stsp
1484 621015ac 2018-07-23 stsp logmsg = logmsg0;
1485 832c249c 2018-06-10 stsp do {
1486 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1487 832c249c 2018-06-10 stsp if (line)
1488 832c249c 2018-06-10 stsp printf(" %s\n", line);
1489 832c249c 2018-06-10 stsp } while (line);
1490 621015ac 2018-07-23 stsp free(logmsg0);
1491 832c249c 2018-06-10 stsp
1492 971751ac 2018-03-27 stsp if (show_patch) {
1493 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1494 971751ac 2018-03-27 stsp if (err == 0)
1495 971751ac 2018-03-27 stsp printf("\n");
1496 971751ac 2018-03-27 stsp }
1497 07862c20 2018-09-15 stsp
1498 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1499 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1500 07862c20 2018-09-15 stsp return err;
1501 f42b1b34 2018-03-12 stsp }
1502 5c860e29 2018-03-12 stsp
1503 f42b1b34 2018-03-12 stsp static const struct got_error *
1504 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1505 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1506 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1507 f42b1b34 2018-03-12 stsp {
1508 f42b1b34 2018-03-12 stsp const struct got_error *err;
1509 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1510 372ccdbb 2018-06-10 stsp
1511 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1512 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1513 f42b1b34 2018-03-12 stsp if (err)
1514 f42b1b34 2018-03-12 stsp return err;
1515 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1516 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1517 372ccdbb 2018-06-10 stsp if (err)
1518 fcc85cad 2018-07-22 stsp goto done;
1519 656b1f76 2019-05-11 jcs for (;;) {
1520 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1521 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1522 8bf5b3c9 2018-03-17 stsp
1523 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1524 84453469 2018-11-11 stsp break;
1525 84453469 2018-11-11 stsp
1526 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1527 372ccdbb 2018-06-10 stsp if (err) {
1528 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1529 9ba79e04 2018-06-11 stsp err = NULL;
1530 9ba79e04 2018-06-11 stsp break;
1531 9ba79e04 2018-06-11 stsp }
1532 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1533 372ccdbb 2018-06-10 stsp break;
1534 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1535 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1536 372ccdbb 2018-06-10 stsp if (err)
1537 372ccdbb 2018-06-10 stsp break;
1538 372ccdbb 2018-06-10 stsp else
1539 372ccdbb 2018-06-10 stsp continue;
1540 7e665116 2018-04-02 stsp }
1541 b43fbaa0 2018-06-11 stsp if (id == NULL)
1542 7e665116 2018-04-02 stsp break;
1543 b43fbaa0 2018-06-11 stsp
1544 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1545 b43fbaa0 2018-06-11 stsp if (err)
1546 fcc85cad 2018-07-22 stsp break;
1547 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1548 199a4027 2019-02-02 stsp refs);
1549 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1550 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1551 7e665116 2018-04-02 stsp break;
1552 31cedeaf 2018-09-15 stsp }
1553 fcc85cad 2018-07-22 stsp done:
1554 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1555 f42b1b34 2018-03-12 stsp return err;
1556 f42b1b34 2018-03-12 stsp }
1557 5c860e29 2018-03-12 stsp
1558 4ed7e80c 2018-05-20 stsp __dead static void
1559 6f3d1eb0 2018-03-12 stsp usage_log(void)
1560 6f3d1eb0 2018-03-12 stsp {
1561 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1562 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1563 6f3d1eb0 2018-03-12 stsp exit(1);
1564 b1ebc001 2019-08-13 stsp }
1565 b1ebc001 2019-08-13 stsp
1566 b1ebc001 2019-08-13 stsp static int
1567 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1568 b1ebc001 2019-08-13 stsp {
1569 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1570 b1ebc001 2019-08-13 stsp long long n;
1571 b1ebc001 2019-08-13 stsp const char *errstr;
1572 b1ebc001 2019-08-13 stsp
1573 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1574 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1575 b1ebc001 2019-08-13 stsp return 0;
1576 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1577 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1578 b1ebc001 2019-08-13 stsp return 0;
1579 b1ebc001 2019-08-13 stsp return n;
1580 6f3d1eb0 2018-03-12 stsp }
1581 6f3d1eb0 2018-03-12 stsp
1582 4ed7e80c 2018-05-20 stsp static const struct got_error *
1583 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1584 f42b1b34 2018-03-12 stsp {
1585 f42b1b34 2018-03-12 stsp const struct got_error *error;
1586 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1587 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1588 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1589 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1590 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1591 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1592 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1593 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1594 64a96a6d 2018-04-01 stsp const char *errstr;
1595 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1596 1b3893a2 2019-03-18 stsp
1597 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1598 5c860e29 2018-03-12 stsp
1599 6715a751 2018-03-16 stsp #ifndef PROFILE
1600 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1601 6098196c 2019-01-04 stsp NULL)
1602 ad242220 2018-09-08 stsp == -1)
1603 f42b1b34 2018-03-12 stsp err(1, "pledge");
1604 6715a751 2018-03-16 stsp #endif
1605 79109fed 2018-03-27 stsp
1606 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1607 b1ebc001 2019-08-13 stsp
1608 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1609 79109fed 2018-03-27 stsp switch (ch) {
1610 79109fed 2018-03-27 stsp case 'p':
1611 79109fed 2018-03-27 stsp show_patch = 1;
1612 d142fc45 2018-04-01 stsp break;
1613 d142fc45 2018-04-01 stsp case 'c':
1614 d142fc45 2018-04-01 stsp start_commit = optarg;
1615 64a96a6d 2018-04-01 stsp break;
1616 c0cc5c62 2018-10-18 stsp case 'C':
1617 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1618 4a8520aa 2018-10-18 stsp &errstr);
1619 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1620 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1621 c0cc5c62 2018-10-18 stsp break;
1622 64a96a6d 2018-04-01 stsp case 'l':
1623 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1624 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1625 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1626 cc54c501 2019-07-15 stsp break;
1627 cc54c501 2019-07-15 stsp case 'f':
1628 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1629 a0603db2 2018-06-10 stsp break;
1630 04ca23f4 2018-07-16 stsp case 'r':
1631 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1632 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1633 04ca23f4 2018-07-16 stsp err(1, "-r option");
1634 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1635 04ca23f4 2018-07-16 stsp break;
1636 79109fed 2018-03-27 stsp default:
1637 2deda0b9 2019-03-07 stsp usage_log();
1638 79109fed 2018-03-27 stsp /* NOTREACHED */
1639 79109fed 2018-03-27 stsp }
1640 79109fed 2018-03-27 stsp }
1641 79109fed 2018-03-27 stsp
1642 79109fed 2018-03-27 stsp argc -= optind;
1643 79109fed 2018-03-27 stsp argv += optind;
1644 f42b1b34 2018-03-12 stsp
1645 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1646 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1647 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1648 04ca23f4 2018-07-16 stsp goto done;
1649 04ca23f4 2018-07-16 stsp }
1650 cffc0aa4 2019-02-05 stsp
1651 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1652 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1653 cffc0aa4 2019-02-05 stsp goto done;
1654 cffc0aa4 2019-02-05 stsp error = NULL;
1655 cffc0aa4 2019-02-05 stsp
1656 e7301579 2019-03-18 stsp if (argc == 0) {
1657 cbd1af7a 2019-03-18 stsp path = strdup("");
1658 e7301579 2019-03-18 stsp if (path == NULL) {
1659 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1660 cbd1af7a 2019-03-18 stsp goto done;
1661 e7301579 2019-03-18 stsp }
1662 e7301579 2019-03-18 stsp } else if (argc == 1) {
1663 e7301579 2019-03-18 stsp if (worktree) {
1664 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1665 e7301579 2019-03-18 stsp argv[0]);
1666 e7301579 2019-03-18 stsp if (error)
1667 e7301579 2019-03-18 stsp goto done;
1668 e7301579 2019-03-18 stsp } else {
1669 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1670 e7301579 2019-03-18 stsp if (path == NULL) {
1671 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1672 e7301579 2019-03-18 stsp goto done;
1673 e7301579 2019-03-18 stsp }
1674 e7301579 2019-03-18 stsp }
1675 cbd1af7a 2019-03-18 stsp } else
1676 cbd1af7a 2019-03-18 stsp usage_log();
1677 cbd1af7a 2019-03-18 stsp
1678 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1679 5486daa2 2019-05-11 stsp repo_path = worktree ?
1680 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1681 5486daa2 2019-05-11 stsp }
1682 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1683 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1684 cffc0aa4 2019-02-05 stsp goto done;
1685 04ca23f4 2018-07-16 stsp }
1686 6098196c 2019-01-04 stsp
1687 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1688 d7d4f210 2018-03-12 stsp if (error != NULL)
1689 04ca23f4 2018-07-16 stsp goto done;
1690 f42b1b34 2018-03-12 stsp
1691 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1692 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1693 c02c541e 2019-03-29 stsp if (error)
1694 c02c541e 2019-03-29 stsp goto done;
1695 c02c541e 2019-03-29 stsp
1696 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1697 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1698 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1699 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1700 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1701 3235492e 2018-04-01 stsp if (error != NULL)
1702 3235492e 2018-04-01 stsp return error;
1703 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1704 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1705 3235492e 2018-04-01 stsp if (error != NULL)
1706 3235492e 2018-04-01 stsp return error;
1707 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1708 3235492e 2018-04-01 stsp } else {
1709 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1710 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1711 3235492e 2018-04-01 stsp if (error == NULL) {
1712 1caad61b 2019-02-01 stsp int obj_type;
1713 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1714 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1715 d1f2edc9 2018-06-13 stsp if (error != NULL)
1716 1caad61b 2019-02-01 stsp goto done;
1717 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1718 1caad61b 2019-02-01 stsp if (error != NULL)
1719 1caad61b 2019-02-01 stsp goto done;
1720 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1721 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1722 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1723 1caad61b 2019-02-01 stsp if (error != NULL)
1724 1caad61b 2019-02-01 stsp goto done;
1725 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1726 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1727 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1728 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1729 1caad61b 2019-02-01 stsp goto done;
1730 1caad61b 2019-02-01 stsp }
1731 1caad61b 2019-02-01 stsp free(id);
1732 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1733 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1734 1caad61b 2019-02-01 stsp if (id == NULL)
1735 638f9024 2019-05-13 stsp error = got_error_from_errno(
1736 230a42bd 2019-05-11 jcs "got_object_id_dup");
1737 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1738 1caad61b 2019-02-01 stsp if (error)
1739 1caad61b 2019-02-01 stsp goto done;
1740 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1741 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1742 1caad61b 2019-02-01 stsp goto done;
1743 1caad61b 2019-02-01 stsp }
1744 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1745 d1f2edc9 2018-06-13 stsp if (error != NULL)
1746 1caad61b 2019-02-01 stsp goto done;
1747 d1f2edc9 2018-06-13 stsp }
1748 15a94983 2018-12-23 stsp if (commit == NULL) {
1749 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1750 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1751 d1f2edc9 2018-06-13 stsp if (error != NULL)
1752 d1f2edc9 2018-06-13 stsp return error;
1753 3235492e 2018-04-01 stsp }
1754 3235492e 2018-04-01 stsp }
1755 d7d4f210 2018-03-12 stsp if (error != NULL)
1756 04ca23f4 2018-07-16 stsp goto done;
1757 04ca23f4 2018-07-16 stsp
1758 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1759 04ca23f4 2018-07-16 stsp if (error != NULL)
1760 04ca23f4 2018-07-16 stsp goto done;
1761 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1762 04ca23f4 2018-07-16 stsp free(path);
1763 04ca23f4 2018-07-16 stsp path = in_repo_path;
1764 04ca23f4 2018-07-16 stsp }
1765 199a4027 2019-02-02 stsp
1766 29606af7 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL);
1767 199a4027 2019-02-02 stsp if (error)
1768 199a4027 2019-02-02 stsp goto done;
1769 04ca23f4 2018-07-16 stsp
1770 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1771 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1772 04ca23f4 2018-07-16 stsp done:
1773 04ca23f4 2018-07-16 stsp free(path);
1774 04ca23f4 2018-07-16 stsp free(repo_path);
1775 04ca23f4 2018-07-16 stsp free(cwd);
1776 f42b1b34 2018-03-12 stsp free(id);
1777 cffc0aa4 2019-02-05 stsp if (worktree)
1778 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1779 ad242220 2018-09-08 stsp if (repo) {
1780 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1781 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1782 ad242220 2018-09-08 stsp if (error == NULL)
1783 ad242220 2018-09-08 stsp error = repo_error;
1784 ad242220 2018-09-08 stsp }
1785 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1786 8bf5b3c9 2018-03-17 stsp return error;
1787 5c860e29 2018-03-12 stsp }
1788 5c860e29 2018-03-12 stsp
1789 4ed7e80c 2018-05-20 stsp __dead static void
1790 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1791 3f8b7d6a 2018-04-01 stsp {
1792 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1793 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1794 3f8b7d6a 2018-04-01 stsp exit(1);
1795 b00d56cd 2018-04-01 stsp }
1796 b00d56cd 2018-04-01 stsp
1797 b72f483a 2019-02-05 stsp struct print_diff_arg {
1798 b72f483a 2019-02-05 stsp struct got_repository *repo;
1799 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1800 b72f483a 2019-02-05 stsp int diff_context;
1801 3fc0c068 2019-02-10 stsp const char *id_str;
1802 3fc0c068 2019-02-10 stsp int header_shown;
1803 98eaaa12 2019-08-03 stsp int diff_staged;
1804 b72f483a 2019-02-05 stsp };
1805 b72f483a 2019-02-05 stsp
1806 4ed7e80c 2018-05-20 stsp static const struct got_error *
1807 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1808 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1809 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1810 b72f483a 2019-02-05 stsp {
1811 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1812 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1813 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1814 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1815 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1816 b72f483a 2019-02-05 stsp struct stat sb;
1817 b72f483a 2019-02-05 stsp
1818 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1819 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1820 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1821 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1822 98eaaa12 2019-08-03 stsp return NULL;
1823 98eaaa12 2019-08-03 stsp } else {
1824 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1825 98eaaa12 2019-08-03 stsp return NULL;
1826 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1827 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1828 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1829 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1830 98eaaa12 2019-08-03 stsp return NULL;
1831 98eaaa12 2019-08-03 stsp }
1832 3fc0c068 2019-02-10 stsp
1833 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1834 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1835 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1836 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1837 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1838 3fc0c068 2019-02-10 stsp }
1839 b72f483a 2019-02-05 stsp
1840 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1841 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1842 98eaaa12 2019-08-03 stsp switch (staged_status) {
1843 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1844 98eaaa12 2019-08-03 stsp label1 = path;
1845 98eaaa12 2019-08-03 stsp label2 = path;
1846 98eaaa12 2019-08-03 stsp break;
1847 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1848 98eaaa12 2019-08-03 stsp label2 = path;
1849 98eaaa12 2019-08-03 stsp break;
1850 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1851 98eaaa12 2019-08-03 stsp label1 = path;
1852 98eaaa12 2019-08-03 stsp break;
1853 98eaaa12 2019-08-03 stsp default:
1854 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1855 98eaaa12 2019-08-03 stsp }
1856 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1857 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1858 98eaaa12 2019-08-03 stsp }
1859 98eaaa12 2019-08-03 stsp
1860 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1861 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
1862 4ce46740 2019-08-08 stsp char *id_str;
1863 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1864 408b4ebc 2019-08-03 stsp 8192);
1865 4ce46740 2019-08-08 stsp if (err)
1866 4ce46740 2019-08-08 stsp goto done;
1867 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
1868 4ce46740 2019-08-08 stsp if (err)
1869 4ce46740 2019-08-08 stsp goto done;
1870 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1871 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
1872 4ce46740 2019-08-08 stsp free(id_str);
1873 4ce46740 2019-08-08 stsp goto done;
1874 4ce46740 2019-08-08 stsp }
1875 4ce46740 2019-08-08 stsp free(id_str);
1876 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
1877 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1878 4ce46740 2019-08-08 stsp if (err)
1879 4ce46740 2019-08-08 stsp goto done;
1880 4ce46740 2019-08-08 stsp }
1881 d00136be 2019-03-26 stsp
1882 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1883 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1884 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1885 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1886 2ec1f75b 2019-03-26 stsp goto done;
1887 2ec1f75b 2019-03-26 stsp }
1888 b72f483a 2019-02-05 stsp
1889 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1890 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1891 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1892 2ec1f75b 2019-03-26 stsp goto done;
1893 2ec1f75b 2019-03-26 stsp }
1894 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1895 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1896 2ec1f75b 2019-03-26 stsp goto done;
1897 2ec1f75b 2019-03-26 stsp }
1898 2ec1f75b 2019-03-26 stsp } else
1899 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1900 b72f483a 2019-02-05 stsp
1901 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1902 4ce46740 2019-08-08 stsp a->diff_context, stdout);
1903 b72f483a 2019-02-05 stsp done:
1904 b72f483a 2019-02-05 stsp if (blob1)
1905 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1906 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1907 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1908 b72f483a 2019-02-05 stsp free(abspath);
1909 927df6b7 2019-02-10 stsp return err;
1910 927df6b7 2019-02-10 stsp }
1911 927df6b7 2019-02-10 stsp
1912 927df6b7 2019-02-10 stsp static const struct got_error *
1913 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
1914 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
1915 01073a5d 2019-08-22 stsp struct got_repository *repo)
1916 0adb7196 2019-08-11 stsp {
1917 0adb7196 2019-08-11 stsp const struct got_error *err;
1918 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
1919 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
1920 0adb7196 2019-08-11 stsp
1921 0adb7196 2019-08-11 stsp *id = NULL;
1922 0adb7196 2019-08-11 stsp *label = NULL;
1923 d24820bf 2019-08-11 stsp
1924 01073a5d 2019-08-22 stsp if (resolve_tags) {
1925 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1926 01073a5d 2019-08-22 stsp repo);
1927 01073a5d 2019-08-22 stsp if (err == NULL) {
1928 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
1929 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
1930 01073a5d 2019-08-22 stsp if (*id == NULL)
1931 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
1932 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
1933 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
1934 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
1935 01073a5d 2019-08-22 stsp free(id);
1936 01073a5d 2019-08-22 stsp *id = NULL;
1937 01073a5d 2019-08-22 stsp }
1938 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
1939 01073a5d 2019-08-22 stsp return err;
1940 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
1941 01073a5d 2019-08-22 stsp return err;
1942 01073a5d 2019-08-22 stsp }
1943 0adb7196 2019-08-11 stsp
1944 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1945 0adb7196 2019-08-11 stsp if (err) {
1946 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1947 0adb7196 2019-08-11 stsp return err;
1948 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
1949 0adb7196 2019-08-11 stsp if (err != NULL)
1950 0adb7196 2019-08-11 stsp goto done;
1951 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
1952 0adb7196 2019-08-11 stsp if (*label == NULL) {
1953 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1954 0adb7196 2019-08-11 stsp goto done;
1955 0adb7196 2019-08-11 stsp }
1956 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
1957 0adb7196 2019-08-11 stsp } else {
1958 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
1959 0adb7196 2019-08-11 stsp if (*label == NULL) {
1960 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1961 0adb7196 2019-08-11 stsp goto done;
1962 0adb7196 2019-08-11 stsp }
1963 0adb7196 2019-08-11 stsp }
1964 0adb7196 2019-08-11 stsp done:
1965 0adb7196 2019-08-11 stsp if (ref)
1966 0adb7196 2019-08-11 stsp got_ref_close(ref);
1967 0adb7196 2019-08-11 stsp return err;
1968 0adb7196 2019-08-11 stsp }
1969 0adb7196 2019-08-11 stsp
1970 0adb7196 2019-08-11 stsp
1971 0adb7196 2019-08-11 stsp static const struct got_error *
1972 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1973 b00d56cd 2018-04-01 stsp {
1974 b00d56cd 2018-04-01 stsp const struct got_error *error;
1975 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1976 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1977 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1978 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1979 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1980 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1981 15a94983 2018-12-23 stsp int type1, type2;
1982 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
1983 c0cc5c62 2018-10-18 stsp const char *errstr;
1984 927df6b7 2019-02-10 stsp char *path = NULL;
1985 b00d56cd 2018-04-01 stsp
1986 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1987 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1988 25eccc22 2019-01-04 stsp NULL) == -1)
1989 b00d56cd 2018-04-01 stsp err(1, "pledge");
1990 b00d56cd 2018-04-01 stsp #endif
1991 b00d56cd 2018-04-01 stsp
1992 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1993 b00d56cd 2018-04-01 stsp switch (ch) {
1994 c0cc5c62 2018-10-18 stsp case 'C':
1995 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1996 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1997 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1998 c0cc5c62 2018-10-18 stsp break;
1999 b72f483a 2019-02-05 stsp case 'r':
2000 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2001 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2002 b72f483a 2019-02-05 stsp err(1, "-r option");
2003 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2004 b72f483a 2019-02-05 stsp break;
2005 98eaaa12 2019-08-03 stsp case 's':
2006 98eaaa12 2019-08-03 stsp diff_staged = 1;
2007 98eaaa12 2019-08-03 stsp break;
2008 b00d56cd 2018-04-01 stsp default:
2009 2deda0b9 2019-03-07 stsp usage_diff();
2010 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2011 b00d56cd 2018-04-01 stsp }
2012 b00d56cd 2018-04-01 stsp }
2013 b00d56cd 2018-04-01 stsp
2014 b00d56cd 2018-04-01 stsp argc -= optind;
2015 b00d56cd 2018-04-01 stsp argv += optind;
2016 b00d56cd 2018-04-01 stsp
2017 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2018 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2019 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2020 b72f483a 2019-02-05 stsp goto done;
2021 b72f483a 2019-02-05 stsp }
2022 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2023 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2024 927df6b7 2019-02-10 stsp goto done;
2025 927df6b7 2019-02-10 stsp if (argc <= 1) {
2026 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2027 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2028 927df6b7 2019-02-10 stsp goto done;
2029 927df6b7 2019-02-10 stsp }
2030 b72f483a 2019-02-05 stsp if (repo_path)
2031 b72f483a 2019-02-05 stsp errx(1,
2032 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2033 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2034 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2035 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2036 927df6b7 2019-02-10 stsp goto done;
2037 927df6b7 2019-02-10 stsp }
2038 927df6b7 2019-02-10 stsp if (argc == 1) {
2039 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2040 cbd1af7a 2019-03-18 stsp argv[0]);
2041 927df6b7 2019-02-10 stsp if (error)
2042 927df6b7 2019-02-10 stsp goto done;
2043 927df6b7 2019-02-10 stsp } else {
2044 927df6b7 2019-02-10 stsp path = strdup("");
2045 927df6b7 2019-02-10 stsp if (path == NULL) {
2046 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2047 927df6b7 2019-02-10 stsp goto done;
2048 927df6b7 2019-02-10 stsp }
2049 927df6b7 2019-02-10 stsp }
2050 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2051 98eaaa12 2019-08-03 stsp if (diff_staged)
2052 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2053 98eaaa12 2019-08-03 stsp "objects in repository");
2054 15a94983 2018-12-23 stsp id_str1 = argv[0];
2055 15a94983 2018-12-23 stsp id_str2 = argv[1];
2056 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2057 30db809c 2019-06-05 stsp repo_path =
2058 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2059 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2060 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2061 30db809c 2019-06-05 stsp goto done;
2062 30db809c 2019-06-05 stsp }
2063 30db809c 2019-06-05 stsp }
2064 b00d56cd 2018-04-01 stsp } else
2065 b00d56cd 2018-04-01 stsp usage_diff();
2066 25eccc22 2019-01-04 stsp
2067 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2068 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2069 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2070 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2071 b72f483a 2019-02-05 stsp }
2072 b00d56cd 2018-04-01 stsp
2073 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
2074 76089277 2018-04-01 stsp free(repo_path);
2075 b00d56cd 2018-04-01 stsp if (error != NULL)
2076 b00d56cd 2018-04-01 stsp goto done;
2077 b00d56cd 2018-04-01 stsp
2078 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2079 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2080 c02c541e 2019-03-29 stsp if (error)
2081 c02c541e 2019-03-29 stsp goto done;
2082 c02c541e 2019-03-29 stsp
2083 30db809c 2019-06-05 stsp if (argc <= 1) {
2084 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2085 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2086 b72f483a 2019-02-05 stsp char *id_str;
2087 72ea6654 2019-07-27 stsp
2088 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2089 72ea6654 2019-07-27 stsp
2090 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2091 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2092 b72f483a 2019-02-05 stsp if (error)
2093 b72f483a 2019-02-05 stsp goto done;
2094 b72f483a 2019-02-05 stsp arg.repo = repo;
2095 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2096 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2097 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2098 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2099 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2100 b72f483a 2019-02-05 stsp
2101 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2102 72ea6654 2019-07-27 stsp if (error)
2103 72ea6654 2019-07-27 stsp goto done;
2104 72ea6654 2019-07-27 stsp
2105 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2106 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2107 b72f483a 2019-02-05 stsp free(id_str);
2108 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2109 b72f483a 2019-02-05 stsp goto done;
2110 b72f483a 2019-02-05 stsp }
2111 b72f483a 2019-02-05 stsp
2112 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2113 01073a5d 2019-08-22 stsp repo);
2114 0adb7196 2019-08-11 stsp if (error)
2115 0adb7196 2019-08-11 stsp goto done;
2116 b00d56cd 2018-04-01 stsp
2117 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2118 01073a5d 2019-08-22 stsp repo);
2119 0adb7196 2019-08-11 stsp if (error)
2120 0adb7196 2019-08-11 stsp goto done;
2121 b00d56cd 2018-04-01 stsp
2122 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2123 15a94983 2018-12-23 stsp if (error)
2124 15a94983 2018-12-23 stsp goto done;
2125 15a94983 2018-12-23 stsp
2126 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2127 15a94983 2018-12-23 stsp if (error)
2128 15a94983 2018-12-23 stsp goto done;
2129 15a94983 2018-12-23 stsp
2130 15a94983 2018-12-23 stsp if (type1 != type2) {
2131 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2132 b00d56cd 2018-04-01 stsp goto done;
2133 b00d56cd 2018-04-01 stsp }
2134 b00d56cd 2018-04-01 stsp
2135 15a94983 2018-12-23 stsp switch (type1) {
2136 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2137 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2138 54156555 2018-12-24 stsp diff_context, repo, stdout);
2139 b00d56cd 2018-04-01 stsp break;
2140 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2141 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2142 54156555 2018-12-24 stsp diff_context, repo, stdout);
2143 b00d56cd 2018-04-01 stsp break;
2144 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2145 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2146 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2147 c0cc5c62 2018-10-18 stsp repo, stdout);
2148 b00d56cd 2018-04-01 stsp break;
2149 b00d56cd 2018-04-01 stsp default:
2150 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2151 b00d56cd 2018-04-01 stsp }
2152 b00d56cd 2018-04-01 stsp
2153 b00d56cd 2018-04-01 stsp done:
2154 5e70831e 2019-05-28 stsp free(label1);
2155 5e70831e 2019-05-28 stsp free(label2);
2156 15a94983 2018-12-23 stsp free(id1);
2157 15a94983 2018-12-23 stsp free(id2);
2158 927df6b7 2019-02-10 stsp free(path);
2159 b72f483a 2019-02-05 stsp if (worktree)
2160 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2161 ad242220 2018-09-08 stsp if (repo) {
2162 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2163 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2164 ad242220 2018-09-08 stsp if (error == NULL)
2165 ad242220 2018-09-08 stsp error = repo_error;
2166 ad242220 2018-09-08 stsp }
2167 b00d56cd 2018-04-01 stsp return error;
2168 404c43c4 2018-06-21 stsp }
2169 404c43c4 2018-06-21 stsp
2170 404c43c4 2018-06-21 stsp __dead static void
2171 404c43c4 2018-06-21 stsp usage_blame(void)
2172 404c43c4 2018-06-21 stsp {
2173 9270e621 2019-02-05 stsp fprintf(stderr,
2174 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2175 404c43c4 2018-06-21 stsp getprogname());
2176 404c43c4 2018-06-21 stsp exit(1);
2177 b00d56cd 2018-04-01 stsp }
2178 b00d56cd 2018-04-01 stsp
2179 28315671 2019-08-14 stsp struct blame_line {
2180 28315671 2019-08-14 stsp int annotated;
2181 28315671 2019-08-14 stsp char *id_str;
2182 82f6abb8 2019-08-14 stsp char *committer;
2183 bcb49d15 2019-08-14 stsp char datebuf[9]; /* YY-MM-DD + NUL */
2184 28315671 2019-08-14 stsp };
2185 28315671 2019-08-14 stsp
2186 28315671 2019-08-14 stsp struct blame_cb_args {
2187 28315671 2019-08-14 stsp struct blame_line *lines;
2188 28315671 2019-08-14 stsp int nlines;
2189 7ef28ff8 2019-08-14 stsp int nlines_prec;
2190 28315671 2019-08-14 stsp int lineno_cur;
2191 28315671 2019-08-14 stsp off_t *line_offsets;
2192 28315671 2019-08-14 stsp FILE *f;
2193 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2194 28315671 2019-08-14 stsp };
2195 28315671 2019-08-14 stsp
2196 404c43c4 2018-06-21 stsp static const struct got_error *
2197 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2198 28315671 2019-08-14 stsp {
2199 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2200 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2201 28315671 2019-08-14 stsp struct blame_line *bline;
2202 82f6abb8 2019-08-14 stsp char *line = NULL;
2203 28315671 2019-08-14 stsp size_t linesize = 0;
2204 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2205 28315671 2019-08-14 stsp off_t offset;
2206 bcb49d15 2019-08-14 stsp struct tm tm;
2207 bcb49d15 2019-08-14 stsp time_t committer_time;
2208 28315671 2019-08-14 stsp
2209 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2210 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2211 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2212 28315671 2019-08-14 stsp
2213 28315671 2019-08-14 stsp if (sigint_received)
2214 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2215 28315671 2019-08-14 stsp
2216 2839f8b9 2019-08-18 stsp if (lineno == -1)
2217 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2218 2839f8b9 2019-08-18 stsp
2219 28315671 2019-08-14 stsp /* Annotate this line. */
2220 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2221 28315671 2019-08-14 stsp if (bline->annotated)
2222 28315671 2019-08-14 stsp return NULL;
2223 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2224 28315671 2019-08-14 stsp if (err)
2225 28315671 2019-08-14 stsp return err;
2226 82f6abb8 2019-08-14 stsp
2227 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2228 82f6abb8 2019-08-14 stsp if (err)
2229 82f6abb8 2019-08-14 stsp goto done;
2230 82f6abb8 2019-08-14 stsp
2231 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2232 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2233 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2234 bcb49d15 2019-08-14 stsp goto done;
2235 bcb49d15 2019-08-14 stsp }
2236 bcb49d15 2019-08-14 stsp
2237 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2238 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2239 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2240 bcb49d15 2019-08-14 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2241 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2242 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2243 82f6abb8 2019-08-14 stsp goto done;
2244 82f6abb8 2019-08-14 stsp }
2245 28315671 2019-08-14 stsp bline->annotated = 1;
2246 28315671 2019-08-14 stsp
2247 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2248 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2249 28315671 2019-08-14 stsp if (!bline->annotated)
2250 82f6abb8 2019-08-14 stsp goto done;
2251 28315671 2019-08-14 stsp
2252 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2253 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2254 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2255 82f6abb8 2019-08-14 stsp goto done;
2256 82f6abb8 2019-08-14 stsp }
2257 28315671 2019-08-14 stsp
2258 28315671 2019-08-14 stsp while (bline->annotated) {
2259 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2260 82f6abb8 2019-08-14 stsp size_t len;
2261 82f6abb8 2019-08-14 stsp
2262 28315671 2019-08-14 stsp if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2263 28315671 2019-08-14 stsp if (ferror(a->f))
2264 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2265 28315671 2019-08-14 stsp break;
2266 28315671 2019-08-14 stsp }
2267 82f6abb8 2019-08-14 stsp
2268 82f6abb8 2019-08-14 stsp committer = bline->committer;
2269 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2270 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2271 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2272 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2273 82f6abb8 2019-08-14 stsp if (at)
2274 82f6abb8 2019-08-14 stsp *at = '\0';
2275 82f6abb8 2019-08-14 stsp len = strlen(committer);
2276 82f6abb8 2019-08-14 stsp if (len >= 9)
2277 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2278 28315671 2019-08-14 stsp
2279 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2280 28315671 2019-08-14 stsp if (nl)
2281 28315671 2019-08-14 stsp *nl = '\0';
2282 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2283 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2284 28315671 2019-08-14 stsp
2285 28315671 2019-08-14 stsp a->lineno_cur++;
2286 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2287 28315671 2019-08-14 stsp }
2288 82f6abb8 2019-08-14 stsp done:
2289 82f6abb8 2019-08-14 stsp if (commit)
2290 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2291 28315671 2019-08-14 stsp free(line);
2292 28315671 2019-08-14 stsp return err;
2293 28315671 2019-08-14 stsp }
2294 28315671 2019-08-14 stsp
2295 28315671 2019-08-14 stsp static const struct got_error *
2296 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2297 404c43c4 2018-06-21 stsp {
2298 404c43c4 2018-06-21 stsp const struct got_error *error;
2299 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2300 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2301 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2302 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2303 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2304 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2305 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2306 28315671 2019-08-14 stsp struct blame_cb_args bca;
2307 28315671 2019-08-14 stsp int ch, obj_type, i;
2308 b02560ec 2019-08-19 stsp size_t filesize;
2309 90f3c347 2019-08-19 stsp
2310 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2311 404c43c4 2018-06-21 stsp
2312 404c43c4 2018-06-21 stsp #ifndef PROFILE
2313 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2314 36e2fb66 2019-01-04 stsp NULL) == -1)
2315 404c43c4 2018-06-21 stsp err(1, "pledge");
2316 404c43c4 2018-06-21 stsp #endif
2317 404c43c4 2018-06-21 stsp
2318 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2319 404c43c4 2018-06-21 stsp switch (ch) {
2320 404c43c4 2018-06-21 stsp case 'c':
2321 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2322 404c43c4 2018-06-21 stsp break;
2323 66bea077 2018-08-02 stsp case 'r':
2324 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2325 66bea077 2018-08-02 stsp if (repo_path == NULL)
2326 66bea077 2018-08-02 stsp err(1, "-r option");
2327 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2328 66bea077 2018-08-02 stsp break;
2329 404c43c4 2018-06-21 stsp default:
2330 2deda0b9 2019-03-07 stsp usage_blame();
2331 404c43c4 2018-06-21 stsp /* NOTREACHED */
2332 404c43c4 2018-06-21 stsp }
2333 404c43c4 2018-06-21 stsp }
2334 404c43c4 2018-06-21 stsp
2335 404c43c4 2018-06-21 stsp argc -= optind;
2336 404c43c4 2018-06-21 stsp argv += optind;
2337 404c43c4 2018-06-21 stsp
2338 a39318fd 2018-08-02 stsp if (argc == 1)
2339 404c43c4 2018-06-21 stsp path = argv[0];
2340 a39318fd 2018-08-02 stsp else
2341 404c43c4 2018-06-21 stsp usage_blame();
2342 404c43c4 2018-06-21 stsp
2343 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2344 66bea077 2018-08-02 stsp if (cwd == NULL) {
2345 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2346 66bea077 2018-08-02 stsp goto done;
2347 66bea077 2018-08-02 stsp }
2348 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2349 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2350 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2351 66bea077 2018-08-02 stsp goto done;
2352 0c06baac 2019-02-05 stsp else
2353 0c06baac 2019-02-05 stsp error = NULL;
2354 0c06baac 2019-02-05 stsp if (worktree) {
2355 0c06baac 2019-02-05 stsp repo_path =
2356 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2357 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2358 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2359 0c06baac 2019-02-05 stsp if (error)
2360 0c06baac 2019-02-05 stsp goto done;
2361 0c06baac 2019-02-05 stsp } else {
2362 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2363 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2364 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2365 0c06baac 2019-02-05 stsp goto done;
2366 0c06baac 2019-02-05 stsp }
2367 66bea077 2018-08-02 stsp }
2368 66bea077 2018-08-02 stsp }
2369 36e2fb66 2019-01-04 stsp
2370 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2371 c02c541e 2019-03-29 stsp if (error != NULL)
2372 36e2fb66 2019-01-04 stsp goto done;
2373 66bea077 2018-08-02 stsp
2374 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2375 c02c541e 2019-03-29 stsp if (error)
2376 404c43c4 2018-06-21 stsp goto done;
2377 404c43c4 2018-06-21 stsp
2378 0c06baac 2019-02-05 stsp if (worktree) {
2379 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2380 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2381 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2382 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2383 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2384 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2385 6efaaa2d 2019-02-05 stsp path) == -1) {
2386 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2387 0c06baac 2019-02-05 stsp goto done;
2388 0c06baac 2019-02-05 stsp }
2389 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2390 0c06baac 2019-02-05 stsp free(p);
2391 0c06baac 2019-02-05 stsp } else {
2392 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2393 0c06baac 2019-02-05 stsp }
2394 0c06baac 2019-02-05 stsp if (error)
2395 66bea077 2018-08-02 stsp goto done;
2396 66bea077 2018-08-02 stsp
2397 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2398 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2399 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2400 404c43c4 2018-06-21 stsp if (error != NULL)
2401 66bea077 2018-08-02 stsp goto done;
2402 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2403 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2404 404c43c4 2018-06-21 stsp if (error != NULL)
2405 66bea077 2018-08-02 stsp goto done;
2406 404c43c4 2018-06-21 stsp } else {
2407 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2408 30837e32 2019-07-25 stsp if (error)
2409 66bea077 2018-08-02 stsp goto done;
2410 404c43c4 2018-06-21 stsp }
2411 404c43c4 2018-06-21 stsp
2412 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2413 28315671 2019-08-14 stsp if (error)
2414 28315671 2019-08-14 stsp goto done;
2415 28315671 2019-08-14 stsp if (obj_id == NULL) {
2416 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2417 28315671 2019-08-14 stsp goto done;
2418 28315671 2019-08-14 stsp }
2419 28315671 2019-08-14 stsp
2420 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2421 28315671 2019-08-14 stsp if (error)
2422 28315671 2019-08-14 stsp goto done;
2423 28315671 2019-08-14 stsp
2424 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2425 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2426 28315671 2019-08-14 stsp goto done;
2427 28315671 2019-08-14 stsp }
2428 28315671 2019-08-14 stsp
2429 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2430 28315671 2019-08-14 stsp if (error)
2431 28315671 2019-08-14 stsp goto done;
2432 28315671 2019-08-14 stsp bca.f = got_opentemp();
2433 28315671 2019-08-14 stsp if (bca.f == NULL) {
2434 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2435 28315671 2019-08-14 stsp goto done;
2436 28315671 2019-08-14 stsp }
2437 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2438 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2439 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2440 28315671 2019-08-14 stsp goto done;
2441 28315671 2019-08-14 stsp
2442 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2443 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2444 b02560ec 2019-08-19 stsp bca.nlines--;
2445 b02560ec 2019-08-19 stsp
2446 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2447 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2448 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2449 28315671 2019-08-14 stsp goto done;
2450 28315671 2019-08-14 stsp }
2451 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2452 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2453 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2454 7ef28ff8 2019-08-14 stsp while (i > 0) {
2455 7ef28ff8 2019-08-14 stsp i /= 10;
2456 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2457 7ef28ff8 2019-08-14 stsp }
2458 82f6abb8 2019-08-14 stsp bca.repo = repo;
2459 7ef28ff8 2019-08-14 stsp
2460 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2461 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2462 28315671 2019-08-14 stsp if (error)
2463 28315671 2019-08-14 stsp goto done;
2464 404c43c4 2018-06-21 stsp done:
2465 66bea077 2018-08-02 stsp free(in_repo_path);
2466 66bea077 2018-08-02 stsp free(repo_path);
2467 66bea077 2018-08-02 stsp free(cwd);
2468 404c43c4 2018-06-21 stsp free(commit_id);
2469 28315671 2019-08-14 stsp free(obj_id);
2470 28315671 2019-08-14 stsp if (blob)
2471 28315671 2019-08-14 stsp got_object_blob_close(blob);
2472 0c06baac 2019-02-05 stsp if (worktree)
2473 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2474 ad242220 2018-09-08 stsp if (repo) {
2475 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2476 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2477 ad242220 2018-09-08 stsp if (error == NULL)
2478 ad242220 2018-09-08 stsp error = repo_error;
2479 ad242220 2018-09-08 stsp }
2480 28315671 2019-08-14 stsp for (i = 0; i < bca.nlines; i++) {
2481 28315671 2019-08-14 stsp struct blame_line *bline = &bca.lines[i];
2482 28315671 2019-08-14 stsp free(bline->id_str);
2483 82f6abb8 2019-08-14 stsp free(bline->committer);
2484 28315671 2019-08-14 stsp }
2485 28315671 2019-08-14 stsp free(bca.lines);
2486 28315671 2019-08-14 stsp free(bca.line_offsets);
2487 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2488 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2489 404c43c4 2018-06-21 stsp return error;
2490 5de5890b 2018-10-18 stsp }
2491 5de5890b 2018-10-18 stsp
2492 5de5890b 2018-10-18 stsp __dead static void
2493 5de5890b 2018-10-18 stsp usage_tree(void)
2494 5de5890b 2018-10-18 stsp {
2495 c1669e2e 2019-01-09 stsp fprintf(stderr,
2496 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2497 5de5890b 2018-10-18 stsp getprogname());
2498 5de5890b 2018-10-18 stsp exit(1);
2499 5de5890b 2018-10-18 stsp }
2500 5de5890b 2018-10-18 stsp
2501 c1669e2e 2019-01-09 stsp static void
2502 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2503 c1669e2e 2019-01-09 stsp const char *root_path)
2504 c1669e2e 2019-01-09 stsp {
2505 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2506 848d6979 2019-08-12 stsp const char *modestr = "";
2507 5de5890b 2018-10-18 stsp
2508 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2509 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2510 c1669e2e 2019-01-09 stsp path++;
2511 c1669e2e 2019-01-09 stsp
2512 848d6979 2019-08-12 stsp if (S_ISLNK(te->mode))
2513 848d6979 2019-08-12 stsp modestr = "@";
2514 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2515 848d6979 2019-08-12 stsp modestr = "/";
2516 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2517 848d6979 2019-08-12 stsp modestr = "*";
2518 848d6979 2019-08-12 stsp
2519 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2520 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2521 c1669e2e 2019-01-09 stsp }
2522 c1669e2e 2019-01-09 stsp
2523 5de5890b 2018-10-18 stsp static const struct got_error *
2524 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2525 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2526 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2527 5de5890b 2018-10-18 stsp {
2528 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2529 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2530 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2531 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2532 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2533 5de5890b 2018-10-18 stsp
2534 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2535 5de5890b 2018-10-18 stsp if (err)
2536 5de5890b 2018-10-18 stsp goto done;
2537 5de5890b 2018-10-18 stsp
2538 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2539 5de5890b 2018-10-18 stsp if (err)
2540 5de5890b 2018-10-18 stsp goto done;
2541 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2542 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2543 5de5890b 2018-10-18 stsp while (te) {
2544 5de5890b 2018-10-18 stsp char *id = NULL;
2545 84453469 2018-11-11 stsp
2546 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2547 84453469 2018-11-11 stsp break;
2548 84453469 2018-11-11 stsp
2549 5de5890b 2018-10-18 stsp if (show_ids) {
2550 5de5890b 2018-10-18 stsp char *id_str;
2551 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2552 5de5890b 2018-10-18 stsp if (err)
2553 5de5890b 2018-10-18 stsp goto done;
2554 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2555 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2556 5de5890b 2018-10-18 stsp free(id_str);
2557 5de5890b 2018-10-18 stsp goto done;
2558 5de5890b 2018-10-18 stsp }
2559 5de5890b 2018-10-18 stsp free(id_str);
2560 5de5890b 2018-10-18 stsp }
2561 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2562 5de5890b 2018-10-18 stsp free(id);
2563 c1669e2e 2019-01-09 stsp
2564 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2565 c1669e2e 2019-01-09 stsp char *child_path;
2566 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2567 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2568 c1669e2e 2019-01-09 stsp te->name) == -1) {
2569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2570 c1669e2e 2019-01-09 stsp goto done;
2571 c1669e2e 2019-01-09 stsp }
2572 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2573 c1669e2e 2019-01-09 stsp root_path, repo);
2574 c1669e2e 2019-01-09 stsp free(child_path);
2575 c1669e2e 2019-01-09 stsp if (err)
2576 c1669e2e 2019-01-09 stsp goto done;
2577 c1669e2e 2019-01-09 stsp }
2578 c1669e2e 2019-01-09 stsp
2579 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2580 5de5890b 2018-10-18 stsp }
2581 5de5890b 2018-10-18 stsp done:
2582 5de5890b 2018-10-18 stsp if (tree)
2583 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2584 5de5890b 2018-10-18 stsp free(tree_id);
2585 5de5890b 2018-10-18 stsp return err;
2586 404c43c4 2018-06-21 stsp }
2587 404c43c4 2018-06-21 stsp
2588 5de5890b 2018-10-18 stsp static const struct got_error *
2589 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2590 5de5890b 2018-10-18 stsp {
2591 5de5890b 2018-10-18 stsp const struct got_error *error;
2592 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2593 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2594 7a2c19d6 2019-02-05 stsp const char *path;
2595 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2596 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2597 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2598 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2599 5de5890b 2018-10-18 stsp int ch;
2600 5de5890b 2018-10-18 stsp
2601 5de5890b 2018-10-18 stsp #ifndef PROFILE
2602 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2603 0f8d269b 2019-01-04 stsp NULL) == -1)
2604 5de5890b 2018-10-18 stsp err(1, "pledge");
2605 5de5890b 2018-10-18 stsp #endif
2606 5de5890b 2018-10-18 stsp
2607 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2608 5de5890b 2018-10-18 stsp switch (ch) {
2609 5de5890b 2018-10-18 stsp case 'c':
2610 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2611 5de5890b 2018-10-18 stsp break;
2612 5de5890b 2018-10-18 stsp case 'r':
2613 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2614 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2615 5de5890b 2018-10-18 stsp err(1, "-r option");
2616 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2617 5de5890b 2018-10-18 stsp break;
2618 5de5890b 2018-10-18 stsp case 'i':
2619 5de5890b 2018-10-18 stsp show_ids = 1;
2620 5de5890b 2018-10-18 stsp break;
2621 c1669e2e 2019-01-09 stsp case 'R':
2622 c1669e2e 2019-01-09 stsp recurse = 1;
2623 c1669e2e 2019-01-09 stsp break;
2624 5de5890b 2018-10-18 stsp default:
2625 2deda0b9 2019-03-07 stsp usage_tree();
2626 5de5890b 2018-10-18 stsp /* NOTREACHED */
2627 5de5890b 2018-10-18 stsp }
2628 5de5890b 2018-10-18 stsp }
2629 5de5890b 2018-10-18 stsp
2630 5de5890b 2018-10-18 stsp argc -= optind;
2631 5de5890b 2018-10-18 stsp argv += optind;
2632 5de5890b 2018-10-18 stsp
2633 5de5890b 2018-10-18 stsp if (argc == 1)
2634 5de5890b 2018-10-18 stsp path = argv[0];
2635 5de5890b 2018-10-18 stsp else if (argc > 1)
2636 5de5890b 2018-10-18 stsp usage_tree();
2637 5de5890b 2018-10-18 stsp else
2638 7a2c19d6 2019-02-05 stsp path = NULL;
2639 7a2c19d6 2019-02-05 stsp
2640 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2641 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2642 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2643 9bf7a39b 2019-02-05 stsp goto done;
2644 9bf7a39b 2019-02-05 stsp }
2645 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2646 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2647 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2648 7a2c19d6 2019-02-05 stsp goto done;
2649 7a2c19d6 2019-02-05 stsp else
2650 7a2c19d6 2019-02-05 stsp error = NULL;
2651 7a2c19d6 2019-02-05 stsp if (worktree) {
2652 7a2c19d6 2019-02-05 stsp repo_path =
2653 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2654 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2655 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2656 7a2c19d6 2019-02-05 stsp if (error)
2657 7a2c19d6 2019-02-05 stsp goto done;
2658 7a2c19d6 2019-02-05 stsp } else {
2659 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2660 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2661 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2662 7a2c19d6 2019-02-05 stsp goto done;
2663 7a2c19d6 2019-02-05 stsp }
2664 5de5890b 2018-10-18 stsp }
2665 5de5890b 2018-10-18 stsp }
2666 5de5890b 2018-10-18 stsp
2667 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2668 5de5890b 2018-10-18 stsp if (error != NULL)
2669 c02c541e 2019-03-29 stsp goto done;
2670 c02c541e 2019-03-29 stsp
2671 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2672 c02c541e 2019-03-29 stsp if (error)
2673 5de5890b 2018-10-18 stsp goto done;
2674 5de5890b 2018-10-18 stsp
2675 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2676 9bf7a39b 2019-02-05 stsp if (worktree) {
2677 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2678 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2679 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2680 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2681 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2682 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2683 9bf7a39b 2019-02-05 stsp goto done;
2684 9bf7a39b 2019-02-05 stsp }
2685 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2686 9bf7a39b 2019-02-05 stsp free(p);
2687 9bf7a39b 2019-02-05 stsp if (error)
2688 9bf7a39b 2019-02-05 stsp goto done;
2689 9bf7a39b 2019-02-05 stsp } else
2690 9bf7a39b 2019-02-05 stsp path = "/";
2691 9bf7a39b 2019-02-05 stsp }
2692 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2693 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2694 9bf7a39b 2019-02-05 stsp if (error != NULL)
2695 9bf7a39b 2019-02-05 stsp goto done;
2696 9bf7a39b 2019-02-05 stsp }
2697 5de5890b 2018-10-18 stsp
2698 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2699 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2700 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2701 5de5890b 2018-10-18 stsp if (error != NULL)
2702 5de5890b 2018-10-18 stsp goto done;
2703 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2704 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2705 5de5890b 2018-10-18 stsp if (error != NULL)
2706 5de5890b 2018-10-18 stsp goto done;
2707 5de5890b 2018-10-18 stsp } else {
2708 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2709 30837e32 2019-07-25 stsp if (error)
2710 5de5890b 2018-10-18 stsp goto done;
2711 5de5890b 2018-10-18 stsp }
2712 5de5890b 2018-10-18 stsp
2713 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2714 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2715 5de5890b 2018-10-18 stsp done:
2716 5de5890b 2018-10-18 stsp free(in_repo_path);
2717 5de5890b 2018-10-18 stsp free(repo_path);
2718 5de5890b 2018-10-18 stsp free(cwd);
2719 5de5890b 2018-10-18 stsp free(commit_id);
2720 7a2c19d6 2019-02-05 stsp if (worktree)
2721 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2722 5de5890b 2018-10-18 stsp if (repo) {
2723 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2724 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2725 5de5890b 2018-10-18 stsp if (error == NULL)
2726 5de5890b 2018-10-18 stsp error = repo_error;
2727 5de5890b 2018-10-18 stsp }
2728 5de5890b 2018-10-18 stsp return error;
2729 5de5890b 2018-10-18 stsp }
2730 5de5890b 2018-10-18 stsp
2731 6bad629b 2019-02-04 stsp __dead static void
2732 6bad629b 2019-02-04 stsp usage_status(void)
2733 6bad629b 2019-02-04 stsp {
2734 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2735 6bad629b 2019-02-04 stsp exit(1);
2736 6bad629b 2019-02-04 stsp }
2737 5c860e29 2018-03-12 stsp
2738 b72f483a 2019-02-05 stsp static const struct got_error *
2739 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2740 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2741 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2742 6bad629b 2019-02-04 stsp {
2743 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2744 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2745 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2746 b72f483a 2019-02-05 stsp return NULL;
2747 6bad629b 2019-02-04 stsp }
2748 5c860e29 2018-03-12 stsp
2749 6bad629b 2019-02-04 stsp static const struct got_error *
2750 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2751 6bad629b 2019-02-04 stsp {
2752 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2753 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2754 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2755 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2756 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2757 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2758 a5edda0a 2019-07-27 stsp int ch;
2759 5c860e29 2018-03-12 stsp
2760 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2761 72ea6654 2019-07-27 stsp
2762 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2763 6bad629b 2019-02-04 stsp switch (ch) {
2764 5c860e29 2018-03-12 stsp default:
2765 2deda0b9 2019-03-07 stsp usage_status();
2766 6bad629b 2019-02-04 stsp /* NOTREACHED */
2767 5c860e29 2018-03-12 stsp }
2768 5c860e29 2018-03-12 stsp }
2769 5c860e29 2018-03-12 stsp
2770 6bad629b 2019-02-04 stsp argc -= optind;
2771 6bad629b 2019-02-04 stsp argv += optind;
2772 5c860e29 2018-03-12 stsp
2773 6bad629b 2019-02-04 stsp #ifndef PROFILE
2774 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2775 6bad629b 2019-02-04 stsp NULL) == -1)
2776 6bad629b 2019-02-04 stsp err(1, "pledge");
2777 f42b1b34 2018-03-12 stsp #endif
2778 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2779 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2780 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2781 927df6b7 2019-02-10 stsp goto done;
2782 927df6b7 2019-02-10 stsp }
2783 927df6b7 2019-02-10 stsp
2784 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2785 927df6b7 2019-02-10 stsp if (error != NULL)
2786 a5edda0a 2019-07-27 stsp goto done;
2787 6bad629b 2019-02-04 stsp
2788 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2789 6bad629b 2019-02-04 stsp if (error != NULL)
2790 6bad629b 2019-02-04 stsp goto done;
2791 6bad629b 2019-02-04 stsp
2792 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2793 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2794 087fb88c 2019-08-04 stsp if (error)
2795 087fb88c 2019-08-04 stsp goto done;
2796 087fb88c 2019-08-04 stsp
2797 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2798 6bad629b 2019-02-04 stsp if (error)
2799 6bad629b 2019-02-04 stsp goto done;
2800 6bad629b 2019-02-04 stsp
2801 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2802 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2803 6bad629b 2019-02-04 stsp done:
2804 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2805 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2806 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2807 927df6b7 2019-02-10 stsp free(cwd);
2808 d0eebce4 2019-03-11 stsp return error;
2809 d0eebce4 2019-03-11 stsp }
2810 d0eebce4 2019-03-11 stsp
2811 d0eebce4 2019-03-11 stsp __dead static void
2812 d0eebce4 2019-03-11 stsp usage_ref(void)
2813 d0eebce4 2019-03-11 stsp {
2814 d0eebce4 2019-03-11 stsp fprintf(stderr,
2815 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2816 d0eebce4 2019-03-11 stsp getprogname());
2817 d0eebce4 2019-03-11 stsp exit(1);
2818 d0eebce4 2019-03-11 stsp }
2819 d0eebce4 2019-03-11 stsp
2820 d0eebce4 2019-03-11 stsp static const struct got_error *
2821 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2822 d0eebce4 2019-03-11 stsp {
2823 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2824 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2825 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2826 d0eebce4 2019-03-11 stsp
2827 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2828 29606af7 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL);
2829 d0eebce4 2019-03-11 stsp if (err)
2830 d0eebce4 2019-03-11 stsp return err;
2831 d0eebce4 2019-03-11 stsp
2832 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2833 d0eebce4 2019-03-11 stsp char *refstr;
2834 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2835 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2836 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2837 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2838 d0eebce4 2019-03-11 stsp free(refstr);
2839 d0eebce4 2019-03-11 stsp }
2840 d0eebce4 2019-03-11 stsp
2841 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2842 d0eebce4 2019-03-11 stsp return NULL;
2843 d0eebce4 2019-03-11 stsp }
2844 d0eebce4 2019-03-11 stsp
2845 d0eebce4 2019-03-11 stsp static const struct got_error *
2846 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2847 d0eebce4 2019-03-11 stsp {
2848 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2849 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2850 d0eebce4 2019-03-11 stsp
2851 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2852 d0eebce4 2019-03-11 stsp if (err)
2853 d0eebce4 2019-03-11 stsp return err;
2854 d0eebce4 2019-03-11 stsp
2855 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2856 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2857 d0eebce4 2019-03-11 stsp return err;
2858 d0eebce4 2019-03-11 stsp }
2859 d0eebce4 2019-03-11 stsp
2860 d0eebce4 2019-03-11 stsp static const struct got_error *
2861 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2862 d0eebce4 2019-03-11 stsp {
2863 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2864 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2865 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2866 d1644381 2019-07-14 stsp
2867 d1644381 2019-07-14 stsp /*
2868 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2869 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2870 d1644381 2019-07-14 stsp * an unintended typo.
2871 d1644381 2019-07-14 stsp */
2872 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2873 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2874 d0eebce4 2019-03-11 stsp
2875 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2876 dd88155e 2019-06-29 stsp repo);
2877 d83d9d5c 2019-05-13 stsp if (err) {
2878 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2879 d0eebce4 2019-03-11 stsp
2880 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2881 d83d9d5c 2019-05-13 stsp return err;
2882 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2883 d83d9d5c 2019-05-13 stsp if (err)
2884 d83d9d5c 2019-05-13 stsp return err;
2885 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2886 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2887 d83d9d5c 2019-05-13 stsp if (err)
2888 d83d9d5c 2019-05-13 stsp return err;
2889 d83d9d5c 2019-05-13 stsp }
2890 d83d9d5c 2019-05-13 stsp
2891 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2892 d0eebce4 2019-03-11 stsp if (err)
2893 d0eebce4 2019-03-11 stsp goto done;
2894 d0eebce4 2019-03-11 stsp
2895 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2896 d0eebce4 2019-03-11 stsp done:
2897 d0eebce4 2019-03-11 stsp if (ref)
2898 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2899 d0eebce4 2019-03-11 stsp free(id);
2900 d1c1ae5f 2019-08-12 stsp return err;
2901 d1c1ae5f 2019-08-12 stsp }
2902 d1c1ae5f 2019-08-12 stsp
2903 d1c1ae5f 2019-08-12 stsp static const struct got_error *
2904 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
2905 d1c1ae5f 2019-08-12 stsp {
2906 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
2907 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
2908 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
2909 d1c1ae5f 2019-08-12 stsp
2910 d1c1ae5f 2019-08-12 stsp /*
2911 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
2912 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
2913 d1c1ae5f 2019-08-12 stsp * an unintended typo.
2914 d1c1ae5f 2019-08-12 stsp */
2915 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
2916 d1c1ae5f 2019-08-12 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2917 d1c1ae5f 2019-08-12 stsp
2918 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
2919 d1c1ae5f 2019-08-12 stsp if (err)
2920 d1c1ae5f 2019-08-12 stsp return err;
2921 d1c1ae5f 2019-08-12 stsp
2922 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
2923 d1c1ae5f 2019-08-12 stsp if (err)
2924 d1c1ae5f 2019-08-12 stsp goto done;
2925 d1c1ae5f 2019-08-12 stsp
2926 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
2927 d1c1ae5f 2019-08-12 stsp done:
2928 d1c1ae5f 2019-08-12 stsp if (target_ref)
2929 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
2930 d1c1ae5f 2019-08-12 stsp if (ref)
2931 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
2932 d0eebce4 2019-03-11 stsp return err;
2933 d0eebce4 2019-03-11 stsp }
2934 d0eebce4 2019-03-11 stsp
2935 d0eebce4 2019-03-11 stsp static const struct got_error *
2936 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2937 d0eebce4 2019-03-11 stsp {
2938 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2939 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2940 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2941 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2942 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
2943 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2944 d0eebce4 2019-03-11 stsp
2945 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2946 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2947 d0eebce4 2019-03-11 stsp switch (ch) {
2948 d0eebce4 2019-03-11 stsp case 'd':
2949 d0eebce4 2019-03-11 stsp delref = optarg;
2950 d0eebce4 2019-03-11 stsp break;
2951 d0eebce4 2019-03-11 stsp case 'r':
2952 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2953 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2954 d0eebce4 2019-03-11 stsp err(1, "-r option");
2955 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2956 d0eebce4 2019-03-11 stsp break;
2957 d0eebce4 2019-03-11 stsp case 'l':
2958 d0eebce4 2019-03-11 stsp do_list = 1;
2959 d1c1ae5f 2019-08-12 stsp break;
2960 d1c1ae5f 2019-08-12 stsp case 's':
2961 d1c1ae5f 2019-08-12 stsp create_symref = 1;
2962 d0eebce4 2019-03-11 stsp break;
2963 d0eebce4 2019-03-11 stsp default:
2964 d0eebce4 2019-03-11 stsp usage_ref();
2965 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2966 d0eebce4 2019-03-11 stsp }
2967 d0eebce4 2019-03-11 stsp }
2968 d0eebce4 2019-03-11 stsp
2969 d0eebce4 2019-03-11 stsp if (do_list && delref)
2970 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2971 d0eebce4 2019-03-11 stsp
2972 d0eebce4 2019-03-11 stsp argc -= optind;
2973 d0eebce4 2019-03-11 stsp argv += optind;
2974 d0eebce4 2019-03-11 stsp
2975 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2976 d1c1ae5f 2019-08-12 stsp if (create_symref)
2977 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
2978 d1c1ae5f 2019-08-12 stsp "-l or -d options");
2979 d0eebce4 2019-03-11 stsp if (argc > 0)
2980 d0eebce4 2019-03-11 stsp usage_ref();
2981 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2982 d0eebce4 2019-03-11 stsp usage_ref();
2983 d0eebce4 2019-03-11 stsp
2984 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2985 e0b57350 2019-03-12 stsp if (do_list) {
2986 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2987 e0b57350 2019-03-12 stsp NULL) == -1)
2988 e0b57350 2019-03-12 stsp err(1, "pledge");
2989 e0b57350 2019-03-12 stsp } else {
2990 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2991 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2992 e0b57350 2019-03-12 stsp err(1, "pledge");
2993 e0b57350 2019-03-12 stsp }
2994 d0eebce4 2019-03-11 stsp #endif
2995 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2996 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2997 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2998 d0eebce4 2019-03-11 stsp goto done;
2999 d0eebce4 2019-03-11 stsp }
3000 d0eebce4 2019-03-11 stsp
3001 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3002 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3003 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3004 d0eebce4 2019-03-11 stsp goto done;
3005 d0eebce4 2019-03-11 stsp else
3006 d0eebce4 2019-03-11 stsp error = NULL;
3007 d0eebce4 2019-03-11 stsp if (worktree) {
3008 d0eebce4 2019-03-11 stsp repo_path =
3009 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3010 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3011 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3012 d0eebce4 2019-03-11 stsp if (error)
3013 d0eebce4 2019-03-11 stsp goto done;
3014 d0eebce4 2019-03-11 stsp } else {
3015 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3016 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3017 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3018 d0eebce4 2019-03-11 stsp goto done;
3019 d0eebce4 2019-03-11 stsp }
3020 d0eebce4 2019-03-11 stsp }
3021 d0eebce4 2019-03-11 stsp }
3022 d0eebce4 2019-03-11 stsp
3023 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
3024 d0eebce4 2019-03-11 stsp if (error != NULL)
3025 d0eebce4 2019-03-11 stsp goto done;
3026 d0eebce4 2019-03-11 stsp
3027 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3028 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3029 c02c541e 2019-03-29 stsp if (error)
3030 c02c541e 2019-03-29 stsp goto done;
3031 c02c541e 2019-03-29 stsp
3032 d0eebce4 2019-03-11 stsp if (do_list)
3033 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3034 d0eebce4 2019-03-11 stsp else if (delref)
3035 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3036 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3037 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3038 d0eebce4 2019-03-11 stsp else
3039 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3040 4e759de4 2019-06-26 stsp done:
3041 4e759de4 2019-06-26 stsp if (repo)
3042 4e759de4 2019-06-26 stsp got_repo_close(repo);
3043 4e759de4 2019-06-26 stsp if (worktree)
3044 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3045 4e759de4 2019-06-26 stsp free(cwd);
3046 4e759de4 2019-06-26 stsp free(repo_path);
3047 4e759de4 2019-06-26 stsp return error;
3048 4e759de4 2019-06-26 stsp }
3049 4e759de4 2019-06-26 stsp
3050 4e759de4 2019-06-26 stsp __dead static void
3051 4e759de4 2019-06-26 stsp usage_branch(void)
3052 4e759de4 2019-06-26 stsp {
3053 4e759de4 2019-06-26 stsp fprintf(stderr,
3054 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
3055 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
3056 4e759de4 2019-06-26 stsp exit(1);
3057 4e759de4 2019-06-26 stsp }
3058 4e759de4 2019-06-26 stsp
3059 4e759de4 2019-06-26 stsp static const struct got_error *
3060 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3061 4e759de4 2019-06-26 stsp {
3062 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3063 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3064 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3065 4e759de4 2019-06-26 stsp
3066 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3067 ba882ee3 2019-07-11 stsp
3068 29606af7 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads");
3069 4e759de4 2019-06-26 stsp if (err)
3070 4e759de4 2019-06-26 stsp return err;
3071 4e759de4 2019-06-26 stsp
3072 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3073 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
3074 4e759de4 2019-06-26 stsp char *refstr;
3075 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
3076 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
3077 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3078 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
3079 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
3080 ba882ee3 2019-07-11 stsp if (err)
3081 ba882ee3 2019-07-11 stsp return err;
3082 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
3083 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3084 ba882ee3 2019-07-11 stsp marker = "* ";
3085 ba882ee3 2019-07-11 stsp else
3086 ba882ee3 2019-07-11 stsp marker = "~ ";
3087 ba882ee3 2019-07-11 stsp free(id);
3088 ba882ee3 2019-07-11 stsp }
3089 29606af7 2019-08-23 stsp refname += strlen("refs/heads/");
3090 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
3091 4e759de4 2019-06-26 stsp if (refstr == NULL)
3092 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
3093 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
3094 4e759de4 2019-06-26 stsp free(refstr);
3095 4e759de4 2019-06-26 stsp }
3096 4e759de4 2019-06-26 stsp
3097 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3098 4e759de4 2019-06-26 stsp return NULL;
3099 4e759de4 2019-06-26 stsp }
3100 4e759de4 2019-06-26 stsp
3101 4e759de4 2019-06-26 stsp static const struct got_error *
3102 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
3103 4e759de4 2019-06-26 stsp {
3104 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3105 4e759de4 2019-06-26 stsp struct got_reference *ref;
3106 4e759de4 2019-06-26 stsp char *refname;
3107 4e759de4 2019-06-26 stsp
3108 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3109 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3110 4e759de4 2019-06-26 stsp
3111 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3112 4e759de4 2019-06-26 stsp if (err)
3113 4e759de4 2019-06-26 stsp goto done;
3114 4e759de4 2019-06-26 stsp
3115 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
3116 4e759de4 2019-06-26 stsp got_ref_close(ref);
3117 4e759de4 2019-06-26 stsp done:
3118 4e759de4 2019-06-26 stsp free(refname);
3119 4e759de4 2019-06-26 stsp return err;
3120 4e759de4 2019-06-26 stsp }
3121 4e759de4 2019-06-26 stsp
3122 4e759de4 2019-06-26 stsp static const struct got_error *
3123 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3124 4e759de4 2019-06-26 stsp const char *base_branch)
3125 4e759de4 2019-06-26 stsp {
3126 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3127 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
3128 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3129 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3130 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
3131 d3f84d51 2019-07-11 stsp
3132 d3f84d51 2019-07-11 stsp /*
3133 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3134 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3135 d3f84d51 2019-07-11 stsp * an unintended typo.
3136 d3f84d51 2019-07-11 stsp */
3137 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3138 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3139 4e759de4 2019-06-26 stsp
3140 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3141 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
3142 4e759de4 2019-06-26 stsp if (base_refname == NULL)
3143 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
3144 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3145 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3146 4e759de4 2019-06-26 stsp
3147 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
3148 4e759de4 2019-06-26 stsp if (err)
3149 4e759de4 2019-06-26 stsp goto done;
3150 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
3151 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
3152 4e759de4 2019-06-26 stsp if (err)
3153 4e759de4 2019-06-26 stsp goto done;
3154 4e759de4 2019-06-26 stsp
3155 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3156 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3157 4e759de4 2019-06-26 stsp goto done;
3158 4e759de4 2019-06-26 stsp }
3159 4e759de4 2019-06-26 stsp
3160 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3161 4e759de4 2019-06-26 stsp if (err == NULL) {
3162 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3163 4e759de4 2019-06-26 stsp goto done;
3164 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3165 4e759de4 2019-06-26 stsp goto done;
3166 4e759de4 2019-06-26 stsp
3167 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
3168 4e759de4 2019-06-26 stsp if (err)
3169 4e759de4 2019-06-26 stsp goto done;
3170 4e759de4 2019-06-26 stsp
3171 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3172 d0eebce4 2019-03-11 stsp done:
3173 4e759de4 2019-06-26 stsp if (ref)
3174 4e759de4 2019-06-26 stsp got_ref_close(ref);
3175 4e759de4 2019-06-26 stsp free(id);
3176 4e759de4 2019-06-26 stsp free(base_refname);
3177 4e759de4 2019-06-26 stsp free(refname);
3178 4e759de4 2019-06-26 stsp return err;
3179 4e759de4 2019-06-26 stsp }
3180 4e759de4 2019-06-26 stsp
3181 4e759de4 2019-06-26 stsp static const struct got_error *
3182 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3183 4e759de4 2019-06-26 stsp {
3184 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3185 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3186 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3187 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3188 4e759de4 2019-06-26 stsp int ch, do_list = 0;
3189 4e759de4 2019-06-26 stsp const char *delref = NULL;
3190 4e759de4 2019-06-26 stsp
3191 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3192 4e759de4 2019-06-26 stsp switch (ch) {
3193 4e759de4 2019-06-26 stsp case 'd':
3194 4e759de4 2019-06-26 stsp delref = optarg;
3195 4e759de4 2019-06-26 stsp break;
3196 4e759de4 2019-06-26 stsp case 'r':
3197 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3198 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3199 4e759de4 2019-06-26 stsp err(1, "-r option");
3200 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3201 4e759de4 2019-06-26 stsp break;
3202 4e759de4 2019-06-26 stsp case 'l':
3203 4e759de4 2019-06-26 stsp do_list = 1;
3204 4e759de4 2019-06-26 stsp break;
3205 4e759de4 2019-06-26 stsp default:
3206 4e759de4 2019-06-26 stsp usage_branch();
3207 4e759de4 2019-06-26 stsp /* NOTREACHED */
3208 4e759de4 2019-06-26 stsp }
3209 4e759de4 2019-06-26 stsp }
3210 4e759de4 2019-06-26 stsp
3211 4e759de4 2019-06-26 stsp if (do_list && delref)
3212 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3213 4e759de4 2019-06-26 stsp
3214 4e759de4 2019-06-26 stsp argc -= optind;
3215 4e759de4 2019-06-26 stsp argv += optind;
3216 4e759de4 2019-06-26 stsp
3217 4e759de4 2019-06-26 stsp if (do_list || delref) {
3218 4e759de4 2019-06-26 stsp if (argc > 0)
3219 4e759de4 2019-06-26 stsp usage_branch();
3220 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
3221 4e759de4 2019-06-26 stsp usage_branch();
3222 4e759de4 2019-06-26 stsp
3223 4e759de4 2019-06-26 stsp #ifndef PROFILE
3224 4e759de4 2019-06-26 stsp if (do_list) {
3225 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3226 4e759de4 2019-06-26 stsp NULL) == -1)
3227 4e759de4 2019-06-26 stsp err(1, "pledge");
3228 4e759de4 2019-06-26 stsp } else {
3229 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3230 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3231 4e759de4 2019-06-26 stsp err(1, "pledge");
3232 4e759de4 2019-06-26 stsp }
3233 4e759de4 2019-06-26 stsp #endif
3234 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3235 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3236 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3237 4e759de4 2019-06-26 stsp goto done;
3238 4e759de4 2019-06-26 stsp }
3239 4e759de4 2019-06-26 stsp
3240 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3241 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3242 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3243 4e759de4 2019-06-26 stsp goto done;
3244 4e759de4 2019-06-26 stsp else
3245 4e759de4 2019-06-26 stsp error = NULL;
3246 4e759de4 2019-06-26 stsp if (worktree) {
3247 4e759de4 2019-06-26 stsp repo_path =
3248 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3249 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3250 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3251 4e759de4 2019-06-26 stsp if (error)
3252 4e759de4 2019-06-26 stsp goto done;
3253 4e759de4 2019-06-26 stsp } else {
3254 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3255 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3256 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3257 4e759de4 2019-06-26 stsp goto done;
3258 4e759de4 2019-06-26 stsp }
3259 4e759de4 2019-06-26 stsp }
3260 4e759de4 2019-06-26 stsp }
3261 4e759de4 2019-06-26 stsp
3262 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
3263 4e759de4 2019-06-26 stsp if (error != NULL)
3264 4e759de4 2019-06-26 stsp goto done;
3265 4e759de4 2019-06-26 stsp
3266 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3267 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3268 4e759de4 2019-06-26 stsp if (error)
3269 4e759de4 2019-06-26 stsp goto done;
3270 4e759de4 2019-06-26 stsp
3271 4e759de4 2019-06-26 stsp if (do_list)
3272 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3273 4e759de4 2019-06-26 stsp else if (delref)
3274 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
3275 4e759de4 2019-06-26 stsp else {
3276 4e759de4 2019-06-26 stsp const char *base_branch;
3277 4e759de4 2019-06-26 stsp if (argc == 1) {
3278 4e759de4 2019-06-26 stsp base_branch = worktree ?
3279 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3280 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3281 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3282 dc5351b4 2019-07-30 stsp base_branch += 11;
3283 4e759de4 2019-06-26 stsp } else
3284 4e759de4 2019-06-26 stsp base_branch = argv[1];
3285 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3286 4e759de4 2019-06-26 stsp }
3287 4e759de4 2019-06-26 stsp done:
3288 d0eebce4 2019-03-11 stsp if (repo)
3289 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3290 d0eebce4 2019-03-11 stsp if (worktree)
3291 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3292 d0eebce4 2019-03-11 stsp free(cwd);
3293 d0eebce4 2019-03-11 stsp free(repo_path);
3294 d00136be 2019-03-26 stsp return error;
3295 d00136be 2019-03-26 stsp }
3296 d00136be 2019-03-26 stsp
3297 8e7bd50a 2019-08-22 stsp
3298 d00136be 2019-03-26 stsp __dead static void
3299 8e7bd50a 2019-08-22 stsp usage_tag(void)
3300 8e7bd50a 2019-08-22 stsp {
3301 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3302 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3303 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3304 8e7bd50a 2019-08-22 stsp exit(1);
3305 8e7bd50a 2019-08-22 stsp }
3306 8e7bd50a 2019-08-22 stsp
3307 8e7bd50a 2019-08-22 stsp static const struct got_error *
3308 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3309 8e7bd50a 2019-08-22 stsp {
3310 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3311 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3312 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3313 8e7bd50a 2019-08-22 stsp
3314 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3315 8e7bd50a 2019-08-22 stsp
3316 29606af7 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags");
3317 8e7bd50a 2019-08-22 stsp if (err)
3318 8e7bd50a 2019-08-22 stsp return err;
3319 8e7bd50a 2019-08-22 stsp
3320 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3321 8e7bd50a 2019-08-22 stsp const char *refname;
3322 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3323 8e7bd50a 2019-08-22 stsp char datebuf[26];
3324 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3325 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3326 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3327 8e7bd50a 2019-08-22 stsp
3328 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3329 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3330 8e7bd50a 2019-08-22 stsp continue;
3331 8e7bd50a 2019-08-22 stsp refname += 10;
3332 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3333 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3334 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3335 8e7bd50a 2019-08-22 stsp break;
3336 8e7bd50a 2019-08-22 stsp }
3337 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3338 8e7bd50a 2019-08-22 stsp free(refstr);
3339 8e7bd50a 2019-08-22 stsp
3340 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3341 8e7bd50a 2019-08-22 stsp if (err)
3342 8e7bd50a 2019-08-22 stsp break;
3343 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3344 8e7bd50a 2019-08-22 stsp free(id);
3345 8e7bd50a 2019-08-22 stsp if (err)
3346 8e7bd50a 2019-08-22 stsp break;
3347 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3348 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3349 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3350 2417344c 2019-08-23 stsp if (datestr)
3351 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3352 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3353 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3354 8e7bd50a 2019-08-22 stsp if (err)
3355 8e7bd50a 2019-08-22 stsp break;
3356 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3357 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3358 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3359 8e7bd50a 2019-08-22 stsp break;
3360 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3361 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3362 8e7bd50a 2019-08-22 stsp break;
3363 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3364 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3365 8e7bd50a 2019-08-22 stsp break;
3366 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3367 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3368 8e7bd50a 2019-08-22 stsp break;
3369 8e7bd50a 2019-08-22 stsp default:
3370 8e7bd50a 2019-08-22 stsp break;
3371 8e7bd50a 2019-08-22 stsp }
3372 8e7bd50a 2019-08-22 stsp free(id_str);
3373 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3374 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3375 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3376 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3377 8e7bd50a 2019-08-22 stsp break;
3378 8e7bd50a 2019-08-22 stsp }
3379 8e7bd50a 2019-08-22 stsp
3380 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3381 8e7bd50a 2019-08-22 stsp do {
3382 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3383 8e7bd50a 2019-08-22 stsp if (line)
3384 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3385 8e7bd50a 2019-08-22 stsp } while (line);
3386 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3387 8e7bd50a 2019-08-22 stsp }
3388 8e7bd50a 2019-08-22 stsp
3389 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3390 8e7bd50a 2019-08-22 stsp return NULL;
3391 8e7bd50a 2019-08-22 stsp }
3392 8e7bd50a 2019-08-22 stsp
3393 8e7bd50a 2019-08-22 stsp static const struct got_error *
3394 62870f63 2019-08-22 stsp get_tag_message(char **tagmsg, const char *commit_id_str,
3395 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3396 8e7bd50a 2019-08-22 stsp {
3397 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3398 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3399 8e7bd50a 2019-08-22 stsp char *tagmsg_path = NULL, *editor = NULL;
3400 8e7bd50a 2019-08-22 stsp int fd = -1;
3401 8e7bd50a 2019-08-22 stsp
3402 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3403 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3404 8e7bd50a 2019-08-22 stsp goto done;
3405 8e7bd50a 2019-08-22 stsp }
3406 8e7bd50a 2019-08-22 stsp
3407 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3408 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3409 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3410 8e7bd50a 2019-08-22 stsp goto done;
3411 8e7bd50a 2019-08-22 stsp }
3412 8e7bd50a 2019-08-22 stsp
3413 8e7bd50a 2019-08-22 stsp err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3414 8e7bd50a 2019-08-22 stsp if (err)
3415 8e7bd50a 2019-08-22 stsp goto done;
3416 8e7bd50a 2019-08-22 stsp
3417 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3418 8e7bd50a 2019-08-22 stsp close(fd);
3419 8e7bd50a 2019-08-22 stsp
3420 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3421 8e7bd50a 2019-08-22 stsp if (err)
3422 8e7bd50a 2019-08-22 stsp goto done;
3423 8e7bd50a 2019-08-22 stsp err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3424 8e7bd50a 2019-08-22 stsp done:
3425 8e7bd50a 2019-08-22 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3426 8e7bd50a 2019-08-22 stsp unlink(tagmsg_path);
3427 8e7bd50a 2019-08-22 stsp free(tagmsg_path);
3428 8e7bd50a 2019-08-22 stsp tagmsg_path = NULL;
3429 8e7bd50a 2019-08-22 stsp }
3430 8e7bd50a 2019-08-22 stsp free(initial_content);
3431 8e7bd50a 2019-08-22 stsp free(template);
3432 8e7bd50a 2019-08-22 stsp free(editor);
3433 8e7bd50a 2019-08-22 stsp
3434 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3435 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3436 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3437 8e7bd50a 2019-08-22 stsp if (err) {
3438 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3439 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3440 8e7bd50a 2019-08-22 stsp }
3441 8e7bd50a 2019-08-22 stsp }
3442 8e7bd50a 2019-08-22 stsp return err;
3443 8e7bd50a 2019-08-22 stsp }
3444 8e7bd50a 2019-08-22 stsp
3445 8e7bd50a 2019-08-22 stsp static const struct got_error *
3446 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3447 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3448 8e7bd50a 2019-08-22 stsp {
3449 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3450 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3451 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3452 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3453 8e7bd50a 2019-08-22 stsp char *refname = NULL, *tagmsg = NULL;
3454 8e7bd50a 2019-08-22 stsp const char *tagger;
3455 8e7bd50a 2019-08-22 stsp
3456 8e7bd50a 2019-08-22 stsp /*
3457 8e7bd50a 2019-08-22 stsp * Don't let the user create a tag named '-'.
3458 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3459 8e7bd50a 2019-08-22 stsp * an unintended typo.
3460 8e7bd50a 2019-08-22 stsp */
3461 8e7bd50a 2019-08-22 stsp if (tag_name[0] == '-' && tag_name[1] == '\0')
3462 8e7bd50a 2019-08-22 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3463 8e7bd50a 2019-08-22 stsp
3464 8e7bd50a 2019-08-22 stsp err = get_author(&tagger);
3465 8e7bd50a 2019-08-22 stsp if (err)
3466 8e7bd50a 2019-08-22 stsp return err;
3467 8e7bd50a 2019-08-22 stsp
3468 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3469 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3470 8e7bd50a 2019-08-22 stsp if (err)
3471 8e7bd50a 2019-08-22 stsp goto done;
3472 8e7bd50a 2019-08-22 stsp
3473 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3474 8e7bd50a 2019-08-22 stsp if (err)
3475 8e7bd50a 2019-08-22 stsp goto done;
3476 8e7bd50a 2019-08-22 stsp
3477 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3478 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3479 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3480 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3481 8e7bd50a 2019-08-22 stsp goto done;
3482 8e7bd50a 2019-08-22 stsp }
3483 8e7bd50a 2019-08-22 stsp tag_name += 10;
3484 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3485 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3486 8e7bd50a 2019-08-22 stsp goto done;
3487 8e7bd50a 2019-08-22 stsp }
3488 8e7bd50a 2019-08-22 stsp
3489 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3490 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3491 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3492 8e7bd50a 2019-08-22 stsp goto done;
3493 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3494 8e7bd50a 2019-08-22 stsp goto done;
3495 8e7bd50a 2019-08-22 stsp
3496 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3497 8e7bd50a 2019-08-22 stsp err = get_tag_message(&tagmsg, commit_id_str,
3498 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3499 8e7bd50a 2019-08-22 stsp if (err)
3500 8e7bd50a 2019-08-22 stsp goto done;
3501 8e7bd50a 2019-08-22 stsp }
3502 8e7bd50a 2019-08-22 stsp
3503 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3504 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3505 8e7bd50a 2019-08-22 stsp if (err)
3506 8e7bd50a 2019-08-22 stsp goto done;
3507 8e7bd50a 2019-08-22 stsp
3508 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3509 8e7bd50a 2019-08-22 stsp if (err)
3510 8e7bd50a 2019-08-22 stsp goto done;
3511 8e7bd50a 2019-08-22 stsp
3512 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3513 8e7bd50a 2019-08-22 stsp
3514 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3515 8e7bd50a 2019-08-22 stsp char *tag_id_str;
3516 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&tag_id_str, tag_id);
3517 8e7bd50a 2019-08-22 stsp printf("Created tag %s\n", tag_id_str);
3518 8e7bd50a 2019-08-22 stsp free(tag_id_str);
3519 8e7bd50a 2019-08-22 stsp }
3520 8e7bd50a 2019-08-22 stsp done:
3521 8e7bd50a 2019-08-22 stsp if (ref)
3522 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3523 8e7bd50a 2019-08-22 stsp free(commit_id);
3524 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3525 8e7bd50a 2019-08-22 stsp free(refname);
3526 8e7bd50a 2019-08-22 stsp free(tagmsg);
3527 8e7bd50a 2019-08-22 stsp return err;
3528 8e7bd50a 2019-08-22 stsp }
3529 8e7bd50a 2019-08-22 stsp
3530 8e7bd50a 2019-08-22 stsp static const struct got_error *
3531 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3532 8e7bd50a 2019-08-22 stsp {
3533 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3534 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3535 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
3536 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3537 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3538 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
3539 8e7bd50a 2019-08-22 stsp
3540 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3541 8e7bd50a 2019-08-22 stsp switch (ch) {
3542 8e7bd50a 2019-08-22 stsp case 'm':
3543 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
3544 8e7bd50a 2019-08-22 stsp break;
3545 8e7bd50a 2019-08-22 stsp case 'r':
3546 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
3547 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3548 8e7bd50a 2019-08-22 stsp err(1, "-r option");
3549 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
3550 8e7bd50a 2019-08-22 stsp break;
3551 8e7bd50a 2019-08-22 stsp case 'l':
3552 8e7bd50a 2019-08-22 stsp do_list = 1;
3553 8e7bd50a 2019-08-22 stsp break;
3554 8e7bd50a 2019-08-22 stsp default:
3555 8e7bd50a 2019-08-22 stsp usage_tag();
3556 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
3557 8e7bd50a 2019-08-22 stsp }
3558 8e7bd50a 2019-08-22 stsp }
3559 8e7bd50a 2019-08-22 stsp
3560 8e7bd50a 2019-08-22 stsp argc -= optind;
3561 8e7bd50a 2019-08-22 stsp argv += optind;
3562 8e7bd50a 2019-08-22 stsp
3563 8e7bd50a 2019-08-22 stsp if (do_list) {
3564 8e7bd50a 2019-08-22 stsp if (tagmsg)
3565 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
3566 8e7bd50a 2019-08-22 stsp if (argc > 0)
3567 8e7bd50a 2019-08-22 stsp usage_tag();
3568 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
3569 8e7bd50a 2019-08-22 stsp usage_tag();
3570 a2887370 2019-08-23 stsp else if (argc > 1)
3571 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
3572 a2887370 2019-08-23 stsp tag_name = argv[0];
3573 8e7bd50a 2019-08-22 stsp
3574 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
3575 8e7bd50a 2019-08-22 stsp if (do_list) {
3576 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3577 8e7bd50a 2019-08-22 stsp NULL) == -1)
3578 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3579 8e7bd50a 2019-08-22 stsp } else {
3580 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3581 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
3582 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3583 8e7bd50a 2019-08-22 stsp }
3584 8e7bd50a 2019-08-22 stsp #endif
3585 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
3586 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
3587 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
3588 8e7bd50a 2019-08-22 stsp goto done;
3589 8e7bd50a 2019-08-22 stsp }
3590 8e7bd50a 2019-08-22 stsp
3591 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
3592 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
3593 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3594 8e7bd50a 2019-08-22 stsp goto done;
3595 8e7bd50a 2019-08-22 stsp else
3596 8e7bd50a 2019-08-22 stsp error = NULL;
3597 8e7bd50a 2019-08-22 stsp if (worktree) {
3598 8e7bd50a 2019-08-22 stsp repo_path =
3599 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
3600 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3601 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
3602 8e7bd50a 2019-08-22 stsp if (error)
3603 8e7bd50a 2019-08-22 stsp goto done;
3604 8e7bd50a 2019-08-22 stsp } else {
3605 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
3606 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
3607 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
3608 8e7bd50a 2019-08-22 stsp goto done;
3609 8e7bd50a 2019-08-22 stsp }
3610 8e7bd50a 2019-08-22 stsp }
3611 8e7bd50a 2019-08-22 stsp }
3612 8e7bd50a 2019-08-22 stsp
3613 8e7bd50a 2019-08-22 stsp error = got_repo_open(&repo, repo_path);
3614 8e7bd50a 2019-08-22 stsp if (error != NULL)
3615 8e7bd50a 2019-08-22 stsp goto done;
3616 8e7bd50a 2019-08-22 stsp
3617 8e7bd50a 2019-08-22 stsp
3618 8e7bd50a 2019-08-22 stsp if (do_list) {
3619 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3620 8e7bd50a 2019-08-22 stsp if (error)
3621 8e7bd50a 2019-08-22 stsp goto done;
3622 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
3623 8e7bd50a 2019-08-22 stsp } else {
3624 8e7bd50a 2019-08-22 stsp if (tagmsg) {
3625 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3626 8e7bd50a 2019-08-22 stsp if (error)
3627 8e7bd50a 2019-08-22 stsp goto done;
3628 8e7bd50a 2019-08-22 stsp }
3629 8e7bd50a 2019-08-22 stsp
3630 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
3631 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
3632 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
3633 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
3634 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3635 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
3636 8e7bd50a 2019-08-22 stsp if (error)
3637 8e7bd50a 2019-08-22 stsp goto done;
3638 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3639 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
3640 8e7bd50a 2019-08-22 stsp if (error)
3641 8e7bd50a 2019-08-22 stsp goto done;
3642 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
3643 8e7bd50a 2019-08-22 stsp free(commit_id);
3644 8e7bd50a 2019-08-22 stsp if (error)
3645 8e7bd50a 2019-08-22 stsp goto done;
3646 8e7bd50a 2019-08-22 stsp }
3647 8e7bd50a 2019-08-22 stsp
3648 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
3649 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3650 8e7bd50a 2019-08-22 stsp }
3651 8e7bd50a 2019-08-22 stsp done:
3652 8e7bd50a 2019-08-22 stsp if (repo)
3653 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
3654 8e7bd50a 2019-08-22 stsp if (worktree)
3655 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
3656 8e7bd50a 2019-08-22 stsp free(cwd);
3657 8e7bd50a 2019-08-22 stsp free(repo_path);
3658 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3659 8e7bd50a 2019-08-22 stsp return error;
3660 8e7bd50a 2019-08-22 stsp }
3661 8e7bd50a 2019-08-22 stsp
3662 8e7bd50a 2019-08-22 stsp __dead static void
3663 d00136be 2019-03-26 stsp usage_add(void)
3664 d00136be 2019-03-26 stsp {
3665 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3666 d00136be 2019-03-26 stsp exit(1);
3667 d00136be 2019-03-26 stsp }
3668 d00136be 2019-03-26 stsp
3669 d00136be 2019-03-26 stsp static const struct got_error *
3670 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
3671 d00136be 2019-03-26 stsp {
3672 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
3673 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
3674 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
3675 1dd54920 2019-05-11 stsp char *cwd = NULL;
3676 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
3677 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3678 6d022e97 2019-08-04 stsp int ch;
3679 1dd54920 2019-05-11 stsp
3680 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
3681 d00136be 2019-03-26 stsp
3682 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3683 d00136be 2019-03-26 stsp switch (ch) {
3684 d00136be 2019-03-26 stsp default:
3685 d00136be 2019-03-26 stsp usage_add();
3686 d00136be 2019-03-26 stsp /* NOTREACHED */
3687 d00136be 2019-03-26 stsp }
3688 d00136be 2019-03-26 stsp }
3689 d00136be 2019-03-26 stsp
3690 d00136be 2019-03-26 stsp argc -= optind;
3691 d00136be 2019-03-26 stsp argv += optind;
3692 d00136be 2019-03-26 stsp
3693 43012d58 2019-07-14 stsp #ifndef PROFILE
3694 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3695 43012d58 2019-07-14 stsp NULL) == -1)
3696 43012d58 2019-07-14 stsp err(1, "pledge");
3697 43012d58 2019-07-14 stsp #endif
3698 723c305c 2019-05-11 jcs if (argc < 1)
3699 d00136be 2019-03-26 stsp usage_add();
3700 d00136be 2019-03-26 stsp
3701 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
3702 d00136be 2019-03-26 stsp if (cwd == NULL) {
3703 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3704 d00136be 2019-03-26 stsp goto done;
3705 d00136be 2019-03-26 stsp }
3706 723c305c 2019-05-11 jcs
3707 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3708 d00136be 2019-03-26 stsp if (error)
3709 d00136be 2019-03-26 stsp goto done;
3710 d00136be 2019-03-26 stsp
3711 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3712 031a5338 2019-03-26 stsp if (error != NULL)
3713 031a5338 2019-03-26 stsp goto done;
3714 031a5338 2019-03-26 stsp
3715 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3716 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3717 d00136be 2019-03-26 stsp if (error)
3718 d00136be 2019-03-26 stsp goto done;
3719 d00136be 2019-03-26 stsp
3720 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3721 6d022e97 2019-08-04 stsp if (error)
3722 6d022e97 2019-08-04 stsp goto done;
3723 723c305c 2019-05-11 jcs
3724 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3725 1dd54920 2019-05-11 stsp NULL, repo);
3726 d00136be 2019-03-26 stsp done:
3727 031a5338 2019-03-26 stsp if (repo)
3728 031a5338 2019-03-26 stsp got_repo_close(repo);
3729 d00136be 2019-03-26 stsp if (worktree)
3730 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3731 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3732 1dd54920 2019-05-11 stsp free((char *)pe->path);
3733 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3734 2ec1f75b 2019-03-26 stsp free(cwd);
3735 2ec1f75b 2019-03-26 stsp return error;
3736 2ec1f75b 2019-03-26 stsp }
3737 2ec1f75b 2019-03-26 stsp
3738 2ec1f75b 2019-03-26 stsp __dead static void
3739 648e4ef7 2019-07-09 stsp usage_remove(void)
3740 2ec1f75b 2019-03-26 stsp {
3741 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3742 2ec1f75b 2019-03-26 stsp exit(1);
3743 2ec1f75b 2019-03-26 stsp }
3744 2ec1f75b 2019-03-26 stsp
3745 2ec1f75b 2019-03-26 stsp static const struct got_error *
3746 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3747 2ec1f75b 2019-03-26 stsp {
3748 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3749 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3750 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3751 17ed4618 2019-06-02 stsp char *cwd = NULL;
3752 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3753 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3754 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
3755 2ec1f75b 2019-03-26 stsp
3756 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3757 17ed4618 2019-06-02 stsp
3758 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3759 2ec1f75b 2019-03-26 stsp switch (ch) {
3760 2ec1f75b 2019-03-26 stsp case 'f':
3761 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3762 2ec1f75b 2019-03-26 stsp break;
3763 2ec1f75b 2019-03-26 stsp default:
3764 2ec1f75b 2019-03-26 stsp usage_add();
3765 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3766 2ec1f75b 2019-03-26 stsp }
3767 2ec1f75b 2019-03-26 stsp }
3768 2ec1f75b 2019-03-26 stsp
3769 2ec1f75b 2019-03-26 stsp argc -= optind;
3770 2ec1f75b 2019-03-26 stsp argv += optind;
3771 2ec1f75b 2019-03-26 stsp
3772 43012d58 2019-07-14 stsp #ifndef PROFILE
3773 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3774 43012d58 2019-07-14 stsp NULL) == -1)
3775 43012d58 2019-07-14 stsp err(1, "pledge");
3776 43012d58 2019-07-14 stsp #endif
3777 17ed4618 2019-06-02 stsp if (argc < 1)
3778 648e4ef7 2019-07-09 stsp usage_remove();
3779 2ec1f75b 2019-03-26 stsp
3780 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3781 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3782 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3783 2ec1f75b 2019-03-26 stsp goto done;
3784 2ec1f75b 2019-03-26 stsp }
3785 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3786 2ec1f75b 2019-03-26 stsp if (error)
3787 2ec1f75b 2019-03-26 stsp goto done;
3788 2ec1f75b 2019-03-26 stsp
3789 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3790 2af4a041 2019-05-11 jcs if (error)
3791 2ec1f75b 2019-03-26 stsp goto done;
3792 2ec1f75b 2019-03-26 stsp
3793 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3794 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3795 2ec1f75b 2019-03-26 stsp if (error)
3796 2ec1f75b 2019-03-26 stsp goto done;
3797 2ec1f75b 2019-03-26 stsp
3798 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3799 6d022e97 2019-08-04 stsp if (error)
3800 6d022e97 2019-08-04 stsp goto done;
3801 17ed4618 2019-06-02 stsp
3802 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3803 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3804 a129376b 2019-03-28 stsp if (error)
3805 a129376b 2019-03-28 stsp goto done;
3806 a129376b 2019-03-28 stsp done:
3807 a129376b 2019-03-28 stsp if (repo)
3808 a129376b 2019-03-28 stsp got_repo_close(repo);
3809 a129376b 2019-03-28 stsp if (worktree)
3810 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3811 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3812 17ed4618 2019-06-02 stsp free((char *)pe->path);
3813 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3814 a129376b 2019-03-28 stsp free(cwd);
3815 a129376b 2019-03-28 stsp return error;
3816 a129376b 2019-03-28 stsp }
3817 a129376b 2019-03-28 stsp
3818 a129376b 2019-03-28 stsp __dead static void
3819 a129376b 2019-03-28 stsp usage_revert(void)
3820 a129376b 2019-03-28 stsp {
3821 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3822 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
3823 a129376b 2019-03-28 stsp exit(1);
3824 a129376b 2019-03-28 stsp }
3825 a129376b 2019-03-28 stsp
3826 1ee397ad 2019-07-12 stsp static const struct got_error *
3827 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3828 a129376b 2019-03-28 stsp {
3829 a129376b 2019-03-28 stsp while (path[0] == '/')
3830 a129376b 2019-03-28 stsp path++;
3831 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3832 33aa809d 2019-08-08 stsp return NULL;
3833 33aa809d 2019-08-08 stsp }
3834 33aa809d 2019-08-08 stsp
3835 33aa809d 2019-08-08 stsp struct choose_patch_arg {
3836 33aa809d 2019-08-08 stsp FILE *patch_script_file;
3837 33aa809d 2019-08-08 stsp const char *action;
3838 33aa809d 2019-08-08 stsp };
3839 33aa809d 2019-08-08 stsp
3840 33aa809d 2019-08-08 stsp static const struct got_error *
3841 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3842 33aa809d 2019-08-08 stsp int nchanges, const char *action)
3843 33aa809d 2019-08-08 stsp {
3844 33aa809d 2019-08-08 stsp char *line = NULL;
3845 33aa809d 2019-08-08 stsp size_t linesize = 0;
3846 33aa809d 2019-08-08 stsp ssize_t linelen;
3847 33aa809d 2019-08-08 stsp
3848 33aa809d 2019-08-08 stsp switch (status) {
3849 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
3850 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
3851 33aa809d 2019-08-08 stsp break;
3852 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
3853 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
3854 33aa809d 2019-08-08 stsp break;
3855 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
3856 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
3857 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
3858 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3859 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3860 33aa809d 2019-08-08 stsp printf("%s", line);
3861 33aa809d 2019-08-08 stsp if (ferror(patch_file))
3862 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3863 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3864 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3865 33aa809d 2019-08-08 stsp path, n, nchanges, action);
3866 33aa809d 2019-08-08 stsp break;
3867 33aa809d 2019-08-08 stsp default:
3868 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
3869 33aa809d 2019-08-08 stsp }
3870 33aa809d 2019-08-08 stsp
3871 33aa809d 2019-08-08 stsp return NULL;
3872 33aa809d 2019-08-08 stsp }
3873 33aa809d 2019-08-08 stsp
3874 33aa809d 2019-08-08 stsp static const struct got_error *
3875 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3876 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
3877 33aa809d 2019-08-08 stsp {
3878 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3879 33aa809d 2019-08-08 stsp char *line = NULL;
3880 33aa809d 2019-08-08 stsp size_t linesize = 0;
3881 33aa809d 2019-08-08 stsp ssize_t linelen;
3882 33aa809d 2019-08-08 stsp int resp = ' ';
3883 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
3884 33aa809d 2019-08-08 stsp
3885 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3886 33aa809d 2019-08-08 stsp
3887 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
3888 33aa809d 2019-08-08 stsp char *nl;
3889 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3890 33aa809d 2019-08-08 stsp a->action);
3891 33aa809d 2019-08-08 stsp if (err)
3892 33aa809d 2019-08-08 stsp return err;
3893 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
3894 33aa809d 2019-08-08 stsp if (linelen == -1) {
3895 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
3896 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3897 33aa809d 2019-08-08 stsp return NULL;
3898 33aa809d 2019-08-08 stsp }
3899 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
3900 33aa809d 2019-08-08 stsp if (nl)
3901 33aa809d 2019-08-08 stsp *nl = '\0';
3902 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
3903 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3904 33aa809d 2019-08-08 stsp printf("y\n");
3905 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
3906 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3907 33aa809d 2019-08-08 stsp printf("n\n");
3908 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
3909 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
3910 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3911 33aa809d 2019-08-08 stsp printf("q\n");
3912 33aa809d 2019-08-08 stsp } else
3913 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
3914 33aa809d 2019-08-08 stsp free(line);
3915 33aa809d 2019-08-08 stsp return NULL;
3916 33aa809d 2019-08-08 stsp }
3917 33aa809d 2019-08-08 stsp
3918 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
3919 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3920 33aa809d 2019-08-08 stsp a->action);
3921 33aa809d 2019-08-08 stsp if (err)
3922 33aa809d 2019-08-08 stsp return err;
3923 33aa809d 2019-08-08 stsp resp = getchar();
3924 33aa809d 2019-08-08 stsp if (resp == '\n')
3925 33aa809d 2019-08-08 stsp resp = getchar();
3926 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
3927 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
3928 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3929 33aa809d 2019-08-08 stsp resp = ' ';
3930 33aa809d 2019-08-08 stsp }
3931 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
3932 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3933 33aa809d 2019-08-08 stsp resp = ' ';
3934 33aa809d 2019-08-08 stsp }
3935 33aa809d 2019-08-08 stsp }
3936 33aa809d 2019-08-08 stsp
3937 33aa809d 2019-08-08 stsp if (resp == 'y')
3938 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3939 33aa809d 2019-08-08 stsp else if (resp == 'n')
3940 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3941 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3942 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3943 33aa809d 2019-08-08 stsp
3944 1ee397ad 2019-07-12 stsp return NULL;
3945 a129376b 2019-03-28 stsp }
3946 a129376b 2019-03-28 stsp
3947 33aa809d 2019-08-08 stsp
3948 a129376b 2019-03-28 stsp static const struct got_error *
3949 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3950 a129376b 2019-03-28 stsp {
3951 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3952 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3953 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3954 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3955 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3956 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
3957 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
3958 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
3959 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
3960 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
3961 a129376b 2019-03-28 stsp
3962 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3963 e20a8b6f 2019-06-04 stsp
3964 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3965 a129376b 2019-03-28 stsp switch (ch) {
3966 33aa809d 2019-08-08 stsp case 'p':
3967 33aa809d 2019-08-08 stsp pflag = 1;
3968 33aa809d 2019-08-08 stsp break;
3969 33aa809d 2019-08-08 stsp case 'F':
3970 33aa809d 2019-08-08 stsp patch_script_path = optarg;
3971 33aa809d 2019-08-08 stsp break;
3972 0f6d7415 2019-08-08 stsp case 'R':
3973 0f6d7415 2019-08-08 stsp can_recurse = 1;
3974 0f6d7415 2019-08-08 stsp break;
3975 a129376b 2019-03-28 stsp default:
3976 a129376b 2019-03-28 stsp usage_revert();
3977 a129376b 2019-03-28 stsp /* NOTREACHED */
3978 a129376b 2019-03-28 stsp }
3979 a129376b 2019-03-28 stsp }
3980 a129376b 2019-03-28 stsp
3981 a129376b 2019-03-28 stsp argc -= optind;
3982 a129376b 2019-03-28 stsp argv += optind;
3983 a129376b 2019-03-28 stsp
3984 43012d58 2019-07-14 stsp #ifndef PROFILE
3985 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3986 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3987 43012d58 2019-07-14 stsp err(1, "pledge");
3988 43012d58 2019-07-14 stsp #endif
3989 e20a8b6f 2019-06-04 stsp if (argc < 1)
3990 a129376b 2019-03-28 stsp usage_revert();
3991 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
3992 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
3993 a129376b 2019-03-28 stsp
3994 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3995 a129376b 2019-03-28 stsp if (cwd == NULL) {
3996 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3997 a129376b 2019-03-28 stsp goto done;
3998 a129376b 2019-03-28 stsp }
3999 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4000 2ec1f75b 2019-03-26 stsp if (error)
4001 2ec1f75b 2019-03-26 stsp goto done;
4002 a129376b 2019-03-28 stsp
4003 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4004 a129376b 2019-03-28 stsp if (error != NULL)
4005 a129376b 2019-03-28 stsp goto done;
4006 a129376b 2019-03-28 stsp
4007 33aa809d 2019-08-08 stsp if (patch_script_path) {
4008 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4009 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4010 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4011 33aa809d 2019-08-08 stsp patch_script_path);
4012 33aa809d 2019-08-08 stsp goto done;
4013 33aa809d 2019-08-08 stsp }
4014 33aa809d 2019-08-08 stsp }
4015 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4016 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4017 a129376b 2019-03-28 stsp if (error)
4018 a129376b 2019-03-28 stsp goto done;
4019 a129376b 2019-03-28 stsp
4020 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4021 6d022e97 2019-08-04 stsp if (error)
4022 6d022e97 2019-08-04 stsp goto done;
4023 00db391e 2019-08-03 stsp
4024 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4025 0f6d7415 2019-08-08 stsp char *ondisk_path;
4026 0f6d7415 2019-08-08 stsp struct stat sb;
4027 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4028 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4029 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4030 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4031 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4032 0f6d7415 2019-08-08 stsp goto done;
4033 0f6d7415 2019-08-08 stsp }
4034 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4035 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4036 0f6d7415 2019-08-08 stsp free(ondisk_path);
4037 0f6d7415 2019-08-08 stsp continue;
4038 0f6d7415 2019-08-08 stsp }
4039 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4040 0f6d7415 2019-08-08 stsp ondisk_path);
4041 0f6d7415 2019-08-08 stsp free(ondisk_path);
4042 0f6d7415 2019-08-08 stsp goto done;
4043 0f6d7415 2019-08-08 stsp }
4044 0f6d7415 2019-08-08 stsp free(ondisk_path);
4045 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4046 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4047 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4048 0f6d7415 2019-08-08 stsp goto done;
4049 0f6d7415 2019-08-08 stsp }
4050 0f6d7415 2019-08-08 stsp }
4051 0f6d7415 2019-08-08 stsp }
4052 0f6d7415 2019-08-08 stsp
4053 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4054 33aa809d 2019-08-08 stsp cpa.action = "revert";
4055 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4056 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4057 a129376b 2019-03-28 stsp if (error)
4058 a129376b 2019-03-28 stsp goto done;
4059 2ec1f75b 2019-03-26 stsp done:
4060 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4061 33aa809d 2019-08-08 stsp error == NULL)
4062 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4063 2ec1f75b 2019-03-26 stsp if (repo)
4064 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4065 2ec1f75b 2019-03-26 stsp if (worktree)
4066 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4067 2ec1f75b 2019-03-26 stsp free(path);
4068 d00136be 2019-03-26 stsp free(cwd);
4069 6bad629b 2019-02-04 stsp return error;
4070 c4296144 2019-05-09 stsp }
4071 c4296144 2019-05-09 stsp
4072 c4296144 2019-05-09 stsp __dead static void
4073 c4296144 2019-05-09 stsp usage_commit(void)
4074 c4296144 2019-05-09 stsp {
4075 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4076 5c1e53bc 2019-07-28 stsp getprogname());
4077 c4296144 2019-05-09 stsp exit(1);
4078 33ad4cbe 2019-05-12 jcs }
4079 33ad4cbe 2019-05-12 jcs
4080 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4081 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4082 e2ba3d07 2019-05-13 stsp const char *editor;
4083 e0870e44 2019-05-13 stsp const char *worktree_path;
4084 76d98825 2019-06-03 stsp const char *branch_name;
4085 314a6357 2019-05-13 stsp const char *repo_path;
4086 e0870e44 2019-05-13 stsp char *logmsg_path;
4087 e2ba3d07 2019-05-13 stsp
4088 e2ba3d07 2019-05-13 stsp };
4089 e0870e44 2019-05-13 stsp
4090 33ad4cbe 2019-05-12 jcs static const struct got_error *
4091 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4092 33ad4cbe 2019-05-12 jcs void *arg)
4093 33ad4cbe 2019-05-12 jcs {
4094 76d98825 2019-06-03 stsp char *initial_content = NULL;
4095 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4096 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4097 e0870e44 2019-05-13 stsp char *template = NULL;
4098 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4099 3ce1b845 2019-07-15 stsp int fd;
4100 33ad4cbe 2019-05-12 jcs size_t len;
4101 33ad4cbe 2019-05-12 jcs
4102 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4103 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4104 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4105 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4106 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4107 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4108 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4109 33ad4cbe 2019-05-12 jcs return NULL;
4110 33ad4cbe 2019-05-12 jcs }
4111 33ad4cbe 2019-05-12 jcs
4112 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4113 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4114 76d98825 2019-06-03 stsp
4115 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4116 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4117 76d98825 2019-06-03 stsp a->branch_name) == -1)
4118 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4119 e0870e44 2019-05-13 stsp
4120 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4121 793c30b5 2019-05-13 stsp if (err)
4122 e0870e44 2019-05-13 stsp goto done;
4123 33ad4cbe 2019-05-12 jcs
4124 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4125 33ad4cbe 2019-05-12 jcs
4126 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4127 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4128 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4129 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4130 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4131 33ad4cbe 2019-05-12 jcs }
4132 33ad4cbe 2019-05-12 jcs close(fd);
4133 33ad4cbe 2019-05-12 jcs
4134 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4135 33ad4cbe 2019-05-12 jcs done:
4136 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4137 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
4138 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
4139 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
4140 e2af0fd1 2019-08-08 stsp }
4141 76d98825 2019-06-03 stsp free(initial_content);
4142 e0870e44 2019-05-13 stsp free(template);
4143 314a6357 2019-05-13 stsp
4144 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4145 3ce1b845 2019-07-15 stsp if (err == NULL) {
4146 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4147 3ce1b845 2019-07-15 stsp if (err) {
4148 3ce1b845 2019-07-15 stsp free(*logmsg);
4149 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4150 3ce1b845 2019-07-15 stsp }
4151 3ce1b845 2019-07-15 stsp }
4152 33ad4cbe 2019-05-12 jcs return err;
4153 6bad629b 2019-02-04 stsp }
4154 c4296144 2019-05-09 stsp
4155 c4296144 2019-05-09 stsp static const struct got_error *
4156 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4157 c4296144 2019-05-09 stsp {
4158 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4159 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4160 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4161 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4162 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4163 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4164 84792843 2019-08-09 stsp const char *author;
4165 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
4166 e2ba3d07 2019-05-13 stsp char *editor = NULL;
4167 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
4168 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4169 5c1e53bc 2019-07-28 stsp
4170 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4171 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4172 c4296144 2019-05-09 stsp
4173 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4174 c4296144 2019-05-09 stsp switch (ch) {
4175 c4296144 2019-05-09 stsp case 'm':
4176 c4296144 2019-05-09 stsp logmsg = optarg;
4177 c4296144 2019-05-09 stsp break;
4178 c4296144 2019-05-09 stsp default:
4179 c4296144 2019-05-09 stsp usage_commit();
4180 c4296144 2019-05-09 stsp /* NOTREACHED */
4181 c4296144 2019-05-09 stsp }
4182 c4296144 2019-05-09 stsp }
4183 c4296144 2019-05-09 stsp
4184 c4296144 2019-05-09 stsp argc -= optind;
4185 c4296144 2019-05-09 stsp argv += optind;
4186 c4296144 2019-05-09 stsp
4187 43012d58 2019-07-14 stsp #ifndef PROFILE
4188 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4189 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4190 43012d58 2019-07-14 stsp err(1, "pledge");
4191 43012d58 2019-07-14 stsp #endif
4192 84792843 2019-08-09 stsp error = get_author(&author);
4193 84792843 2019-08-09 stsp if (error)
4194 84792843 2019-08-09 stsp return error;
4195 c4296144 2019-05-09 stsp
4196 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4197 c4296144 2019-05-09 stsp if (cwd == NULL) {
4198 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4199 c4296144 2019-05-09 stsp goto done;
4200 c4296144 2019-05-09 stsp }
4201 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4202 7d5807f4 2019-07-11 stsp if (error)
4203 7d5807f4 2019-07-11 stsp goto done;
4204 7d5807f4 2019-07-11 stsp
4205 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4206 c4296144 2019-05-09 stsp if (error)
4207 a698f62e 2019-07-25 stsp goto done;
4208 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4209 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4210 c4296144 2019-05-09 stsp goto done;
4211 a698f62e 2019-07-25 stsp }
4212 5c1e53bc 2019-07-28 stsp
4213 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4214 916f288c 2019-07-30 stsp worktree);
4215 5c1e53bc 2019-07-28 stsp if (error)
4216 5c1e53bc 2019-07-28 stsp goto done;
4217 c4296144 2019-05-09 stsp
4218 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4219 c4296144 2019-05-09 stsp if (error != NULL)
4220 c4296144 2019-05-09 stsp goto done;
4221 c4296144 2019-05-09 stsp
4222 314a6357 2019-05-13 stsp /*
4223 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4224 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4225 314a6357 2019-05-13 stsp */
4226 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4227 314a6357 2019-05-13 stsp error = get_editor(&editor);
4228 314a6357 2019-05-13 stsp else
4229 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4230 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4231 c4296144 2019-05-09 stsp if (error)
4232 c4296144 2019-05-09 stsp goto done;
4233 c4296144 2019-05-09 stsp
4234 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4235 087fb88c 2019-08-04 stsp if (error)
4236 087fb88c 2019-08-04 stsp goto done;
4237 087fb88c 2019-08-04 stsp
4238 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4239 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4240 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4241 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4242 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4243 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4244 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4245 916f288c 2019-07-30 stsp goto done;
4246 916f288c 2019-07-30 stsp }
4247 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4248 916f288c 2019-07-30 stsp }
4249 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4250 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4251 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4252 e0870e44 2019-05-13 stsp if (error) {
4253 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
4254 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4255 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
4256 c4296144 2019-05-09 stsp goto done;
4257 e0870e44 2019-05-13 stsp }
4258 c4296144 2019-05-09 stsp
4259 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
4260 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
4261 e0870e44 2019-05-13 stsp
4262 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4263 c4296144 2019-05-09 stsp if (error)
4264 c4296144 2019-05-09 stsp goto done;
4265 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4266 c4296144 2019-05-09 stsp done:
4267 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4268 c4296144 2019-05-09 stsp if (repo)
4269 c4296144 2019-05-09 stsp got_repo_close(repo);
4270 c4296144 2019-05-09 stsp if (worktree)
4271 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4272 c4296144 2019-05-09 stsp free(cwd);
4273 c4296144 2019-05-09 stsp free(id_str);
4274 e2ba3d07 2019-05-13 stsp free(editor);
4275 234035bc 2019-06-01 stsp return error;
4276 234035bc 2019-06-01 stsp }
4277 234035bc 2019-06-01 stsp
4278 234035bc 2019-06-01 stsp __dead static void
4279 234035bc 2019-06-01 stsp usage_cherrypick(void)
4280 234035bc 2019-06-01 stsp {
4281 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4282 234035bc 2019-06-01 stsp exit(1);
4283 234035bc 2019-06-01 stsp }
4284 234035bc 2019-06-01 stsp
4285 234035bc 2019-06-01 stsp static const struct got_error *
4286 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4287 234035bc 2019-06-01 stsp {
4288 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4289 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4290 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4291 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4292 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4293 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4294 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4295 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4296 234035bc 2019-06-01 stsp int ch, did_something = 0;
4297 234035bc 2019-06-01 stsp
4298 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4299 234035bc 2019-06-01 stsp switch (ch) {
4300 234035bc 2019-06-01 stsp default:
4301 234035bc 2019-06-01 stsp usage_cherrypick();
4302 234035bc 2019-06-01 stsp /* NOTREACHED */
4303 234035bc 2019-06-01 stsp }
4304 234035bc 2019-06-01 stsp }
4305 234035bc 2019-06-01 stsp
4306 234035bc 2019-06-01 stsp argc -= optind;
4307 234035bc 2019-06-01 stsp argv += optind;
4308 234035bc 2019-06-01 stsp
4309 43012d58 2019-07-14 stsp #ifndef PROFILE
4310 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4311 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4312 43012d58 2019-07-14 stsp err(1, "pledge");
4313 43012d58 2019-07-14 stsp #endif
4314 234035bc 2019-06-01 stsp if (argc != 1)
4315 234035bc 2019-06-01 stsp usage_cherrypick();
4316 234035bc 2019-06-01 stsp
4317 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4318 234035bc 2019-06-01 stsp if (cwd == NULL) {
4319 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4320 234035bc 2019-06-01 stsp goto done;
4321 234035bc 2019-06-01 stsp }
4322 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4323 234035bc 2019-06-01 stsp if (error)
4324 234035bc 2019-06-01 stsp goto done;
4325 234035bc 2019-06-01 stsp
4326 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4327 234035bc 2019-06-01 stsp if (error != NULL)
4328 234035bc 2019-06-01 stsp goto done;
4329 234035bc 2019-06-01 stsp
4330 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4331 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4332 234035bc 2019-06-01 stsp if (error)
4333 234035bc 2019-06-01 stsp goto done;
4334 234035bc 2019-06-01 stsp
4335 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4336 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4337 234035bc 2019-06-01 stsp if (error != NULL) {
4338 234035bc 2019-06-01 stsp struct got_reference *ref;
4339 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4340 234035bc 2019-06-01 stsp goto done;
4341 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4342 234035bc 2019-06-01 stsp if (error != NULL)
4343 234035bc 2019-06-01 stsp goto done;
4344 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4345 234035bc 2019-06-01 stsp got_ref_close(ref);
4346 234035bc 2019-06-01 stsp if (error != NULL)
4347 234035bc 2019-06-01 stsp goto done;
4348 234035bc 2019-06-01 stsp }
4349 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4350 234035bc 2019-06-01 stsp if (error)
4351 234035bc 2019-06-01 stsp goto done;
4352 234035bc 2019-06-01 stsp
4353 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4354 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4355 234035bc 2019-06-01 stsp if (error != NULL)
4356 234035bc 2019-06-01 stsp goto done;
4357 234035bc 2019-06-01 stsp
4358 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4359 234035bc 2019-06-01 stsp if (error) {
4360 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4361 234035bc 2019-06-01 stsp goto done;
4362 234035bc 2019-06-01 stsp error = NULL;
4363 234035bc 2019-06-01 stsp } else {
4364 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4365 234035bc 2019-06-01 stsp goto done;
4366 234035bc 2019-06-01 stsp }
4367 234035bc 2019-06-01 stsp
4368 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4369 234035bc 2019-06-01 stsp if (error)
4370 234035bc 2019-06-01 stsp goto done;
4371 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4372 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4373 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4374 03415a1a 2019-06-02 stsp NULL);
4375 234035bc 2019-06-01 stsp if (error != NULL)
4376 234035bc 2019-06-01 stsp goto done;
4377 234035bc 2019-06-01 stsp
4378 234035bc 2019-06-01 stsp if (did_something)
4379 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4380 234035bc 2019-06-01 stsp done:
4381 234035bc 2019-06-01 stsp if (commit)
4382 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4383 234035bc 2019-06-01 stsp free(commit_id_str);
4384 234035bc 2019-06-01 stsp if (head_ref)
4385 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4386 234035bc 2019-06-01 stsp if (worktree)
4387 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4388 234035bc 2019-06-01 stsp if (repo)
4389 234035bc 2019-06-01 stsp got_repo_close(repo);
4390 c4296144 2019-05-09 stsp return error;
4391 c4296144 2019-05-09 stsp }
4392 5ef14e63 2019-06-02 stsp
4393 5ef14e63 2019-06-02 stsp __dead static void
4394 5ef14e63 2019-06-02 stsp usage_backout(void)
4395 5ef14e63 2019-06-02 stsp {
4396 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4397 5ef14e63 2019-06-02 stsp exit(1);
4398 5ef14e63 2019-06-02 stsp }
4399 5ef14e63 2019-06-02 stsp
4400 5ef14e63 2019-06-02 stsp static const struct got_error *
4401 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4402 5ef14e63 2019-06-02 stsp {
4403 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4404 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4405 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4406 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4407 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4408 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4409 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4410 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4411 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4412 5ef14e63 2019-06-02 stsp
4413 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4414 5ef14e63 2019-06-02 stsp switch (ch) {
4415 5ef14e63 2019-06-02 stsp default:
4416 5ef14e63 2019-06-02 stsp usage_backout();
4417 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4418 5ef14e63 2019-06-02 stsp }
4419 5ef14e63 2019-06-02 stsp }
4420 5ef14e63 2019-06-02 stsp
4421 5ef14e63 2019-06-02 stsp argc -= optind;
4422 5ef14e63 2019-06-02 stsp argv += optind;
4423 5ef14e63 2019-06-02 stsp
4424 43012d58 2019-07-14 stsp #ifndef PROFILE
4425 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4426 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4427 43012d58 2019-07-14 stsp err(1, "pledge");
4428 43012d58 2019-07-14 stsp #endif
4429 5ef14e63 2019-06-02 stsp if (argc != 1)
4430 5ef14e63 2019-06-02 stsp usage_backout();
4431 5ef14e63 2019-06-02 stsp
4432 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4433 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4434 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4435 5ef14e63 2019-06-02 stsp goto done;
4436 5ef14e63 2019-06-02 stsp }
4437 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4438 5ef14e63 2019-06-02 stsp if (error)
4439 5ef14e63 2019-06-02 stsp goto done;
4440 5ef14e63 2019-06-02 stsp
4441 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4442 5ef14e63 2019-06-02 stsp if (error != NULL)
4443 5ef14e63 2019-06-02 stsp goto done;
4444 5ef14e63 2019-06-02 stsp
4445 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4446 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4447 5ef14e63 2019-06-02 stsp if (error)
4448 5ef14e63 2019-06-02 stsp goto done;
4449 5ef14e63 2019-06-02 stsp
4450 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4451 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4452 5ef14e63 2019-06-02 stsp if (error != NULL) {
4453 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4454 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4455 5ef14e63 2019-06-02 stsp goto done;
4456 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4457 5ef14e63 2019-06-02 stsp if (error != NULL)
4458 5ef14e63 2019-06-02 stsp goto done;
4459 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4460 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4461 5ef14e63 2019-06-02 stsp if (error != NULL)
4462 5ef14e63 2019-06-02 stsp goto done;
4463 5ef14e63 2019-06-02 stsp }
4464 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4465 5ef14e63 2019-06-02 stsp if (error)
4466 5ef14e63 2019-06-02 stsp goto done;
4467 5ef14e63 2019-06-02 stsp
4468 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4469 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4470 5ef14e63 2019-06-02 stsp if (error != NULL)
4471 5ef14e63 2019-06-02 stsp goto done;
4472 5ef14e63 2019-06-02 stsp
4473 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4474 5ef14e63 2019-06-02 stsp if (error)
4475 5ef14e63 2019-06-02 stsp goto done;
4476 5ef14e63 2019-06-02 stsp
4477 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4478 5ef14e63 2019-06-02 stsp if (error)
4479 5ef14e63 2019-06-02 stsp goto done;
4480 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4481 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4482 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4483 5ef14e63 2019-06-02 stsp goto done;
4484 5ef14e63 2019-06-02 stsp }
4485 5ef14e63 2019-06-02 stsp
4486 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4487 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4488 5ef14e63 2019-06-02 stsp if (error != NULL)
4489 5ef14e63 2019-06-02 stsp goto done;
4490 5ef14e63 2019-06-02 stsp
4491 5ef14e63 2019-06-02 stsp if (did_something)
4492 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4493 5ef14e63 2019-06-02 stsp done:
4494 5ef14e63 2019-06-02 stsp if (commit)
4495 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4496 5ef14e63 2019-06-02 stsp free(commit_id_str);
4497 5ef14e63 2019-06-02 stsp if (head_ref)
4498 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4499 818c7501 2019-07-11 stsp if (worktree)
4500 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4501 818c7501 2019-07-11 stsp if (repo)
4502 818c7501 2019-07-11 stsp got_repo_close(repo);
4503 818c7501 2019-07-11 stsp return error;
4504 818c7501 2019-07-11 stsp }
4505 818c7501 2019-07-11 stsp
4506 818c7501 2019-07-11 stsp __dead static void
4507 818c7501 2019-07-11 stsp usage_rebase(void)
4508 818c7501 2019-07-11 stsp {
4509 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4510 af54c8f8 2019-07-11 stsp getprogname());
4511 818c7501 2019-07-11 stsp exit(1);
4512 0ebf8283 2019-07-24 stsp }
4513 0ebf8283 2019-07-24 stsp
4514 0ebf8283 2019-07-24 stsp void
4515 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4516 0ebf8283 2019-07-24 stsp {
4517 0ebf8283 2019-07-24 stsp char *nl;
4518 0ebf8283 2019-07-24 stsp size_t len;
4519 0ebf8283 2019-07-24 stsp
4520 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4521 0ebf8283 2019-07-24 stsp if (len > limit)
4522 0ebf8283 2019-07-24 stsp len = limit;
4523 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4524 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4525 0ebf8283 2019-07-24 stsp if (nl)
4526 0ebf8283 2019-07-24 stsp *nl = '\0';
4527 0ebf8283 2019-07-24 stsp }
4528 0ebf8283 2019-07-24 stsp
4529 0ebf8283 2019-07-24 stsp static const struct got_error *
4530 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4531 0ebf8283 2019-07-24 stsp {
4532 5943eee2 2019-08-13 stsp const struct got_error *err;
4533 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4534 5943eee2 2019-08-13 stsp const char *s;
4535 0ebf8283 2019-07-24 stsp
4536 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4537 5943eee2 2019-08-13 stsp if (err)
4538 5943eee2 2019-08-13 stsp return err;
4539 0ebf8283 2019-07-24 stsp
4540 5943eee2 2019-08-13 stsp s = logmsg0;
4541 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4542 5943eee2 2019-08-13 stsp s++;
4543 5943eee2 2019-08-13 stsp
4544 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4545 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4546 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4547 5943eee2 2019-08-13 stsp goto done;
4548 5943eee2 2019-08-13 stsp }
4549 0ebf8283 2019-07-24 stsp
4550 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4551 5943eee2 2019-08-13 stsp done:
4552 5943eee2 2019-08-13 stsp free(logmsg0);
4553 5943eee2 2019-08-13 stsp return err;
4554 818c7501 2019-07-11 stsp }
4555 818c7501 2019-07-11 stsp
4556 818c7501 2019-07-11 stsp static const struct got_error *
4557 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4558 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
4559 818c7501 2019-07-11 stsp {
4560 818c7501 2019-07-11 stsp const struct got_error *err;
4561 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4562 818c7501 2019-07-11 stsp
4563 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
4564 818c7501 2019-07-11 stsp if (err)
4565 818c7501 2019-07-11 stsp goto done;
4566 818c7501 2019-07-11 stsp
4567 ff0d2220 2019-07-11 stsp if (new_id) {
4568 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
4569 ff0d2220 2019-07-11 stsp if (err)
4570 ff0d2220 2019-07-11 stsp goto done;
4571 ff0d2220 2019-07-11 stsp }
4572 818c7501 2019-07-11 stsp
4573 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
4574 ff0d2220 2019-07-11 stsp if (new_id_str)
4575 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
4576 0ebf8283 2019-07-24 stsp
4577 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4578 0ebf8283 2019-07-24 stsp if (err)
4579 0ebf8283 2019-07-24 stsp goto done;
4580 0ebf8283 2019-07-24 stsp
4581 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
4582 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4583 818c7501 2019-07-11 stsp done:
4584 818c7501 2019-07-11 stsp free(old_id_str);
4585 818c7501 2019-07-11 stsp free(new_id_str);
4586 818c7501 2019-07-11 stsp return err;
4587 818c7501 2019-07-11 stsp }
4588 818c7501 2019-07-11 stsp
4589 1ee397ad 2019-07-12 stsp static const struct got_error *
4590 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
4591 818c7501 2019-07-11 stsp {
4592 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
4593 818c7501 2019-07-11 stsp
4594 818c7501 2019-07-11 stsp while (path[0] == '/')
4595 818c7501 2019-07-11 stsp path++;
4596 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
4597 818c7501 2019-07-11 stsp
4598 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
4599 1ee397ad 2019-07-12 stsp return NULL;
4600 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4601 818c7501 2019-07-11 stsp *rebase_status = status;
4602 1ee397ad 2019-07-12 stsp return NULL;
4603 818c7501 2019-07-11 stsp }
4604 818c7501 2019-07-11 stsp
4605 818c7501 2019-07-11 stsp static const struct got_error *
4606 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4607 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
4608 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
4609 818c7501 2019-07-11 stsp {
4610 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4611 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4612 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4613 818c7501 2019-07-11 stsp }
4614 818c7501 2019-07-11 stsp
4615 818c7501 2019-07-11 stsp static const struct got_error *
4616 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4617 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4618 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4619 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4620 ff0d2220 2019-07-11 stsp {
4621 ff0d2220 2019-07-11 stsp const struct got_error *error;
4622 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4623 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4624 ff0d2220 2019-07-11 stsp
4625 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4626 ff0d2220 2019-07-11 stsp if (error)
4627 ff0d2220 2019-07-11 stsp return error;
4628 ff0d2220 2019-07-11 stsp
4629 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4630 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
4631 ff0d2220 2019-07-11 stsp if (error) {
4632 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4633 ff0d2220 2019-07-11 stsp goto done;
4634 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
4635 ff0d2220 2019-07-11 stsp } else {
4636 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
4637 ff0d2220 2019-07-11 stsp free(new_commit_id);
4638 ff0d2220 2019-07-11 stsp }
4639 ff0d2220 2019-07-11 stsp done:
4640 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
4641 ff0d2220 2019-07-11 stsp return error;
4642 64c6d990 2019-07-11 stsp }
4643 64c6d990 2019-07-11 stsp
4644 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
4645 64c6d990 2019-07-11 stsp const char *path_prefix;
4646 64c6d990 2019-07-11 stsp size_t len;
4647 8ca9bd68 2019-07-25 stsp int errcode;
4648 64c6d990 2019-07-11 stsp };
4649 64c6d990 2019-07-11 stsp
4650 64c6d990 2019-07-11 stsp static const struct got_error *
4651 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4652 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
4653 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
4654 64c6d990 2019-07-11 stsp struct got_repository *repo)
4655 64c6d990 2019-07-11 stsp {
4656 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
4657 64c6d990 2019-07-11 stsp
4658 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4659 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4660 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
4661 64c6d990 2019-07-11 stsp
4662 64c6d990 2019-07-11 stsp return NULL;
4663 64c6d990 2019-07-11 stsp }
4664 64c6d990 2019-07-11 stsp
4665 64c6d990 2019-07-11 stsp static const struct got_error *
4666 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
4667 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
4668 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
4669 64c6d990 2019-07-11 stsp {
4670 64c6d990 2019-07-11 stsp const struct got_error *err;
4671 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4672 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
4673 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
4674 64c6d990 2019-07-11 stsp
4675 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
4676 64c6d990 2019-07-11 stsp return NULL;
4677 64c6d990 2019-07-11 stsp
4678 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4679 64c6d990 2019-07-11 stsp if (err)
4680 64c6d990 2019-07-11 stsp goto done;
4681 64c6d990 2019-07-11 stsp
4682 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4683 64c6d990 2019-07-11 stsp if (err)
4684 64c6d990 2019-07-11 stsp goto done;
4685 64c6d990 2019-07-11 stsp
4686 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
4687 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
4688 64c6d990 2019-07-11 stsp if (err)
4689 64c6d990 2019-07-11 stsp goto done;
4690 64c6d990 2019-07-11 stsp
4691 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
4692 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
4693 64c6d990 2019-07-11 stsp if (err)
4694 64c6d990 2019-07-11 stsp goto done;
4695 64c6d990 2019-07-11 stsp
4696 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
4697 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
4698 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
4699 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
4700 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
4701 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
4702 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
4703 64c6d990 2019-07-11 stsp done:
4704 64c6d990 2019-07-11 stsp if (tree1)
4705 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
4706 64c6d990 2019-07-11 stsp if (tree2)
4707 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
4708 64c6d990 2019-07-11 stsp if (commit)
4709 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
4710 64c6d990 2019-07-11 stsp if (parent_commit)
4711 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
4712 64c6d990 2019-07-11 stsp return err;
4713 ff0d2220 2019-07-11 stsp }
4714 ff0d2220 2019-07-11 stsp
4715 ff0d2220 2019-07-11 stsp static const struct got_error *
4716 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
4717 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
4718 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4719 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
4720 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
4721 af61c510 2019-07-19 stsp {
4722 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
4723 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
4724 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
4725 af61c510 2019-07-19 stsp struct got_object_qid *qid;
4726 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
4727 af61c510 2019-07-19 stsp
4728 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4729 af61c510 2019-07-19 stsp if (err)
4730 af61c510 2019-07-19 stsp return err;
4731 af61c510 2019-07-19 stsp
4732 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4733 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4734 af61c510 2019-07-19 stsp if (err)
4735 af61c510 2019-07-19 stsp goto done;
4736 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4737 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
4738 af61c510 2019-07-19 stsp if (err) {
4739 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
4740 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
4741 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
4742 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
4743 af61c510 2019-07-19 stsp "been reached?!?");
4744 af61c510 2019-07-19 stsp goto done;
4745 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4746 af61c510 2019-07-19 stsp goto done;
4747 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
4748 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4749 af61c510 2019-07-19 stsp if (err)
4750 af61c510 2019-07-19 stsp goto done;
4751 af61c510 2019-07-19 stsp } else {
4752 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
4753 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
4754 af61c510 2019-07-19 stsp if (err)
4755 af61c510 2019-07-19 stsp goto done;
4756 af61c510 2019-07-19 stsp
4757 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
4758 af61c510 2019-07-19 stsp if (err)
4759 af61c510 2019-07-19 stsp goto done;
4760 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4761 af61c510 2019-07-19 stsp commit_id = parent_id;
4762 af61c510 2019-07-19 stsp }
4763 af61c510 2019-07-19 stsp }
4764 af61c510 2019-07-19 stsp done:
4765 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
4766 af61c510 2019-07-19 stsp return err;
4767 af61c510 2019-07-19 stsp }
4768 af61c510 2019-07-19 stsp
4769 af61c510 2019-07-19 stsp static const struct got_error *
4770 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
4771 818c7501 2019-07-11 stsp {
4772 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
4773 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
4774 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
4775 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4776 818c7501 2019-07-11 stsp char *cwd = NULL;
4777 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
4778 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4779 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
4780 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
4781 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4782 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
4783 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4784 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4785 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
4786 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
4787 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
4788 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
4789 818c7501 2019-07-11 stsp
4790 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
4791 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
4792 818c7501 2019-07-11 stsp
4793 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
4794 818c7501 2019-07-11 stsp switch (ch) {
4795 818c7501 2019-07-11 stsp case 'a':
4796 818c7501 2019-07-11 stsp abort_rebase = 1;
4797 818c7501 2019-07-11 stsp break;
4798 818c7501 2019-07-11 stsp case 'c':
4799 818c7501 2019-07-11 stsp continue_rebase = 1;
4800 818c7501 2019-07-11 stsp break;
4801 818c7501 2019-07-11 stsp default:
4802 818c7501 2019-07-11 stsp usage_rebase();
4803 818c7501 2019-07-11 stsp /* NOTREACHED */
4804 818c7501 2019-07-11 stsp }
4805 818c7501 2019-07-11 stsp }
4806 818c7501 2019-07-11 stsp
4807 818c7501 2019-07-11 stsp argc -= optind;
4808 818c7501 2019-07-11 stsp argv += optind;
4809 818c7501 2019-07-11 stsp
4810 43012d58 2019-07-14 stsp #ifndef PROFILE
4811 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4812 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4813 43012d58 2019-07-14 stsp err(1, "pledge");
4814 43012d58 2019-07-14 stsp #endif
4815 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
4816 818c7501 2019-07-11 stsp usage_rebase();
4817 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
4818 818c7501 2019-07-11 stsp if (argc != 0)
4819 818c7501 2019-07-11 stsp usage_rebase();
4820 818c7501 2019-07-11 stsp } else if (argc != 1)
4821 818c7501 2019-07-11 stsp usage_rebase();
4822 818c7501 2019-07-11 stsp
4823 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
4824 818c7501 2019-07-11 stsp if (cwd == NULL) {
4825 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
4826 818c7501 2019-07-11 stsp goto done;
4827 818c7501 2019-07-11 stsp }
4828 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
4829 818c7501 2019-07-11 stsp if (error)
4830 818c7501 2019-07-11 stsp goto done;
4831 818c7501 2019-07-11 stsp
4832 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4833 818c7501 2019-07-11 stsp if (error != NULL)
4834 818c7501 2019-07-11 stsp goto done;
4835 818c7501 2019-07-11 stsp
4836 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4837 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4838 818c7501 2019-07-11 stsp if (error)
4839 818c7501 2019-07-11 stsp goto done;
4840 818c7501 2019-07-11 stsp
4841 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4842 818c7501 2019-07-11 stsp if (error)
4843 818c7501 2019-07-11 stsp goto done;
4844 818c7501 2019-07-11 stsp
4845 f6794adc 2019-07-23 stsp if (abort_rebase) {
4846 818c7501 2019-07-11 stsp int did_something;
4847 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4848 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4849 f6794adc 2019-07-23 stsp goto done;
4850 f6794adc 2019-07-23 stsp }
4851 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4852 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4853 3e3a69f1 2019-07-25 stsp worktree, repo);
4854 818c7501 2019-07-11 stsp if (error)
4855 818c7501 2019-07-11 stsp goto done;
4856 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
4857 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
4858 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
4859 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
4860 818c7501 2019-07-11 stsp if (error)
4861 818c7501 2019-07-11 stsp goto done;
4862 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4863 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
4864 818c7501 2019-07-11 stsp }
4865 818c7501 2019-07-11 stsp
4866 818c7501 2019-07-11 stsp if (continue_rebase) {
4867 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4868 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4869 f6794adc 2019-07-23 stsp goto done;
4870 f6794adc 2019-07-23 stsp }
4871 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4872 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4873 3e3a69f1 2019-07-25 stsp worktree, repo);
4874 818c7501 2019-07-11 stsp if (error)
4875 818c7501 2019-07-11 stsp goto done;
4876 818c7501 2019-07-11 stsp
4877 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4878 01757395 2019-07-12 stsp resume_commit_id, repo);
4879 818c7501 2019-07-11 stsp if (error)
4880 818c7501 2019-07-11 stsp goto done;
4881 818c7501 2019-07-11 stsp
4882 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
4883 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
4884 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
4885 818c7501 2019-07-11 stsp goto done;
4886 818c7501 2019-07-11 stsp }
4887 818c7501 2019-07-11 stsp } else {
4888 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
4889 818c7501 2019-07-11 stsp if (error != NULL)
4890 818c7501 2019-07-11 stsp goto done;
4891 ff0d2220 2019-07-11 stsp }
4892 818c7501 2019-07-11 stsp
4893 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4894 ff0d2220 2019-07-11 stsp if (error)
4895 ff0d2220 2019-07-11 stsp goto done;
4896 ff0d2220 2019-07-11 stsp
4897 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
4898 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
4899 a51a74b3 2019-07-27 stsp
4900 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
4901 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4902 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
4903 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4904 818c7501 2019-07-11 stsp if (error)
4905 818c7501 2019-07-11 stsp goto done;
4906 818c7501 2019-07-11 stsp if (yca_id == NULL) {
4907 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
4908 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
4909 818c7501 2019-07-11 stsp "with work tree's branch");
4910 818c7501 2019-07-11 stsp goto done;
4911 818c7501 2019-07-11 stsp }
4912 818c7501 2019-07-11 stsp
4913 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
4914 a51a74b3 2019-07-27 stsp if (error) {
4915 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
4916 a51a74b3 2019-07-27 stsp goto done;
4917 a51a74b3 2019-07-27 stsp error = NULL;
4918 a51a74b3 2019-07-27 stsp } else {
4919 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
4920 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
4921 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
4922 a51a74b3 2019-07-27 stsp goto done;
4923 a51a74b3 2019-07-27 stsp }
4924 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
4925 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
4926 818c7501 2019-07-11 stsp if (error)
4927 818c7501 2019-07-11 stsp goto done;
4928 818c7501 2019-07-11 stsp }
4929 818c7501 2019-07-11 stsp
4930 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
4931 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4932 818c7501 2019-07-11 stsp if (error)
4933 818c7501 2019-07-11 stsp goto done;
4934 818c7501 2019-07-11 stsp
4935 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4936 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4937 fc66b545 2019-08-12 stsp if (pid == NULL) {
4938 fc66b545 2019-08-12 stsp if (!continue_rebase) {
4939 fc66b545 2019-08-12 stsp int did_something;
4940 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
4941 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
4942 fc66b545 2019-08-12 stsp &did_something);
4943 fc66b545 2019-08-12 stsp if (error)
4944 fc66b545 2019-08-12 stsp goto done;
4945 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
4946 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
4947 fc66b545 2019-08-12 stsp }
4948 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4949 fc66b545 2019-08-12 stsp goto done;
4950 fc66b545 2019-08-12 stsp }
4951 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4952 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4953 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4954 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4955 818c7501 2019-07-11 stsp commit = NULL;
4956 818c7501 2019-07-11 stsp if (error)
4957 818c7501 2019-07-11 stsp goto done;
4958 64c6d990 2019-07-11 stsp
4959 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4960 ff0d2220 2019-07-11 stsp if (continue_rebase)
4961 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4962 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4963 ff0d2220 2019-07-11 stsp else
4964 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4965 818c7501 2019-07-11 stsp goto done;
4966 818c7501 2019-07-11 stsp }
4967 818c7501 2019-07-11 stsp
4968 818c7501 2019-07-11 stsp pid = NULL;
4969 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4970 818c7501 2019-07-11 stsp commit_id = qid->id;
4971 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4972 818c7501 2019-07-11 stsp pid = qid;
4973 818c7501 2019-07-11 stsp
4974 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4975 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4976 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4977 818c7501 2019-07-11 stsp if (error)
4978 818c7501 2019-07-11 stsp goto done;
4979 818c7501 2019-07-11 stsp
4980 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4981 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4982 818c7501 2019-07-11 stsp break;
4983 01757395 2019-07-12 stsp }
4984 818c7501 2019-07-11 stsp
4985 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4986 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4987 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4988 818c7501 2019-07-11 stsp if (error)
4989 818c7501 2019-07-11 stsp goto done;
4990 818c7501 2019-07-11 stsp }
4991 818c7501 2019-07-11 stsp
4992 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4993 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4994 818c7501 2019-07-11 stsp if (error)
4995 818c7501 2019-07-11 stsp goto done;
4996 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4997 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4998 818c7501 2019-07-11 stsp } else
4999 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5000 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5001 818c7501 2019-07-11 stsp done:
5002 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5003 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5004 818c7501 2019-07-11 stsp free(resume_commit_id);
5005 818c7501 2019-07-11 stsp free(yca_id);
5006 818c7501 2019-07-11 stsp if (commit)
5007 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5008 818c7501 2019-07-11 stsp if (branch)
5009 818c7501 2019-07-11 stsp got_ref_close(branch);
5010 818c7501 2019-07-11 stsp if (new_base_branch)
5011 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5012 818c7501 2019-07-11 stsp if (tmp_branch)
5013 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5014 5ef14e63 2019-06-02 stsp if (worktree)
5015 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5016 5ef14e63 2019-06-02 stsp if (repo)
5017 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5018 5ef14e63 2019-06-02 stsp return error;
5019 0ebf8283 2019-07-24 stsp }
5020 0ebf8283 2019-07-24 stsp
5021 0ebf8283 2019-07-24 stsp __dead static void
5022 0ebf8283 2019-07-24 stsp usage_histedit(void)
5023 0ebf8283 2019-07-24 stsp {
5024 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5025 0ebf8283 2019-07-24 stsp getprogname());
5026 0ebf8283 2019-07-24 stsp exit(1);
5027 0ebf8283 2019-07-24 stsp }
5028 0ebf8283 2019-07-24 stsp
5029 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5030 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5031 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5032 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5033 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5034 0ebf8283 2019-07-24 stsp
5035 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5036 0ebf8283 2019-07-24 stsp unsigned char code;
5037 0ebf8283 2019-07-24 stsp const char *name;
5038 0ebf8283 2019-07-24 stsp const char *desc;
5039 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5040 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5041 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5042 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5043 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5044 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5045 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5046 0ebf8283 2019-07-24 stsp };
5047 0ebf8283 2019-07-24 stsp
5048 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5049 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5050 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5051 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5052 0ebf8283 2019-07-24 stsp char *logmsg;
5053 0ebf8283 2019-07-24 stsp };
5054 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5055 0ebf8283 2019-07-24 stsp
5056 0ebf8283 2019-07-24 stsp static const struct got_error *
5057 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5058 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5059 0ebf8283 2019-07-24 stsp {
5060 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5061 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5062 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5063 8138f3e1 2019-08-11 stsp int n;
5064 0ebf8283 2019-07-24 stsp
5065 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5066 0ebf8283 2019-07-24 stsp if (err)
5067 0ebf8283 2019-07-24 stsp goto done;
5068 0ebf8283 2019-07-24 stsp
5069 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5070 0ebf8283 2019-07-24 stsp if (err)
5071 0ebf8283 2019-07-24 stsp goto done;
5072 0ebf8283 2019-07-24 stsp
5073 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5074 0ebf8283 2019-07-24 stsp if (err)
5075 0ebf8283 2019-07-24 stsp goto done;
5076 0ebf8283 2019-07-24 stsp
5077 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5078 0ebf8283 2019-07-24 stsp if (n < 0)
5079 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5080 0ebf8283 2019-07-24 stsp done:
5081 0ebf8283 2019-07-24 stsp if (commit)
5082 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5083 0ebf8283 2019-07-24 stsp free(id_str);
5084 0ebf8283 2019-07-24 stsp free(logmsg);
5085 0ebf8283 2019-07-24 stsp return err;
5086 0ebf8283 2019-07-24 stsp }
5087 0ebf8283 2019-07-24 stsp
5088 0ebf8283 2019-07-24 stsp static const struct got_error *
5089 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5090 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5091 0ebf8283 2019-07-24 stsp {
5092 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5093 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5094 0ebf8283 2019-07-24 stsp
5095 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5096 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5097 0ebf8283 2019-07-24 stsp
5098 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5099 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5100 0ebf8283 2019-07-24 stsp f, repo);
5101 0ebf8283 2019-07-24 stsp if (err)
5102 0ebf8283 2019-07-24 stsp break;
5103 0ebf8283 2019-07-24 stsp }
5104 0ebf8283 2019-07-24 stsp
5105 0ebf8283 2019-07-24 stsp return err;
5106 0ebf8283 2019-07-24 stsp }
5107 0ebf8283 2019-07-24 stsp
5108 0ebf8283 2019-07-24 stsp static const struct got_error *
5109 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5110 0ebf8283 2019-07-24 stsp {
5111 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5112 0ebf8283 2019-07-24 stsp int n, i;
5113 0ebf8283 2019-07-24 stsp
5114 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5115 0ebf8283 2019-07-24 stsp if (n < 0)
5116 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5117 0ebf8283 2019-07-24 stsp
5118 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5119 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5120 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5121 0ebf8283 2019-07-24 stsp cmd->desc);
5122 0ebf8283 2019-07-24 stsp if (n < 0) {
5123 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5124 0ebf8283 2019-07-24 stsp break;
5125 0ebf8283 2019-07-24 stsp }
5126 0ebf8283 2019-07-24 stsp }
5127 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5128 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5129 0ebf8283 2019-07-24 stsp if (n < 0)
5130 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5131 0ebf8283 2019-07-24 stsp return err;
5132 0ebf8283 2019-07-24 stsp }
5133 0ebf8283 2019-07-24 stsp
5134 0ebf8283 2019-07-24 stsp static const struct got_error *
5135 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5136 0ebf8283 2019-07-24 stsp {
5137 0ebf8283 2019-07-24 stsp static char msg[42];
5138 0ebf8283 2019-07-24 stsp int ret;
5139 0ebf8283 2019-07-24 stsp
5140 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5141 0ebf8283 2019-07-24 stsp lineno);
5142 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5143 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5144 0ebf8283 2019-07-24 stsp
5145 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5146 0ebf8283 2019-07-24 stsp }
5147 0ebf8283 2019-07-24 stsp
5148 0ebf8283 2019-07-24 stsp static const struct got_error *
5149 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5150 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5151 0ebf8283 2019-07-24 stsp {
5152 0ebf8283 2019-07-24 stsp const struct got_error *err;
5153 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5154 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5155 0ebf8283 2019-07-24 stsp
5156 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5157 0ebf8283 2019-07-24 stsp if (err)
5158 0ebf8283 2019-07-24 stsp return err;
5159 0ebf8283 2019-07-24 stsp
5160 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5161 0ebf8283 2019-07-24 stsp if (err)
5162 0ebf8283 2019-07-24 stsp goto done;
5163 0ebf8283 2019-07-24 stsp
5164 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5165 5943eee2 2019-08-13 stsp if (err)
5166 5943eee2 2019-08-13 stsp goto done;
5167 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5168 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5169 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5170 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5171 0ebf8283 2019-07-24 stsp goto done;
5172 0ebf8283 2019-07-24 stsp }
5173 0ebf8283 2019-07-24 stsp done:
5174 0ebf8283 2019-07-24 stsp if (folded_commit)
5175 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5176 0ebf8283 2019-07-24 stsp free(id_str);
5177 5943eee2 2019-08-13 stsp free(folded_logmsg);
5178 0ebf8283 2019-07-24 stsp return err;
5179 0ebf8283 2019-07-24 stsp }
5180 0ebf8283 2019-07-24 stsp
5181 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5182 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5183 0ebf8283 2019-07-24 stsp {
5184 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5185 0ebf8283 2019-07-24 stsp
5186 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5187 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5188 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5189 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5190 3f9de99f 2019-07-24 stsp folded = prev;
5191 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5192 0ebf8283 2019-07-24 stsp }
5193 0ebf8283 2019-07-24 stsp
5194 0ebf8283 2019-07-24 stsp return folded;
5195 0ebf8283 2019-07-24 stsp }
5196 0ebf8283 2019-07-24 stsp
5197 0ebf8283 2019-07-24 stsp static const struct got_error *
5198 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5199 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5200 0ebf8283 2019-07-24 stsp {
5201 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5202 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5203 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5204 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5205 0ebf8283 2019-07-24 stsp int fd;
5206 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5207 0ebf8283 2019-07-24 stsp
5208 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5209 0ebf8283 2019-07-24 stsp if (err)
5210 0ebf8283 2019-07-24 stsp return err;
5211 0ebf8283 2019-07-24 stsp
5212 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5213 0ebf8283 2019-07-24 stsp if (folded) {
5214 0ebf8283 2019-07-24 stsp while (folded != hle) {
5215 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5216 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5217 3f9de99f 2019-07-24 stsp continue;
5218 3f9de99f 2019-07-24 stsp }
5219 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5220 0ebf8283 2019-07-24 stsp logmsg, repo);
5221 0ebf8283 2019-07-24 stsp if (err)
5222 0ebf8283 2019-07-24 stsp goto done;
5223 0ebf8283 2019-07-24 stsp free(logmsg);
5224 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5225 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5226 0ebf8283 2019-07-24 stsp }
5227 0ebf8283 2019-07-24 stsp }
5228 0ebf8283 2019-07-24 stsp
5229 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5230 0ebf8283 2019-07-24 stsp if (err)
5231 0ebf8283 2019-07-24 stsp goto done;
5232 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5233 5943eee2 2019-08-13 stsp if (err)
5234 5943eee2 2019-08-13 stsp goto done;
5235 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5236 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5237 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5238 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5239 0ebf8283 2019-07-24 stsp goto done;
5240 0ebf8283 2019-07-24 stsp }
5241 0ebf8283 2019-07-24 stsp free(logmsg);
5242 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5243 0ebf8283 2019-07-24 stsp
5244 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5245 0ebf8283 2019-07-24 stsp if (err)
5246 0ebf8283 2019-07-24 stsp goto done;
5247 0ebf8283 2019-07-24 stsp
5248 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5249 0ebf8283 2019-07-24 stsp if (err)
5250 0ebf8283 2019-07-24 stsp goto done;
5251 0ebf8283 2019-07-24 stsp
5252 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5253 0ebf8283 2019-07-24 stsp close(fd);
5254 0ebf8283 2019-07-24 stsp
5255 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5256 0ebf8283 2019-07-24 stsp if (err)
5257 0ebf8283 2019-07-24 stsp goto done;
5258 0ebf8283 2019-07-24 stsp
5259 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5260 0ebf8283 2019-07-24 stsp if (err) {
5261 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5262 0ebf8283 2019-07-24 stsp goto done;
5263 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5264 0ebf8283 2019-07-24 stsp }
5265 0ebf8283 2019-07-24 stsp done:
5266 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5267 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5268 0ebf8283 2019-07-24 stsp free(logmsg_path);
5269 0ebf8283 2019-07-24 stsp free(logmsg);
5270 5943eee2 2019-08-13 stsp free(orig_logmsg);
5271 0ebf8283 2019-07-24 stsp free(editor);
5272 0ebf8283 2019-07-24 stsp if (commit)
5273 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5274 0ebf8283 2019-07-24 stsp return err;
5275 5ef14e63 2019-06-02 stsp }
5276 0ebf8283 2019-07-24 stsp
5277 0ebf8283 2019-07-24 stsp static const struct got_error *
5278 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5279 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5280 0ebf8283 2019-07-24 stsp {
5281 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5282 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5283 0ebf8283 2019-07-24 stsp size_t size;
5284 0ebf8283 2019-07-24 stsp ssize_t len;
5285 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5286 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5287 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5288 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5289 0ebf8283 2019-07-24 stsp
5290 0ebf8283 2019-07-24 stsp for (;;) {
5291 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5292 0ebf8283 2019-07-24 stsp if (len == -1) {
5293 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5294 0ebf8283 2019-07-24 stsp if (feof(f))
5295 0ebf8283 2019-07-24 stsp break;
5296 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5297 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5298 0ebf8283 2019-07-24 stsp break;
5299 0ebf8283 2019-07-24 stsp }
5300 0ebf8283 2019-07-24 stsp lineno++;
5301 0ebf8283 2019-07-24 stsp p = line;
5302 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5303 0ebf8283 2019-07-24 stsp p++;
5304 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5305 0ebf8283 2019-07-24 stsp free(line);
5306 0ebf8283 2019-07-24 stsp line = NULL;
5307 0ebf8283 2019-07-24 stsp continue;
5308 0ebf8283 2019-07-24 stsp }
5309 0ebf8283 2019-07-24 stsp cmd = NULL;
5310 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5311 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5312 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5313 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5314 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5315 0ebf8283 2019-07-24 stsp break;
5316 0ebf8283 2019-07-24 stsp }
5317 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5318 0ebf8283 2019-07-24 stsp p++;
5319 0ebf8283 2019-07-24 stsp break;
5320 0ebf8283 2019-07-24 stsp }
5321 0ebf8283 2019-07-24 stsp }
5322 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5323 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5324 0ebf8283 2019-07-24 stsp break;
5325 0ebf8283 2019-07-24 stsp }
5326 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5327 0ebf8283 2019-07-24 stsp p++;
5328 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5329 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5330 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5331 0ebf8283 2019-07-24 stsp break;
5332 0ebf8283 2019-07-24 stsp }
5333 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5334 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5335 0ebf8283 2019-07-24 stsp if (err)
5336 0ebf8283 2019-07-24 stsp break;
5337 0ebf8283 2019-07-24 stsp } else {
5338 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5339 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5340 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5341 0ebf8283 2019-07-24 stsp break;
5342 0ebf8283 2019-07-24 stsp }
5343 0ebf8283 2019-07-24 stsp }
5344 0ebf8283 2019-07-24 stsp free(line);
5345 0ebf8283 2019-07-24 stsp line = NULL;
5346 0ebf8283 2019-07-24 stsp continue;
5347 0ebf8283 2019-07-24 stsp } else {
5348 0ebf8283 2019-07-24 stsp end = p;
5349 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5350 0ebf8283 2019-07-24 stsp end++;
5351 0ebf8283 2019-07-24 stsp *end = '\0';
5352 0ebf8283 2019-07-24 stsp
5353 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5354 0ebf8283 2019-07-24 stsp if (err) {
5355 0ebf8283 2019-07-24 stsp /* override error code */
5356 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5357 0ebf8283 2019-07-24 stsp break;
5358 0ebf8283 2019-07-24 stsp }
5359 0ebf8283 2019-07-24 stsp }
5360 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5361 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5362 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5363 0ebf8283 2019-07-24 stsp break;
5364 0ebf8283 2019-07-24 stsp }
5365 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5366 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5367 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5368 0ebf8283 2019-07-24 stsp commit_id = NULL;
5369 0ebf8283 2019-07-24 stsp free(line);
5370 0ebf8283 2019-07-24 stsp line = NULL;
5371 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5372 0ebf8283 2019-07-24 stsp }
5373 0ebf8283 2019-07-24 stsp
5374 0ebf8283 2019-07-24 stsp free(line);
5375 0ebf8283 2019-07-24 stsp free(commit_id);
5376 0ebf8283 2019-07-24 stsp return err;
5377 0ebf8283 2019-07-24 stsp }
5378 0ebf8283 2019-07-24 stsp
5379 0ebf8283 2019-07-24 stsp static const struct got_error *
5380 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5381 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5382 bfce7f83 2019-07-27 stsp {
5383 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5384 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5385 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5386 bfce7f83 2019-07-27 stsp static char msg[80];
5387 bfce7f83 2019-07-27 stsp char *id_str;
5388 bfce7f83 2019-07-27 stsp
5389 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5390 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5391 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5392 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5393 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5394 bfce7f83 2019-07-27 stsp
5395 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5396 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5397 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5398 bfce7f83 2019-07-27 stsp break;
5399 bfce7f83 2019-07-27 stsp }
5400 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5401 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5402 bfce7f83 2019-07-27 stsp if (err)
5403 bfce7f83 2019-07-27 stsp return err;
5404 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5405 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5406 bfce7f83 2019-07-27 stsp free(id_str);
5407 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5408 bfce7f83 2019-07-27 stsp }
5409 bfce7f83 2019-07-27 stsp }
5410 bfce7f83 2019-07-27 stsp
5411 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5412 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5413 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5414 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5415 bfce7f83 2019-07-27 stsp
5416 bfce7f83 2019-07-27 stsp return NULL;
5417 bfce7f83 2019-07-27 stsp }
5418 bfce7f83 2019-07-27 stsp
5419 bfce7f83 2019-07-27 stsp static const struct got_error *
5420 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5421 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5422 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5423 0ebf8283 2019-07-24 stsp {
5424 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5425 0ebf8283 2019-07-24 stsp char *editor;
5426 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5427 0ebf8283 2019-07-24 stsp
5428 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5429 0ebf8283 2019-07-24 stsp if (err)
5430 0ebf8283 2019-07-24 stsp return err;
5431 0ebf8283 2019-07-24 stsp
5432 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5433 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5434 0ebf8283 2019-07-24 stsp goto done;
5435 0ebf8283 2019-07-24 stsp }
5436 0ebf8283 2019-07-24 stsp
5437 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5438 0ebf8283 2019-07-24 stsp if (f == NULL) {
5439 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5440 0ebf8283 2019-07-24 stsp goto done;
5441 0ebf8283 2019-07-24 stsp }
5442 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5443 bfce7f83 2019-07-27 stsp if (err)
5444 bfce7f83 2019-07-27 stsp goto done;
5445 bfce7f83 2019-07-27 stsp
5446 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5447 0ebf8283 2019-07-24 stsp done:
5448 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5449 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5450 0ebf8283 2019-07-24 stsp free(editor);
5451 0ebf8283 2019-07-24 stsp return err;
5452 0ebf8283 2019-07-24 stsp }
5453 0ebf8283 2019-07-24 stsp
5454 0ebf8283 2019-07-24 stsp static const struct got_error *
5455 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5456 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5457 0ebf8283 2019-07-24 stsp
5458 0ebf8283 2019-07-24 stsp static const struct got_error *
5459 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5460 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5461 0ebf8283 2019-07-24 stsp {
5462 0ebf8283 2019-07-24 stsp const struct got_error *err;
5463 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5464 0ebf8283 2019-07-24 stsp char *path = NULL;
5465 0ebf8283 2019-07-24 stsp
5466 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5467 0ebf8283 2019-07-24 stsp if (err)
5468 0ebf8283 2019-07-24 stsp return err;
5469 0ebf8283 2019-07-24 stsp
5470 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5471 0ebf8283 2019-07-24 stsp if (err)
5472 0ebf8283 2019-07-24 stsp goto done;
5473 0ebf8283 2019-07-24 stsp
5474 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5475 0ebf8283 2019-07-24 stsp if (err)
5476 0ebf8283 2019-07-24 stsp goto done;
5477 0ebf8283 2019-07-24 stsp
5478 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5479 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5480 0ebf8283 2019-07-24 stsp goto done;
5481 0ebf8283 2019-07-24 stsp }
5482 0ebf8283 2019-07-24 stsp f = NULL;
5483 0ebf8283 2019-07-24 stsp
5484 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5485 0ebf8283 2019-07-24 stsp if (err) {
5486 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5487 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5488 0ebf8283 2019-07-24 stsp goto done;
5489 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5490 0ebf8283 2019-07-24 stsp commits, path, repo);
5491 0ebf8283 2019-07-24 stsp }
5492 0ebf8283 2019-07-24 stsp done:
5493 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5494 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5495 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5496 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5497 0ebf8283 2019-07-24 stsp free(path);
5498 0ebf8283 2019-07-24 stsp return err;
5499 0ebf8283 2019-07-24 stsp }
5500 0ebf8283 2019-07-24 stsp
5501 0ebf8283 2019-07-24 stsp static const struct got_error *
5502 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5503 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5504 0ebf8283 2019-07-24 stsp {
5505 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5506 0ebf8283 2019-07-24 stsp char *path = NULL;
5507 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5508 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5509 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5510 0ebf8283 2019-07-24 stsp
5511 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5512 0ebf8283 2019-07-24 stsp if (err)
5513 0ebf8283 2019-07-24 stsp return err;
5514 0ebf8283 2019-07-24 stsp
5515 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5516 0ebf8283 2019-07-24 stsp if (f == NULL) {
5517 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5518 0ebf8283 2019-07-24 stsp goto done;
5519 0ebf8283 2019-07-24 stsp }
5520 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5521 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5522 0ebf8283 2019-07-24 stsp repo);
5523 0ebf8283 2019-07-24 stsp if (err)
5524 0ebf8283 2019-07-24 stsp break;
5525 0ebf8283 2019-07-24 stsp
5526 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5527 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5528 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5529 0ebf8283 2019-07-24 stsp if (n < 0) {
5530 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5531 0ebf8283 2019-07-24 stsp break;
5532 0ebf8283 2019-07-24 stsp }
5533 0ebf8283 2019-07-24 stsp }
5534 0ebf8283 2019-07-24 stsp }
5535 0ebf8283 2019-07-24 stsp done:
5536 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5537 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5538 0ebf8283 2019-07-24 stsp free(path);
5539 0ebf8283 2019-07-24 stsp if (commit)
5540 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5541 0ebf8283 2019-07-24 stsp return err;
5542 0ebf8283 2019-07-24 stsp }
5543 0ebf8283 2019-07-24 stsp
5544 bfce7f83 2019-07-27 stsp void
5545 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5546 bfce7f83 2019-07-27 stsp {
5547 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5548 bfce7f83 2019-07-27 stsp
5549 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5550 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5551 bfce7f83 2019-07-27 stsp free(hle);
5552 bfce7f83 2019-07-27 stsp }
5553 bfce7f83 2019-07-27 stsp }
5554 bfce7f83 2019-07-27 stsp
5555 0ebf8283 2019-07-24 stsp static const struct got_error *
5556 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5557 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5558 0ebf8283 2019-07-24 stsp {
5559 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5560 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5561 0ebf8283 2019-07-24 stsp
5562 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5563 0ebf8283 2019-07-24 stsp if (f == NULL) {
5564 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5565 0ebf8283 2019-07-24 stsp goto done;
5566 0ebf8283 2019-07-24 stsp }
5567 0ebf8283 2019-07-24 stsp
5568 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5569 0ebf8283 2019-07-24 stsp done:
5570 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5571 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5572 0ebf8283 2019-07-24 stsp return err;
5573 0ebf8283 2019-07-24 stsp }
5574 0ebf8283 2019-07-24 stsp
5575 0ebf8283 2019-07-24 stsp static const struct got_error *
5576 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5577 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
5578 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5579 0ebf8283 2019-07-24 stsp {
5580 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
5581 0ebf8283 2019-07-24 stsp int resp = ' ';
5582 0ebf8283 2019-07-24 stsp
5583 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
5584 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5585 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
5586 0ebf8283 2019-07-24 stsp resp = getchar();
5587 a61a4414 2019-08-07 stsp if (resp == '\n')
5588 a61a4414 2019-08-07 stsp resp = getchar();
5589 426ebf2e 2019-07-25 stsp if (resp == 'c') {
5590 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5591 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
5592 bfce7f83 2019-07-27 stsp repo);
5593 426ebf2e 2019-07-25 stsp if (err) {
5594 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5595 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5596 426ebf2e 2019-07-25 stsp break;
5597 bfce7f83 2019-07-27 stsp prev_err = err;
5598 426ebf2e 2019-07-25 stsp resp = ' ';
5599 426ebf2e 2019-07-25 stsp continue;
5600 426ebf2e 2019-07-25 stsp }
5601 bfce7f83 2019-07-27 stsp break;
5602 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
5603 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5604 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
5605 0ebf8283 2019-07-24 stsp commits, repo);
5606 426ebf2e 2019-07-25 stsp if (err) {
5607 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5608 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5609 426ebf2e 2019-07-25 stsp break;
5610 bfce7f83 2019-07-27 stsp prev_err = err;
5611 426ebf2e 2019-07-25 stsp resp = ' ';
5612 426ebf2e 2019-07-25 stsp continue;
5613 426ebf2e 2019-07-25 stsp }
5614 bfce7f83 2019-07-27 stsp break;
5615 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
5616 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5617 0ebf8283 2019-07-24 stsp break;
5618 426ebf2e 2019-07-25 stsp } else
5619 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5620 0ebf8283 2019-07-24 stsp }
5621 0ebf8283 2019-07-24 stsp
5622 0ebf8283 2019-07-24 stsp return err;
5623 0ebf8283 2019-07-24 stsp }
5624 0ebf8283 2019-07-24 stsp
5625 0ebf8283 2019-07-24 stsp static const struct got_error *
5626 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
5627 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5628 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
5629 0ebf8283 2019-07-24 stsp {
5630 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5631 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5632 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5633 3e3a69f1 2019-07-25 stsp branch, repo);
5634 0ebf8283 2019-07-24 stsp }
5635 0ebf8283 2019-07-24 stsp
5636 0ebf8283 2019-07-24 stsp static const struct got_error *
5637 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
5638 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5639 0ebf8283 2019-07-24 stsp {
5640 0ebf8283 2019-07-24 stsp const struct got_error *err;
5641 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5642 0ebf8283 2019-07-24 stsp
5643 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
5644 0ebf8283 2019-07-24 stsp if (err)
5645 0ebf8283 2019-07-24 stsp goto done;
5646 0ebf8283 2019-07-24 stsp
5647 0ebf8283 2019-07-24 stsp if (new_id) {
5648 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
5649 0ebf8283 2019-07-24 stsp if (err)
5650 0ebf8283 2019-07-24 stsp goto done;
5651 0ebf8283 2019-07-24 stsp }
5652 0ebf8283 2019-07-24 stsp
5653 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
5654 0ebf8283 2019-07-24 stsp if (new_id_str)
5655 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
5656 0ebf8283 2019-07-24 stsp
5657 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5658 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
5659 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
5660 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5661 0ebf8283 2019-07-24 stsp goto done;
5662 0ebf8283 2019-07-24 stsp }
5663 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
5664 0ebf8283 2019-07-24 stsp } else {
5665 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5666 0ebf8283 2019-07-24 stsp if (err)
5667 0ebf8283 2019-07-24 stsp goto done;
5668 0ebf8283 2019-07-24 stsp }
5669 0ebf8283 2019-07-24 stsp
5670 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
5671 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
5672 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
5673 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
5674 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5675 0ebf8283 2019-07-24 stsp break;
5676 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
5677 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
5678 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5679 0ebf8283 2019-07-24 stsp logmsg);
5680 0ebf8283 2019-07-24 stsp break;
5681 0ebf8283 2019-07-24 stsp default:
5682 0ebf8283 2019-07-24 stsp break;
5683 0ebf8283 2019-07-24 stsp }
5684 0ebf8283 2019-07-24 stsp
5685 0ebf8283 2019-07-24 stsp done:
5686 0ebf8283 2019-07-24 stsp free(old_id_str);
5687 0ebf8283 2019-07-24 stsp free(new_id_str);
5688 0ebf8283 2019-07-24 stsp return err;
5689 0ebf8283 2019-07-24 stsp }
5690 0ebf8283 2019-07-24 stsp
5691 0ebf8283 2019-07-24 stsp static const struct got_error *
5692 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
5693 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5694 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5695 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5696 0ebf8283 2019-07-24 stsp {
5697 0ebf8283 2019-07-24 stsp const struct got_error *err;
5698 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5699 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
5700 0ebf8283 2019-07-24 stsp
5701 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5702 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
5703 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5704 0ebf8283 2019-07-24 stsp if (err)
5705 0ebf8283 2019-07-24 stsp return err;
5706 0ebf8283 2019-07-24 stsp }
5707 0ebf8283 2019-07-24 stsp
5708 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5709 0ebf8283 2019-07-24 stsp if (err)
5710 0ebf8283 2019-07-24 stsp return err;
5711 0ebf8283 2019-07-24 stsp
5712 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5713 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
5714 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
5715 0ebf8283 2019-07-24 stsp if (err) {
5716 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5717 0ebf8283 2019-07-24 stsp goto done;
5718 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
5719 0ebf8283 2019-07-24 stsp } else {
5720 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
5721 0ebf8283 2019-07-24 stsp free(new_commit_id);
5722 0ebf8283 2019-07-24 stsp }
5723 0ebf8283 2019-07-24 stsp done:
5724 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5725 0ebf8283 2019-07-24 stsp return err;
5726 0ebf8283 2019-07-24 stsp }
5727 0ebf8283 2019-07-24 stsp
5728 0ebf8283 2019-07-24 stsp static const struct got_error *
5729 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
5730 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5731 0ebf8283 2019-07-24 stsp {
5732 0ebf8283 2019-07-24 stsp const struct got_error *error;
5733 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5734 0ebf8283 2019-07-24 stsp
5735 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5736 0ebf8283 2019-07-24 stsp repo);
5737 0ebf8283 2019-07-24 stsp if (error)
5738 0ebf8283 2019-07-24 stsp return error;
5739 0ebf8283 2019-07-24 stsp
5740 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5741 0ebf8283 2019-07-24 stsp if (error)
5742 0ebf8283 2019-07-24 stsp return error;
5743 0ebf8283 2019-07-24 stsp
5744 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
5745 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5746 0ebf8283 2019-07-24 stsp return error;
5747 0ebf8283 2019-07-24 stsp }
5748 0ebf8283 2019-07-24 stsp
5749 0ebf8283 2019-07-24 stsp static const struct got_error *
5750 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
5751 0ebf8283 2019-07-24 stsp {
5752 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
5753 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
5754 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5755 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
5756 0ebf8283 2019-07-24 stsp char *cwd = NULL;
5757 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
5758 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
5759 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
5760 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
5761 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
5762 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5763 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
5764 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5765 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
5766 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5767 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
5768 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
5769 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
5770 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
5771 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
5772 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5773 0ebf8283 2019-07-24 stsp
5774 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
5775 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
5776 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
5777 0ebf8283 2019-07-24 stsp
5778 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
5779 0ebf8283 2019-07-24 stsp switch (ch) {
5780 0ebf8283 2019-07-24 stsp case 'a':
5781 0ebf8283 2019-07-24 stsp abort_edit = 1;
5782 0ebf8283 2019-07-24 stsp break;
5783 0ebf8283 2019-07-24 stsp case 'c':
5784 0ebf8283 2019-07-24 stsp continue_edit = 1;
5785 0ebf8283 2019-07-24 stsp break;
5786 0ebf8283 2019-07-24 stsp case 'F':
5787 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
5788 0ebf8283 2019-07-24 stsp break;
5789 0ebf8283 2019-07-24 stsp default:
5790 0ebf8283 2019-07-24 stsp usage_histedit();
5791 0ebf8283 2019-07-24 stsp /* NOTREACHED */
5792 0ebf8283 2019-07-24 stsp }
5793 0ebf8283 2019-07-24 stsp }
5794 0ebf8283 2019-07-24 stsp
5795 0ebf8283 2019-07-24 stsp argc -= optind;
5796 0ebf8283 2019-07-24 stsp argv += optind;
5797 0ebf8283 2019-07-24 stsp
5798 0ebf8283 2019-07-24 stsp #ifndef PROFILE
5799 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5800 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
5801 0ebf8283 2019-07-24 stsp err(1, "pledge");
5802 0ebf8283 2019-07-24 stsp #endif
5803 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
5804 0ebf8283 2019-07-24 stsp usage_histedit();
5805 0ebf8283 2019-07-24 stsp if (argc != 0)
5806 0ebf8283 2019-07-24 stsp usage_histedit();
5807 0ebf8283 2019-07-24 stsp
5808 0ebf8283 2019-07-24 stsp /*
5809 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
5810 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
5811 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
5812 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
5813 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
5814 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
5815 0ebf8283 2019-07-24 stsp */
5816 0ebf8283 2019-07-24 stsp
5817 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
5818 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
5819 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
5820 0ebf8283 2019-07-24 stsp goto done;
5821 0ebf8283 2019-07-24 stsp }
5822 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
5823 0ebf8283 2019-07-24 stsp if (error)
5824 0ebf8283 2019-07-24 stsp goto done;
5825 0ebf8283 2019-07-24 stsp
5826 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5827 0ebf8283 2019-07-24 stsp if (error != NULL)
5828 0ebf8283 2019-07-24 stsp goto done;
5829 0ebf8283 2019-07-24 stsp
5830 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5831 0ebf8283 2019-07-24 stsp if (error)
5832 0ebf8283 2019-07-24 stsp goto done;
5833 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
5834 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
5835 0ebf8283 2019-07-24 stsp goto done;
5836 0ebf8283 2019-07-24 stsp }
5837 0ebf8283 2019-07-24 stsp
5838 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5839 0ebf8283 2019-07-24 stsp if (error)
5840 0ebf8283 2019-07-24 stsp goto done;
5841 0ebf8283 2019-07-24 stsp
5842 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
5843 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5844 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5845 3e3a69f1 2019-07-25 stsp worktree, repo);
5846 0ebf8283 2019-07-24 stsp if (error)
5847 0ebf8283 2019-07-24 stsp goto done;
5848 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5849 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5850 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
5851 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
5852 0ebf8283 2019-07-24 stsp if (error)
5853 0ebf8283 2019-07-24 stsp goto done;
5854 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
5855 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5856 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
5857 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
5858 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5859 0ebf8283 2019-07-24 stsp goto done;
5860 0ebf8283 2019-07-24 stsp }
5861 0ebf8283 2019-07-24 stsp
5862 0ebf8283 2019-07-24 stsp if (continue_edit) {
5863 0ebf8283 2019-07-24 stsp char *path;
5864 0ebf8283 2019-07-24 stsp
5865 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
5866 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5867 0ebf8283 2019-07-24 stsp goto done;
5868 0ebf8283 2019-07-24 stsp }
5869 0ebf8283 2019-07-24 stsp
5870 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
5871 0ebf8283 2019-07-24 stsp if (error)
5872 0ebf8283 2019-07-24 stsp goto done;
5873 0ebf8283 2019-07-24 stsp
5874 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
5875 0ebf8283 2019-07-24 stsp free(path);
5876 0ebf8283 2019-07-24 stsp if (error)
5877 0ebf8283 2019-07-24 stsp goto done;
5878 0ebf8283 2019-07-24 stsp
5879 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5880 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5881 3e3a69f1 2019-07-25 stsp worktree, repo);
5882 0ebf8283 2019-07-24 stsp if (error)
5883 0ebf8283 2019-07-24 stsp goto done;
5884 0ebf8283 2019-07-24 stsp
5885 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5886 0ebf8283 2019-07-24 stsp if (error)
5887 0ebf8283 2019-07-24 stsp goto done;
5888 0ebf8283 2019-07-24 stsp
5889 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5890 0ebf8283 2019-07-24 stsp head_commit_id);
5891 0ebf8283 2019-07-24 stsp if (error)
5892 0ebf8283 2019-07-24 stsp goto done;
5893 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5894 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5895 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5896 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5897 3aac7cf7 2019-07-25 stsp goto done;
5898 3aac7cf7 2019-07-25 stsp }
5899 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5900 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
5901 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5902 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5903 0ebf8283 2019-07-24 stsp commit = NULL;
5904 0ebf8283 2019-07-24 stsp if (error)
5905 0ebf8283 2019-07-24 stsp goto done;
5906 0ebf8283 2019-07-24 stsp } else {
5907 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
5908 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5909 0ebf8283 2019-07-24 stsp goto done;
5910 0ebf8283 2019-07-24 stsp }
5911 0ebf8283 2019-07-24 stsp
5912 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
5913 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
5914 0ebf8283 2019-07-24 stsp if (error != NULL)
5915 0ebf8283 2019-07-24 stsp goto done;
5916 0ebf8283 2019-07-24 stsp
5917 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5918 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5919 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
5920 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
5921 c7d20a3f 2019-07-30 stsp goto done;
5922 c7d20a3f 2019-07-30 stsp }
5923 c7d20a3f 2019-07-30 stsp
5924 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5925 bfce7f83 2019-07-27 stsp got_ref_close(branch);
5926 bfce7f83 2019-07-27 stsp branch = NULL;
5927 0ebf8283 2019-07-24 stsp if (error)
5928 0ebf8283 2019-07-24 stsp goto done;
5929 0ebf8283 2019-07-24 stsp
5930 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5931 0ebf8283 2019-07-24 stsp head_commit_id);
5932 0ebf8283 2019-07-24 stsp if (error)
5933 0ebf8283 2019-07-24 stsp goto done;
5934 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5935 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5936 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5937 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5938 3aac7cf7 2019-07-25 stsp goto done;
5939 3aac7cf7 2019-07-25 stsp }
5940 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5941 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
5942 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
5943 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5944 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5945 0ebf8283 2019-07-24 stsp commit = NULL;
5946 0ebf8283 2019-07-24 stsp if (error)
5947 0ebf8283 2019-07-24 stsp goto done;
5948 0ebf8283 2019-07-24 stsp
5949 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5950 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
5951 bfce7f83 2019-07-27 stsp if (error)
5952 bfce7f83 2019-07-27 stsp goto done;
5953 bfce7f83 2019-07-27 stsp
5954 0ebf8283 2019-07-24 stsp if (edit_script_path) {
5955 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
5956 0ebf8283 2019-07-24 stsp edit_script_path, repo);
5957 6ba2bea2 2019-07-27 stsp if (error) {
5958 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5959 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5960 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5961 0ebf8283 2019-07-24 stsp goto done;
5962 6ba2bea2 2019-07-27 stsp }
5963 0ebf8283 2019-07-24 stsp } else {
5964 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
5965 0ebf8283 2019-07-24 stsp repo);
5966 6ba2bea2 2019-07-27 stsp if (error) {
5967 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5968 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5969 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5970 0ebf8283 2019-07-24 stsp goto done;
5971 6ba2bea2 2019-07-27 stsp }
5972 0ebf8283 2019-07-24 stsp
5973 0ebf8283 2019-07-24 stsp }
5974 0ebf8283 2019-07-24 stsp
5975 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5976 0ebf8283 2019-07-24 stsp repo);
5977 6ba2bea2 2019-07-27 stsp if (error) {
5978 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5979 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5980 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5981 0ebf8283 2019-07-24 stsp goto done;
5982 6ba2bea2 2019-07-27 stsp }
5983 0ebf8283 2019-07-24 stsp
5984 0ebf8283 2019-07-24 stsp }
5985 0ebf8283 2019-07-24 stsp
5986 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5987 0ebf8283 2019-07-24 stsp if (error)
5988 0ebf8283 2019-07-24 stsp goto done;
5989 0ebf8283 2019-07-24 stsp
5990 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5991 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5992 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5993 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5994 0ebf8283 2019-07-24 stsp continue;
5995 0ebf8283 2019-07-24 stsp
5996 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5997 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5998 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5999 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6000 0ebf8283 2019-07-24 stsp repo);
6001 0ebf8283 2019-07-24 stsp } else {
6002 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6003 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6004 0ebf8283 2019-07-24 stsp }
6005 0ebf8283 2019-07-24 stsp if (error)
6006 0ebf8283 2019-07-24 stsp goto done;
6007 0ebf8283 2019-07-24 stsp continue;
6008 0ebf8283 2019-07-24 stsp }
6009 0ebf8283 2019-07-24 stsp
6010 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6011 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6012 0ebf8283 2019-07-24 stsp if (error)
6013 0ebf8283 2019-07-24 stsp goto done;
6014 0ebf8283 2019-07-24 stsp continue;
6015 0ebf8283 2019-07-24 stsp }
6016 0ebf8283 2019-07-24 stsp
6017 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6018 0ebf8283 2019-07-24 stsp hle->commit_id);
6019 0ebf8283 2019-07-24 stsp if (error)
6020 0ebf8283 2019-07-24 stsp goto done;
6021 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6022 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6023 0ebf8283 2019-07-24 stsp
6024 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6025 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6026 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6027 0ebf8283 2019-07-24 stsp if (error)
6028 0ebf8283 2019-07-24 stsp goto done;
6029 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6030 0ebf8283 2019-07-24 stsp commit = NULL;
6031 0ebf8283 2019-07-24 stsp
6032 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6033 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6034 0ebf8283 2019-07-24 stsp break;
6035 0ebf8283 2019-07-24 stsp }
6036 0ebf8283 2019-07-24 stsp
6037 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6038 0ebf8283 2019-07-24 stsp char *id_str;
6039 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6040 0ebf8283 2019-07-24 stsp if (error)
6041 0ebf8283 2019-07-24 stsp goto done;
6042 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6043 0ebf8283 2019-07-24 stsp id_str);
6044 0ebf8283 2019-07-24 stsp free(id_str);
6045 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6046 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6047 3e3a69f1 2019-07-25 stsp fileindex);
6048 0ebf8283 2019-07-24 stsp goto done;
6049 0ebf8283 2019-07-24 stsp }
6050 0ebf8283 2019-07-24 stsp
6051 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6052 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6053 0ebf8283 2019-07-24 stsp if (error)
6054 0ebf8283 2019-07-24 stsp goto done;
6055 0ebf8283 2019-07-24 stsp continue;
6056 0ebf8283 2019-07-24 stsp }
6057 0ebf8283 2019-07-24 stsp
6058 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6059 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6060 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6061 0ebf8283 2019-07-24 stsp if (error)
6062 0ebf8283 2019-07-24 stsp goto done;
6063 0ebf8283 2019-07-24 stsp }
6064 0ebf8283 2019-07-24 stsp
6065 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6066 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6067 0ebf8283 2019-07-24 stsp if (error)
6068 0ebf8283 2019-07-24 stsp goto done;
6069 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6070 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6071 0ebf8283 2019-07-24 stsp } else
6072 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6073 3e3a69f1 2019-07-25 stsp branch, repo);
6074 0ebf8283 2019-07-24 stsp done:
6075 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6076 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6077 0ebf8283 2019-07-24 stsp free(head_commit_id);
6078 0ebf8283 2019-07-24 stsp free(base_commit_id);
6079 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6080 0ebf8283 2019-07-24 stsp if (commit)
6081 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6082 0ebf8283 2019-07-24 stsp if (branch)
6083 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6084 0ebf8283 2019-07-24 stsp if (tmp_branch)
6085 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6086 0ebf8283 2019-07-24 stsp if (worktree)
6087 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6088 715dc77e 2019-08-03 stsp if (repo)
6089 715dc77e 2019-08-03 stsp got_repo_close(repo);
6090 715dc77e 2019-08-03 stsp return error;
6091 715dc77e 2019-08-03 stsp }
6092 715dc77e 2019-08-03 stsp
6093 715dc77e 2019-08-03 stsp __dead static void
6094 715dc77e 2019-08-03 stsp usage_stage(void)
6095 715dc77e 2019-08-03 stsp {
6096 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6097 f3055044 2019-08-07 stsp "[file-path ...]\n",
6098 715dc77e 2019-08-03 stsp getprogname());
6099 715dc77e 2019-08-03 stsp exit(1);
6100 715dc77e 2019-08-03 stsp }
6101 715dc77e 2019-08-03 stsp
6102 715dc77e 2019-08-03 stsp static const struct got_error *
6103 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6104 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6105 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6106 408b4ebc 2019-08-03 stsp {
6107 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6108 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6109 408b4ebc 2019-08-03 stsp
6110 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6111 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6112 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6113 408b4ebc 2019-08-03 stsp return NULL;
6114 408b4ebc 2019-08-03 stsp
6115 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6116 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6117 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6118 408b4ebc 2019-08-03 stsp else
6119 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6120 408b4ebc 2019-08-03 stsp if (err)
6121 408b4ebc 2019-08-03 stsp return err;
6122 408b4ebc 2019-08-03 stsp
6123 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6124 408b4ebc 2019-08-03 stsp free(id_str);
6125 dc424a06 2019-08-07 stsp return NULL;
6126 dc424a06 2019-08-07 stsp }
6127 2e1f37b0 2019-08-08 stsp
6128 dc424a06 2019-08-07 stsp static const struct got_error *
6129 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6130 715dc77e 2019-08-03 stsp {
6131 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6132 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6133 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6134 715dc77e 2019-08-03 stsp char *cwd = NULL;
6135 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6136 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6137 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6138 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6139 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6140 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6141 715dc77e 2019-08-03 stsp
6142 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6143 715dc77e 2019-08-03 stsp
6144 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6145 715dc77e 2019-08-03 stsp switch (ch) {
6146 408b4ebc 2019-08-03 stsp case 'l':
6147 408b4ebc 2019-08-03 stsp list_stage = 1;
6148 408b4ebc 2019-08-03 stsp break;
6149 dc424a06 2019-08-07 stsp case 'p':
6150 dc424a06 2019-08-07 stsp pflag = 1;
6151 dc424a06 2019-08-07 stsp break;
6152 dc424a06 2019-08-07 stsp case 'F':
6153 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6154 dc424a06 2019-08-07 stsp break;
6155 715dc77e 2019-08-03 stsp default:
6156 715dc77e 2019-08-03 stsp usage_stage();
6157 715dc77e 2019-08-03 stsp /* NOTREACHED */
6158 715dc77e 2019-08-03 stsp }
6159 715dc77e 2019-08-03 stsp }
6160 715dc77e 2019-08-03 stsp
6161 715dc77e 2019-08-03 stsp argc -= optind;
6162 715dc77e 2019-08-03 stsp argv += optind;
6163 715dc77e 2019-08-03 stsp
6164 715dc77e 2019-08-03 stsp #ifndef PROFILE
6165 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6166 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6167 715dc77e 2019-08-03 stsp err(1, "pledge");
6168 715dc77e 2019-08-03 stsp #endif
6169 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6170 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6171 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6172 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6173 715dc77e 2019-08-03 stsp
6174 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6175 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6176 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6177 715dc77e 2019-08-03 stsp goto done;
6178 715dc77e 2019-08-03 stsp }
6179 715dc77e 2019-08-03 stsp
6180 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6181 715dc77e 2019-08-03 stsp if (error)
6182 715dc77e 2019-08-03 stsp goto done;
6183 715dc77e 2019-08-03 stsp
6184 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6185 715dc77e 2019-08-03 stsp if (error != NULL)
6186 715dc77e 2019-08-03 stsp goto done;
6187 715dc77e 2019-08-03 stsp
6188 dc424a06 2019-08-07 stsp if (patch_script_path) {
6189 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6190 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6191 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6192 dc424a06 2019-08-07 stsp patch_script_path);
6193 dc424a06 2019-08-07 stsp goto done;
6194 dc424a06 2019-08-07 stsp }
6195 dc424a06 2019-08-07 stsp }
6196 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6197 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6198 715dc77e 2019-08-03 stsp if (error)
6199 715dc77e 2019-08-03 stsp goto done;
6200 715dc77e 2019-08-03 stsp
6201 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6202 6d022e97 2019-08-04 stsp if (error)
6203 6d022e97 2019-08-04 stsp goto done;
6204 715dc77e 2019-08-03 stsp
6205 6d022e97 2019-08-04 stsp if (list_stage)
6206 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6207 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6208 2e1f37b0 2019-08-08 stsp else {
6209 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6210 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6211 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6212 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6213 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6214 2e1f37b0 2019-08-08 stsp }
6215 ad493afc 2019-08-03 stsp done:
6216 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6217 2e1f37b0 2019-08-08 stsp error == NULL)
6218 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6219 ad493afc 2019-08-03 stsp if (repo)
6220 ad493afc 2019-08-03 stsp got_repo_close(repo);
6221 ad493afc 2019-08-03 stsp if (worktree)
6222 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6223 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6224 ad493afc 2019-08-03 stsp free((char *)pe->path);
6225 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6226 ad493afc 2019-08-03 stsp free(cwd);
6227 ad493afc 2019-08-03 stsp return error;
6228 ad493afc 2019-08-03 stsp }
6229 ad493afc 2019-08-03 stsp
6230 ad493afc 2019-08-03 stsp __dead static void
6231 ad493afc 2019-08-03 stsp usage_unstage(void)
6232 ad493afc 2019-08-03 stsp {
6233 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6234 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6235 ad493afc 2019-08-03 stsp getprogname());
6236 ad493afc 2019-08-03 stsp exit(1);
6237 ad493afc 2019-08-03 stsp }
6238 ad493afc 2019-08-03 stsp
6239 ad493afc 2019-08-03 stsp
6240 ad493afc 2019-08-03 stsp static const struct got_error *
6241 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6242 ad493afc 2019-08-03 stsp {
6243 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6244 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6245 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6246 ad493afc 2019-08-03 stsp char *cwd = NULL;
6247 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6248 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6249 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6250 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6251 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6252 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6253 ad493afc 2019-08-03 stsp
6254 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6255 ad493afc 2019-08-03 stsp
6256 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
6257 ad493afc 2019-08-03 stsp switch (ch) {
6258 2e1f37b0 2019-08-08 stsp case 'p':
6259 2e1f37b0 2019-08-08 stsp pflag = 1;
6260 2e1f37b0 2019-08-08 stsp break;
6261 2e1f37b0 2019-08-08 stsp case 'F':
6262 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
6263 2e1f37b0 2019-08-08 stsp break;
6264 ad493afc 2019-08-03 stsp default:
6265 ad493afc 2019-08-03 stsp usage_unstage();
6266 ad493afc 2019-08-03 stsp /* NOTREACHED */
6267 ad493afc 2019-08-03 stsp }
6268 ad493afc 2019-08-03 stsp }
6269 ad493afc 2019-08-03 stsp
6270 ad493afc 2019-08-03 stsp argc -= optind;
6271 ad493afc 2019-08-03 stsp argv += optind;
6272 ad493afc 2019-08-03 stsp
6273 ad493afc 2019-08-03 stsp #ifndef PROFILE
6274 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6275 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
6276 ad493afc 2019-08-03 stsp err(1, "pledge");
6277 ad493afc 2019-08-03 stsp #endif
6278 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
6279 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6280 2e1f37b0 2019-08-08 stsp
6281 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
6282 ad493afc 2019-08-03 stsp if (cwd == NULL) {
6283 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
6284 ad493afc 2019-08-03 stsp goto done;
6285 ad493afc 2019-08-03 stsp }
6286 ad493afc 2019-08-03 stsp
6287 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6288 ad493afc 2019-08-03 stsp if (error)
6289 ad493afc 2019-08-03 stsp goto done;
6290 ad493afc 2019-08-03 stsp
6291 ad493afc 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6292 ad493afc 2019-08-03 stsp if (error != NULL)
6293 ad493afc 2019-08-03 stsp goto done;
6294 ad493afc 2019-08-03 stsp
6295 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
6296 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6297 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
6298 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
6299 2e1f37b0 2019-08-08 stsp patch_script_path);
6300 2e1f37b0 2019-08-08 stsp goto done;
6301 2e1f37b0 2019-08-08 stsp }
6302 2e1f37b0 2019-08-08 stsp }
6303 2e1f37b0 2019-08-08 stsp
6304 ad493afc 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6305 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
6306 ad493afc 2019-08-03 stsp if (error)
6307 ad493afc 2019-08-03 stsp goto done;
6308 ad493afc 2019-08-03 stsp
6309 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6310 ad493afc 2019-08-03 stsp if (error)
6311 ad493afc 2019-08-03 stsp goto done;
6312 ad493afc 2019-08-03 stsp
6313 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6314 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
6315 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
6316 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6317 715dc77e 2019-08-03 stsp done:
6318 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6319 2e1f37b0 2019-08-08 stsp error == NULL)
6320 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6321 0ebf8283 2019-07-24 stsp if (repo)
6322 0ebf8283 2019-07-24 stsp got_repo_close(repo);
6323 715dc77e 2019-08-03 stsp if (worktree)
6324 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
6325 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6326 715dc77e 2019-08-03 stsp free((char *)pe->path);
6327 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
6328 715dc77e 2019-08-03 stsp free(cwd);
6329 01073a5d 2019-08-22 stsp return error;
6330 01073a5d 2019-08-22 stsp }
6331 01073a5d 2019-08-22 stsp
6332 01073a5d 2019-08-22 stsp __dead static void
6333 01073a5d 2019-08-22 stsp usage_cat(void)
6334 01073a5d 2019-08-22 stsp {
6335 01073a5d 2019-08-22 stsp fprintf(stderr, "usage: %s cat [-r repository ] object1 "
6336 01073a5d 2019-08-22 stsp "[object2 ...]\n", getprogname());
6337 01073a5d 2019-08-22 stsp exit(1);
6338 01073a5d 2019-08-22 stsp }
6339 01073a5d 2019-08-22 stsp
6340 01073a5d 2019-08-22 stsp static const struct got_error *
6341 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6342 01073a5d 2019-08-22 stsp {
6343 01073a5d 2019-08-22 stsp const struct got_error *err;
6344 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
6345 01073a5d 2019-08-22 stsp
6346 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
6347 01073a5d 2019-08-22 stsp if (err)
6348 01073a5d 2019-08-22 stsp return err;
6349 01073a5d 2019-08-22 stsp
6350 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6351 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
6352 01073a5d 2019-08-22 stsp return err;
6353 01073a5d 2019-08-22 stsp }
6354 01073a5d 2019-08-22 stsp
6355 01073a5d 2019-08-22 stsp static const struct got_error *
6356 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6357 01073a5d 2019-08-22 stsp {
6358 01073a5d 2019-08-22 stsp const struct got_error *err;
6359 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
6360 01073a5d 2019-08-22 stsp const struct got_tree_entries *entries;
6361 01073a5d 2019-08-22 stsp struct got_tree_entry *te;
6362 01073a5d 2019-08-22 stsp
6363 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
6364 01073a5d 2019-08-22 stsp if (err)
6365 01073a5d 2019-08-22 stsp return err;
6366 01073a5d 2019-08-22 stsp
6367 01073a5d 2019-08-22 stsp entries = got_object_tree_get_entries(tree);
6368 01073a5d 2019-08-22 stsp te = SIMPLEQ_FIRST(&entries->head);
6369 01073a5d 2019-08-22 stsp while (te) {
6370 01073a5d 2019-08-22 stsp char *id_str;
6371 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
6372 01073a5d 2019-08-22 stsp break;
6373 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, te->id);
6374 01073a5d 2019-08-22 stsp if (err)
6375 01073a5d 2019-08-22 stsp break;
6376 01073a5d 2019-08-22 stsp fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6377 01073a5d 2019-08-22 stsp free(id_str);
6378 01073a5d 2019-08-22 stsp te = SIMPLEQ_NEXT(te, entry);
6379 01073a5d 2019-08-22 stsp }
6380 01073a5d 2019-08-22 stsp
6381 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
6382 01073a5d 2019-08-22 stsp return err;
6383 01073a5d 2019-08-22 stsp }
6384 01073a5d 2019-08-22 stsp
6385 01073a5d 2019-08-22 stsp static const struct got_error *
6386 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6387 01073a5d 2019-08-22 stsp {
6388 01073a5d 2019-08-22 stsp const struct got_error *err;
6389 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
6390 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
6391 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
6392 01073a5d 2019-08-22 stsp char *id_str = NULL;
6393 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
6394 01073a5d 2019-08-22 stsp
6395 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
6396 01073a5d 2019-08-22 stsp if (err)
6397 01073a5d 2019-08-22 stsp return err;
6398 01073a5d 2019-08-22 stsp
6399 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6400 01073a5d 2019-08-22 stsp if (err)
6401 01073a5d 2019-08-22 stsp goto done;
6402 01073a5d 2019-08-22 stsp
6403 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6404 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6405 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
6406 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
6407 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6408 01073a5d 2019-08-22 stsp char *pid_str;
6409 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
6410 01073a5d 2019-08-22 stsp if (err)
6411 01073a5d 2019-08-22 stsp goto done;
6412 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6413 01073a5d 2019-08-22 stsp free(pid_str);
6414 01073a5d 2019-08-22 stsp }
6415 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6416 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6417 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
6418 01073a5d 2019-08-22 stsp
6419 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6420 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6421 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
6422 01073a5d 2019-08-22 stsp
6423 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
6424 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6425 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
6426 01073a5d 2019-08-22 stsp done:
6427 01073a5d 2019-08-22 stsp free(id_str);
6428 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
6429 01073a5d 2019-08-22 stsp return err;
6430 01073a5d 2019-08-22 stsp }
6431 01073a5d 2019-08-22 stsp
6432 01073a5d 2019-08-22 stsp static const struct got_error *
6433 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6434 01073a5d 2019-08-22 stsp {
6435 01073a5d 2019-08-22 stsp const struct got_error *err;
6436 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
6437 01073a5d 2019-08-22 stsp char *id_str = NULL;
6438 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
6439 01073a5d 2019-08-22 stsp
6440 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6441 01073a5d 2019-08-22 stsp if (err)
6442 01073a5d 2019-08-22 stsp return err;
6443 01073a5d 2019-08-22 stsp
6444 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6445 01073a5d 2019-08-22 stsp if (err)
6446 01073a5d 2019-08-22 stsp goto done;
6447 01073a5d 2019-08-22 stsp
6448 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6449 e15d5241 2019-08-22 stsp
6450 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
6451 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
6452 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6453 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
6454 01073a5d 2019-08-22 stsp break;
6455 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
6456 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6457 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
6458 01073a5d 2019-08-22 stsp break;
6459 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
6460 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6461 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
6462 01073a5d 2019-08-22 stsp break;
6463 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
6464 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6465 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
6466 01073a5d 2019-08-22 stsp break;
6467 01073a5d 2019-08-22 stsp default:
6468 01073a5d 2019-08-22 stsp break;
6469 01073a5d 2019-08-22 stsp }
6470 01073a5d 2019-08-22 stsp
6471 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6472 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
6473 e15d5241 2019-08-22 stsp
6474 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6475 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
6476 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
6477 01073a5d 2019-08-22 stsp
6478 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
6479 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6480 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
6481 01073a5d 2019-08-22 stsp done:
6482 01073a5d 2019-08-22 stsp free(id_str);
6483 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
6484 01073a5d 2019-08-22 stsp return err;
6485 01073a5d 2019-08-22 stsp }
6486 01073a5d 2019-08-22 stsp
6487 01073a5d 2019-08-22 stsp static const struct got_error *
6488 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
6489 01073a5d 2019-08-22 stsp {
6490 01073a5d 2019-08-22 stsp const struct got_error *error;
6491 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
6492 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
6493 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
6494 01073a5d 2019-08-22 stsp struct got_object_id *id = NULL;
6495 01073a5d 2019-08-22 stsp int ch, obj_type, i;
6496 01073a5d 2019-08-22 stsp
6497 01073a5d 2019-08-22 stsp #ifndef PROFILE
6498 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6499 01073a5d 2019-08-22 stsp NULL) == -1)
6500 01073a5d 2019-08-22 stsp err(1, "pledge");
6501 01073a5d 2019-08-22 stsp #endif
6502 01073a5d 2019-08-22 stsp
6503 01073a5d 2019-08-22 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6504 01073a5d 2019-08-22 stsp switch (ch) {
6505 01073a5d 2019-08-22 stsp case 'r':
6506 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6507 01073a5d 2019-08-22 stsp if (repo_path == NULL)
6508 01073a5d 2019-08-22 stsp err(1, "-r option");
6509 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6510 01073a5d 2019-08-22 stsp break;
6511 01073a5d 2019-08-22 stsp default:
6512 01073a5d 2019-08-22 stsp usage_cat();
6513 01073a5d 2019-08-22 stsp /* NOTREACHED */
6514 01073a5d 2019-08-22 stsp }
6515 01073a5d 2019-08-22 stsp }
6516 01073a5d 2019-08-22 stsp
6517 01073a5d 2019-08-22 stsp argc -= optind;
6518 01073a5d 2019-08-22 stsp argv += optind;
6519 01073a5d 2019-08-22 stsp
6520 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
6521 01073a5d 2019-08-22 stsp if (cwd == NULL) {
6522 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
6523 01073a5d 2019-08-22 stsp goto done;
6524 01073a5d 2019-08-22 stsp }
6525 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6526 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6527 01073a5d 2019-08-22 stsp goto done;
6528 01073a5d 2019-08-22 stsp if (worktree) {
6529 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6530 01073a5d 2019-08-22 stsp repo_path = strdup(
6531 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
6532 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6533 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
6534 01073a5d 2019-08-22 stsp goto done;
6535 01073a5d 2019-08-22 stsp }
6536 01073a5d 2019-08-22 stsp }
6537 01073a5d 2019-08-22 stsp }
6538 01073a5d 2019-08-22 stsp
6539 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6540 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
6541 01073a5d 2019-08-22 stsp if (repo_path == NULL)
6542 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
6543 01073a5d 2019-08-22 stsp }
6544 01073a5d 2019-08-22 stsp
6545 01073a5d 2019-08-22 stsp error = got_repo_open(&repo, repo_path);
6546 01073a5d 2019-08-22 stsp free(repo_path);
6547 01073a5d 2019-08-22 stsp if (error != NULL)
6548 01073a5d 2019-08-22 stsp goto done;
6549 01073a5d 2019-08-22 stsp
6550 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6551 01073a5d 2019-08-22 stsp if (error)
6552 01073a5d 2019-08-22 stsp goto done;
6553 01073a5d 2019-08-22 stsp
6554 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
6555 01073a5d 2019-08-22 stsp error = match_object_id(&id, &label, argv[i],
6556 01073a5d 2019-08-22 stsp GOT_OBJ_TYPE_ANY, 0, repo);
6557 01073a5d 2019-08-22 stsp if (error)
6558 01073a5d 2019-08-22 stsp break;
6559 01073a5d 2019-08-22 stsp
6560 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
6561 01073a5d 2019-08-22 stsp if (error)
6562 01073a5d 2019-08-22 stsp break;
6563 01073a5d 2019-08-22 stsp
6564 01073a5d 2019-08-22 stsp switch (obj_type) {
6565 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
6566 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
6567 01073a5d 2019-08-22 stsp break;
6568 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
6569 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
6570 01073a5d 2019-08-22 stsp break;
6571 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
6572 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
6573 01073a5d 2019-08-22 stsp break;
6574 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
6575 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
6576 01073a5d 2019-08-22 stsp break;
6577 01073a5d 2019-08-22 stsp default:
6578 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
6579 01073a5d 2019-08-22 stsp break;
6580 01073a5d 2019-08-22 stsp }
6581 01073a5d 2019-08-22 stsp if (error)
6582 01073a5d 2019-08-22 stsp break;
6583 01073a5d 2019-08-22 stsp free(label);
6584 01073a5d 2019-08-22 stsp label = NULL;
6585 01073a5d 2019-08-22 stsp free(id);
6586 01073a5d 2019-08-22 stsp id = NULL;
6587 01073a5d 2019-08-22 stsp }
6588 01073a5d 2019-08-22 stsp
6589 01073a5d 2019-08-22 stsp done:
6590 01073a5d 2019-08-22 stsp free(label);
6591 01073a5d 2019-08-22 stsp free(id);
6592 01073a5d 2019-08-22 stsp if (worktree)
6593 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
6594 01073a5d 2019-08-22 stsp if (repo) {
6595 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
6596 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
6597 01073a5d 2019-08-22 stsp if (error == NULL)
6598 01073a5d 2019-08-22 stsp error = repo_error;
6599 01073a5d 2019-08-22 stsp }
6600 0ebf8283 2019-07-24 stsp return error;
6601 0ebf8283 2019-07-24 stsp }