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